diff options
Diffstat (limited to 'packages/base/src/Data/Packed/Numeric.hs')
-rw-r--r-- | packages/base/src/Data/Packed/Numeric.hs | 74 |
1 files changed, 26 insertions, 48 deletions
diff --git a/packages/base/src/Data/Packed/Numeric.hs b/packages/base/src/Data/Packed/Numeric.hs index e90c612..d2a20be 100644 --- a/packages/base/src/Data/Packed/Numeric.hs +++ b/packages/base/src/Data/Packed/Numeric.hs | |||
@@ -1,4 +1,3 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | 1 | {-# LANGUAGE FlexibleContexts #-} |
3 | {-# LANGUAGE FlexibleInstances #-} | 2 | {-# LANGUAGE FlexibleInstances #-} |
4 | {-# LANGUAGE MultiParamTypeClasses #-} | 3 | {-# LANGUAGE MultiParamTypeClasses #-} |
@@ -95,67 +94,46 @@ linspace 1 (a,b) = fromList[(a+b)/2] | |||
95 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] | 94 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] |
96 | where s = (b-a)/fromIntegral (n-1) | 95 | where s = (b-a)/fromIntegral (n-1) |
97 | 96 | ||
98 | -------------------------------------------------------- | 97 | -------------------------------------------------------------------------------- |
99 | |||
100 | {- Matrix product, matrix - vector product, and dot product (equivalent to 'contraction') | ||
101 | |||
102 | (This operator can also be written using the unicode symbol ◇ (25c7).) | ||
103 | |||
104 | Examples: | ||
105 | |||
106 | >>> let a = (3><4) [1..] :: Matrix Double | ||
107 | >>> let v = fromList [1,0,2,-1] :: Vector Double | ||
108 | >>> let u = fromList [1,2,3] :: Vector Double | ||
109 | |||
110 | >>> a | ||
111 | (3><4) | ||
112 | [ 1.0, 2.0, 3.0, 4.0 | ||
113 | , 5.0, 6.0, 7.0, 8.0 | ||
114 | , 9.0, 10.0, 11.0, 12.0 ] | ||
115 | |||
116 | matrix × matrix: | ||
117 | 98 | ||
118 | >>> disp 2 (a <.> trans a) | 99 | infixl 7 <.> |
119 | 3x3 | 100 | -- | An infix synonym for 'dot' |
120 | 30 70 110 | 101 | (<.>) :: Numeric t => Vector t -> Vector t -> t |
121 | 70 174 278 | 102 | (<.>) = dot |
122 | 110 278 446 | ||
123 | 103 | ||
124 | matrix × vector: | ||
125 | 104 | ||
126 | >>> a <.> v | 105 | infixr 8 <·>, #> |
127 | fromList [3.0,11.0,19.0] | ||
128 | 106 | ||
129 | dot product: | 107 | {- | dot product |
130 | 108 | ||
131 | >>> u <.> fromList[3,2,1::Double] | 109 | >>> vect [1,2,3,4] <·> vect [-2,0,1,1] |
132 | 10 | 110 | 5.0 |
133 | 111 | ||
134 | For complex vectors the first argument is conjugated: | 112 | >>> let 𝑖 = 0:+1 :: ℂ |
113 | >>> fromList [1+𝑖,1] <·> fromList [1,1+𝑖] | ||
114 | 2.0 :+ 0.0 | ||
135 | 115 | ||
136 | >>> fromList [1,i] <.> fromList[2*i+1,3] | 116 | (the dot symbol "·" is obtained by Alt-Gr .) |
137 | 1.0 :+ (-1.0) | ||
138 | 117 | ||
139 | >>> fromList [1,i,1-i] <.> complex a | ||
140 | fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] | ||
141 | -} | 118 | -} |
119 | (<·>) :: Numeric t => Vector t -> Vector t -> t | ||
120 | (<·>) = dot | ||
142 | 121 | ||
143 | 122 | ||
144 | -------------------------------------------------------------------------------- | 123 | {- | dense matrix-vector product |
145 | |||
146 | infixl 7 <.> | ||
147 | -- | An infix synonym for 'dot' | ||
148 | (<.>) :: Numeric t => Vector t -> Vector t -> t | ||
149 | (<.>) = dot | ||
150 | 124 | ||
125 | >>> let m = (2><3) [1..] | ||
126 | >>> m | ||
127 | (2><3) | ||
128 | [ 1.0, 2.0, 3.0 | ||
129 | , 4.0, 5.0, 6.0 ] | ||
151 | 130 | ||
152 | infixr 8 <·>, #> | 131 | >>> let v = vect [10,20,30] |
153 | -- | dot product | ||
154 | (<·>) :: Numeric t => Vector t -> Vector t -> t | ||
155 | (<·>) = dot | ||
156 | 132 | ||
133 | >>> m #> v | ||
134 | fromList [140.0,320.0] | ||
157 | 135 | ||
158 | -- | matrix-vector product | 136 | -} |
159 | (#>) :: Numeric t => Matrix t -> Vector t -> Vector t | 137 | (#>) :: Numeric t => Matrix t -> Vector t -> Vector t |
160 | (#>) = mXv | 138 | (#>) = mXv |
161 | 139 | ||
@@ -291,4 +269,4 @@ instance Numeric (Complex Double) | |||
291 | instance Numeric Float | 269 | instance Numeric Float |
292 | instance Numeric (Complex Float) | 270 | instance Numeric (Complex Float) |
293 | 271 | ||
294 | -------------------------------------------------------------------------------- | 272 | |