diff options
Diffstat (limited to 'packages/base/src/Numeric/LinearAlgebra/Real.hs')
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Real.hs | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/packages/base/src/Numeric/LinearAlgebra/Real.hs b/packages/base/src/Numeric/LinearAlgebra/Real.hs index 0e54555..8627084 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Real.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Real.hs | |||
@@ -29,7 +29,7 @@ module Numeric.LinearAlgebra.Real( | |||
29 | -- * Vector | 29 | -- * Vector |
30 | R, C, | 30 | R, C, |
31 | vec2, vec3, vec4, (&), (#), | 31 | vec2, vec3, vec4, (&), (#), |
32 | vect, | 32 | vector, |
33 | linspace, range, dim, | 33 | linspace, range, dim, |
34 | -- * Matrix | 34 | -- * Matrix |
35 | L, Sq, M, | 35 | L, Sq, M, |
@@ -39,7 +39,7 @@ module Numeric.LinearAlgebra.Real( | |||
39 | eye, | 39 | eye, |
40 | diagR, diag, | 40 | diagR, diag, |
41 | blockAt, | 41 | blockAt, |
42 | mat, | 42 | matrix, |
43 | -- * Products | 43 | -- * Products |
44 | (<>),(#>),(<ยท>), | 44 | (<>),(#>),(<ยท>), |
45 | -- * Linear Systems | 45 | -- * Linear Systems |
@@ -64,6 +64,7 @@ import Data.Proxy(Proxy) | |||
64 | import Numeric.LinearAlgebra.Static | 64 | import Numeric.LinearAlgebra.Static |
65 | import Text.Printf | 65 | import Text.Printf |
66 | 66 | ||
67 | |||
67 | ๐ :: Sized โ s c => s | 68 | ๐ :: Sized โ s c => s |
68 | ๐ = konst i_C | 69 | ๐ = konst i_C |
69 | 70 | ||
@@ -71,7 +72,7 @@ instance forall n . KnownNat n => Show (R n) | |||
71 | where | 72 | where |
72 | show (ud1 -> v) | 73 | show (ud1 -> v) |
73 | | singleV v = "("++show (v!0)++" :: R "++show d++")" | 74 | | singleV v = "("++show (v!0)++" :: R "++show d++")" |
74 | | otherwise = "(vect"++ drop 8 (show v)++" :: R "++show d++")" | 75 | | otherwise = "(vector"++ drop 8 (show v)++" :: R "++show d++")" |
75 | where | 76 | where |
76 | d = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | 77 | d = fromIntegral . natVal $ (undefined :: Proxy n) :: Int |
77 | 78 | ||
@@ -79,7 +80,7 @@ instance forall n . KnownNat n => Show (C n) | |||
79 | where | 80 | where |
80 | show (C (Dim v)) | 81 | show (C (Dim v)) |
81 | | singleV v = "("++show (v!0)++" :: C "++show d++")" | 82 | | singleV v = "("++show (v!0)++" :: C "++show d++")" |
82 | | otherwise = "(fromList"++ drop 8 (show v)++" :: C "++show d++")" | 83 | | otherwise = "(vector"++ drop 8 (show v)++" :: C "++show d++")" |
83 | where | 84 | where |
84 | d = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | 85 | d = fromIntegral . natVal $ (undefined :: Proxy n) :: Int |
85 | 86 | ||
@@ -117,8 +118,11 @@ vec3 a b c = R (gvec3 a b c) | |||
117 | vec4 :: โ -> โ -> โ -> โ -> R 4 | 118 | vec4 :: โ -> โ -> โ -> โ -> R 4 |
118 | vec4 a b c d = R (gvec4 a b c d) | 119 | vec4 a b c d = R (gvec4 a b c d) |
119 | 120 | ||
120 | vect :: forall n . KnownNat n => [โ] -> R n | 121 | vector :: KnownNat n => [โ] -> R n |
121 | vect xs = R (gvect "R" xs) | 122 | vector = fromList |
123 | |||
124 | matrix :: (KnownNat m, KnownNat n) => [โ] -> L m n | ||
125 | matrix = fromList | ||
122 | 126 | ||
123 | linspace :: forall n . KnownNat n => (โ,โ) -> R n | 127 | linspace :: forall n . KnownNat n => (โ,โ) -> R n |
124 | linspace (a,b) = mkR (LA.linspace d (a,b)) | 128 | linspace (a,b) = mkR (LA.linspace d (a,b)) |
@@ -157,7 +161,7 @@ instance forall m n . (KnownNat m, KnownNat n) => Show (L m n) | |||
157 | show (isDiag -> Just (z,y,(m',n'))) = printf "(diag %s %s :: L %d %d)" (show z) (drop 9 $ show y) m' n' | 161 | show (isDiag -> Just (z,y,(m',n'))) = printf "(diag %s %s :: L %d %d)" (show z) (drop 9 $ show y) m' n' |
158 | show (ud2 -> x) | 162 | show (ud2 -> x) |
159 | | singleM x = printf "(%s :: L %d %d)" (show (x `atIndex` (0,0))) m' n' | 163 | | singleM x = printf "(%s :: L %d %d)" (show (x `atIndex` (0,0))) m' n' |
160 | | otherwise = "(mat"++ dropWhile (/='\n') (show x)++" :: L "++show m'++" "++show n'++")" | 164 | | otherwise = "(matrix"++ dropWhile (/='\n') (show x)++" :: L "++show m'++" "++show n'++")" |
161 | where | 165 | where |
162 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | 166 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int |
163 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | 167 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int |
@@ -167,7 +171,7 @@ instance forall m n . (KnownNat m, KnownNat n) => Show (M m n) | |||
167 | show (isDiagC -> Just (z,y,(m',n'))) = printf "(diag %s %s :: M %d %d)" (show z) (drop 9 $ show y) m' n' | 171 | show (isDiagC -> Just (z,y,(m',n'))) = printf "(diag %s %s :: M %d %d)" (show z) (drop 9 $ show y) m' n' |
168 | show (M (Dim (Dim x))) | 172 | show (M (Dim (Dim x))) |
169 | | singleM x = printf "(%s :: M %d %d)" (show (x `atIndex` (0,0))) m' n' | 173 | | singleM x = printf "(%s :: M %d %d)" (show (x `atIndex` (0,0))) m' n' |
170 | | otherwise = "(fromList"++ dropWhile (/='\n') (show x)++" :: M "++show m'++" "++show n'++")" | 174 | | otherwise = "(matrix"++ dropWhile (/='\n') (show x)++" :: M "++show m'++" "++show n'++")" |
171 | where | 175 | where |
172 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int | 176 | m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int |
173 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int | 177 | n' = fromIntegral . natVal $ (undefined :: Proxy n) :: Int |
@@ -191,7 +195,7 @@ instance forall n. KnownNat n => Sized โ (R n) (Vector โ) | |||
191 | where | 195 | where |
192 | konst x = mkR (LA.scalar x) | 196 | konst x = mkR (LA.scalar x) |
193 | unwrap = ud1 | 197 | unwrap = ud1 |
194 | fromList = vect | 198 | fromList xs = R (gvect "R" xs) |
195 | extract (unwrap -> v) | 199 | extract (unwrap -> v) |
196 | | singleV v = LA.konst (v!0) d | 200 | | singleV v = LA.konst (v!0) d |
197 | | otherwise = v | 201 | | otherwise = v |
@@ -203,7 +207,7 @@ instance forall n. KnownNat n => Sized โ (R n) (Vector โ) | |||
203 | instance forall m n . (KnownNat m, KnownNat n) => Sized โ (L m n) (Matrix โ) | 207 | instance forall m n . (KnownNat m, KnownNat n) => Sized โ (L m n) (Matrix โ) |
204 | where | 208 | where |
205 | konst x = mkL (LA.scalar x) | 209 | konst x = mkL (LA.scalar x) |
206 | fromList = mat | 210 | fromList xs = L (gmat "L" xs) |
207 | unwrap = ud2 | 211 | unwrap = ud2 |
208 | extract (isDiag -> Just (z,y,(m',n'))) = diagRect z y m' n' | 212 | extract (isDiag -> Just (z,y,(m',n'))) = diagRect z y m' n' |
209 | extract (unwrap -> a) | 213 | extract (unwrap -> a) |
@@ -260,8 +264,7 @@ blockAt x r c a = mkL res | |||
260 | 264 | ||
261 | 265 | ||
262 | 266 | ||
263 | mat :: forall m n . (KnownNat m, KnownNat n) => [โ] -> L m n | 267 | |
264 | mat xs = L (gmat "L" xs) | ||
265 | 268 | ||
266 | -------------------------------------------------------------------------------- | 269 | -------------------------------------------------------------------------------- |
267 | 270 | ||
@@ -505,8 +508,8 @@ instance forall m n . (KnownNat m, KnownNat n, n <= m+1) => Diag (L m n) (R n) | |||
505 | 508 | ||
506 | -------------------------------------------------------------------------------- | 509 | -------------------------------------------------------------------------------- |
507 | 510 | ||
508 | linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> L m n | 511 | linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> Maybe (L m n) |
509 | linSolve (extract -> a) (extract -> b) = mkL (LA.linearSolve a b) | 512 | linSolve (extract -> a) (extract -> b) = fmap mkL (LA.linearSolve a b) |
510 | 513 | ||
511 | (<\>) :: (KnownNat m, KnownNat n, KnownNat r) => L m n -> L m r -> L n r | 514 | (<\>) :: (KnownNat m, KnownNat n, KnownNat r) => L m n -> L m r -> L n r |
512 | (extract -> a) <\> (extract -> b) = mkL (a LA.<\> b) | 515 | (extract -> a) <\> (extract -> b) = mkL (a LA.<\> b) |
@@ -617,7 +620,7 @@ test = (ok,info) | |||
617 | 620 | ||
618 | u = vec2 3 5 | 621 | u = vec2 3 5 |
619 | 622 | ||
620 | ๐ง x = vect [x] :: R 1 | 623 | ๐ง x = vector [x] :: R 1 |
621 | 624 | ||
622 | v = ๐ง 2 & 4 & 7 | 625 | v = ๐ง 2 & 4 & 7 |
623 | 626 | ||