diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-06-10 13:08:17 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-06-10 13:08:17 +0200 |
commit | a928a3a1713704cf3d5148bedc7ff8acb1347599 (patch) | |
tree | 8843ac9f0f323ef8666b0fee9044d5a506348cdc /packages/base | |
parent | b165193b98f143445fc20be64ce19bc0a570b1bf (diff) |
module and function names
Diffstat (limited to 'packages/base')
-rw-r--r-- | packages/base/hmatrix.cabal | 11 | ||||
-rw-r--r-- | packages/base/src/Data/Packed/Numeric.hs | 4 | ||||
-rw-r--r-- | packages/base/src/Numeric/Complex.hs (renamed from packages/base/src/Numeric/LinearAlgebra/Complex.hs) | 30 | ||||
-rw-r--r-- | packages/base/src/Numeric/HMatrix.hs | 634 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Data.hs | 4 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/HMatrix.hs | 201 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Real.hs | 480 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Static.hs | 2 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Util.hs | 14 | ||||
-rw-r--r-- | packages/base/src/Numeric/Sparse.hs | 2 |
10 files changed, 696 insertions, 686 deletions
diff --git a/packages/base/hmatrix.cabal b/packages/base/hmatrix.cabal index 30d88cd..3f16f5f 100644 --- a/packages/base/hmatrix.cabal +++ b/packages/base/hmatrix.cabal | |||
@@ -47,14 +47,14 @@ library | |||
47 | Numeric.HMatrix | 47 | Numeric.HMatrix |
48 | Numeric.LinearAlgebra.Devel | 48 | Numeric.LinearAlgebra.Devel |
49 | Numeric.LinearAlgebra.Data | 49 | Numeric.LinearAlgebra.Data |
50 | Numeric.LinearAlgebra.Real | 50 | Numeric.LinearAlgebra.HMatrix |
51 | -- Numeric.LinearAlgebra.Complex | 51 | |
52 | 52 | ||
53 | 53 | ||
54 | other-modules: Data.Packed.Internal, | 54 | other-modules: Data.Packed.Internal, |
55 | Data.Packed.Internal.Common, | 55 | Data.Packed.Internal.Common |
56 | Data.Packed.Internal.Signatures, | 56 | Data.Packed.Internal.Signatures |
57 | Data.Packed.Internal.Vector, | 57 | Data.Packed.Internal.Vector |
58 | Data.Packed.Internal.Matrix | 58 | Data.Packed.Internal.Matrix |
59 | Data.Packed.IO | 59 | Data.Packed.IO |
60 | Numeric.Chain | 60 | Numeric.Chain |
@@ -68,6 +68,7 @@ library | |||
68 | Numeric.LinearAlgebra.Random | 68 | Numeric.LinearAlgebra.Random |
69 | Numeric.Conversion | 69 | Numeric.Conversion |
70 | Numeric.Sparse | 70 | Numeric.Sparse |
71 | Numeric.Complex | ||
71 | Numeric.LinearAlgebra.Static | 72 | Numeric.LinearAlgebra.Static |
72 | 73 | ||
73 | C-sources: src/C/lapack-aux.c | 74 | C-sources: src/C/lapack-aux.c |
diff --git a/packages/base/src/Data/Packed/Numeric.hs b/packages/base/src/Data/Packed/Numeric.hs index d2a20be..e59c1cd 100644 --- a/packages/base/src/Data/Packed/Numeric.hs +++ b/packages/base/src/Data/Packed/Numeric.hs | |||
@@ -106,7 +106,7 @@ infixr 8 <ยท>, #> | |||
106 | 106 | ||
107 | {- | dot product | 107 | {- | dot product |
108 | 108 | ||
109 | >>> vect [1,2,3,4] <ยท> vect [-2,0,1,1] | 109 | >>> vector [1,2,3,4] <ยท> vector [-2,0,1,1] |
110 | 5.0 | 110 | 5.0 |
111 | 111 | ||
112 | >>> let ๐ = 0:+1 :: โ | 112 | >>> let ๐ = 0:+1 :: โ |
@@ -128,7 +128,7 @@ infixr 8 <ยท>, #> | |||
128 | [ 1.0, 2.0, 3.0 | 128 | [ 1.0, 2.0, 3.0 |
129 | , 4.0, 5.0, 6.0 ] | 129 | , 4.0, 5.0, 6.0 ] |
130 | 130 | ||
131 | >>> let v = vect [10,20,30] | 131 | >>> let v = vector [10,20,30] |
132 | 132 | ||
133 | >>> m #> v | 133 | >>> m #> v |
134 | fromList [140.0,320.0] | 134 | fromList [140.0,320.0] |
diff --git a/packages/base/src/Numeric/LinearAlgebra/Complex.hs b/packages/base/src/Numeric/Complex.hs index 17bc397..d194af3 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Complex.hs +++ b/packages/base/src/Numeric/Complex.hs | |||
@@ -14,43 +14,35 @@ | |||
14 | 14 | ||
15 | 15 | ||
16 | {- | | 16 | {- | |
17 | Module : Numeric.LinearAlgebra.Complex | 17 | Module : Numeric.HMatrix.Static.Complex |
18 | Copyright : (c) Alberto Ruiz 2006-14 | 18 | Copyright : (c) Alberto Ruiz 2006-14 |
19 | License : BSD3 | 19 | License : BSD3 |
20 | Stability : experimental | 20 | Stability : experimental |
21 | 21 | ||
22 | -} | 22 | -} |
23 | 23 | ||
24 | module Numeric.LinearAlgebra.Complex( | 24 | module Numeric.Complex( |
25 | C, | 25 | C, M, |
26 | vec2, vec3, vec4, (&), (#), | 26 | vec2, vec3, vec4, (&), (#), |
27 | vect, | 27 | vect, |
28 | R | 28 | Her, her, ๐, |
29 | ) where | 29 | ) where |
30 | 30 | ||
31 | import GHC.TypeLits | 31 | import GHC.TypeLits |
32 | import Numeric.HMatrix hiding ( | 32 | import Numeric.LinearAlgebra.Util(โ,iC) |
33 | (<>),(#>),(<ยท>),Konst(..),diag, disp,(ยฆ),(โโ),row,col,vect,mat,linspace) | 33 | import qualified Numeric.LinearAlgebra.HMatrix as LA |
34 | import qualified Numeric.HMatrix as LA | ||
35 | import Data.Proxy(Proxy) | ||
36 | import Numeric.LinearAlgebra.Static | 34 | import Numeric.LinearAlgebra.Static |
37 | 35 | ||
38 | 36 | ||
37 | ๐ :: Sized โ s c => s | ||
38 | ๐ = konst iC | ||
39 | 39 | ||
40 | instance forall n . KnownNat n => Show (C n) | 40 | newtype Her n = Her (M n n) |
41 | where | ||
42 | show (ud1 -> v) | ||
43 | | size v == 1 = "("++show (v!0)++" :: C "++show d++")" | ||
44 | | otherwise = "(vect"++ drop 8 (show v)++" :: C "++show d++")" | ||
45 | where | ||
46 | d = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | ||
47 | 41 | ||
42 | her :: KnownNat n => M n n -> Her n | ||
43 | her m = Her $ (m + LA.tr m)/2 | ||
48 | 44 | ||
49 | ud1 :: C n -> Vector โ | ||
50 | ud1 (C (Dim v)) = v | ||
51 | 45 | ||
52 | mkC :: Vector โ -> C n | ||
53 | mkC = C . Dim | ||
54 | 46 | ||
55 | 47 | ||
56 | infixl 4 & | 48 | infixl 4 & |
diff --git a/packages/base/src/Numeric/HMatrix.hs b/packages/base/src/Numeric/HMatrix.hs index ec96bfc..421333a 100644 --- a/packages/base/src/Numeric/HMatrix.hs +++ b/packages/base/src/Numeric/HMatrix.hs | |||
@@ -1,201 +1,497 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | {-# LANGUAGE DataKinds #-} |
2 | {-# LANGUAGE KindSignatures #-} | ||
3 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
4 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
5 | {-# LANGUAGE FunctionalDependencies #-} | ||
6 | {-# LANGUAGE FlexibleContexts #-} | ||
7 | {-# LANGUAGE ScopedTypeVariables #-} | ||
8 | {-# LANGUAGE EmptyDataDecls #-} | ||
9 | {-# LANGUAGE Rank2Types #-} | ||
10 | {-# LANGUAGE FlexibleInstances #-} | ||
11 | {-# LANGUAGE TypeOperators #-} | ||
12 | {-# LANGUAGE ViewPatterns #-} | ||
13 | {-# LANGUAGE GADTs #-} | ||
14 | {-# LANGUAGE OverlappingInstances #-} | ||
15 | {-# LANGUAGE TypeFamilies #-} | ||
16 | |||
17 | |||
2 | {- | | 18 | {- | |
3 | Module : Numeric.HMatrix | 19 | Module : Numeric.HMatrix |
4 | Copyright : (c) Alberto Ruiz 2006-14 | 20 | Copyright : (c) Alberto Ruiz 2006-14 |
5 | License : BSD3 | 21 | License : BSD3 |
6 | Maintainer : Alberto Ruiz | 22 | Stability : experimental |
7 | Stability : provisional | 23 | |
24 | Experimental interface for real arrays with statically checked dimensions. | ||
8 | 25 | ||
9 | -} | 26 | -} |
10 | ----------------------------------------------------------------------------- | ||
11 | module Numeric.HMatrix ( | ||
12 | |||
13 | -- * Basic types and data processing | ||
14 | module Numeric.LinearAlgebra.Data, | ||
15 | |||
16 | -- * Arithmetic and numeric classes | ||
17 | -- | | ||
18 | -- The standard numeric classes are defined elementwise: | ||
19 | -- | ||
20 | -- >>> vect [1,2,3] * vect [3,0,-2] | ||
21 | -- fromList [3.0,0.0,-6.0] | ||
22 | -- | ||
23 | -- >>> mat 3 [1..9] * ident 3 | ||
24 | -- (3><3) | ||
25 | -- [ 1.0, 0.0, 0.0 | ||
26 | -- , 0.0, 5.0, 0.0 | ||
27 | -- , 0.0, 0.0, 9.0 ] | ||
28 | -- | ||
29 | -- In arithmetic operations single-element vectors and matrices | ||
30 | -- (created from numeric literals or using 'scalar') automatically | ||
31 | -- expand to match the dimensions of the other operand: | ||
32 | -- | ||
33 | -- >>> 5 + 2*ident 3 :: Matrix Double | ||
34 | -- (3><3) | ||
35 | -- [ 7.0, 5.0, 5.0 | ||
36 | -- , 5.0, 7.0, 5.0 | ||
37 | -- , 5.0, 5.0, 7.0 ] | ||
38 | -- | ||
39 | -- >>> mat 3 [1..9] + mat 1 [10,20,30] | ||
40 | -- (3><3) | ||
41 | -- [ 11.0, 12.0, 13.0 | ||
42 | -- , 24.0, 25.0, 26.0 | ||
43 | -- , 37.0, 38.0, 39.0 ] | ||
44 | -- | ||
45 | 27 | ||
28 | module Numeric.HMatrix( | ||
29 | -- * Vector | ||
30 | R, | ||
31 | vec2, vec3, vec4, (&), (#), split, headTail, | ||
32 | vector, | ||
33 | linspace, range, dim, | ||
34 | -- * Matrix | ||
35 | L, Sq, build, | ||
36 | row, col, (ยฆ),(โโ), splitRows, splitCols, | ||
37 | unrow, uncol, | ||
38 | |||
39 | eye, | ||
40 | diagR, diag, | ||
41 | blockAt, | ||
42 | matrix, | ||
46 | -- * Products | 43 | -- * Products |
47 | -- ** dot | 44 | (<>),(#>),(<ยท>), |
48 | (<ยท>), | ||
49 | -- ** matrix-vector | ||
50 | (#>), (!#>), | ||
51 | -- ** matrix-matrix | ||
52 | (<>), | ||
53 | -- | The matrix x matrix product is also implemented in the "Data.Monoid" instance, where | ||
54 | -- single-element matrices (created from numeric literals or using 'scalar') | ||
55 | -- are used for scaling. | ||
56 | -- | ||
57 | -- >>> import Data.Monoid as M | ||
58 | -- >>> let m = mat 3 [1..6] | ||
59 | -- >>> m M.<> 2 M.<> diagl[0.5,1,0] | ||
60 | -- (2><3) | ||
61 | -- [ 1.0, 4.0, 0.0 | ||
62 | -- , 4.0, 10.0, 0.0 ] | ||
63 | -- | ||
64 | -- 'mconcat' uses 'optimiseMult' to get the optimal association order. | ||
65 | |||
66 | |||
67 | -- ** other | ||
68 | outer, kronecker, cross, | ||
69 | scale, | ||
70 | sumElements, prodElements, | ||
71 | |||
72 | -- * Linear Systems | 45 | -- * Linear Systems |
73 | (<\>), | 46 | linSolve, (<\>), |
74 | linearSolve, | 47 | -- * Factorizations |
75 | linearSolveLS, | 48 | svd, svdTall, svdFlat, Eigen(..), |
76 | linearSolveSVD, | 49 | withNullspace, |
77 | luSolve, | 50 | -- * Misc |
78 | cholSolve, | 51 | Disp(..), |
79 | cgSolve, | 52 | withVector, withMatrix, |
80 | cgSolve', | 53 | toRows, toColumns, |
54 | Sized(..), Diag(..), Sym, sym, | ||
55 | module Numeric.LinearAlgebra.HMatrix | ||
56 | ) where | ||
81 | 57 | ||
82 | -- * Inverse and pseudoinverse | ||
83 | inv, pinv, pinvTol, | ||
84 | 58 | ||
85 | -- * Determinant and rank | 59 | import GHC.TypeLits |
86 | rcond, rank, | 60 | import Numeric.LinearAlgebra.HMatrix hiding ( |
87 | det, invlndet, | 61 | (<>),(#>),(<ยท>),Konst(..),diag, disp,(ยฆ),(โโ),row,col,vector,matrix,linspace,toRows,toColumns, |
62 | (<\>),fromList,takeDiag,svd,eig,eigSH,eigSH',eigenvalues,eigenvaluesSH,eigenvaluesSH',build) | ||
63 | import qualified Numeric.LinearAlgebra.HMatrix as LA | ||
64 | import Data.Proxy(Proxy) | ||
65 | import Numeric.LinearAlgebra.Static | ||
66 | import Control.Arrow((***)) | ||
88 | 67 | ||
89 | -- * Norms | ||
90 | Normed(..), | ||
91 | norm_Frob, norm_nuclear, | ||
92 | 68 | ||
93 | -- * Nullspace and range | ||
94 | orth, | ||
95 | nullspace, null1, null1sym, | ||
96 | 69 | ||
97 | -- * SVD | ||
98 | svd, | ||
99 | fullSVD, | ||
100 | thinSVD, | ||
101 | compactSVD, | ||
102 | singularValues, | ||
103 | leftSV, rightSV, | ||
104 | 70 | ||
105 | -- * Eigensystems | ||
106 | eig, eigSH, eigSH', | ||
107 | eigenvalues, eigenvaluesSH, eigenvaluesSH', | ||
108 | geigSH', | ||
109 | 71 | ||
110 | -- * QR | 72 | ud1 :: R n -> Vector โ |
111 | qr, rq, qrRaw, qrgr, | 73 | ud1 (R (Dim v)) = v |
112 | 74 | ||
113 | -- * Cholesky | ||
114 | chol, cholSH, mbCholSH, | ||
115 | 75 | ||
116 | -- * Hessenberg | 76 | infixl 4 & |
117 | hess, | 77 | (&) :: forall n . KnownNat n |
78 | => R n -> โ -> R (n+1) | ||
79 | u & x = u # (konst x :: R 1) | ||
118 | 80 | ||
119 | -- * Schur | 81 | infixl 4 # |
120 | schur, | 82 | (#) :: forall n m . (KnownNat n, KnownNat m) |
83 | => R n -> R m -> R (n+m) | ||
84 | (R u) # (R v) = R (vconcat u v) | ||
121 | 85 | ||
122 | -- * LU | ||
123 | lu, luPacked, | ||
124 | 86 | ||
125 | -- * Matrix functions | ||
126 | expm, | ||
127 | sqrtm, | ||
128 | matFunc, | ||
129 | 87 | ||
130 | -- * Correlation and convolution | 88 | vec2 :: โ -> โ -> R 2 |
131 | corr, conv, corrMin, corr2, conv2, | 89 | vec2 a b = R (gvec2 a b) |
132 | 90 | ||
133 | -- * Random arrays | 91 | vec3 :: โ -> โ -> โ -> R 3 |
92 | vec3 a b c = R (gvec3 a b c) | ||
93 | |||
94 | |||
95 | vec4 :: โ -> โ -> โ -> โ -> R 4 | ||
96 | vec4 a b c d = R (gvec4 a b c d) | ||
97 | |||
98 | vector :: KnownNat n => [โ] -> R n | ||
99 | vector = fromList | ||
100 | |||
101 | matrix :: (KnownNat m, KnownNat n) => [โ] -> L m n | ||
102 | matrix = fromList | ||
103 | |||
104 | linspace :: forall n . KnownNat n => (โ,โ) -> R n | ||
105 | linspace (a,b) = mkR (LA.linspace d (a,b)) | ||
106 | where | ||
107 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
108 | |||
109 | range :: forall n . KnownNat n => R n | ||
110 | range = mkR (LA.linspace d (1,fromIntegral d)) | ||
111 | where | ||
112 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
113 | |||
114 | dim :: forall n . KnownNat n => R n | ||
115 | dim = mkR (scalar d) | ||
116 | where | ||
117 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
118 | |||
119 | |||
120 | -------------------------------------------------------------------------------- | ||
121 | |||
122 | |||
123 | ud2 :: L m n -> Matrix โ | ||
124 | ud2 (L (Dim (Dim x))) = x | ||
125 | |||
126 | |||
127 | -------------------------------------------------------------------------------- | ||
128 | -------------------------------------------------------------------------------- | ||
129 | |||
130 | diagR :: forall m n k . (KnownNat m, KnownNat n, KnownNat k) => โ -> R k -> L m n | ||
131 | diagR x v = mkL (asRow (vjoin [scalar x, ev, zeros])) | ||
132 | where | ||
133 | ev = extract v | ||
134 | zeros = LA.konst x (max 0 ((min m' n') - size ev)) | ||
135 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
136 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
137 | |||
138 | diag :: KnownNat n => R n -> Sq n | ||
139 | diag = diagR 0 | ||
140 | |||
141 | eye :: KnownNat n => Sq n | ||
142 | eye = diag 1 | ||
143 | |||
144 | -------------------------------------------------------------------------------- | ||
145 | |||
146 | blockAt :: forall m n . (KnownNat m, KnownNat n) => โ -> Int -> Int -> Matrix Double -> L m n | ||
147 | blockAt x r c a = mkL res | ||
148 | where | ||
149 | z = scalar x | ||
150 | z1 = LA.konst x (r,c) | ||
151 | z2 = LA.konst x (max 0 (m'-(ra+r)), max 0 (n'-(ca+c))) | ||
152 | ra = min (rows a) . max 0 $ m'-r | ||
153 | ca = min (cols a) . max 0 $ n'-c | ||
154 | sa = subMatrix (0,0) (ra, ca) a | ||
155 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
156 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
157 | res = fromBlocks [[z1,z,z],[z,sa,z],[z,z,z2]] | ||
158 | |||
159 | |||
160 | |||
161 | |||
162 | |||
163 | -------------------------------------------------------------------------------- | ||
164 | |||
165 | |||
166 | row :: R n -> L 1 n | ||
167 | row = mkL . asRow . ud1 | ||
168 | |||
169 | --col :: R n -> L n 1 | ||
170 | col v = tr . row $ v | ||
171 | |||
172 | unrow :: L 1 n -> R n | ||
173 | unrow = mkR . head . LA.toRows . ud2 | ||
174 | |||
175 | --uncol :: L n 1 -> R n | ||
176 | uncol v = unrow . tr $ v | ||
177 | |||
178 | |||
179 | infixl 2 โโ | ||
180 | (โโ) :: (KnownNat r1, KnownNat r2, KnownNat c) => L r1 c -> L r2 c -> L (r1+r2) c | ||
181 | a โโ b = mkL (extract a LA.โโ extract b) | ||
182 | |||
183 | |||
184 | infixl 3 ยฆ | ||
185 | -- (ยฆ) :: (KnownNat r, KnownNat c1, KnownNat c2) => L r c1 -> L r c2 -> L r (c1+c2) | ||
186 | a ยฆ b = tr (tr a โโ tr b) | ||
187 | |||
188 | |||
189 | type Sq n = L n n | ||
190 | --type CSq n = CL n n | ||
191 | |||
192 | type GL = (KnownNat n, KnownNat m) => L m n | ||
193 | type GSq = KnownNat n => Sq n | ||
194 | |||
195 | isKonst :: forall m n . (KnownNat m, KnownNat n) => L m n -> Maybe (โ,(Int,Int)) | ||
196 | isKonst (unwrap -> x) | ||
197 | | singleM x = Just (x `atIndex` (0,0), (m',n')) | ||
198 | | otherwise = Nothing | ||
199 | where | ||
200 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | ||
201 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | ||
134 | 202 | ||
135 | Seed, RandDist(..), randomVector, rand, randn, gaussianSample, uniformSample, | ||
136 | 203 | ||
137 | -- * Misc | ||
138 | meanCov, peps, relativeError, haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv, | ||
139 | โ,โ,iC, | ||
140 | -- * Auxiliary classes | ||
141 | Element, Container, Product, Numeric, LSDiv, | ||
142 | Complexable, RealElement, | ||
143 | RealOf, ComplexOf, SingleOf, DoubleOf, | ||
144 | IndexOf, | ||
145 | Field, | ||
146 | -- Normed, | ||
147 | Transposable, | ||
148 | CGState(..), | ||
149 | Testable(..) | ||
150 | ) where | ||
151 | 204 | ||
152 | import Numeric.LinearAlgebra.Data | ||
153 | |||
154 | import Numeric.Matrix() | ||
155 | import Numeric.Vector() | ||
156 | import Data.Packed.Numeric hiding ((<>)) | ||
157 | import Numeric.LinearAlgebra.Algorithms hiding (linearSolve,Normed,orth) | ||
158 | import qualified Numeric.LinearAlgebra.Algorithms as A | ||
159 | import Numeric.LinearAlgebra.Util | ||
160 | import Numeric.LinearAlgebra.Random | ||
161 | import Numeric.Sparse((!#>)) | ||
162 | import Numeric.LinearAlgebra.Util.CG | ||
163 | |||
164 | {- | dense matrix product | ||
165 | |||
166 | >>> let a = (3><5) [1..] | ||
167 | >>> a | ||
168 | (3><5) | ||
169 | [ 1.0, 2.0, 3.0, 4.0, 5.0 | ||
170 | , 6.0, 7.0, 8.0, 9.0, 10.0 | ||
171 | , 11.0, 12.0, 13.0, 14.0, 15.0 ] | ||
172 | |||
173 | >>> let b = (5><2) [1,3, 0,2, -1,5, 7,7, 6,0] | ||
174 | >>> b | ||
175 | (5><2) | ||
176 | [ 1.0, 3.0 | ||
177 | , 0.0, 2.0 | ||
178 | , -1.0, 5.0 | ||
179 | , 7.0, 7.0 | ||
180 | , 6.0, 0.0 ] | ||
181 | |||
182 | >>> a <> b | ||
183 | (3><2) | ||
184 | [ 56.0, 50.0 | ||
185 | , 121.0, 135.0 | ||
186 | , 186.0, 220.0 ] | ||
187 | 205 | ||
188 | -} | ||
189 | (<>) :: Numeric t => Matrix t -> Matrix t -> Matrix t | ||
190 | (<>) = mXm | ||
191 | infixr 8 <> | 206 | infixr 8 <> |
207 | (<>) :: forall m k n. (KnownNat m, KnownNat k, KnownNat n) => L m k -> L k n -> L m n | ||
208 | |||
209 | (isKonst -> Just (a,(_,k))) <> (isKonst -> Just (b,_)) = konst (a * b * fromIntegral k) | ||
210 | |||
211 | (isDiag -> Just (0,a,_)) <> (isDiag -> Just (0,b,_)) = diagR 0 (mkR v :: R k) | ||
212 | where | ||
213 | v = a' * b' | ||
214 | n = min (size a) (size b) | ||
215 | a' = subVector 0 n a | ||
216 | b' = subVector 0 n b | ||
217 | |||
218 | (isDiag -> Just (0,a,_)) <> (extract -> b) = mkL (asColumn a * takeRows (size a) b) | ||
219 | |||
220 | (extract -> a) <> (isDiag -> Just (0,b,_)) = mkL (takeColumns (size b) a * asRow b) | ||
221 | |||
222 | a <> b = mkL (extract a LA.<> extract b) | ||
223 | |||
224 | infixr 8 #> | ||
225 | (#>) :: (KnownNat m, KnownNat n) => L m n -> R n -> R m | ||
226 | (isDiag -> Just (0, w, _)) #> v = mkR (w * subVector 0 (size w) (extract v)) | ||
227 | m #> v = mkR (extract m LA.#> extract v) | ||
228 | |||
229 | |||
230 | infixr 8 <ยท> | ||
231 | (<ยท>) :: R n -> R n -> โ | ||
232 | (ud1 -> u) <ยท> (ud1 -> v) | ||
233 | | singleV u || singleV v = sumElements (u * v) | ||
234 | | otherwise = udot u v | ||
235 | |||
236 | -------------------------------------------------------------------------------- | ||
237 | |||
238 | {- | ||
239 | class Minim (n :: Nat) (m :: Nat) | ||
240 | where | ||
241 | type Mini n m :: Nat | ||
242 | |||
243 | instance forall (n :: Nat) . Minim n n | ||
244 | where | ||
245 | type Mini n n = n | ||
246 | |||
247 | |||
248 | instance forall (n :: Nat) (m :: Nat) . (n <= m+1) => Minim n m | ||
249 | where | ||
250 | type Mini n m = n | ||
251 | |||
252 | instance forall (n :: Nat) (m :: Nat) . (m <= n+1) => Minim n m | ||
253 | where | ||
254 | type Mini n m = m | ||
255 | -} | ||
256 | |||
257 | class Diag m d | m -> d | ||
258 | where | ||
259 | takeDiag :: m -> d | ||
260 | |||
261 | |||
262 | |||
263 | instance forall n . (KnownNat n) => Diag (L n n) (R n) | ||
264 | where | ||
265 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
266 | |||
267 | |||
268 | instance forall m n . (KnownNat m, KnownNat n, m <= n+1) => Diag (L m n) (R m) | ||
269 | where | ||
270 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
271 | |||
272 | |||
273 | instance forall m n . (KnownNat m, KnownNat n, n <= m+1) => Diag (L m n) (R n) | ||
274 | where | ||
275 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
276 | |||
277 | |||
278 | -------------------------------------------------------------------------------- | ||
279 | |||
280 | linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> Maybe (L m n) | ||
281 | linSolve (extract -> a) (extract -> b) = fmap mkL (LA.linearSolve a b) | ||
282 | |||
283 | (<\>) :: (KnownNat m, KnownNat n, KnownNat r) => L m n -> L m r -> L n r | ||
284 | (extract -> a) <\> (extract -> b) = mkL (a LA.<\> b) | ||
285 | |||
286 | svd :: (KnownNat m, KnownNat n) => L m n -> (L m m, R n, L n n) | ||
287 | svd (extract -> m) = (mkL u, mkR s', mkL v) | ||
288 | where | ||
289 | (u,s,v) = LA.svd m | ||
290 | s' = vjoin [s, z] | ||
291 | z = LA.konst 0 (max 0 (cols m - size s)) | ||
292 | |||
293 | |||
294 | svdTall :: (KnownNat m, KnownNat n, n <= m) => L m n -> (L m n, R n, L n n) | ||
295 | svdTall (extract -> m) = (mkL u, mkR s, mkL v) | ||
296 | where | ||
297 | (u,s,v) = LA.thinSVD m | ||
298 | |||
299 | |||
300 | svdFlat :: (KnownNat m, KnownNat n, m <= n) => L m n -> (L m m, R m, L n m) | ||
301 | svdFlat (extract -> m) = (mkL u, mkR s, mkL v) | ||
302 | where | ||
303 | (u,s,v) = LA.thinSVD m | ||
304 | |||
305 | -------------------------------------------------------------------------------- | ||
306 | |||
307 | class Eigen m l v | m -> l, m -> v | ||
308 | where | ||
309 | eigensystem :: m -> (l,v) | ||
310 | eigenvalues :: m -> l | ||
311 | |||
312 | newtype Sym n = Sym (Sq n) deriving Show | ||
313 | |||
314 | |||
315 | sym :: KnownNat n => Sq n -> Sym n | ||
316 | sym m = Sym $ (m + tr m)/2 | ||
317 | |||
318 | |||
319 | |||
320 | instance KnownNat n => Eigen (Sym n) (R n) (L n n) | ||
321 | where | ||
322 | eigenvalues (Sym (extract -> m)) = mkR . LA.eigenvaluesSH' $ m | ||
323 | eigensystem (Sym (extract -> m)) = (mkR l, mkL v) | ||
324 | where | ||
325 | (l,v) = LA.eigSH' m | ||
326 | |||
327 | instance KnownNat n => Eigen (Sq n) (C n) (M n n) | ||
328 | where | ||
329 | eigenvalues (extract -> m) = mkC . LA.eigenvalues $ m | ||
330 | eigensystem (extract -> m) = (mkC l, mkM v) | ||
331 | where | ||
332 | (l,v) = LA.eig m | ||
333 | |||
334 | -------------------------------------------------------------------------------- | ||
335 | |||
336 | |||
337 | withNullspace | ||
338 | :: forall m n z . (KnownNat m, KnownNat n) | ||
339 | => L m n | ||
340 | -> (forall k . (KnownNat k) => L n k -> z) | ||
341 | -> z | ||
342 | withNullspace (LA.nullspace . extract -> a) f = | ||
343 | case someNatVal $ fromIntegral $ cols a of | ||
344 | Nothing -> error "static/dynamic mismatch" | ||
345 | Just (SomeNat (_ :: Proxy k)) -> f (mkL a :: L n k) | ||
346 | |||
347 | -------------------------------------------------------------------------------- | ||
348 | |||
349 | split :: forall p n . (KnownNat p, KnownNat n, p<=n) => R n -> (R p, R (n-p)) | ||
350 | split (extract -> v) = ( mkR (subVector 0 p' v) , | ||
351 | mkR (subVector p' (size v - p') v) ) | ||
352 | where | ||
353 | p' = fromIntegral . natVal $ (undefined :: Proxy p) :: Int | ||
354 | |||
355 | |||
356 | headTail :: (KnownNat n, 1<=n) => R n -> (โ, R (n-1)) | ||
357 | headTail = ((!0) . extract *** id) . split | ||
358 | |||
359 | |||
360 | splitRows :: forall p m n . (KnownNat p, KnownNat m, KnownNat n, p<=m) => L m n -> (L p n, L (m-p) n) | ||
361 | splitRows (extract -> x) = ( mkL (takeRows p' x) , | ||
362 | mkL (dropRows p' x) ) | ||
363 | where | ||
364 | p' = fromIntegral . natVal $ (undefined :: Proxy p) :: Int | ||
365 | |||
366 | splitCols :: forall p m n. (KnownNat p, KnownNat m, KnownNat n, KnownNat (n-p), p<=n) => L m n -> (L m p, L m (n-p)) | ||
367 | splitCols = (tr *** tr) . splitRows . tr | ||
368 | |||
369 | |||
370 | toRows :: forall m n . (KnownNat m, KnownNat n) => L m n -> [R n] | ||
371 | toRows (LA.toRows . extract -> vs) = map mkR vs | ||
372 | |||
373 | |||
374 | toColumns :: forall m n . (KnownNat m, KnownNat n) => L m n -> [R m] | ||
375 | toColumns (LA.toColumns . extract -> vs) = map mkR vs | ||
376 | |||
377 | |||
378 | splittest | ||
379 | = do | ||
380 | let v = range :: R 7 | ||
381 | a = snd (split v) :: R 4 | ||
382 | print $ a | ||
383 | print $ snd . headTail . snd . headTail $ v | ||
384 | print $ first (vec3 1 2 3) | ||
385 | print $ second (vec3 1 2 3) | ||
386 | print $ third (vec3 1 2 3) | ||
387 | print $ (snd $ splitRows eye :: L 4 6) | ||
388 | where | ||
389 | first v = fst . headTail $ v | ||
390 | second v = first . snd . headTail $ v | ||
391 | third v = first . snd . headTail . snd . headTail $ v | ||
392 | |||
393 | -------------------------------------------------------------------------------- | ||
394 | |||
395 | build | ||
396 | :: forall m n . (KnownNat n, KnownNat m) | ||
397 | => (โ -> โ -> โ) | ||
398 | -> L m n | ||
399 | build f = mkL $ LA.build (m',n') f | ||
400 | where | ||
401 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | ||
402 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | ||
192 | 403 | ||
193 | -- | Solve a linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, returning Nothing for a singular system. For underconstrained or overconstrained systems use 'linearSolveLS' or 'linearSolveSVD'. | 404 | -------------------------------------------------------------------------------- |
194 | linearSolve m b = A.mbLinearSolve m b | ||
195 | 405 | ||
196 | -- | return an orthonormal basis of the null space of a matrix. See also 'nullspaceSVD'. | 406 | withVector |
197 | nullspace m = nullspaceSVD (Left (1*eps)) m (rightSV m) | 407 | :: forall z |
408 | . Vector โ | ||
409 | -> (forall n . (KnownNat n) => R n -> z) | ||
410 | -> z | ||
411 | withVector v f = | ||
412 | case someNatVal $ fromIntegral $ size v of | ||
413 | Nothing -> error "static/dynamic mismatch" | ||
414 | Just (SomeNat (_ :: Proxy m)) -> f (mkR v :: R m) | ||
415 | |||
416 | |||
417 | withMatrix | ||
418 | :: forall z | ||
419 | . Matrix โ | ||
420 | -> (forall m n . (KnownNat m, KnownNat n) => L m n -> z) | ||
421 | -> z | ||
422 | withMatrix a f = | ||
423 | case someNatVal $ fromIntegral $ rows a of | ||
424 | Nothing -> error "static/dynamic mismatch" | ||
425 | Just (SomeNat (_ :: Proxy m)) -> | ||
426 | case someNatVal $ fromIntegral $ cols a of | ||
427 | Nothing -> error "static/dynamic mismatch" | ||
428 | Just (SomeNat (_ :: Proxy n)) -> | ||
429 | f (mkL a :: L m n) | ||
430 | |||
431 | |||
432 | -------------------------------------------------------------------------------- | ||
433 | |||
434 | test :: (Bool, IO ()) | ||
435 | test = (ok,info) | ||
436 | where | ||
437 | ok = extract (eye :: Sq 5) == ident 5 | ||
438 | && unwrap (mTm sm :: Sq 3) == tr ((3><3)[1..]) LA.<> (3><3)[1..] | ||
439 | && unwrap (tm :: L 3 5) == LA.matrix 5 [1..15] | ||
440 | && thingS == thingD | ||
441 | && precS == precD | ||
442 | && withVector (LA.vector [1..15]) sumV == sumElements (LA.fromList [1..15]) | ||
443 | |||
444 | info = do | ||
445 | print $ u | ||
446 | print $ v | ||
447 | print (eye :: Sq 3) | ||
448 | print $ ((u & 5) + 1) <ยท> v | ||
449 | print (tm :: L 2 5) | ||
450 | print (tm <> sm :: L 2 3) | ||
451 | print thingS | ||
452 | print thingD | ||
453 | print precS | ||
454 | print precD | ||
455 | print $ withVector (LA.vector [1..15]) sumV | ||
456 | splittest | ||
457 | |||
458 | sumV w = w <ยท> konst 1 | ||
459 | |||
460 | u = vec2 3 5 | ||
461 | |||
462 | ๐ง x = vector [x] :: R 1 | ||
463 | |||
464 | v = ๐ง 2 & 4 & 7 | ||
465 | |||
466 | -- mTm :: L n m -> Sq m | ||
467 | mTm a = tr a <> a | ||
468 | |||
469 | tm :: GL | ||
470 | tm = lmat 0 [1..] | ||
471 | |||
472 | lmat :: forall m n . (KnownNat m, KnownNat n) => โ -> [โ] -> L m n | ||
473 | lmat z xs = mkL . reshape n' . LA.fromList . take (m'*n') $ xs ++ repeat z | ||
474 | where | ||
475 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
476 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
477 | |||
478 | sm :: GSq | ||
479 | sm = lmat 0 [1..] | ||
480 | |||
481 | thingS = (u & 1) <ยท> tr q #> q #> v | ||
482 | where | ||
483 | q = tm :: L 10 3 | ||
484 | |||
485 | thingD = vjoin [ud1 u, 1] LA.<ยท> tr m LA.#> m LA.#> ud1 v | ||
486 | where | ||
487 | m = LA.matrix 3 [1..30] | ||
488 | |||
489 | precS = (1::Double) + (2::Double) * ((1 :: R 3) * (u & 6)) <ยท> konst 2 #> v | ||
490 | precD = 1 + 2 * vjoin[ud1 u, 6] LA.<ยท> LA.konst 2 (size (ud1 u) +1, size (ud1 v)) LA.#> ud1 v | ||
491 | |||
492 | |||
493 | instance (KnownNat n', KnownNat m') => Testable (L n' m') | ||
494 | where | ||
495 | checkT _ = test | ||
198 | 496 | ||
199 | -- | return an orthonormal basis of the range space of a matrix. See also 'orthSVD'. | ||
200 | orth m = orthSVD (Left (1*eps)) m (leftSV m) | ||
201 | 497 | ||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs index 20f3b81..4e4868a 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Data.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs | |||
@@ -16,11 +16,11 @@ module Numeric.LinearAlgebra.Data( | |||
16 | -- * Vector | 16 | -- * Vector |
17 | -- | 1D arrays are storable vectors from the vector package. | 17 | -- | 1D arrays are storable vectors from the vector package. |
18 | 18 | ||
19 | vect, (|>), | 19 | vector, (|>), |
20 | 20 | ||
21 | -- * Matrix | 21 | -- * Matrix |
22 | 22 | ||
23 | mat, (><), tr, | 23 | matrix, (><), tr, |
24 | 24 | ||
25 | -- * Indexing | 25 | -- * Indexing |
26 | 26 | ||
diff --git a/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs b/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs new file mode 100644 index 0000000..54ddd68 --- /dev/null +++ b/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs | |||
@@ -0,0 +1,201 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | {- | | ||
3 | Module : Numeric.LinearAlgebra.HMatrix | ||
4 | Copyright : (c) Alberto Ruiz 2006-14 | ||
5 | License : BSD3 | ||
6 | Maintainer : Alberto Ruiz | ||
7 | Stability : provisional | ||
8 | |||
9 | -} | ||
10 | ----------------------------------------------------------------------------- | ||
11 | module Numeric.LinearAlgebra.HMatrix ( | ||
12 | |||
13 | -- * Basic types and data processing | ||
14 | module Numeric.LinearAlgebra.Data, | ||
15 | |||
16 | -- * Arithmetic and numeric classes | ||
17 | -- | | ||
18 | -- The standard numeric classes are defined elementwise: | ||
19 | -- | ||
20 | -- >>> vector [1,2,3] * vector [3,0,-2] | ||
21 | -- fromList [3.0,0.0,-6.0] | ||
22 | -- | ||
23 | -- >>> matrix 3 [1..9] * ident 3 | ||
24 | -- (3><3) | ||
25 | -- [ 1.0, 0.0, 0.0 | ||
26 | -- , 0.0, 5.0, 0.0 | ||
27 | -- , 0.0, 0.0, 9.0 ] | ||
28 | -- | ||
29 | -- In arithmetic operations single-element vectors and matrices | ||
30 | -- (created from numeric literals or using 'scalar') automatically | ||
31 | -- expand to match the dimensions of the other operand: | ||
32 | -- | ||
33 | -- >>> 5 + 2*ident 3 :: Matrix Double | ||
34 | -- (3><3) | ||
35 | -- [ 7.0, 5.0, 5.0 | ||
36 | -- , 5.0, 7.0, 5.0 | ||
37 | -- , 5.0, 5.0, 7.0 ] | ||
38 | -- | ||
39 | -- >>> matrix 3 [1..9] + matrix 1 [10,20,30] | ||
40 | -- (3><3) | ||
41 | -- [ 11.0, 12.0, 13.0 | ||
42 | -- , 24.0, 25.0, 26.0 | ||
43 | -- , 37.0, 38.0, 39.0 ] | ||
44 | -- | ||
45 | |||
46 | -- * Products | ||
47 | -- ** dot | ||
48 | (<ยท>), | ||
49 | -- ** matrix-vector | ||
50 | (#>), (!#>), | ||
51 | -- ** matrix-matrix | ||
52 | (<>), | ||
53 | -- | The matrix x matrix product is also implemented in the "Data.Monoid" instance, where | ||
54 | -- single-element matrices (created from numeric literals or using 'scalar') | ||
55 | -- are used for scaling. | ||
56 | -- | ||
57 | -- >>> import Data.Monoid as M | ||
58 | -- >>> let m = matrix 3 [1..6] | ||
59 | -- >>> m M.<> 2 M.<> diagl[0.5,1,0] | ||
60 | -- (2><3) | ||
61 | -- [ 1.0, 4.0, 0.0 | ||
62 | -- , 4.0, 10.0, 0.0 ] | ||
63 | -- | ||
64 | -- 'mconcat' uses 'optimiseMult' to get the optimal association order. | ||
65 | |||
66 | |||
67 | -- ** other | ||
68 | outer, kronecker, cross, | ||
69 | scale, | ||
70 | sumElements, prodElements, | ||
71 | |||
72 | -- * Linear Systems | ||
73 | (<\>), | ||
74 | linearSolve, | ||
75 | linearSolveLS, | ||
76 | linearSolveSVD, | ||
77 | luSolve, | ||
78 | cholSolve, | ||
79 | cgSolve, | ||
80 | cgSolve', | ||
81 | |||
82 | -- * Inverse and pseudoinverse | ||
83 | inv, pinv, pinvTol, | ||
84 | |||
85 | -- * Determinant and rank | ||
86 | rcond, rank, | ||
87 | det, invlndet, | ||
88 | |||
89 | -- * Norms | ||
90 | Normed(..), | ||
91 | norm_Frob, norm_nuclear, | ||
92 | |||
93 | -- * Nullspace and range | ||
94 | orth, | ||
95 | nullspace, null1, null1sym, | ||
96 | |||
97 | -- * SVD | ||
98 | svd, | ||
99 | fullSVD, | ||
100 | thinSVD, | ||
101 | compactSVD, | ||
102 | singularValues, | ||
103 | leftSV, rightSV, | ||
104 | |||
105 | -- * Eigensystems | ||
106 | eig, eigSH, eigSH', | ||
107 | eigenvalues, eigenvaluesSH, eigenvaluesSH', | ||
108 | geigSH', | ||
109 | |||
110 | -- * QR | ||
111 | qr, rq, qrRaw, qrgr, | ||
112 | |||
113 | -- * Cholesky | ||
114 | chol, cholSH, mbCholSH, | ||
115 | |||
116 | -- * Hessenberg | ||
117 | hess, | ||
118 | |||
119 | -- * Schur | ||
120 | schur, | ||
121 | |||
122 | -- * LU | ||
123 | lu, luPacked, | ||
124 | |||
125 | -- * Matrix functions | ||
126 | expm, | ||
127 | sqrtm, | ||
128 | matFunc, | ||
129 | |||
130 | -- * Correlation and convolution | ||
131 | corr, conv, corrMin, corr2, conv2, | ||
132 | |||
133 | -- * Random arrays | ||
134 | |||
135 | Seed, RandDist(..), randomVector, rand, randn, gaussianSample, uniformSample, | ||
136 | |||
137 | -- * Misc | ||
138 | meanCov, peps, relativeError, haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv, | ||
139 | โ,โ,iC, | ||
140 | -- * Auxiliary classes | ||
141 | Element, Container, Product, Numeric, LSDiv, | ||
142 | Complexable, RealElement, | ||
143 | RealOf, ComplexOf, SingleOf, DoubleOf, | ||
144 | IndexOf, | ||
145 | Field, | ||
146 | -- Normed, | ||
147 | Transposable, | ||
148 | CGState(..), | ||
149 | Testable(..) | ||
150 | ) where | ||
151 | |||
152 | import Numeric.LinearAlgebra.Data | ||
153 | |||
154 | import Numeric.Matrix() | ||
155 | import Numeric.Vector() | ||
156 | import Data.Packed.Numeric hiding ((<>)) | ||
157 | import Numeric.LinearAlgebra.Algorithms hiding (linearSolve,Normed,orth) | ||
158 | import qualified Numeric.LinearAlgebra.Algorithms as A | ||
159 | import Numeric.LinearAlgebra.Util | ||
160 | import Numeric.LinearAlgebra.Random | ||
161 | import Numeric.Sparse((!#>)) | ||
162 | import Numeric.LinearAlgebra.Util.CG | ||
163 | |||
164 | {- | dense matrix product | ||
165 | |||
166 | >>> let a = (3><5) [1..] | ||
167 | >>> a | ||
168 | (3><5) | ||
169 | [ 1.0, 2.0, 3.0, 4.0, 5.0 | ||
170 | , 6.0, 7.0, 8.0, 9.0, 10.0 | ||
171 | , 11.0, 12.0, 13.0, 14.0, 15.0 ] | ||
172 | |||
173 | >>> let b = (5><2) [1,3, 0,2, -1,5, 7,7, 6,0] | ||
174 | >>> b | ||
175 | (5><2) | ||
176 | [ 1.0, 3.0 | ||
177 | , 0.0, 2.0 | ||
178 | , -1.0, 5.0 | ||
179 | , 7.0, 7.0 | ||
180 | , 6.0, 0.0 ] | ||
181 | |||
182 | >>> a <> b | ||
183 | (3><2) | ||
184 | [ 56.0, 50.0 | ||
185 | , 121.0, 135.0 | ||
186 | , 186.0, 220.0 ] | ||
187 | |||
188 | -} | ||
189 | (<>) :: Numeric t => Matrix t -> Matrix t -> Matrix t | ||
190 | (<>) = mXm | ||
191 | infixr 8 <> | ||
192 | |||
193 | -- | Solve a linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, returning Nothing for a singular system. For underconstrained or overconstrained systems use 'linearSolveLS' or 'linearSolveSVD'. | ||
194 | linearSolve m b = A.mbLinearSolve m b | ||
195 | |||
196 | -- | return an orthonormal basis of the null space of a matrix. See also 'nullspaceSVD'. | ||
197 | nullspace m = nullspaceSVD (Left (1*eps)) m (rightSV m) | ||
198 | |||
199 | -- | return an orthonormal basis of the range space of a matrix. See also 'orthSVD'. | ||
200 | orth m = orthSVD (Left (1*eps)) m (leftSV m) | ||
201 | |||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Real.hs b/packages/base/src/Numeric/LinearAlgebra/Real.hs deleted file mode 100644 index 97c462e..0000000 --- a/packages/base/src/Numeric/LinearAlgebra/Real.hs +++ /dev/null | |||
@@ -1,480 +0,0 @@ | |||
1 | {-# LANGUAGE DataKinds #-} | ||
2 | {-# LANGUAGE KindSignatures #-} | ||
3 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
4 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
5 | {-# LANGUAGE FunctionalDependencies #-} | ||
6 | {-# LANGUAGE FlexibleContexts #-} | ||
7 | {-# LANGUAGE ScopedTypeVariables #-} | ||
8 | {-# LANGUAGE EmptyDataDecls #-} | ||
9 | {-# LANGUAGE Rank2Types #-} | ||
10 | {-# LANGUAGE FlexibleInstances #-} | ||
11 | {-# LANGUAGE TypeOperators #-} | ||
12 | {-# LANGUAGE ViewPatterns #-} | ||
13 | {-# LANGUAGE GADTs #-} | ||
14 | {-# LANGUAGE OverlappingInstances #-} | ||
15 | {-# LANGUAGE TypeFamilies #-} | ||
16 | |||
17 | |||
18 | {- | | ||
19 | Module : Numeric.LinearAlgebra.Real | ||
20 | Copyright : (c) Alberto Ruiz 2006-14 | ||
21 | License : BSD3 | ||
22 | Stability : experimental | ||
23 | |||
24 | Experimental interface for real arrays with statically checked dimensions. | ||
25 | |||
26 | -} | ||
27 | |||
28 | module Numeric.LinearAlgebra.Real( | ||
29 | -- * Vector | ||
30 | R, C, | ||
31 | vec2, vec3, vec4, (&), (#), split, headTail, | ||
32 | vector, | ||
33 | linspace, range, dim, | ||
34 | -- * Matrix | ||
35 | L, Sq, M, def, | ||
36 | row, col, (ยฆ),(โโ), splitRows, splitCols, | ||
37 | unrow, uncol, | ||
38 | |||
39 | eye, | ||
40 | diagR, diag, | ||
41 | blockAt, | ||
42 | matrix, | ||
43 | -- * Products | ||
44 | (<>),(#>),(<ยท>), | ||
45 | -- * Linear Systems | ||
46 | linSolve, (<\>), | ||
47 | -- * Factorizations | ||
48 | svd, svdTall, svdFlat, Eigen(..), | ||
49 | -- * Pretty printing | ||
50 | Disp(..), | ||
51 | -- * Misc | ||
52 | withVector, withMatrix, | ||
53 | Sized(..), Diag(..), Sym, sym, Her, her, ๐, | ||
54 | module Numeric.HMatrix | ||
55 | ) where | ||
56 | |||
57 | |||
58 | import GHC.TypeLits | ||
59 | import Numeric.HMatrix hiding ( | ||
60 | (<>),(#>),(<ยท>),Konst(..),diag, disp,(ยฆ),(โโ),row,col,vect,mat,linspace, | ||
61 | (<\>),fromList,takeDiag,svd,eig,eigSH,eigSH',eigenvalues,eigenvaluesSH,eigenvaluesSH',build) | ||
62 | import qualified Numeric.HMatrix as LA | ||
63 | import Data.Proxy(Proxy) | ||
64 | import Numeric.LinearAlgebra.Static | ||
65 | import Control.Arrow((***)) | ||
66 | |||
67 | |||
68 | ๐ :: Sized โ s c => s | ||
69 | ๐ = konst iC | ||
70 | |||
71 | |||
72 | |||
73 | |||
74 | |||
75 | ud1 :: R n -> Vector โ | ||
76 | ud1 (R (Dim v)) = v | ||
77 | |||
78 | |||
79 | infixl 4 & | ||
80 | (&) :: forall n . KnownNat n | ||
81 | => R n -> โ -> R (n+1) | ||
82 | u & x = u # (konst x :: R 1) | ||
83 | |||
84 | infixl 4 # | ||
85 | (#) :: forall n m . (KnownNat n, KnownNat m) | ||
86 | => R n -> R m -> R (n+m) | ||
87 | (R u) # (R v) = R (vconcat u v) | ||
88 | |||
89 | |||
90 | |||
91 | vec2 :: โ -> โ -> R 2 | ||
92 | vec2 a b = R (gvec2 a b) | ||
93 | |||
94 | vec3 :: โ -> โ -> โ -> R 3 | ||
95 | vec3 a b c = R (gvec3 a b c) | ||
96 | |||
97 | |||
98 | vec4 :: โ -> โ -> โ -> โ -> R 4 | ||
99 | vec4 a b c d = R (gvec4 a b c d) | ||
100 | |||
101 | vector :: KnownNat n => [โ] -> R n | ||
102 | vector = fromList | ||
103 | |||
104 | matrix :: (KnownNat m, KnownNat n) => [โ] -> L m n | ||
105 | matrix = fromList | ||
106 | |||
107 | linspace :: forall n . KnownNat n => (โ,โ) -> R n | ||
108 | linspace (a,b) = mkR (LA.linspace d (a,b)) | ||
109 | where | ||
110 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
111 | |||
112 | range :: forall n . KnownNat n => R n | ||
113 | range = mkR (LA.linspace d (1,fromIntegral d)) | ||
114 | where | ||
115 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
116 | |||
117 | dim :: forall n . KnownNat n => R n | ||
118 | dim = mkR (scalar d) | ||
119 | where | ||
120 | d = fromIntegral . natVal $ (undefined :: Proxy n) | ||
121 | |||
122 | |||
123 | -------------------------------------------------------------------------------- | ||
124 | |||
125 | |||
126 | ud2 :: L m n -> Matrix โ | ||
127 | ud2 (L (Dim (Dim x))) = x | ||
128 | |||
129 | |||
130 | -------------------------------------------------------------------------------- | ||
131 | -------------------------------------------------------------------------------- | ||
132 | |||
133 | diagR :: forall m n k . (KnownNat m, KnownNat n, KnownNat k) => โ -> R k -> L m n | ||
134 | diagR x v = mkL (asRow (vjoin [scalar x, ev, zeros])) | ||
135 | where | ||
136 | ev = extract v | ||
137 | zeros = LA.konst x (max 0 ((min m' n') - size ev)) | ||
138 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
139 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
140 | |||
141 | diag :: KnownNat n => R n -> Sq n | ||
142 | diag = diagR 0 | ||
143 | |||
144 | eye :: KnownNat n => Sq n | ||
145 | eye = diag 1 | ||
146 | |||
147 | -------------------------------------------------------------------------------- | ||
148 | |||
149 | blockAt :: forall m n . (KnownNat m, KnownNat n) => โ -> Int -> Int -> Matrix Double -> L m n | ||
150 | blockAt x r c a = mkL res | ||
151 | where | ||
152 | z = scalar x | ||
153 | z1 = LA.konst x (r,c) | ||
154 | z2 = LA.konst x (max 0 (m'-(ra+r)), max 0 (n'-(ca+c))) | ||
155 | ra = min (rows a) . max 0 $ m'-r | ||
156 | ca = min (cols a) . max 0 $ n'-c | ||
157 | sa = subMatrix (0,0) (ra, ca) a | ||
158 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
159 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
160 | res = fromBlocks [[z1,z,z],[z,sa,z],[z,z,z2]] | ||
161 | |||
162 | |||
163 | |||
164 | |||
165 | |||
166 | -------------------------------------------------------------------------------- | ||
167 | |||
168 | |||
169 | row :: R n -> L 1 n | ||
170 | row = mkL . asRow . ud1 | ||
171 | |||
172 | --col :: R n -> L n 1 | ||
173 | col v = tr . row $ v | ||
174 | |||
175 | unrow :: L 1 n -> R n | ||
176 | unrow = mkR . head . toRows . ud2 | ||
177 | |||
178 | --uncol :: L n 1 -> R n | ||
179 | uncol v = unrow . tr $ v | ||
180 | |||
181 | |||
182 | infixl 2 โโ | ||
183 | (โโ) :: (KnownNat r1, KnownNat r2, KnownNat c) => L r1 c -> L r2 c -> L (r1+r2) c | ||
184 | a โโ b = mkL (extract a LA.โโ extract b) | ||
185 | |||
186 | |||
187 | infixl 3 ยฆ | ||
188 | -- (ยฆ) :: (KnownNat r, KnownNat c1, KnownNat c2) => L r c1 -> L r c2 -> L r (c1+c2) | ||
189 | a ยฆ b = tr (tr a โโ tr b) | ||
190 | |||
191 | |||
192 | type Sq n = L n n | ||
193 | --type CSq n = CL n n | ||
194 | |||
195 | type GL = (KnownNat n, KnownNat m) => L m n | ||
196 | type GSq = KnownNat n => Sq n | ||
197 | |||
198 | isKonst :: forall m n . (KnownNat m, KnownNat n) => L m n -> Maybe (โ,(Int,Int)) | ||
199 | isKonst (unwrap -> x) | ||
200 | | singleM x = Just (x `atIndex` (0,0), (m',n')) | ||
201 | | otherwise = Nothing | ||
202 | where | ||
203 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | ||
204 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | ||
205 | |||
206 | |||
207 | |||
208 | |||
209 | infixr 8 <> | ||
210 | (<>) :: forall m k n. (KnownNat m, KnownNat k, KnownNat n) => L m k -> L k n -> L m n | ||
211 | |||
212 | (isKonst -> Just (a,(_,k))) <> (isKonst -> Just (b,_)) = konst (a * b * fromIntegral k) | ||
213 | |||
214 | (isDiag -> Just (0,a,_)) <> (isDiag -> Just (0,b,_)) = diagR 0 (mkR v :: R k) | ||
215 | where | ||
216 | v = a' * b' | ||
217 | n = min (size a) (size b) | ||
218 | a' = subVector 0 n a | ||
219 | b' = subVector 0 n b | ||
220 | |||
221 | (isDiag -> Just (0,a,_)) <> (extract -> b) = mkL (asColumn a * takeRows (size a) b) | ||
222 | |||
223 | (extract -> a) <> (isDiag -> Just (0,b,_)) = mkL (takeColumns (size b) a * asRow b) | ||
224 | |||
225 | a <> b = mkL (extract a LA.<> extract b) | ||
226 | |||
227 | infixr 8 #> | ||
228 | (#>) :: (KnownNat m, KnownNat n) => L m n -> R n -> R m | ||
229 | (isDiag -> Just (0, w, _)) #> v = mkR (w * subVector 0 (size w) (extract v)) | ||
230 | m #> v = mkR (extract m LA.#> extract v) | ||
231 | |||
232 | |||
233 | infixr 8 <ยท> | ||
234 | (<ยท>) :: R n -> R n -> โ | ||
235 | (ud1 -> u) <ยท> (ud1 -> v) | ||
236 | | singleV u || singleV v = sumElements (u * v) | ||
237 | | otherwise = udot u v | ||
238 | |||
239 | -------------------------------------------------------------------------------- | ||
240 | |||
241 | {- | ||
242 | class Minim (n :: Nat) (m :: Nat) | ||
243 | where | ||
244 | type Mini n m :: Nat | ||
245 | |||
246 | instance forall (n :: Nat) . Minim n n | ||
247 | where | ||
248 | type Mini n n = n | ||
249 | |||
250 | |||
251 | instance forall (n :: Nat) (m :: Nat) . (n <= m+1) => Minim n m | ||
252 | where | ||
253 | type Mini n m = n | ||
254 | |||
255 | instance forall (n :: Nat) (m :: Nat) . (m <= n+1) => Minim n m | ||
256 | where | ||
257 | type Mini n m = m | ||
258 | -} | ||
259 | |||
260 | class Diag m d | m -> d | ||
261 | where | ||
262 | takeDiag :: m -> d | ||
263 | |||
264 | |||
265 | |||
266 | instance forall n . (KnownNat n) => Diag (L n n) (R n) | ||
267 | where | ||
268 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
269 | |||
270 | |||
271 | instance forall m n . (KnownNat m, KnownNat n, m <= n+1) => Diag (L m n) (R m) | ||
272 | where | ||
273 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
274 | |||
275 | |||
276 | instance forall m n . (KnownNat m, KnownNat n, n <= m+1) => Diag (L m n) (R n) | ||
277 | where | ||
278 | takeDiag m = mkR (LA.takeDiag (extract m)) | ||
279 | |||
280 | |||
281 | -------------------------------------------------------------------------------- | ||
282 | |||
283 | linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> Maybe (L m n) | ||
284 | linSolve (extract -> a) (extract -> b) = fmap mkL (LA.linearSolve a b) | ||
285 | |||
286 | (<\>) :: (KnownNat m, KnownNat n, KnownNat r) => L m n -> L m r -> L n r | ||
287 | (extract -> a) <\> (extract -> b) = mkL (a LA.<\> b) | ||
288 | |||
289 | svd :: (KnownNat m, KnownNat n) => L m n -> (L m m, R n, L n n) | ||
290 | svd (extract -> m) = (mkL u, mkR s', mkL v) | ||
291 | where | ||
292 | (u,s,v) = LA.svd m | ||
293 | s' = vjoin [s, z] | ||
294 | z = LA.konst 0 (max 0 (cols m - size s)) | ||
295 | |||
296 | |||
297 | svdTall :: (KnownNat m, KnownNat n, n <= m) => L m n -> (L m n, R n, L n n) | ||
298 | svdTall (extract -> m) = (mkL u, mkR s, mkL v) | ||
299 | where | ||
300 | (u,s,v) = LA.thinSVD m | ||
301 | |||
302 | |||
303 | svdFlat :: (KnownNat m, KnownNat n, m <= n) => L m n -> (L m m, R m, L m n) | ||
304 | svdFlat (extract -> m) = (mkL u, mkR s, mkL v) | ||
305 | where | ||
306 | (u,s,v) = LA.thinSVD m | ||
307 | |||
308 | -------------------------------------------------------------------------------- | ||
309 | |||
310 | class Eigen m l v | m -> l, m -> v | ||
311 | where | ||
312 | eigensystem :: m -> (l,v) | ||
313 | eigenvalues :: m -> l | ||
314 | |||
315 | newtype Sym n = Sym (Sq n) deriving Show | ||
316 | |||
317 | newtype Her n = Her (M n n) | ||
318 | |||
319 | sym :: KnownNat n => Sq n -> Sym n | ||
320 | sym m = Sym $ (m + tr m)/2 | ||
321 | |||
322 | her :: KnownNat n => M n n -> Her n | ||
323 | her m = Her $ (m + tr m)/2 | ||
324 | |||
325 | instance KnownNat n => Eigen (Sym n) (R n) (L n n) | ||
326 | where | ||
327 | eigenvalues (Sym (extract -> m)) = mkR . LA.eigenvaluesSH' $ m | ||
328 | eigensystem (Sym (extract -> m)) = (mkR l, mkL v) | ||
329 | where | ||
330 | (l,v) = LA.eigSH' m | ||
331 | |||
332 | instance KnownNat n => Eigen (Sq n) (C n) (M n n) | ||
333 | where | ||
334 | eigenvalues (extract -> m) = mkC . LA.eigenvalues $ m | ||
335 | eigensystem (extract -> m) = (mkC l, mkM v) | ||
336 | where | ||
337 | (l,v) = LA.eig m | ||
338 | |||
339 | -------------------------------------------------------------------------------- | ||
340 | |||
341 | split :: forall p n . (KnownNat p, KnownNat n, p<=n) => R n -> (R p, R (n-p)) | ||
342 | split (extract -> v) = ( mkR (subVector 0 p' v) , | ||
343 | mkR (subVector p' (size v - p') v) ) | ||
344 | where | ||
345 | p' = fromIntegral . natVal $ (undefined :: Proxy p) :: Int | ||
346 | |||
347 | |||
348 | headTail :: (KnownNat n, 1<=n) => R n -> (โ, R (n-1)) | ||
349 | headTail = ((!0) . extract *** id) . split | ||
350 | |||
351 | |||
352 | splitRows :: forall p m n. (KnownNat p, KnownNat m, KnownNat n, p<=m) => L m n -> (L p n, L (m-p) n) | ||
353 | splitRows (extract -> x) = ( mkL (takeRows p' x) , | ||
354 | mkL (dropRows p' x) ) | ||
355 | where | ||
356 | p' = fromIntegral . natVal $ (undefined :: Proxy p) :: Int | ||
357 | |||
358 | splitCols :: forall p m n. (KnownNat p, KnownNat m, KnownNat n, KnownNat (n-p), p<=n) => L m n -> (L m p, L m (n-p)) | ||
359 | splitCols = (tr *** tr) . splitRows . tr | ||
360 | |||
361 | |||
362 | splittest | ||
363 | = do | ||
364 | let v = range :: R 7 | ||
365 | a = snd (split v) :: R 4 | ||
366 | print $ a | ||
367 | print $ snd . headTail . snd . headTail $ v | ||
368 | print $ first (vec3 1 2 3) | ||
369 | print $ second (vec3 1 2 3) | ||
370 | print $ third (vec3 1 2 3) | ||
371 | print $ (snd $ splitRows eye :: L 4 6) | ||
372 | where | ||
373 | first v = fst . headTail $ v | ||
374 | second v = first . snd . headTail $ v | ||
375 | third v = first . snd . headTail . snd . headTail $ v | ||
376 | |||
377 | -------------------------------------------------------------------------------- | ||
378 | |||
379 | def | ||
380 | :: forall m n . (KnownNat n, KnownNat m) | ||
381 | => (โ -> โ -> โ) | ||
382 | -> L m n | ||
383 | def f = mkL $ LA.build (m',n') f | ||
384 | where | ||
385 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | ||
386 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | ||
387 | |||
388 | -------------------------------------------------------------------------------- | ||
389 | |||
390 | withVector | ||
391 | :: forall z | ||
392 | . Vector โ | ||
393 | -> (forall n . (KnownNat n) => R n -> z) | ||
394 | -> z | ||
395 | withVector v f = | ||
396 | case someNatVal $ fromIntegral $ size v of | ||
397 | Nothing -> error "static/dynamic mismatch" | ||
398 | Just (SomeNat (_ :: Proxy m)) -> f (mkR v :: R m) | ||
399 | |||
400 | |||
401 | withMatrix | ||
402 | :: forall z | ||
403 | . Matrix โ | ||
404 | -> (forall m n . (KnownNat m, KnownNat n) => L m n -> z) | ||
405 | -> z | ||
406 | withMatrix a f = | ||
407 | case someNatVal $ fromIntegral $ rows a of | ||
408 | Nothing -> error "static/dynamic mismatch" | ||
409 | Just (SomeNat (_ :: Proxy m)) -> | ||
410 | case someNatVal $ fromIntegral $ cols a of | ||
411 | Nothing -> error "static/dynamic mismatch" | ||
412 | Just (SomeNat (_ :: Proxy n)) -> | ||
413 | f (mkL a :: L n m) | ||
414 | |||
415 | -------------------------------------------------------------------------------- | ||
416 | |||
417 | test :: (Bool, IO ()) | ||
418 | test = (ok,info) | ||
419 | where | ||
420 | ok = extract (eye :: Sq 5) == ident 5 | ||
421 | && unwrap (mTm sm :: Sq 3) == tr ((3><3)[1..]) LA.<> (3><3)[1..] | ||
422 | && unwrap (tm :: L 3 5) == LA.mat 5 [1..15] | ||
423 | && thingS == thingD | ||
424 | && precS == precD | ||
425 | && withVector (LA.vect [1..15]) sumV == sumElements (LA.fromList [1..15]) | ||
426 | |||
427 | info = do | ||
428 | print $ u | ||
429 | print $ v | ||
430 | print (eye :: Sq 3) | ||
431 | print $ ((u & 5) + 1) <ยท> v | ||
432 | print (tm :: L 2 5) | ||
433 | print (tm <> sm :: L 2 3) | ||
434 | print thingS | ||
435 | print thingD | ||
436 | print precS | ||
437 | print precD | ||
438 | print $ withVector (LA.vect [1..15]) sumV | ||
439 | splittest | ||
440 | |||
441 | sumV w = w <ยท> konst 1 | ||
442 | |||
443 | u = vec2 3 5 | ||
444 | |||
445 | ๐ง x = vector [x] :: R 1 | ||
446 | |||
447 | v = ๐ง 2 & 4 & 7 | ||
448 | |||
449 | -- mTm :: L n m -> Sq m | ||
450 | mTm a = tr a <> a | ||
451 | |||
452 | tm :: GL | ||
453 | tm = lmat 0 [1..] | ||
454 | |||
455 | lmat :: forall m n . (KnownNat m, KnownNat n) => โ -> [โ] -> L m n | ||
456 | lmat z xs = mkL . reshape n' . LA.fromList . take (m'*n') $ xs ++ repeat z | ||
457 | where | ||
458 | m' = fromIntegral . natVal $ (undefined :: Proxy m) | ||
459 | n' = fromIntegral . natVal $ (undefined :: Proxy n) | ||
460 | |||
461 | sm :: GSq | ||
462 | sm = lmat 0 [1..] | ||
463 | |||
464 | thingS = (u & 1) <ยท> tr q #> q #> v | ||
465 | where | ||
466 | q = tm :: L 10 3 | ||
467 | |||
468 | thingD = vjoin [ud1 u, 1] LA.<ยท> tr m LA.#> m LA.#> ud1 v | ||
469 | where | ||
470 | m = LA.mat 3 [1..30] | ||
471 | |||
472 | precS = (1::Double) + (2::Double) * ((1 :: R 3) * (u & 6)) <ยท> konst 2 #> v | ||
473 | precD = 1 + 2 * vjoin[ud1 u, 6] LA.<ยท> LA.konst 2 (size (ud1 u) +1, size (ud1 v)) LA.#> ud1 v | ||
474 | |||
475 | |||
476 | instance (KnownNat n', KnownNat m') => Testable (L n' m') | ||
477 | where | ||
478 | checkT _ = test | ||
479 | |||
480 | |||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Static.hs b/packages/base/src/Numeric/LinearAlgebra/Static.hs index 2647f20..daf8d80 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Static.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Static.hs | |||
@@ -25,7 +25,7 @@ module Numeric.LinearAlgebra.Static where | |||
25 | 25 | ||
26 | 26 | ||
27 | import GHC.TypeLits | 27 | import GHC.TypeLits |
28 | import Numeric.HMatrix as LA | 28 | import Numeric.LinearAlgebra.HMatrix as LA |
29 | import Data.Packed as D | 29 | import Data.Packed as D |
30 | import Data.Packed.ST | 30 | import Data.Packed.ST |
31 | import Data.Proxy(Proxy) | 31 | import Data.Proxy(Proxy) |
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs index 4824af4..0ac4634 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs | |||
@@ -19,7 +19,7 @@ Stability : provisional | |||
19 | module Numeric.LinearAlgebra.Util( | 19 | module Numeric.LinearAlgebra.Util( |
20 | 20 | ||
21 | -- * Convenience functions | 21 | -- * Convenience functions |
22 | vect, mat, | 22 | vector, matrix, |
23 | disp, | 23 | disp, |
24 | zeros, ones, | 24 | zeros, ones, |
25 | diagl, | 25 | diagl, |
@@ -79,27 +79,27 @@ iC = 0:+1 | |||
79 | 79 | ||
80 | {- | create a real vector | 80 | {- | create a real vector |
81 | 81 | ||
82 | >>> vect [1..5] | 82 | >>> vector [1..5] |
83 | fromList [1.0,2.0,3.0,4.0,5.0] | 83 | fromList [1.0,2.0,3.0,4.0,5.0] |
84 | 84 | ||
85 | -} | 85 | -} |
86 | vect :: [โ] -> Vector โ | 86 | vector :: [โ] -> Vector โ |
87 | vect = fromList | 87 | vector = fromList |
88 | 88 | ||
89 | {- | create a real matrix | 89 | {- | create a real matrix |
90 | 90 | ||
91 | >>> mat 5 [1..15] | 91 | >>> matrix 5 [1..15] |
92 | (3><5) | 92 | (3><5) |
93 | [ 1.0, 2.0, 3.0, 4.0, 5.0 | 93 | [ 1.0, 2.0, 3.0, 4.0, 5.0 |
94 | , 6.0, 7.0, 8.0, 9.0, 10.0 | 94 | , 6.0, 7.0, 8.0, 9.0, 10.0 |
95 | , 11.0, 12.0, 13.0, 14.0, 15.0 ] | 95 | , 11.0, 12.0, 13.0, 14.0, 15.0 ] |
96 | 96 | ||
97 | -} | 97 | -} |
98 | mat | 98 | matrix |
99 | :: Int -- ^ columns | 99 | :: Int -- ^ columns |
100 | -> [โ] -- ^ elements | 100 | -> [โ] -- ^ elements |
101 | -> Matrix โ | 101 | -> Matrix โ |
102 | mat c = reshape c . fromList | 102 | matrix c = reshape c . fromList |
103 | 103 | ||
104 | 104 | ||
105 | {- | print a real matrix with given number of digits after the decimal point | 105 | {- | print a real matrix with given number of digits after the decimal point |
diff --git a/packages/base/src/Numeric/Sparse.hs b/packages/base/src/Numeric/Sparse.hs index f495e3a..f1516ec 100644 --- a/packages/base/src/Numeric/Sparse.hs +++ b/packages/base/src/Numeric/Sparse.hs | |||
@@ -168,7 +168,7 @@ gmXv Dense{..} v | |||
168 | {- | general matrix - vector product | 168 | {- | general matrix - vector product |
169 | 169 | ||
170 | >>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)] | 170 | >>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)] |
171 | >>> m !#> vect[1..2000] | 171 | >>> m !#> vector [1..2000] |
172 | fromList [1000.0,4000.0] | 172 | fromList [1000.0,4000.0] |
173 | 173 | ||
174 | -} | 174 | -} |