summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/base/CHANGELOG4
-rw-r--r--packages/base/src/Internal/Algorithms.hs71
-rw-r--r--packages/base/src/Internal/Util.hs2
-rw-r--r--packages/base/src/Numeric/LinearAlgebra.hs4
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Static.hs2
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs13
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs2
7 files changed, 50 insertions, 48 deletions
diff --git a/packages/base/CHANGELOG b/packages/base/CHANGELOG
index 5b0adc1..581d2ac 100644
--- a/packages/base/CHANGELOG
+++ b/packages/base/CHANGELOG
@@ -2,9 +2,9 @@
2-------- 2--------
3 3
4 * eigSH, chol, and other functions that work with Hermitian or symmetric matrices 4 * eigSH, chol, and other functions that work with Hermitian or symmetric matrices
5 now take a special "Sym" argument that can be created by means of "sym" 5 now take a special "Herm" argument that can be created by means of "sym"
6 or "mTm". The unchecked versions of those functions have been removed and we 6 or "mTm". The unchecked versions of those functions have been removed and we
7 use "trustSym" to create the Sym type when the matrix is known to be Hermitian/symmetric. 7 use "trustSym" to create the Herm type when the matrix is known to be Hermitian/symmetric.
8 8
9 * Improved matrix extraction (??) and rectangular matrix slices without data copy 9 * Improved matrix extraction (??) and rectangular matrix slices without data copy
10 10
diff --git a/packages/base/src/Internal/Algorithms.hs b/packages/base/src/Internal/Algorithms.hs
index ee3ddff..c4f1a60 100644
--- a/packages/base/src/Internal/Algorithms.hs
+++ b/packages/base/src/Internal/Algorithms.hs
@@ -4,12 +4,6 @@
4{-# LANGUAGE UndecidableInstances #-} 4{-# LANGUAGE UndecidableInstances #-}
5{-# LANGUAGE TypeFamilies #-} 5{-# LANGUAGE TypeFamilies #-}
6 6
7{-# LANGUAGE FlexibleContexts, FlexibleInstances #-}
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE MultiParamTypeClasses #-}
10{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE TypeFamilies #-}
12
13----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
14{- | 8{- |
15Module : Internal.Algorithms 9Module : Internal.Algorithms
@@ -376,8 +370,8 @@ ldlPackedSH x = {-# SCC "ldlPacked" #-} LDL m p
376 (m,p) = ldlPacked' x 370 (m,p) = ldlPacked' x
377 371
378-- | Obtains the LDL decomposition of a matrix in a compact data structure suitable for 'ldlSolve'. 372-- | Obtains the LDL decomposition of a matrix in a compact data structure suitable for 'ldlSolve'.
379ldlPacked :: Field t => Her t -> LDL t 373ldlPacked :: Field t => Herm t -> LDL t
380ldlPacked (Her m) = ldlPackedSH m 374ldlPacked (Herm m) = ldlPackedSH m
381 375
382-- | Solution of a linear system (for several right hand sides) from a precomputed LDL factorization obtained by 'ldlPacked'. 376-- | Solution of a linear system (for several right hand sides) from a precomputed LDL factorization obtained by 'ldlPacked'.
383-- 377--
@@ -461,18 +455,23 @@ fromList [11.344814282762075,0.17091518882717918,-0.5157294715892575]
4613.000 5.000 6.000 4553.000 5.000 6.000
462 456
463-} 457-}
464eigSH :: Field t => Her t -> (Vector Double, Matrix t) 458eigSH :: Field t => Herm t -> (Vector Double, Matrix t)
465eigSH (Her m) = eigSH' m 459eigSH (Herm m) = eigSH' m
466 460
467-- | Eigenvalues (in descending order) of a complex hermitian or real symmetric matrix. 461-- | Eigenvalues (in descending order) of a complex hermitian or real symmetric matrix.
468eigenvaluesSH :: Field t => Her t -> Vector Double 462eigenvaluesSH :: Field t => Herm t -> Vector Double
469eigenvaluesSH (Her m) = eigenvaluesSH' m 463eigenvaluesSH (Herm m) = eigenvaluesSH' m
470 464
471-------------------------------------------------------------- 465--------------------------------------------------------------
472 466
473-- | QR decomposition of a matrix in compact form. (The orthogonal matrix is not explicitly formed.) 467-- | QR decomposition of a matrix in compact form. (The orthogonal matrix is not explicitly formed.)
474data QR t = QR (Matrix t) (Vector t) 468data QR t = QR (Matrix t) (Vector t)
475 469
470instance (NFData t, Numeric t) => NFData (QR t)
471 where
472 rnf (QR m _) = rnf m
473
474
476-- | QR factorization. 475-- | QR factorization.
477-- 476--
478-- If @(q,r) = qr m@ then @m == q \<> r@, where q is unitary and r is upper triangular. 477-- If @(q,r) = qr m@ then @m == q \<> r@, where q is unitary and r is upper triangular.
@@ -533,12 +532,12 @@ cholSH = cholSH'
533-- | Cholesky factorization of a positive definite hermitian or symmetric matrix. 532-- | Cholesky factorization of a positive definite hermitian or symmetric matrix.
534-- 533--
535-- If @c = chol m@ then @c@ is upper triangular and @m == tr c \<> c@. 534-- If @c = chol m@ then @c@ is upper triangular and @m == tr c \<> c@.
536chol :: Field t => Her t -> Matrix t 535chol :: Field t => Herm t -> Matrix t
537chol (Her m) = {-# SCC "chol" #-} cholSH' m 536chol (Herm m) = {-# SCC "chol" #-} cholSH' m
538 537
539-- | Similar to 'chol', but instead of an error (e.g., caused by a matrix not positive definite) it returns 'Nothing'. 538-- | Similar to 'chol', but instead of an error (e.g., caused by a matrix not positive definite) it returns 'Nothing'.
540mbChol :: Field t => Her t -> Maybe (Matrix t) 539mbChol :: Field t => Herm t -> Maybe (Matrix t)
541mbChol (Her m) = {-# SCC "mbChol" #-} mbCholSH' m 540mbChol (Herm m) = {-# SCC "mbChol" #-} mbCholSH' m
542 541
543 542
544 543
@@ -977,10 +976,10 @@ relativeError norm a b = r
977-- | Generalized symmetric positive definite eigensystem Av = lBv, 976-- | Generalized symmetric positive definite eigensystem Av = lBv,
978-- for A and B symmetric, B positive definite. 977-- for A and B symmetric, B positive definite.
979geigSH :: Field t 978geigSH :: Field t
980 => Her t -- ^ A 979 => Herm t -- ^ A
981 -> Her t -- ^ B 980 -> Herm t -- ^ B
982 -> (Vector Double, Matrix t) 981 -> (Vector Double, Matrix t)
983geigSH (Her a) (Her b) = geigSH' a b 982geigSH (Herm a) (Herm b) = geigSH' a b
984 983
985geigSH' :: Field t 984geigSH' :: Field t
986 => Matrix t -- ^ A 985 => Matrix t -- ^ A
@@ -999,29 +998,33 @@ geigSH' a b = (l,v')
999 998
1000-- | A matrix that, by construction, it is known to be complex Hermitian or real symmetric. 999-- | A matrix that, by construction, it is known to be complex Hermitian or real symmetric.
1001-- 1000--
1002-- It can be created using 'sym', 'xTx', or 'trustSym', and the matrix can be extracted using 'her'. 1001-- It can be created using 'sym', 'mTm', or 'trustSym', and the matrix can be extracted using 'unSym'.
1003data Her t = Her (Matrix t) deriving Show 1002newtype Herm t = Herm (Matrix t) deriving Show
1003
1004instance (NFData t, Numeric t) => NFData (Herm t)
1005 where
1006 rnf (Herm m) = rnf m
1004 1007
1005-- | Extract the general matrix from a 'Her' structure, forgetting its symmetric or Hermitian property. 1008-- | Extract the general matrix from a 'Herm' structure, forgetting its symmetric or Hermitian property.
1006her :: Her t -> Matrix t 1009unSym :: Herm t -> Matrix t
1007her (Her x) = x 1010unSym (Herm x) = x
1008 1011
1009-- | Compute the complex Hermitian or real symmetric part of a square matrix (@(x + tr x)/2@). 1012-- | Compute the complex Hermitian or real symmetric part of a square matrix (@(x + tr x)/2@).
1010sym :: Field t => Matrix t -> Her t 1013sym :: Field t => Matrix t -> Herm t
1011sym x = Her (scale 0.5 (tr x `add` x)) 1014sym x = Herm (scale 0.5 (tr x `add` x))
1012 1015
1013-- | Compute the contraction @tr x <> x@ of a general matrix. 1016-- | Compute the contraction @tr x <> x@ of a general matrix.
1014xTx :: Numeric t => Matrix t -> Her t 1017mTm :: Numeric t => Matrix t -> Herm t
1015xTx x = Her (tr x `mXm` x) 1018mTm x = Herm (tr x `mXm` x)
1016 1019
1017instance Field t => Linear t Her where 1020instance Field t => Linear t Herm where
1018 scale x (Her m) = Her (scale x m) 1021 scale x (Herm m) = Herm (scale x m)
1019 1022
1020instance Field t => Additive (Her t) where 1023instance Field t => Additive (Herm t) where
1021 add (Her a) (Her b) = Her (a `add` b) 1024 add (Herm a) (Herm b) = Herm (a `add` b)
1022 1025
1023-- | At your own risk, declare that a matrix is complex Hermitian or real symmetric 1026-- | At your own risk, declare that a matrix is complex Hermitian or real symmetric
1024-- for usage in 'chol', 'eigSH', etc. Only a triangular part of the matrix will be used. 1027-- for usage in 'chol', 'eigSH', etc. Only a triangular part of the matrix will be used.
1025trustSym :: Matrix t -> Her t 1028trustSym :: Matrix t -> Herm t
1026trustSym x = (Her x) 1029trustSym x = (Herm x)
1027 1030
diff --git a/packages/base/src/Internal/Util.hs b/packages/base/src/Internal/Util.hs
index 36b7855..cf42961 100644
--- a/packages/base/src/Internal/Util.hs
+++ b/packages/base/src/Internal/Util.hs
@@ -462,7 +462,7 @@ null1 :: Matrix R -> Vector R
462null1 = last . toColumns . snd . rightSV 462null1 = last . toColumns . snd . rightSV
463 463
464-- | solution of overconstrained homogeneous symmetric linear system 464-- | solution of overconstrained homogeneous symmetric linear system
465null1sym :: Her R -> Vector R 465null1sym :: Herm R -> Vector R
466null1sym = last . toColumns . snd . eigSH 466null1sym = last . toColumns . snd . eigSH
467 467
468-------------------------------------------------------------------------------- 468--------------------------------------------------------------------------------
diff --git a/packages/base/src/Numeric/LinearAlgebra.hs b/packages/base/src/Numeric/LinearAlgebra.hs
index ea54932..6a9c33a 100644
--- a/packages/base/src/Numeric/LinearAlgebra.hs
+++ b/packages/base/src/Numeric/LinearAlgebra.hs
@@ -154,9 +154,9 @@ module Numeric.LinearAlgebra (
154 -- * Misc 154 -- * Misc
155 meanCov, rowOuters, pairwiseD2, unitary, peps, relativeError, magnit, 155 meanCov, rowOuters, pairwiseD2, unitary, peps, relativeError, magnit,
156 haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv, 156 haussholder, optimiseMult, udot, nullspaceSVD, orthSVD, ranksv,
157 iC, sym, xTx, trustSym, her, 157 iC, sym, mTm, trustSym, unSym,
158 -- * Auxiliary classes 158 -- * Auxiliary classes
159 Element, Container, Product, Numeric, LSDiv, Her, 159 Element, Container, Product, Numeric, LSDiv, Herm,
160 Complexable, RealElement, 160 Complexable, RealElement,
161 RealOf, ComplexOf, SingleOf, DoubleOf, 161 RealOf, ComplexOf, SingleOf, DoubleOf,
162 IndexOf, 162 IndexOf,
diff --git a/packages/base/src/Numeric/LinearAlgebra/Static.hs b/packages/base/src/Numeric/LinearAlgebra/Static.hs
index ded69fa..843c727 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Static.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Static.hs
@@ -65,7 +65,7 @@ import Numeric.LinearAlgebra hiding (
65 row,col,vector,matrix,linspace,toRows,toColumns, 65 row,col,vector,matrix,linspace,toRows,toColumns,
66 (<\>),fromList,takeDiag,svd,eig,eigSH, 66 (<\>),fromList,takeDiag,svd,eig,eigSH,
67 eigenvalues,eigenvaluesSH,build, 67 eigenvalues,eigenvaluesSH,build,
68 qr,size,dot,chol,range,R,C,Her,her,sym) 68 qr,size,dot,chol,range,R,C,sym,mTm,unSym)
69import qualified Numeric.LinearAlgebra as LA 69import qualified Numeric.LinearAlgebra as LA
70import Data.Proxy(Proxy) 70import Data.Proxy(Proxy)
71import Internal.Static 71import Internal.Static
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
index 4704989..3d5441d 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
@@ -81,8 +81,7 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where
81 81
82 82
83-- a complex hermitian or real symmetric matrix 83-- a complex hermitian or real symmetric matrix
84--newtype (Her a) = Her (Matrix a) deriving Show 84instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Herm a) where
85instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where
86 arbitrary = do 85 arbitrary = do
87 Sq m <- arbitrary 86 Sq m <- arbitrary
88 let m' = m/2 87 let m' = m/2
@@ -127,7 +126,7 @@ instance (Numeric a, ArbitraryField a, Num (Vector a))
127 arbitrary = do 126 arbitrary = do
128 m <- arbitrary 127 m <- arbitrary
129 let (_,v) = eigSH m 128 let (_,v) = eigSH m
130 n = rows (her m) 129 n = rows (unSym m)
131 l <- replicateM n (choose (0,100)) 130 l <- replicateM n (choose (0,100))
132 let s = diag (fromList l) 131 let s = diag (fromList l)
133 p = v <> real s <> tr v 132 p = v <> real s <> tr v
@@ -161,8 +160,8 @@ fM m = m :: FM
161zM m = m :: ZM 160zM m = m :: ZM
162 161
163 162
164rHer m = her m :: RM 163rHer m = unSym m :: RM
165cHer m = her m :: CM 164cHer m = unSym m :: CM
166 165
167rRot (Rot m) = m :: RM 166rRot (Rot m) = m :: RM
168cRot (Rot m) = m :: CM 167cRot (Rot m) = m :: CM
@@ -176,8 +175,8 @@ cWC (WC m) = m :: CM
176rSqWC (SqWC m) = m :: RM 175rSqWC (SqWC m) = m :: RM
177cSqWC (SqWC m) = m :: CM 176cSqWC (SqWC m) = m :: CM
178 177
179rSymWC (SqWC m) = sym m :: Her R 178rSymWC (SqWC m) = sym m :: Herm R
180cSymWC (SqWC m) = sym m :: Her C 179cSymWC (SqWC m) = sym m :: Herm C
181 180
182rPosDef (PosDef m) = m :: RM 181rPosDef (PosDef m) = m :: RM
183cPosDef (PosDef m) = m :: CM 182cPosDef (PosDef m) = m :: CM
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
index 2ac3588..720b7bd 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
@@ -263,7 +263,7 @@ multProp2 p (a,b) = (tr (a <> b)) :~p~: (tr b <> tr a)
263 263
264linearSolveProp f m = f m m |~| ident (rows m) 264linearSolveProp f m = f m m |~| ident (rows m)
265 265
266linearSolvePropH f m = f m (her m) |~| ident (rows (her m)) 266linearSolvePropH f m = f m (unSym m) |~| ident (rows (unSym m))
267 267
268linearSolveProp2 f (a,x) = not wc `trivial` (not wc || a <> f a b |~| b) 268linearSolveProp2 f (a,x) = not wc `trivial` (not wc || a <> f a b |~| b)
269 where q = min (rows a) (cols a) 269 where q = min (rows a) (cols a)