summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-09 07:55:32 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-09 07:55:32 +0000
commitc1441ca0451f34e5615b3861175f9eef312dfc62 (patch)
treee1c38a11c0111b21e1b17ef969700540d65af926
parent945ae28d720ab6367a293d8f6a5723b8f7bf835d (diff)
joined Vectors and Product
-rw-r--r--lib/Numeric/Container.hs52
-rw-r--r--lib/Numeric/Vector.hs2
2 files changed, 21 insertions, 33 deletions
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs
index a3a039a..0a07adc 100644
--- a/lib/Numeric/Container.hs
+++ b/lib/Numeric/Container.hs
@@ -20,9 +20,7 @@
20----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
21 21
22module Numeric.Container ( 22module Numeric.Container (
23 --Linear(..),
24 Container(..), 23 Container(..),
25 Vectors(..),
26 Product(..), 24 Product(..),
27 mXm,mXv,vXm, 25 mXm,mXv,vXm,
28 outer, kronecker, 26 outer, kronecker,
@@ -212,68 +210,58 @@ instance (Container Vector a) => Container Matrix a where
212 210
213 211
214-- | Linear algebraic properties of objects 212-- | Linear algebraic properties of objects
215class Num e => Vectors a e where 213class Element e => Product e where
214 -- | matrix product
215 multiply :: Matrix e -> Matrix e -> Matrix e
216 -- | conjugate transpose
217 ctrans :: Matrix e -> Matrix e
216 -- | dot (inner) product 218 -- | dot (inner) product
217 dot :: a e -> a e -> e 219 dot :: Vector e -> Vector e -> e
218 -- | sum of absolute value of elements (differs in complex case from @norm1@ 220 -- | sum of absolute value of elements (differs in complex case from @norm1@
219 absSum :: a e -> RealOf e 221 absSum :: Vector e -> RealOf e
220 -- | sum of absolute value of elements 222 -- | sum of absolute value of elements
221 norm1 :: a e -> RealOf e 223 norm1 :: Vector e -> RealOf e
222 -- | euclidean norm 224 -- | euclidean norm
223 norm2 :: a e -> RealOf e 225 norm2 :: Vector e -> RealOf e
224 -- | element of maximum magnitude 226 -- | element of maximum magnitude
225 normInf :: a e -> RealOf e 227 normInf :: Vector e -> RealOf e
226 228
227instance Vectors Vector Float where 229instance Product Float where
228 norm2 = toScalarF Norm2 230 norm2 = toScalarF Norm2
229 absSum = toScalarF AbsSum 231 absSum = toScalarF AbsSum
230 dot = dotF 232 dot = dotF
231 norm1 = toScalarF AbsSum 233 norm1 = toScalarF AbsSum
232 normInf = maxElement . vectorMapF Abs 234 normInf = maxElement . vectorMapF Abs
235 multiply = multiplyF
236 ctrans = trans
233 237
234instance Vectors Vector Double where 238instance Product Double where
235 norm2 = toScalarR Norm2 239 norm2 = toScalarR Norm2
236 absSum = toScalarR AbsSum 240 absSum = toScalarR AbsSum
237 dot = dotR 241 dot = dotR
238 norm1 = toScalarR AbsSum 242 norm1 = toScalarR AbsSum
239 normInf = maxElement . vectorMapR Abs 243 normInf = maxElement . vectorMapR Abs
244 multiply = multiplyR
245 ctrans = trans
240 246
241instance Vectors Vector (Complex Float) where 247instance Product (Complex Float) where
242 norm2 = toScalarQ Norm2 248 norm2 = toScalarQ Norm2
243 absSum = toScalarQ AbsSum 249 absSum = toScalarQ AbsSum
244 dot = dotQ 250 dot = dotQ
245 norm1 = sumElements . fst . fromComplex . vectorMapQ Abs 251 norm1 = sumElements . fst . fromComplex . vectorMapQ Abs
246 normInf = maxElement . fst . fromComplex . vectorMapQ Abs 252 normInf = maxElement . fst . fromComplex . vectorMapQ Abs
253 multiply = multiplyQ
254 ctrans = conj . trans
247 255
248instance Vectors Vector (Complex Double) where 256instance Product (Complex Double) where
249 norm2 = toScalarC Norm2 257 norm2 = toScalarC Norm2
250 absSum = toScalarC AbsSum 258 absSum = toScalarC AbsSum
251 dot = dotC 259 dot = dotC
252 norm1 = sumElements . fst . fromComplex . vectorMapC Abs 260 norm1 = sumElements . fst . fromComplex . vectorMapC Abs
253 normInf = maxElement . fst . fromComplex . vectorMapC Abs 261 normInf = maxElement . fst . fromComplex . vectorMapC Abs
254
255----------------------------------------------------
256
257class Element t => Product t where
258 multiply :: Matrix t -> Matrix t -> Matrix t
259 ctrans :: Matrix t -> Matrix t
260
261instance Product Double where
262 multiply = multiplyR
263 ctrans = trans
264
265instance Product (Complex Double) where
266 multiply = multiplyC 262 multiply = multiplyC
267 ctrans = conj . trans 263 ctrans = conj . trans
268 264
269instance Product Float where
270 multiply = multiplyF
271 ctrans = trans
272
273instance Product (Complex Float) where
274 multiply = multiplyQ
275 ctrans = conj . trans
276
277---------------------------------------------------------- 265----------------------------------------------------------
278 266
279-- synonym for matrix product 267-- synonym for matrix product
diff --git a/lib/Numeric/Vector.hs b/lib/Numeric/Vector.hs
index 9427243..7ecd0eb 100644
--- a/lib/Numeric/Vector.hs
+++ b/lib/Numeric/Vector.hs
@@ -99,7 +99,7 @@ linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1]
99 where s = (b-a)/fromIntegral (n-1) 99 where s = (b-a)/fromIntegral (n-1)
100 100
101-- | Dot product: @u \<.\> v = dot u v@ 101-- | Dot product: @u \<.\> v = dot u v@
102(<.>) :: Vectors Vector t => Vector t -> Vector t -> t 102(<.>) :: Product t => Vector t -> Vector t -> t
103infixl 7 <.> 103infixl 7 <.>
104(<.>) = dot 104(<.>) = dot
105 105