summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-rw-r--r--examples/error.hs1
-rw-r--r--examples/plot.hs2
-rw-r--r--lib/Data/Packed/Internal/Common.hs9
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs14
-rw-r--r--lib/Numeric/LinearAlgebra/LAPACK.hs24
-rw-r--r--lib/Numeric/LinearAlgebra/Tests.hs9
7 files changed, 46 insertions, 15 deletions
diff --git a/CHANGES b/CHANGES
index f4d634e..69902ef 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,7 +1,7 @@
10.9.2.0 10.9.2.0
2======= 2=======
3 3
4- cholSolve 4- cholSolve, mbCholSH
5 5
6- GSL Nonlinear Least-Squares fitting using Levenberg-Marquardt. 6- GSL Nonlinear Least-Squares fitting using Levenberg-Marquardt.
7 7
diff --git a/examples/error.hs b/examples/error.hs
index f13a48e..5efae7c 100644
--- a/examples/error.hs
+++ b/examples/error.hs
@@ -1,4 +1,5 @@
1import Numeric.GSL 1import Numeric.GSL
2import Numeric.GSL.Special
2import Numeric.LinearAlgebra 3import Numeric.LinearAlgebra
3import Prelude hiding (catch) 4import Prelude hiding (catch)
4import Control.Exception 5import Control.Exception
diff --git a/examples/plot.hs b/examples/plot.hs
index 1583dfe..f950aa5 100644
--- a/examples/plot.hs
+++ b/examples/plot.hs
@@ -1,6 +1,6 @@
1import Numeric.LinearAlgebra 1import Numeric.LinearAlgebra
2import Graphics.Plot 2import Graphics.Plot
3import Numeric.GSL(erf_Z, erf) 3import Numeric.GSL.Special(erf_Z, erf)
4 4
5sombrero n = f x y where 5sombrero n = f x y where
6 (x,y) = meshdom range range 6 (x,y) = meshdom range range
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs
index bfa63f1..455b176 100644
--- a/lib/Data/Packed/Internal/Common.hs
+++ b/lib/Data/Packed/Internal/Common.hs
@@ -17,7 +17,7 @@
17module Data.Packed.Internal.Common( 17module Data.Packed.Internal.Common(
18 Adapt, 18 Adapt,
19 app1, app2, app3, app4, 19 app1, app2, app3, app4,
20 (//), check, 20 (//), check, mbCatch,
21 splitEvery, common, compatdim, 21 splitEvery, common, compatdim,
22 fi, 22 fi,
23 table 23 table
@@ -29,6 +29,7 @@ import Foreign.C.String(peekCString)
29import Foreign.C.Types 29import Foreign.C.Types
30import Foreign.Storable.Complex() 30import Foreign.Storable.Complex()
31import Data.List(transpose,intersperse) 31import Data.List(transpose,intersperse)
32import Control.Exception as E
32 33
33-- | @splitEvery 3 [1..9] == [[1,2,3],[4,5,6],[7,8,9]]@ 34-- | @splitEvery 3 [1..9] == [[1,2,3],[4,5,6],[7,8,9]]@
34splitEvery :: Int -> [a] -> [[a]] 35splitEvery :: Int -> [a] -> [[a]]
@@ -151,3 +152,9 @@ check msg f = do
151 152
152-- | description of GSL error codes 153-- | description of GSL error codes
153foreign import ccall "auxi.h gsl_strerror" gsl_strerror :: CInt -> IO (Ptr CChar) 154foreign import ccall "auxi.h gsl_strerror" gsl_strerror :: CInt -> IO (Ptr CChar)
155
156-- | Error capture and conversion to Maybe
157mbCatch :: IO x -> IO (Maybe x)
158mbCatch act = E.catch (Just `fmap` act) f
159 where f :: SomeException -> IO (Maybe x)
160 f _ = return Nothing
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index aa18d37..60f5971 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -46,7 +46,7 @@ module Numeric.LinearAlgebra.Algorithms (
46-- ** QR 46-- ** QR
47 qr, rq, 47 qr, rq,
48-- ** Cholesky 48-- ** Cholesky
49 chol, cholSH, 49 chol, cholSH, mbCholSH,
50-- ** Hessenberg 50-- ** Hessenberg
51 hess, 51 hess,
52-- ** Schur 52-- ** Schur
@@ -100,6 +100,7 @@ class (Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t where
100 eigOnly :: Matrix t -> Vector (Complex Double) 100 eigOnly :: Matrix t -> Vector (Complex Double)
101 eigOnlySH :: Matrix t -> Vector Double 101 eigOnlySH :: Matrix t -> Vector Double
102 cholSH' :: Matrix t -> Matrix t 102 cholSH' :: Matrix t -> Matrix t
103 mbCholSH' :: Matrix t -> Maybe (Matrix t)
103 qr' :: Matrix t -> (Matrix t, Matrix t) 104 qr' :: Matrix t -> (Matrix t, Matrix t)
104 hess' :: Matrix t -> (Matrix t, Matrix t) 105 hess' :: Matrix t -> (Matrix t, Matrix t)
105 schur' :: Matrix t -> (Matrix t, Matrix t) 106 schur' :: Matrix t -> (Matrix t, Matrix t)
@@ -123,6 +124,7 @@ instance Field Double where
123 eigOnly = eigOnlyR 124 eigOnly = eigOnlyR
124 eigOnlySH = eigOnlyS 125 eigOnlySH = eigOnlyS
125 cholSH' = cholS 126 cholSH' = cholS
127 mbCholSH' = mbCholS
126 qr' = unpackQR . qrR 128 qr' = unpackQR . qrR
127 hess' = unpackHess hessR 129 hess' = unpackHess hessR
128 schur' = schurR 130 schur' = schurR
@@ -149,6 +151,7 @@ instance Field (Complex Double) where
149 eigSH'' = eigH 151 eigSH'' = eigH
150 eigOnlySH = eigOnlyH 152 eigOnlySH = eigOnlyH
151 cholSH' = cholH 153 cholSH' = cholH
154 mbCholSH' = mbCholH
152 qr' = unpackQR . qrC 155 qr' = unpackQR . qrC
153 hess' = unpackHess hessC 156 hess' = unpackHess hessC
154 schur' = schurC 157 schur' = schurC
@@ -263,11 +266,11 @@ eig = eig'
263eigenvalues :: Field t => Matrix t -> Vector (Complex Double) 266eigenvalues :: Field t => Matrix t -> Vector (Complex Double)
264eigenvalues = eigOnly 267eigenvalues = eigOnly
265 268
266-- | Similar to 'eigSH' without checking that the input matrix is hermitian or symmetric. 269-- | Similar to 'eigSH' without checking that the input matrix is hermitian or symmetric. It works with the upper triangular part.
267eigSH' :: Field t => Matrix t -> (Vector Double, Matrix t) 270eigSH' :: Field t => Matrix t -> (Vector Double, Matrix t)
268eigSH' = eigSH'' 271eigSH' = eigSH''
269 272
270-- | Similar to 'eigenvaluesSH' without checking that the input matrix is hermitian or symmetric. 273-- | Similar to 'eigenvaluesSH' without checking that the input matrix is hermitian or symmetric. It works with the upper triangular part.
271eigenvaluesSH' :: Field t => Matrix t -> Vector Double 274eigenvaluesSH' :: Field t => Matrix t -> Vector Double
272eigenvaluesSH' = eigOnlySH 275eigenvaluesSH' = eigOnlySH
273 276
@@ -328,8 +331,11 @@ ctrans = ctrans'
328multiply :: Field t => Matrix t -> Matrix t -> Matrix t 331multiply :: Field t => Matrix t -> Matrix t -> Matrix t
329multiply = multiply' 332multiply = multiply'
330 333
334-- | Similar to 'cholSH', but instead of an error (e.g., caused by a matrix not positive definite) it returns 'Nothing'.
335mbCholSH :: Field t => Matrix t -> Maybe (Matrix t)
336mbCholSH = mbCholSH'
331 337
332-- | Similar to 'chol' without checking that the input matrix is hermitian or symmetric. 338-- | Similar to 'chol', without checking that the input matrix is hermitian or symmetric. It works with the upper triangular part.
333cholSH :: Field t => Matrix t -> Matrix t 339cholSH :: Field t => Matrix t -> Matrix t
334cholSH = cholSH' 340cholSH = cholSH'
335 341
diff --git a/lib/Numeric/LinearAlgebra/LAPACK.hs b/lib/Numeric/LinearAlgebra/LAPACK.hs
index 5d4eb0d..539ffb9 100644
--- a/lib/Numeric/LinearAlgebra/LAPACK.hs
+++ b/lib/Numeric/LinearAlgebra/LAPACK.hs
@@ -32,7 +32,7 @@ module Numeric.LinearAlgebra.LAPACK (
32 -- * LU 32 -- * LU
33 luR, luC, 33 luR, luC,
34 -- * Cholesky 34 -- * Cholesky
35 cholS, cholH, 35 cholS, cholH, mbCholS, mbCholH,
36 -- * QR 36 -- * QR
37 qrR, qrC, 37 qrR, qrC,
38 -- * Hessenberg 38 -- * Hessenberg
@@ -392,19 +392,27 @@ linearSolveSVDC Nothing a b = linearSolveSVDC (Just (-1)) (fmat a) (fmat b)
392foreign import ccall "LAPACK/lapack-aux.h chol_l_H" zpotrf :: TCMCM 392foreign import ccall "LAPACK/lapack-aux.h chol_l_H" zpotrf :: TCMCM
393foreign import ccall "LAPACK/lapack-aux.h chol_l_S" dpotrf :: TMM 393foreign import ccall "LAPACK/lapack-aux.h chol_l_S" dpotrf :: TMM
394 394
395cholAux f st a = do
396 r <- createMatrix ColumnMajor n n
397 app2 f mat a mat r st
398 return r
399 where n = rows a
400
395-- | Cholesky factorization of a complex Hermitian positive definite matrix, using LAPACK's /zpotrf/. 401-- | Cholesky factorization of a complex Hermitian positive definite matrix, using LAPACK's /zpotrf/.
396cholH :: Matrix (Complex Double) -> Matrix (Complex Double) 402cholH :: Matrix (Complex Double) -> Matrix (Complex Double)
397cholH = cholAux zpotrf "cholH" . fmat 403cholH = unsafePerformIO . cholAux zpotrf "cholH" . fmat
398 404
399-- | Cholesky factorization of a real symmetric positive definite matrix, using LAPACK's /dpotrf/. 405-- | Cholesky factorization of a real symmetric positive definite matrix, using LAPACK's /dpotrf/.
400cholS :: Matrix Double -> Matrix Double 406cholS :: Matrix Double -> Matrix Double
401cholS = cholAux dpotrf "cholS" . fmat 407cholS = unsafePerformIO . cholAux dpotrf "cholS" . fmat
402 408
403cholAux f st a = unsafePerformIO $ do 409-- | Cholesky factorization of a complex Hermitian positive definite matrix, using LAPACK's /zpotrf/ ('Maybe' version).
404 r <- createMatrix ColumnMajor n n 410mbCholH :: Matrix (Complex Double) -> Maybe (Matrix (Complex Double))
405 app2 f mat a mat r st 411mbCholH = unsafePerformIO . mbCatch . cholAux zpotrf "cholH" . fmat
406 return r 412
407 where n = rows a 413-- | Cholesky factorization of a real symmetric positive definite matrix, using LAPACK's /dpotrf/ ('Maybe' version).
414mbCholS :: Matrix Double -> Maybe (Matrix Double)
415mbCholS = unsafePerformIO . mbCatch . cholAux dpotrf "cholS" . fmat
408 416
409----------------------------------------------------------------------------------- 417-----------------------------------------------------------------------------------
410foreign import ccall "LAPACK/lapack-aux.h qr_l_R" dgeqr2 :: TMVM 418foreign import ccall "LAPACK/lapack-aux.h qr_l_R" dgeqr2 :: TMVM
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs
index 60c60e6..ac26466 100644
--- a/lib/Numeric/LinearAlgebra/Tests.hs
+++ b/lib/Numeric/LinearAlgebra/Tests.hs
@@ -161,6 +161,14 @@ fittingTest = utest "levmar" (ok1 && ok2)
161 ok1 = and (zipWith f sols [5,0.1,1]) where f (x,d) r = abs (x-r)<2*d 161 ok1 = and (zipWith f sols [5,0.1,1]) where f (x,d) r = abs (x-r)<2*d
162 ok2 = pnorm PNorm2 (fromList (map fst sols) - fromList sol) < 1E-5 162 ok2 = pnorm PNorm2 (fromList (map fst sols) - fromList sol) < 1E-5
163 163
164-----------------------------------------------------
165
166mbCholTest = utest "mbCholTest" (ok1 && ok2) where
167 m1 = (2><2) [2,5,5,8 :: Double]
168 m2 = (2><2) [3,5,5,9 :: Complex Double]
169 ok1 = mbCholSH m1 == Nothing
170 ok2 = mbCholSH m2 == Just (chol m2)
171
164--------------------------------------------------------------------- 172---------------------------------------------------------------------
165 173
166randomTestGaussian = c :~1~: snd (meanCov dat) where 174randomTestGaussian = c :~1~: snd (meanCov dat) where
@@ -325,6 +333,7 @@ runTests n = do
325 , utest "block" $ fromBlocks [[ident 3,0],[0,ident 4]] == (ident 7 :: CM) 333 , utest "block" $ fromBlocks [[ident 3,0],[0,ident 4]] == (ident 7 :: CM)
326 , odeTest 334 , odeTest
327 , fittingTest 335 , fittingTest
336 , mbCholTest
328 ] 337 ]
329 return () 338 return ()
330 339