summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-22 11:49:23 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-22 11:49:23 +0200
commit5158a1717f1d4caee25669a0781602fe64787302 (patch)
tree2846767b931e3a3429e0f2c5aed709c6800c94cc /packages/base/src/Numeric
parent3916d70b9d170633c6122cb3c46000f0b3f32018 (diff)
initial support for sparse matrix
Diffstat (limited to 'packages/base/src/Numeric')
-rw-r--r--packages/base/src/Numeric/Container.hs37
-rw-r--r--packages/base/src/Numeric/LinearAlgebra.hs14
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Data.hs7
3 files changed, 35 insertions, 23 deletions
diff --git a/packages/base/src/Numeric/Container.hs b/packages/base/src/Numeric/Container.hs
index 264a619..0633640 100644
--- a/packages/base/src/Numeric/Container.hs
+++ b/packages/base/src/Numeric/Container.hs
@@ -32,11 +32,11 @@ module Numeric.Container (
32 diag, ident, 32 diag, ident,
33 ctrans, 33 ctrans,
34 -- * Generic operations 34 -- * Generic operations
35 Container(..), 35 Container(..), Transposable(..), Linear(..),
36 -- * Matrix product 36 -- * Matrix product
37 Product(..), udot, dot, (◇), 37 Product(..), udot, dot, (◇),
38 Mul(..), 38 Mul(..),
39 Contraction(..), 39 Contraction(..),(<.>),
40 optimiseMult, 40 optimiseMult,
41 mXm,mXv,vXm,LSDiv(..), 41 mXm,mXv,vXm,LSDiv(..),
42 outer, kronecker, 42 outer, kronecker,
@@ -55,7 +55,9 @@ module Numeric.Container (
55 IndexOf, 55 IndexOf,
56 module Data.Complex, 56 module Data.Complex,
57 -- * IO 57 -- * IO
58 module Data.Packed.IO 58 module Data.Packed.IO,
59 -- * Misc
60 Testable(..)
59) where 61) where
60 62
61import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ) 63import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ)
@@ -87,10 +89,9 @@ linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n
87 89
88-------------------------------------------------------- 90--------------------------------------------------------
89 91
90class Contraction a b c | a b -> c 92{- | Matrix product, matrix - vector product, and dot product (equivalent to 'contraction')
91 where 93
92 infixl 7 <.> 94(This operator can also be written using the unicode symbol ◇ (25c7).)
93 {- | Matrix product, matrix - vector product, and dot product
94 95
95Examples: 96Examples:
96 97
@@ -129,22 +130,28 @@ For complex vectors the first argument is conjugated:
129 130
130>>> fromList [1,i,1-i] <.> complex a 131>>> fromList [1,i,1-i] <.> complex a
131fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] 132fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0]
132
133-} 133-}
134 (<.>) :: a -> b -> c 134infixl 7 <.>
135(<.>) :: Contraction a b c => a -> b -> c
136(<.>) = contraction
135 137
136 138
139class Contraction a b c | a b -> c
140 where
141 -- | Matrix product, matrix - vector product, and dot product
142 contraction :: a -> b -> c
143
137instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where 144instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where
138 u <.> v = conj u `udot` v 145 u `contraction` v = conj u `udot` v
139 146
140instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where 147instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where
141 (<.>) = mXv 148 contraction = mXv
142 149
143instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where 150instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where
144 (<.>) v m = (conj v) `vXm` m 151 contraction v m = (conj v) `vXm` m
145 152
146instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where 153instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where
147 (<.>) = mXm 154 contraction = mXm
148 155
149 156
150-------------------------------------------------------------------------------- 157--------------------------------------------------------------------------------
@@ -229,10 +236,10 @@ instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e
229 236
230-------------------------------------------------------------------------------- 237--------------------------------------------------------------------------------
231 238
232-- | alternative unicode symbol (25c7) for the contraction operator '(\<.\>)' 239-- | alternative unicode symbol (25c7) for 'contraction'
233(◇) :: Contraction a b c => a -> b -> c 240(◇) :: Contraction a b c => a -> b -> c
234infixl 7 ◇ 241infixl 7 ◇
235(◇) = (<.>) 242(◇) = contraction
236 243
237-- | dot product: @cdot u v = 'udot' ('conj' u) v@ 244-- | dot product: @cdot u v = 'udot' ('conj' u) v@
238dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t 245dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t
diff --git a/packages/base/src/Numeric/LinearAlgebra.hs b/packages/base/src/Numeric/LinearAlgebra.hs
index 96bf29f..549ebd0 100644
--- a/packages/base/src/Numeric/LinearAlgebra.hs
+++ b/packages/base/src/Numeric/LinearAlgebra.hs
@@ -37,11 +37,11 @@ module Numeric.LinearAlgebra (
37 37
38 -- * Matrix product 38 -- * Matrix product
39 (<.>), 39 (<.>),
40 40
41 -- | This operator can also be written using the unicode symbol ◇ (25c7). 41 -- | The overloaded multiplication operator may need type annotations to remove
42 -- ambiguity. In those cases we can also use the specific functions 'mXm', 'mXv', and 'dot'.
42 -- 43 --
43 44 -- The matrix x matrix product is also implemented in the "Data.Monoid" instance, where
44 -- | The matrix x matrix product is also implemented in the "Data.Monoid" instance, where
45 -- single-element matrices (created from numeric literals or using 'scalar') 45 -- single-element matrices (created from numeric literals or using 'scalar')
46 -- are used for scaling. 46 -- are used for scaling.
47 -- 47 --
@@ -52,6 +52,7 @@ module Numeric.LinearAlgebra (
52 -- , 4.0, 10.0, 0.0 ] 52 -- , 4.0, 10.0, 0.0 ]
53 -- 53 --
54 -- 'mconcat' uses 'optimiseMult' to get the optimal association order. 54 -- 'mconcat' uses 'optimiseMult' to get the optimal association order.
55
55 56
56 -- * Other products 57 -- * Other products
57 outer, kronecker, cross, 58 outer, kronecker, cross,
@@ -125,7 +126,7 @@ module Numeric.LinearAlgebra (
125 RandDist(..), randomVector, rand, randn, gaussianSample, uniformSample, 126 RandDist(..), randomVector, rand, randn, gaussianSample, uniformSample,
126 127
127 -- * Misc 128 -- * Misc
128 meanCov, peps, relativeError, haussholder, optimiseMult, udot, Seed, (◇) 129 meanCov, peps, relativeError, haussholder, optimiseMult, dot, udot, mXm, mXv, smXv, (<>), (◇), Seed, checkT
129) where 130) where
130 131
131import Numeric.LinearAlgebra.Data 132import Numeric.LinearAlgebra.Data
@@ -136,6 +137,5 @@ import Numeric.Container
136import Numeric.LinearAlgebra.Algorithms 137import Numeric.LinearAlgebra.Algorithms
137import Numeric.LinearAlgebra.Util 138import Numeric.LinearAlgebra.Util
138import Numeric.LinearAlgebra.Random 139import Numeric.LinearAlgebra.Random
139 140import Data.Packed.Internal.Sparse(smXv)
140
141 141
diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs
index 7e8af03..49bc1c0 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Data.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs
@@ -48,6 +48,10 @@ module Numeric.LinearAlgebra.Data(
48 latexFormat, 48 latexFormat,
49 dispf, disps, dispcf, format, 49 dispf, disps, dispcf, format,
50 50
51 -- * Sparse
52 SMatrix, AssocMatrix, mkCSR, toDense,
53 mkDiag,
54
51-- * Conversion 55-- * Conversion
52 Convert(..), 56 Convert(..),
53 57
@@ -56,7 +60,7 @@ module Numeric.LinearAlgebra.Data(
56 rows, cols, 60 rows, cols,
57 separable, 61 separable,
58 62
59 module Data.Complex 63 module Data.Complex,
60 64
61) where 65) where
62 66
@@ -65,4 +69,5 @@ import Data.Packed.Matrix
65import Numeric.Container 69import Numeric.Container
66import Numeric.LinearAlgebra.Util 70import Numeric.LinearAlgebra.Util
67import Data.Complex 71import Data.Complex
72import Data.Packed.Internal.Sparse
68 73