summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Linear.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-10-01 15:04:16 +0000
committerAlberto Ruiz <aruiz@um.es>2007-10-01 15:04:16 +0000
commitc99b8fd6e3f8a2fb365ec12baf838f864b118ece (patch)
tree11b5b8515861fe88d547253ae10c2182d5fadaf2 /lib/Numeric/LinearAlgebra/Linear.hs
parent768f08d4134a066d773d56a9c03ae688e3850352 (diff)
LinearAlgebra and GSL moved to Numeric
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs102
1 files changed, 102 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
new file mode 100644
index 0000000..a3a0183
--- /dev/null
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -0,0 +1,102 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
2-----------------------------------------------------------------------------
3{- |
4Module : Numeric.LinearAlgebra.Linear
5Copyright : (c) Alberto Ruiz 2006-7
6License : GPL-style
7
8Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional
10Portability : uses ffi
11
12Basic optimized operations on vectors and matrices.
13
14-}
15-----------------------------------------------------------------------------
16
17module Numeric.LinearAlgebra.Linear (
18 Linear(..),
19 multiply, dot, outer
20) where
21
22
23import Data.Packed.Internal
24import Data.Packed
25import Numeric.GSL.Vector
26import Complex
27
28-- | basic optimized operations
29class (Container c e) => Linear c e where
30 scale :: e -> c e -> c e
31 addConstant :: e -> c e -> c e
32 add :: c e -> c e -> c e
33 sub :: c e -> c e -> c e
34 -- | element by element multiplication
35 mul :: c e -> c e -> c e
36 -- | element by element division
37 divide :: c e -> c e -> c e
38 -- | scale the element by element reciprocal of the object: @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@
39 scaleRecip :: e -> c e -> c e
40 equal :: c e -> c e -> Bool
41-- numequal :: Double -> c e -> c e -> Bool
42
43instance Linear Vector Double where
44 scale = vectorMapValR Scale
45 scaleRecip = vectorMapValR Recip
46 addConstant = vectorMapValR AddConstant
47 add = vectorZipR Add
48 sub = vectorZipR Sub
49 mul = vectorZipR Mul
50 divide = vectorZipR Div
51 equal u v = dim u == dim v && vectorMax (vectorMapR Abs (sub u v)) == 0.0
52
53instance Linear Vector (Complex Double) where
54 scale = vectorMapValC Scale
55 scaleRecip = vectorMapValC Recip
56 addConstant = vectorMapValC AddConstant
57 add = vectorZipC Add
58 sub = vectorZipC Sub
59 mul = vectorZipC Mul
60 divide = vectorZipC Div
61 equal u v = dim u == dim v && vectorMax (liftVector magnitude (sub u v)) == 0.0
62
63instance Linear Matrix Double where
64 scale x = liftMatrix (scale x)
65 scaleRecip x = liftMatrix (scaleRecip x)
66 addConstant x = liftMatrix (addConstant x)
67 add = liftMatrix2 add
68 sub = liftMatrix2 sub
69 mul = liftMatrix2 mul
70 divide = liftMatrix2 divide
71 equal a b = cols a == cols b && cdat a `equal` cdat b
72
73
74instance Linear Matrix (Complex Double) where
75 scale x = liftMatrix (scale x)
76 scaleRecip x = liftMatrix (scaleRecip x)
77 addConstant x = liftMatrix (addConstant x)
78 add = liftMatrix2 add
79 sub = liftMatrix2 sub
80 mul = liftMatrix2 mul
81 divide = liftMatrix2 divide
82 equal a b = cols a == cols b && cdat a `equal` cdat b
83
84--------------------------------------------------
85
86-- | euclidean inner product
87dot :: (Field t) => Vector t -> Vector t -> t
88dot u v = dat (multiply r c) `at` 0
89 where r = asRow u
90 c = asColumn v
91
92
93{- | Outer product of two vectors.
94
95@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]
96(3><3)
97 [ 5.0, 2.0, 3.0
98 , 10.0, 4.0, 6.0
99 , 15.0, 6.0, 9.0 ]@
100-}
101outer :: (Field t) => Vector t -> Vector t -> Matrix t
102outer u v = asColumn u `multiply` asRow v