diff options
Diffstat (limited to 'lib/Numeric/Matrix.hs')
-rw-r--r-- | lib/Numeric/Matrix.hs | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/lib/Numeric/Matrix.hs b/lib/Numeric/Matrix.hs index f240384..73515c1 100644 --- a/lib/Numeric/Matrix.hs +++ b/lib/Numeric/Matrix.hs | |||
@@ -27,11 +27,13 @@ module Numeric.Matrix ( | |||
27 | import Data.Packed.Vector | 27 | import Data.Packed.Vector |
28 | import Data.Packed.Matrix | 28 | import Data.Packed.Matrix |
29 | import Numeric.Container | 29 | import Numeric.Container |
30 | import Numeric.LinearAlgebra.Linear | 30 | --import Numeric.LinearAlgebra.Linear |
31 | --import Numeric.Vector | 31 | import Numeric.Vector() |
32 | 32 | ||
33 | import Control.Monad(ap) | 33 | import Control.Monad(ap) |
34 | 34 | ||
35 | import Control.Arrow((***)) | ||
36 | |||
35 | ------------------------------------------------------------------- | 37 | ------------------------------------------------------------------- |
36 | 38 | ||
37 | instance Linear Matrix a => Eq (Matrix a) where | 39 | instance Linear Matrix a => Eq (Matrix a) where |
@@ -75,7 +77,34 @@ instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floati | |||
75 | 77 | ||
76 | --------------------------------------------------------------- | 78 | --------------------------------------------------------------- |
77 | 79 | ||
78 | instance (Linear Vector a, NumericContainer Matrix) => (Linear Matrix a) where | 80 | instance NumericContainer Matrix where |
81 | toComplex = uncurry $ liftMatrix2 $ curry toComplex | ||
82 | fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z | ||
83 | where c = cols z | ||
84 | complex' = liftMatrix complex' | ||
85 | conj = liftMatrix conj | ||
86 | -- cmap f = liftMatrix (cmap f) | ||
87 | single' = liftMatrix single' | ||
88 | double' = liftMatrix double' | ||
89 | |||
90 | --------------------------------------------------------------- | ||
91 | {- | ||
92 | instance (RealElement e, Complexable Vector e) => Complexable Matrix e where | ||
93 | v_toComplex = uncurry $ liftMatrix2 $ curry toComplex | ||
94 | v_fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z | ||
95 | where c = cols z | ||
96 | v_conj = liftMatrix conj | ||
97 | v_complex' = liftMatrix complex' | ||
98 | |||
99 | --------------------------------------------------------------- | ||
100 | |||
101 | instance (Precisionable Vector e) => Precisionable Matrix e where | ||
102 | v_single' = liftMatrix single' | ||
103 | v_double' = liftMatrix double' | ||
104 | -} | ||
105 | --------------------------------------------------------------- | ||
106 | |||
107 | instance (Linear Vector a, Container Matrix a) => Linear Matrix a where | ||
79 | scale x = liftMatrix (scale x) | 108 | scale x = liftMatrix (scale x) |
80 | scaleRecip x = liftMatrix (scaleRecip x) | 109 | scaleRecip x = liftMatrix (scaleRecip x) |
81 | addConstant x = liftMatrix (addConstant x) | 110 | addConstant x = liftMatrix (addConstant x) |
@@ -85,16 +114,19 @@ instance (Linear Vector a, NumericContainer Matrix) => (Linear Matrix a) where | |||
85 | divide = liftMatrix2 divide | 114 | divide = liftMatrix2 divide |
86 | equal a b = cols a == cols b && flatten a `equal` flatten b | 115 | equal a b = cols a == cols b && flatten a `equal` flatten b |
87 | scalar x = (1><1) [x] | 116 | scalar x = (1><1) [x] |
88 | 117 | -- | |
89 | 118 | instance (Container Vector a) => Container Matrix a where | |
90 | instance (Linear Vector a, NumericContainer Matrix) => (Container Matrix a) where | 119 | cmap f = liftMatrix (mapVector f) |
120 | atIndex = (@@>) | ||
91 | minIndex m = let (r,c) = (rows m,cols m) | 121 | minIndex m = let (r,c) = (rows m,cols m) |
92 | i = 1 + (minIndex $ flatten m) | 122 | i = (minIndex $ flatten m) |
93 | in (i `div` r,i `mod` r) | 123 | in (i `div` c,(i `mod` c) + 1) |
94 | maxIndex m = let (r,c) = (rows m,cols m) | 124 | maxIndex m = let (r,c) = (rows m,cols m) |
95 | i = 1 + (maxIndex $ flatten m) | 125 | i = (maxIndex $ flatten m) |
96 | in (i `div` r,i `mod` r) | 126 | in (i `div` c,(i `mod` c) + 1) |
97 | minElement = ap (@@>) minIndex | 127 | minElement = ap (@@>) minIndex |
98 | maxElement = ap (@@>) maxIndex | 128 | maxElement = ap (@@>) maxIndex |
129 | sumElements = sumElements . flatten | ||
130 | prodElements = prodElements . flatten | ||
99 | 131 | ||
100 | ---------------------------------------------------- | 132 | ---------------------------------------------------- |