summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric
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
parentf459fcb1adfd733de406f2eb81bb0a57f5ce6779 (diff)
maybe variant of linearSolve
Diffstat (limited to 'packages/base/src/Numeric')
-rw-r--r--packages/base/src/Numeric/HMatrix.hs6
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Algorithms.hs8
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/LAPACK.hs19
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Real.hs33
4 files changed, 44 insertions, 22 deletions
diff --git a/packages/base/src/Numeric/HMatrix.hs b/packages/base/src/Numeric/HMatrix.hs
index 786fb6d..024e462 100644
--- a/packages/base/src/Numeric/HMatrix.hs
+++ b/packages/base/src/Numeric/HMatrix.hs
@@ -152,7 +152,8 @@ import Numeric.LinearAlgebra.Data
152import Numeric.Matrix() 152import Numeric.Matrix()
153import Numeric.Vector() 153import Numeric.Vector()
154import Data.Packed.Numeric hiding ((<>)) 154import Data.Packed.Numeric hiding ((<>))
155import Numeric.LinearAlgebra.Algorithms 155import Numeric.LinearAlgebra.Algorithms hiding (linearSolve)
156import qualified Numeric.LinearAlgebra.Algorithms as A
156import Numeric.LinearAlgebra.Util 157import Numeric.LinearAlgebra.Util
157import Numeric.LinearAlgebra.Random 158import Numeric.LinearAlgebra.Random
158import Numeric.Sparse((!#>)) 159import Numeric.Sparse((!#>))
@@ -163,3 +164,6 @@ import Numeric.LinearAlgebra.Util.CG
163(<>) = mXm 164(<>) = mXm
164infixr 8 <> 165infixr 8 <>
165 166
167-- | 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'.
168linearSolve m b = A.mbLinearSolve m b
169
diff --git a/packages/base/src/Numeric/LinearAlgebra/Algorithms.hs b/packages/base/src/Numeric/LinearAlgebra/Algorithms.hs
index bbcc513..7e36978 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Algorithms.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Algorithms.hs
@@ -26,6 +26,7 @@ module Numeric.LinearAlgebra.Algorithms (
26 Field(), 26 Field(),
27-- * Linear Systems 27-- * Linear Systems
28 linearSolve, 28 linearSolve,
29 mbLinearSolve,
29 luSolve, 30 luSolve,
30 cholSolve, 31 cholSolve,
31 linearSolveLS, 32 linearSolveLS,
@@ -102,6 +103,7 @@ class (Product t,
102 sv' :: Matrix t -> Vector Double 103 sv' :: Matrix t -> Vector Double
103 luPacked' :: Matrix t -> (Matrix t, [Int]) 104 luPacked' :: Matrix t -> (Matrix t, [Int])
104 luSolve' :: (Matrix t, [Int]) -> Matrix t -> Matrix t 105 luSolve' :: (Matrix t, [Int]) -> Matrix t -> Matrix t
106 mbLinearSolve' :: Matrix t -> Matrix t -> Maybe (Matrix t)
105 linearSolve' :: Matrix t -> Matrix t -> Matrix t 107 linearSolve' :: Matrix t -> Matrix t -> Matrix t
106 cholSolve' :: Matrix t -> Matrix t -> Matrix t 108 cholSolve' :: Matrix t -> Matrix t -> Matrix t
107 linearSolveSVD' :: Matrix t -> Matrix t -> Matrix t 109 linearSolveSVD' :: Matrix t -> Matrix t -> Matrix t
@@ -125,6 +127,7 @@ instance Field Double where
125 luPacked' = luR 127 luPacked' = luR
126 luSolve' (l_u,perm) = lusR l_u perm 128 luSolve' (l_u,perm) = lusR l_u perm
127 linearSolve' = linearSolveR -- (luSolve . luPacked) ?? 129 linearSolve' = linearSolveR -- (luSolve . luPacked) ??
130 mbLinearSolve' = mbLinearSolveR
128 cholSolve' = cholSolveR 131 cholSolve' = cholSolveR
129 linearSolveLS' = linearSolveLSR 132 linearSolveLS' = linearSolveLSR
130 linearSolveSVD' = linearSolveSVDR Nothing 133 linearSolveSVD' = linearSolveSVDR Nothing
@@ -151,6 +154,7 @@ instance Field (Complex Double) where
151 luPacked' = luC 154 luPacked' = luC
152 luSolve' (l_u,perm) = lusC l_u perm 155 luSolve' (l_u,perm) = lusC l_u perm
153 linearSolve' = linearSolveC 156 linearSolve' = linearSolveC
157 mbLinearSolve' = mbLinearSolveC
154 cholSolve' = cholSolveC 158 cholSolve' = cholSolveC
155 linearSolveLS' = linearSolveLSC 159 linearSolveLS' = linearSolveLSC
156 linearSolveSVD' = linearSolveSVDC Nothing 160 linearSolveSVD' = linearSolveSVDC Nothing
@@ -234,6 +238,10 @@ luSolve = {-# SCC "luSolve" #-} luSolve'
234linearSolve :: Field t => Matrix t -> Matrix t -> Matrix t 238linearSolve :: Field t => Matrix t -> Matrix t -> Matrix t
235linearSolve = {-# SCC "linearSolve" #-} linearSolve' 239linearSolve = {-# SCC "linearSolve" #-} linearSolve'
236 240
241-- | 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'.
242mbLinearSolve :: Field t => Matrix t -> Matrix t -> Maybe (Matrix t)
243mbLinearSolve = {-# SCC "linearSolve" #-} mbLinearSolve'
244
237-- | Solve a symmetric or Hermitian positive definite linear system using a precomputed Cholesky decomposition obtained by 'chol'. 245-- | Solve a symmetric or Hermitian positive definite linear system using a precomputed Cholesky decomposition obtained by 'chol'.
238cholSolve :: Field t => Matrix t -> Matrix t -> Matrix t 246cholSolve :: Field t => Matrix t -> Matrix t -> Matrix t
239cholSolve = {-# SCC "cholSolve" #-} cholSolve' 247cholSolve = {-# SCC "cholSolve" #-} cholSolve'
diff --git a/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs b/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
index 40fef45..b32b67f 100644
--- a/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
@@ -17,6 +17,7 @@ module Numeric.LinearAlgebra.LAPACK (
17 multiplyR, multiplyC, multiplyF, multiplyQ, 17 multiplyR, multiplyC, multiplyF, multiplyQ,
18 -- * Linear systems 18 -- * Linear systems
19 linearSolveR, linearSolveC, 19 linearSolveR, linearSolveC,
20 mbLinearSolveR, mbLinearSolveC,
20 lusR, lusC, 21 lusR, lusC,
21 cholSolveR, cholSolveC, 22 cholSolveR, cholSolveC,
22 linearSolveLSR, linearSolveLSC, 23 linearSolveLSR, linearSolveLSC,
@@ -329,8 +330,8 @@ foreign import ccall unsafe "linearSolveC_l" zgesv :: TCMCMCM
329foreign import ccall unsafe "cholSolveR_l" dpotrs :: TMMM 330foreign import ccall unsafe "cholSolveR_l" dpotrs :: TMMM
330foreign import ccall unsafe "cholSolveC_l" zpotrs :: TCMCMCM 331foreign import ccall unsafe "cholSolveC_l" zpotrs :: TCMCMCM
331 332
332linearSolveSQAux f st a b 333linearSolveSQAux g f st a b
333 | n1==n2 && n1==r = unsafePerformIO $ do 334 | n1==n2 && n1==r = unsafePerformIO . g $ do
334 s <- createMatrix ColumnMajor r c 335 s <- createMatrix ColumnMajor r c
335 app3 f mat a mat b mat s st 336 app3 f mat a mat b mat s st
336 return s 337 return s
@@ -342,20 +343,26 @@ linearSolveSQAux f st a b
342 343
343-- | Solve a real linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /dgesv/. For underconstrained or overconstrained systems use 'linearSolveLSR' or 'linearSolveSVDR'. See also 'lusR'. 344-- | Solve a real linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /dgesv/. For underconstrained or overconstrained systems use 'linearSolveLSR' or 'linearSolveSVDR'. See also 'lusR'.
344linearSolveR :: Matrix Double -> Matrix Double -> Matrix Double 345linearSolveR :: Matrix Double -> Matrix Double -> Matrix Double
345linearSolveR a b = linearSolveSQAux dgesv "linearSolveR" (fmat a) (fmat b) 346linearSolveR a b = linearSolveSQAux id dgesv "linearSolveR" (fmat a) (fmat b)
347
348mbLinearSolveR :: Matrix Double -> Matrix Double -> Maybe (Matrix Double)
349mbLinearSolveR a b = linearSolveSQAux mbCatch dgesv "linearSolveR" (fmat a) (fmat b)
350
346 351
347-- | Solve a complex linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /zgesv/. For underconstrained or overconstrained systems use 'linearSolveLSC' or 'linearSolveSVDC'. See also 'lusC'. 352-- | Solve a complex linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /zgesv/. For underconstrained or overconstrained systems use 'linearSolveLSC' or 'linearSolveSVDC'. See also 'lusC'.
348linearSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) 353linearSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double)
349linearSolveC a b = linearSolveSQAux zgesv "linearSolveC" (fmat a) (fmat b) 354linearSolveC a b = linearSolveSQAux id zgesv "linearSolveC" (fmat a) (fmat b)
350 355
356mbLinearSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Maybe (Matrix (Complex Double))
357mbLinearSolveC a b = linearSolveSQAux mbCatch zgesv "linearSolveC" (fmat a) (fmat b)
351 358
352-- | Solves a symmetric positive definite system of linear equations using a precomputed Cholesky factorization obtained by 'cholS'. 359-- | Solves a symmetric positive definite system of linear equations using a precomputed Cholesky factorization obtained by 'cholS'.
353cholSolveR :: Matrix Double -> Matrix Double -> Matrix Double 360cholSolveR :: Matrix Double -> Matrix Double -> Matrix Double
354cholSolveR a b = linearSolveSQAux dpotrs "cholSolveR" (fmat a) (fmat b) 361cholSolveR a b = linearSolveSQAux id dpotrs "cholSolveR" (fmat a) (fmat b)
355 362
356-- | Solves a Hermitian positive definite system of linear equations using a precomputed Cholesky factorization obtained by 'cholH'. 363-- | Solves a Hermitian positive definite system of linear equations using a precomputed Cholesky factorization obtained by 'cholH'.
357cholSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) 364cholSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double)
358cholSolveC a b = linearSolveSQAux zpotrs "cholSolveC" (fmat a) (fmat b) 365cholSolveC a b = linearSolveSQAux id zpotrs "cholSolveC" (fmat a) (fmat b)
359 366
360----------------------------------------------------------------------------------- 367-----------------------------------------------------------------------------------
361foreign import ccall unsafe "linearSolveLSR_l" dgels :: TMMM 368foreign import ccall unsafe "linearSolveLSR_l" dgels :: TMMM
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