nuha-0.3.0.0: Multidimensional arrays, Linear algebra, Numerical analysis
Copyright(c) Johannes Kropp
LicenseBSD 3-Clause
MaintainerJohannes Kropp <jodak932@gmail.com>
Safe HaskellNone
LanguageHaskell2010

Math.Nuha.Base

Description

 
Synopsis

Constructions

holor Source #

Arguments

:: Unbox a 
=> [Int]

shape

-> [a]

values

-> Holor a 

Basic function for creating a holor

>>> holor [2,2] [1,2,3,4]
  1.0  2.0
  3.0  4.0
>>> holor [2,2] [1,2,3,4] :: Holor Int
  1  2
  3  4
>>> holor [2,2,4] [1 .. 16]
[0,:,:] =
  1.0  2.0  3.0  4.0
  5.0  6.0  7.0  8.0
[1,:,:] =
  9.0  10.0  11.0  12.0
  13.0  14.0  15.0  16.0

vector :: Unbox a => [a] -> Holor a Source #

Creates a column-vector (2d-holor with shape [m,1])

>>> vector [1,2,3,4]
 1.0
 2.0
 3.0
 4.0

matrix :: Unbox a => [[a]] -> Holor a Source #

Creates a matrix (2d-holor)

>>> matrix [[1,2],[3,4]]
  1.0  2.0
  3.0  4.0

vector2 :: Unbox a => T2 a -> Holor a Source #

Creates a 2x1 holor from a tuple

vector3 :: Unbox a => T3 a -> Holor a Source #

Creates a 3x1 holor from a tuple

vector4 :: Unbox a => T4 a -> Holor a Source #

Creates a 4x1 holor from a tuple

matrix22 :: Unbox a => T22 a -> Holor a Source #

Creates a 2x2 holor from nested tuples

matrix32 :: Unbox a => T32 a -> Holor a Source #

Creates a 3x2 holor from nested tuples

matrix42 :: Unbox a => T42 a -> Holor a Source #

Creates a 4x2 holor from nested tuples

matrix23 :: Unbox a => T23 a -> Holor a Source #

Creates a 2x3 holor from nested tuples

matrix33 :: Unbox a => T33 a -> Holor a Source #

Creates a 3x3 holor from nested tuples

matrix43 :: Unbox a => T43 a -> Holor a Source #

Creates a 4x3 holor from nested tuples

matrix24 :: Unbox a => T24 a -> Holor a Source #

Creates a 2x4 holor from nested tuples

matrix34 :: Unbox a => T34 a -> Holor a Source #

Creates a 3x4 holor from nested tuples

matrix44 :: Unbox a => T44 a -> Holor a Source #

Creates a 4x4 holor from nested tuples

replicate :: Unbox a => [Int] -> a -> Holor a Source #

Creates a holor for a given shape with all entries the same

>>> replicate [1,3] 0
  0.0  0.0  0.0

Properties

shape :: Unbox a => Holor a -> [Int] Source #

Shape of a holor

numElems :: Unbox a => Holor a -> Int Source #

Number of holor entrys

sizeOfElems :: (Unbox a, Storable a) => Holor a -> Int Source #

Size of all holor elements in bytes (not equal to consumed memory of the holor)

sizeOfElem :: forall a. (Unbox a, Storable a) => Holor a -> Int Source #

Size of a single holor element in bytes

dim :: Unbox a => Holor a -> Int Source #

Dimension of a holor (length of shape)

>>> dim $ holor [2,2,2] [0..7]
3

Accessors

(!) :: Unbox a => Holor a -> Int -> a infixl 9 Source #

Indexing a holor with shape (n,1) (column-vector) or (1,n) (row-vector)

>>> v = vector [5,6,7]
>>> v!1
6.0

(!!) :: Unbox a => Holor a -> Int -> a infixl 9 Source #

Unsafe version of (!) without checking for row or column vector and not checking bounds

(|!) :: Unbox a => Holor a -> [Int] -> a infixl 9 Source #

Indexing a single entry of a holor

>>> m = matrix [[2,3,4],[5,6,3],[9,0,2]]
>>> m|![0,2]
4.0

(|!!) :: Unbox a => Holor a -> [Int] -> a infixl 9 Source #

Unsafe version of (|!) without checking bounds

(||!) :: Unbox a => Holor a -> [Int] -> Holor a infixl 9 Source #

Extract a subholor by indices in the first dimension. Applied to a vector the result is a vector with the elements at the given element-indices. Applied to a matrix the result is a matrix with the rows at the given row-indices.

>>> h = holor [4,4] [1..16]
>>> h
  1.0  2.0  3.0  4.0
  5.0  6.0  7.0  8.0
  9.0  10.0  11.0  12.0
  13.0  14.0  15.0  16.0
>>> h||![1,3]
  5.0  6.0  7.0  8.0
  13.0  14.0  15.0  16.0

(||!!) :: Unbox a => Holor a -> [Int] -> Holor a infixl 9 Source #

Unsafe version of (||!) without checking indices

(|||!) :: Unbox a => Holor a -> [[Int]] -> Holor a infixl 9 Source #

Multidimensional indexing. For each dimension a list of indices define the axes to extract. This means multiindexing is done by a nested list of indices which has the same length as the shape. The dimension of the extracted subholor remains the same as that of the original holor.

