summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/LinearAlgebra/Real.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-06-05 17:55:08 +0200
committerAlberto Ruiz <aruiz@um.es>2014-06-05 17:55:08 +0200
commita40ed5c42f779561151b3119df0ebeddfcec183c (patch)
treea7ae3d3b15a4e96bdcc4ca2a595fe4ac750c4fd9 /packages/base/src/Numeric/LinearAlgebra/Real.hs
parentf459fcb1adfd733de406f2eb81bb0a57f5ce6779 (diff)
maybe variant of linearSolve
Diffstat (limited to 'packages/base/src/Numeric/LinearAlgebra/Real.hs')
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Real.hs33
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)
64import Numeric.LinearAlgebra.Static 64import Numeric.LinearAlgebra.Static
65import Text.Printf 65import 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)
117vec4 :: โ„ -> โ„ -> โ„ -> โ„ -> R 4 118vec4 :: โ„ -> โ„ -> โ„ -> โ„ -> R 4
118vec4 a b c d = R (gvec4 a b c d) 119vec4 a b c d = R (gvec4 a b c d)
119 120
120vect :: forall n . KnownNat n => [โ„] -> R n 121vector :: KnownNat n => [โ„] -> R n
121vect xs = R (gvect "R" xs) 122vector = fromList
123
124matrix :: (KnownNat m, KnownNat n) => [โ„] -> L m n
125matrix = fromList
122 126
123linspace :: forall n . KnownNat n => (โ„,โ„) -> R n 127linspace :: forall n . KnownNat n => (โ„,โ„) -> R n
124linspace (a,b) = mkR (LA.linspace d (a,b)) 128linspace (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 โ„)
203instance forall m n . (KnownNat m, KnownNat n) => Sized โ„ (L m n) (Matrix โ„) 207instance 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
263mat :: forall m n . (KnownNat m, KnownNat n) => [โ„] -> L m n 267
264mat 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
508linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> L m n 511linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> Maybe (L m n)
509linSolve (extract -> a) (extract -> b) = mkL (LA.linearSolve a b) 512linSolve (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