summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Algorithms.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-08-26 17:49:45 +0000
committerAlberto Ruiz <aruiz@um.es>2010-08-26 17:49:45 +0000
commit6058e1b17c005be1ea95ebb7d98d9fd15bb538d2 (patch)
treec4277e00c2c92a0ed8f3750255154fa8e2b6fe2d /lib/Numeric/LinearAlgebra/Algorithms.hs
parentf541d7dbdc8338b1dd1c0538751d837a16740bd8 (diff)
Float matrix product
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Algorithms.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs81
1 files changed, 1 insertions, 80 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index f4b7ee9..8962c60 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -21,9 +21,6 @@ imported from "Numeric.LinearAlgebra.LAPACK".
21module Numeric.LinearAlgebra.Algorithms ( 21module Numeric.LinearAlgebra.Algorithms (
22-- * Supported types 22-- * Supported types
23 Field(), 23 Field(),
24-- * Products
25 multiply, -- dot, moved dot to typeclass
26 outer, kronecker,
27-- * Linear Systems 24-- * Linear Systems
28 linearSolve, 25 linearSolve,
29 luSolve, 26 luSolve,
@@ -64,7 +61,6 @@ module Numeric.LinearAlgebra.Algorithms (
64-- * Norms 61-- * Norms
65 Normed(..), NormType(..), 62 Normed(..), NormType(..),
66-- * Misc 63-- * Misc
67 ctrans,
68 eps, i, 64 eps, i,
69-- * Util 65-- * Util
70 haussholder, 66 haussholder,
@@ -86,7 +82,7 @@ import Data.List(foldl1')
86import Data.Array 82import Data.Array
87 83
88-- | Auxiliary typeclass used to define generic computations for both real and complex matrices. 84-- | Auxiliary typeclass used to define generic computations for both real and complex matrices.
89class (Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t where 85class (Prod t, Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t where
90 svd' :: Matrix t -> (Matrix t, Vector Double, Matrix t) 86 svd' :: Matrix t -> (Matrix t, Vector Double, Matrix t)
91 thinSVD' :: Matrix t -> (Matrix t, Vector Double, Matrix t) 87 thinSVD' :: Matrix t -> (Matrix t, Vector Double, Matrix t)
92 sv' :: Matrix t -> Vector Double 88 sv' :: Matrix t -> Vector Double
@@ -105,8 +101,6 @@ class (Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t where
105 qr' :: Matrix t -> (Matrix t, Matrix t) 101 qr' :: Matrix t -> (Matrix t, Matrix t)
106 hess' :: Matrix t -> (Matrix t, Matrix t) 102 hess' :: Matrix t -> (Matrix t, Matrix t)
107 schur' :: Matrix t -> (Matrix t, Matrix t) 103 schur' :: Matrix t -> (Matrix t, Matrix t)
108 ctrans' :: Matrix t -> Matrix t
109 multiply' :: Matrix t -> Matrix t -> Matrix t
110 104
111 105
112instance Field Double where 106instance Field Double where
@@ -119,7 +113,6 @@ instance Field Double where
119 cholSolve' = cholSolveR 113 cholSolve' = cholSolveR
120 linearSolveLS' = linearSolveLSR 114 linearSolveLS' = linearSolveLSR
121 linearSolveSVD' = linearSolveSVDR Nothing 115 linearSolveSVD' = linearSolveSVDR Nothing
122 ctrans' = trans
123 eig' = eigR 116 eig' = eigR
124 eigSH'' = eigS 117 eigSH'' = eigS
125 eigOnly = eigOnlyR 118 eigOnly = eigOnlyR
@@ -129,7 +122,6 @@ instance Field Double where
129 qr' = unpackQR . qrR 122 qr' = unpackQR . qrR
130 hess' = unpackHess hessR 123 hess' = unpackHess hessR
131 schur' = schurR 124 schur' = schurR
132 multiply' = multiplyR
133 125
134instance Field (Complex Double) where 126instance Field (Complex Double) where
135#ifdef NOZGESDD 127#ifdef NOZGESDD
@@ -146,7 +138,6 @@ instance Field (Complex Double) where
146 cholSolve' = cholSolveC 138 cholSolve' = cholSolveC
147 linearSolveLS' = linearSolveLSC 139 linearSolveLS' = linearSolveLSC
148 linearSolveSVD' = linearSolveSVDC Nothing 140 linearSolveSVD' = linearSolveSVDC Nothing
149 ctrans' = conj . trans
150 eig' = eigC 141 eig' = eigC
151 eigOnly = eigOnlyC 142 eigOnly = eigOnlyC
152 eigSH'' = eigH 143 eigSH'' = eigH
@@ -156,7 +147,6 @@ instance Field (Complex Double) where
156 qr' = unpackQR . qrC 147 qr' = unpackQR . qrC
157 hess' = unpackHess hessC 148 hess' = unpackHess hessC
158 schur' = schurC 149 schur' = schurC
159 multiply' = multiplyC
160 150
161-------------------------------------------------------------- 151--------------------------------------------------------------
162 152
@@ -324,13 +314,6 @@ hess = hess'
324schur :: Field t => Matrix t -> (Matrix t, Matrix t) 314schur :: Field t => Matrix t -> (Matrix t, Matrix t)
325schur = schur' 315schur = schur'
326 316
327-- | Generic conjugate transpose.
328ctrans :: Field t => Matrix t -> Matrix t
329ctrans = ctrans'
330
331-- | Matrix product.
332multiply :: Field t => Matrix t -> Matrix t -> Matrix t
333multiply = {-# SCC "multiply" #-} multiply'
334 317
335-- | Similar to 'cholSH', but instead of an error (e.g., caused by a matrix not positive definite) it returns 'Nothing'. 318-- | Similar to 'cholSH', but instead of an error (e.g., caused by a matrix not positive definite) it returns 'Nothing'.
336mbCholSH :: Field t => Matrix t -> Maybe (Matrix t) 319mbCholSH :: Field t => Matrix t -> Maybe (Matrix t)
@@ -404,20 +387,6 @@ peps x = 2.0**(fromIntegral $ 1-floatDigits x)
404i :: Complex Double 387i :: Complex Double
405i = 0:+1 388i = 0:+1
406 389
407
408-- matrix product
409mXm :: (Num t, Field t) => Matrix t -> Matrix t -> Matrix t
410mXm = multiply
411
412-- matrix - vector product
413mXv :: (Num t, Field t) => Matrix t -> Vector t -> Vector t
414mXv m v = flatten $ m `mXm` (asColumn v)
415
416-- vector - matrix product
417vXm :: (Num t, Field t) => Vector t -> Matrix t -> Vector t
418vXm v m = flatten $ (asRow v) `mXm` m
419
420
421--------------------------------------------------------------------------- 390---------------------------------------------------------------------------
422 391
423norm2 :: Vector Double -> Double 392norm2 :: Vector Double -> Double
@@ -723,51 +692,3 @@ luFact (l_u,perm) | r <= c = (l ,u ,p, s)
723 (|*|) = mul 692 (|*|) = mul
724 693
725-------------------------------------------------- 694--------------------------------------------------
726
727{- moved to Numeric.LinearAlgebra.Interface Vector typeclass
728-- | Euclidean inner product.
729dot :: (Field t) => Vector t -> Vector t -> t
730dot u v = multiply r c @@> (0,0)
731 where r = asRow u
732 c = asColumn v
733-}
734
735{- | Outer product of two vectors.
736
737@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]
738(3><3)
739 [ 5.0, 2.0, 3.0
740 , 10.0, 4.0, 6.0
741 , 15.0, 6.0, 9.0 ]@
742-}
743outer :: (Field t) => Vector t -> Vector t -> Matrix t
744outer u v = asColumn u `multiply` asRow v
745
746{- | Kronecker product of two matrices.
747
748@m1=(2><3)
749 [ 1.0, 2.0, 0.0
750 , 0.0, -1.0, 3.0 ]
751m2=(4><3)
752 [ 1.0, 2.0, 3.0
753 , 4.0, 5.0, 6.0
754 , 7.0, 8.0, 9.0
755 , 10.0, 11.0, 12.0 ]@
756
757@\> kronecker m1 m2
758(8><9)
759 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0
760 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0
761 , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0
762 , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0
763 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0
764 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0
765 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0
766 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@
767-}
768kronecker :: (Field t) => Matrix t -> Matrix t -> Matrix t
769kronecker a b = fromBlocks
770 . splitEvery (cols a)
771 . map (reshape (cols b))
772 . toRows
773 $ flatten a `outer` flatten b