diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-09-04 09:34:13 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-09-04 09:34:13 +0000 |
commit | 7aa4500e6c71964094374d553faad1b1754cbc65 (patch) | |
tree | e3de3c60d1eecd12236afd635abf7625528cd6df /lib/Numeric/LinearAlgebra/Linear.hs | |
parent | 4486e93da02c7ef9e1fdf785c88f78986048c332 (diff) |
vector norms moved to Vectors
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 61 |
1 files changed, 17 insertions, 44 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 6c21a16..4d7f608 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -24,8 +24,6 @@ module Numeric.LinearAlgebra.Linear ( | |||
24 | Product(..), | 24 | Product(..), |
25 | mXm,mXv,vXm, | 25 | mXm,mXv,vXm, |
26 | outer, kronecker, | 26 | outer, kronecker, |
27 | -- * Norms | ||
28 | Norm(..), Norm2(..), | ||
29 | -- * Creation of numeric vectors | 27 | -- * Creation of numeric vectors |
30 | constant, linspace | 28 | constant, linspace |
31 | ) where | 29 | ) where |
@@ -42,53 +40,64 @@ import Control.Monad(ap) | |||
42 | class Num e => Vectors a e where | 40 | class Num e => Vectors a e where |
43 | -- the C functions sumX are twice as fast as using foldVector | 41 | -- the C functions sumX are twice as fast as using foldVector |
44 | vectorSum :: a e -> e | 42 | vectorSum :: a e -> e |
45 | euclidean :: a e -> e | ||
46 | absSum :: a e -> e | 43 | absSum :: a e -> e |
47 | vectorMin :: a e -> e | 44 | vectorMin :: a e -> e |
48 | vectorMax :: a e -> e | 45 | vectorMax :: a e -> e |
49 | minIdx :: a e -> Int | 46 | minIdx :: a e -> Int |
50 | maxIdx :: a e -> Int | 47 | maxIdx :: a e -> Int |
51 | dot :: a e -> a e -> e | 48 | dot :: a e -> a e -> e |
49 | norm1 :: a e -> e | ||
50 | norm2 :: a e -> e | ||
51 | normInf :: a e -> e | ||
52 | |||
52 | 53 | ||
53 | instance Vectors Vector Float where | 54 | instance Vectors Vector Float where |
54 | vectorSum = sumF | 55 | vectorSum = sumF |
55 | euclidean = toScalarF Norm2 | 56 | norm2 = toScalarF Norm2 |
56 | absSum = toScalarF AbsSum | 57 | absSum = toScalarF AbsSum |
57 | vectorMin = toScalarF Min | 58 | vectorMin = toScalarF Min |
58 | vectorMax = toScalarF Max | 59 | vectorMax = toScalarF Max |
59 | minIdx = round . toScalarF MinIdx | 60 | minIdx = round . toScalarF MinIdx |
60 | maxIdx = round . toScalarF MaxIdx | 61 | maxIdx = round . toScalarF MaxIdx |
61 | dot = dotF | 62 | dot = dotF |
63 | norm1 = toScalarF AbsSum | ||
64 | normInf = vectorMax . vectorMapF Abs | ||
62 | 65 | ||
63 | instance Vectors Vector Double where | 66 | instance Vectors Vector Double where |
64 | vectorSum = sumR | 67 | vectorSum = sumR |
65 | euclidean = toScalarR Norm2 | 68 | norm2 = toScalarR Norm2 |
66 | absSum = toScalarR AbsSum | 69 | absSum = toScalarR AbsSum |
67 | vectorMin = toScalarR Min | 70 | vectorMin = toScalarR Min |
68 | vectorMax = toScalarR Max | 71 | vectorMax = toScalarR Max |
69 | minIdx = round . toScalarR MinIdx | 72 | minIdx = round . toScalarR MinIdx |
70 | maxIdx = round . toScalarR MaxIdx | 73 | maxIdx = round . toScalarR MaxIdx |
71 | dot = dotR | 74 | dot = dotR |
75 | norm1 = toScalarR AbsSum | ||
76 | normInf = vectorMax . vectorMapR Abs | ||
72 | 77 | ||
73 | instance Vectors Vector (Complex Float) where | 78 | instance Vectors Vector (Complex Float) where |
74 | vectorSum = sumQ | 79 | vectorSum = sumQ |
75 | euclidean = (:+ 0) . toScalarQ Norm2 | 80 | norm2 = (:+ 0) . toScalarQ Norm2 |
76 | absSum = (:+ 0) . toScalarQ AbsSum | 81 | absSum = (:+ 0) . toScalarQ AbsSum |
77 | vectorMin = ap (@>) minIdx | 82 | vectorMin = ap (@>) minIdx |
78 | vectorMax = ap (@>) maxIdx | 83 | vectorMax = ap (@>) maxIdx |
79 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 84 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
80 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 85 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
81 | dot = dotQ | 86 | dot = dotQ |
87 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs | ||
88 | normInf = (:+ 0) . vectorMax . fst . fromComplex . vectorMapQ Abs | ||
82 | 89 | ||
83 | instance Vectors Vector (Complex Double) where | 90 | instance Vectors Vector (Complex Double) where |
84 | vectorSum = sumC | 91 | vectorSum = sumC |
85 | euclidean = (:+ 0) . toScalarC Norm2 | 92 | norm2 = (:+ 0) . toScalarC Norm2 |
86 | absSum = (:+ 0) . toScalarC AbsSum | 93 | absSum = (:+ 0) . toScalarC AbsSum |
87 | vectorMin = ap (@>) minIdx | 94 | vectorMin = ap (@>) minIdx |
88 | vectorMax = ap (@>) maxIdx | 95 | vectorMax = ap (@>) maxIdx |
89 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 96 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
90 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 97 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
91 | dot = dotC | 98 | dot = dotC |
99 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs | ||
100 | normInf = (:+ 0) . vectorMax . fst . fromComplex . vectorMapC Abs | ||
92 | 101 | ||
93 | ---------------------------------------------------- | 102 | ---------------------------------------------------- |
94 | 103 | ||
@@ -268,39 +277,3 @@ kronecker a b = fromBlocks | |||
268 | $ flatten a `outer` flatten b | 277 | $ flatten a `outer` flatten b |
269 | 278 | ||
270 | -------------------------------------------------- | 279 | -------------------------------------------------- |
271 | |||
272 | -- | simple norms | ||
273 | class (Element t, RealFloat (RealOf t)) => Norm c t where | ||
274 | norm1 :: c t -> RealOf t | ||
275 | normInf :: c t -> RealOf t | ||
276 | normFrob :: c t -> RealOf t | ||
277 | |||
278 | instance Norm Vector Double where | ||
279 | normFrob = toScalarR Norm2 | ||
280 | norm1 = toScalarR AbsSum | ||
281 | normInf = vectorMax . vectorMapR Abs | ||
282 | |||
283 | instance Norm Vector Float where | ||
284 | normFrob = toScalarF Norm2 | ||
285 | norm1 = toScalarF AbsSum | ||
286 | normInf = vectorMax . vectorMapF Abs | ||
287 | |||
288 | instance (Norm Vector t, Vectors Vector t, RealElement t | ||
289 | , RealOf t ~ t, RealOf (Complex t) ~ t | ||
290 | ) => Norm Vector (Complex t) where | ||
291 | normFrob = normFrob . asReal | ||
292 | norm1 = norm1 . mapVector magnitude | ||
293 | normInf = vectorMax . mapVector magnitude | ||
294 | |||
295 | instance Norm Vector t => Norm Matrix t where | ||
296 | normFrob = normFrob . flatten | ||
297 | norm1 = maximum . map norm1 . toColumns | ||
298 | normInf = norm1 . trans | ||
299 | |||
300 | class Norm2 c t where | ||
301 | norm2 :: c t -> RealOf t | ||
302 | |||
303 | instance Norm Vector t => Norm2 Vector t where | ||
304 | norm2 = normFrob | ||
305 | |||
306 | -- (the instance Norm2 Matrix t requires singular values and is defined later) | ||