>>> h = holor [4,4] [1..16]
>>> h
  1.0  2.0  3.0  4.0
  5.0  6.0  7.0  8.0
  9.0  10.0  11.0  12.0
  13.0  14.0  15.0  16.0
>>> h|||![[1,2,3],[0,1,1,2,3]]
  5.0  6.0  6.0  7.0  8.0
  9.0  10.0  10.0  11.0  12.0
  13.0  14.0  14.0  15.0  16.0

(|||!!) :: Unbox a => Holor a -> [[Int]] -> Holor a infixl 9 Source #

Unsafe version of (|||!) without checking multiindices

Operations

map :: (Unbox a, Unbox b) => (a -> b) -> Holor a -> Holor b Source #

Map a function over all entries of a holor

filter :: Unbox a => (a -> Bool) -> Holor a -> [a] Source #

Filter the values of a holor by a condition. The remaining elements are returned in a list.

>>> h = holor [3,3,3] [1 .. 27] :: Holor Int
>>> filter (\e -> mod e 3 == 0) h
[3,6,9,12,15,18,21,24,27]

selectBy :: Unbox a => (a -> Bool) -> Holor a -> [[Int]] Source #

Select all multiindices of a holor whose corresponding elements fulfill a condition.

>>> m = matrix [[-1,1],[-2,3]]
>>> selectBy (>0) m
  [[0,1],[1,1]]

countBy :: Unbox a => (a -> Bool) -> Holor a -> Int Source #

Count elements of a holor that fulfill a condition

accumulate :: Unbox a => (a -> a -> a) -> Holor a -> [a] Source #

Accumulate the values of a holor by a function.

>>> v = vector [2,3,4,1] :: Holor Int
>>> accumulate (+) v
  [2,5,9,10]
>>> accumulate max v
  [2,3,4,4]

flatten :: Unbox a => Holor a -> Holor a Source #

Flatten a holor to a column vector

transpose :: Unbox a => Holor a -> Holor a Source #

Transpose a holor

reshape :: Unbox a => [Int] -> Holor a -> Holor a Source #

Reshape a holor without changing the order of the underlying values

>>> h = holor [2,2,2] [1 .. 8]
>>> reshape [2,4] h
  1.0  2.0  3.0  4.0
  5.0  6.0  7.0  8.0

diagonal :: Unbox a => Holor a -> Holor a Source #

Get diagonal of a quadratical matrix (2d-holor) as a column vector

Conversions

toList :: Unbox a => Holor a -> [a] Source #

Returns the holor values in a list

toList2 :: Unbox a => Holor a -> [[a]] Source #

Returns the values of an 2d-holor in a list of lists

toScalar :: Unbox a => Holor a -> a Source #

Converts a holor with a single element to the element itself

toT2 :: Unbox a => Holor a -> T2 a Source #

Converts a holor to a 2-tuple if possible

toT3 :: Unbox a => Holor a -> T3 a Source #

Converts a holor to a 3-tuple if possible

toT4 :: Unbox a => Holor a -> T4 a Source #

Converts a holor to a 4-tuple if possible

toT22 :: Unbox a => Holor a -> T22 a Source #

Converts a holor to nested 2,2-tuple if possible

toT32 :: Unbox a => Holor a -> T32 a Source #

Converts a holor to nested 3,2-tuple if possible

toT42 :: Unbox a => Holor a -> T42 a Source #

Converts a holor to nested 4,2-tuple if possible

toT23 :: Unbox a => Holor a -> T23 a Source #

Converts a holor to nested 2,3-tuple if possible

toT33 :: Unbox a => Holor a -> T33 a Source #

Converts a holor to nested 3,3-tuple if possible

toT43 :: Unbox a => Holor a -> T43 a Source #

Converts a holor to nested 4,3-tuple if possible

toT24 :: Unbox a => Holor a -> T24 a Source #

Converts a holor to nested 2,4-tuple if possible

toT34 :: Unbox a => Holor a -> T34 a Source #

Converts a holor to nested 3,4-tuple if possible

toT44 :: Unbox a => Holor a -> T44 a Source #

Converts a holor to nested 4,4-tuple if possible

Modifications

setElem :: Unbox a => [Int] -> a -> Holor a -> Holor a Source #

Modify a single entry of a holor

>>> v = vector [1,2,3]
>>> setElem [1,0] 22 v
  1.0  22.0  3.0

setElems :: Unbox a => [[Int]] -> [a] -> Holor a -> Holor a Source #

Modify many elements of a holor at once

>>> h = zeros [3,3] :: Holor Int
>>> setElems [[0,0],[1,1],[2,2]] [4,5,6] h
  4  0  0
  0  5  0
  0  0  6

Checks

isValidHolor :: Unbox a => Holor a -> Bool Source #

Tests if a holor is instanced correctly, i.e. shape, strides and values fit together

isVector :: Unbox a => Holor a -> Bool Source #

Tests if a holor has shape n,1 or 1,n

isSquare :: Unbox a => Holor a -> Bool Source #

Tests if a holor has the shape of a square matrix

isUpperTri :: (Unbox a, Eq a, Num a) => Holor a -> Bool Source #

Tests if a matrix A with shape [m,n] is of upper triangular form. If m<n, false is returned. If m>=n, all elements on the first diagonal must be unequal to zero and all elements below the first diagonal must be zero.