diff options
Diffstat (limited to 'lib/LinearAlgebra/Instances.hs')
-rw-r--r-- | lib/LinearAlgebra/Instances.hs | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/lib/LinearAlgebra/Instances.hs b/lib/LinearAlgebra/Instances.hs new file mode 100644 index 0000000..3dbe5a7 --- /dev/null +++ b/lib/LinearAlgebra/Instances.hs | |||
@@ -0,0 +1,140 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : LinearAlgebra.Instances | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : portable | ||
11 | |||
12 | Numeric instances for Vector and Matrix. | ||
13 | |||
14 | In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. | ||
15 | |||
16 | -} | ||
17 | ----------------------------------------------------------------------------- | ||
18 | |||
19 | module LinearAlgebra.Instances( | ||
20 | ) where | ||
21 | |||
22 | import LinearAlgebra.Linear | ||
23 | import GSL.Vector | ||
24 | import Data.Packed.Matrix | ||
25 | import Data.Packed.Vector | ||
26 | import Complex | ||
27 | |||
28 | adaptScalar f1 f2 f3 x y | ||
29 | | dim x == 1 = f1 (x@>0) y | ||
30 | | dim y == 1 = f3 x (y@>0) | ||
31 | | otherwise = f2 x y | ||
32 | |||
33 | liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
34 | liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2)) | ||
35 | | otherwise = error "nonconformant matrices in liftMatrix2'" | ||
36 | |||
37 | compat' :: Matrix a -> Matrix b -> Bool | ||
38 | compat' m1 m2 = rows m1 == 1 && cols m1 == 1 | ||
39 | || rows m2 == 1 && cols m2 == 1 | ||
40 | || rows m1 == rows m2 && cols m1 == cols m2 | ||
41 | |||
42 | instance (Eq a, Field a) => Eq (Vector a) where | ||
43 | a == b = dim a == dim b && toList a == toList b | ||
44 | |||
45 | instance (Linear Vector a) => Num (Vector a) where | ||
46 | (+) = adaptScalar addConstant add (flip addConstant) | ||
47 | negate = scale (-1) | ||
48 | (*) = adaptScalar scale mul (flip scale) | ||
49 | signum = liftVector signum | ||
50 | abs = liftVector abs | ||
51 | fromInteger = fromList . return . fromInteger | ||
52 | |||
53 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
54 | a == b = cols a == cols b && flatten a == flatten b | ||
55 | |||
56 | instance (Linear Vector a) => Num (Matrix a) where | ||
57 | (+) = liftMatrix2' (+) | ||
58 | (-) = liftMatrix2' (-) | ||
59 | negate = liftMatrix negate | ||
60 | (*) = liftMatrix2' (*) | ||
61 | signum = liftMatrix signum | ||
62 | abs = liftMatrix abs | ||
63 | fromInteger = (1><1) . return . fromInteger | ||
64 | |||
65 | --------------------------------------------------- | ||
66 | |||
67 | instance (Linear Vector a) => Fractional (Vector a) where | ||
68 | fromRational n = fromList [fromRational n] | ||
69 | (/) = adaptScalar f divide g where | ||
70 | r `f` v = scaleRecip r v | ||
71 | v `g` r = scale (recip r) v | ||
72 | |||
73 | ------------------------------------------------------- | ||
74 | |||
75 | instance (Linear Vector a, Fractional (Vector a)) => Fractional (Matrix a) where | ||
76 | fromRational n = (1><1) [fromRational n] | ||
77 | (/) = liftMatrix2' (/) | ||
78 | |||
79 | --------------------------------------------------------- | ||
80 | |||
81 | instance Floating (Vector Double) where | ||
82 | sin = vectorMapR Sin | ||
83 | cos = vectorMapR Cos | ||
84 | tan = vectorMapR Tan | ||
85 | asin = vectorMapR ASin | ||
86 | acos = vectorMapR ACos | ||
87 | atan = vectorMapR ATan | ||
88 | sinh = vectorMapR Sinh | ||
89 | cosh = vectorMapR Cosh | ||
90 | tanh = vectorMapR Tanh | ||
91 | asinh = vectorMapR ASinh | ||
92 | acosh = vectorMapR ACosh | ||
93 | atanh = vectorMapR ATanh | ||
94 | exp = vectorMapR Exp | ||
95 | log = vectorMapR Log | ||
96 | sqrt = vectorMapR Sqrt | ||
97 | (**) = adaptScalar (vectorMapValR PowSV) (vectorZipR Pow) (flip (vectorMapValR PowVS)) | ||
98 | pi = fromList [pi] | ||
99 | |||
100 | ------------------------------------------------------------- | ||
101 | |||
102 | instance Floating (Vector (Complex Double)) where | ||
103 | sin = vectorMapC Sin | ||
104 | cos = vectorMapC Cos | ||
105 | tan = vectorMapC Tan | ||
106 | asin = vectorMapC ASin | ||
107 | acos = vectorMapC ACos | ||
108 | atan = vectorMapC ATan | ||
109 | sinh = vectorMapC Sinh | ||
110 | cosh = vectorMapC Cosh | ||
111 | tanh = vectorMapC Tanh | ||
112 | asinh = vectorMapC ASinh | ||
113 | acosh = vectorMapC ACosh | ||
114 | atanh = vectorMapC ATanh | ||
115 | exp = vectorMapC Exp | ||
116 | log = vectorMapC Log | ||
117 | sqrt = vectorMapC Sqrt | ||
118 | (**) = adaptScalar (vectorMapValC PowSV) (vectorZipC Pow) (flip (vectorMapValC PowVS)) | ||
119 | pi = fromList [pi] | ||
120 | |||
121 | ----------------------------------------------------------- | ||
122 | |||
123 | instance (Linear Vector a, Floating (Vector a)) => Floating (Matrix a) where | ||
124 | sin = liftMatrix sin | ||
125 | cos = liftMatrix cos | ||
126 | tan = liftMatrix tan | ||
127 | asin = liftMatrix asin | ||
128 | acos = liftMatrix acos | ||
129 | atan = liftMatrix atan | ||
130 | sinh = liftMatrix sinh | ||
131 | cosh = liftMatrix cosh | ||
132 | tanh = liftMatrix tanh | ||
133 | asinh = liftMatrix asinh | ||
134 | acosh = liftMatrix acosh | ||
135 | atanh = liftMatrix atanh | ||
136 | exp = liftMatrix exp | ||
137 | log = liftMatrix log | ||
138 | (**) = liftMatrix2' (**) | ||
139 | sqrt = liftMatrix sqrt | ||
140 | pi = (1><1) [pi] | ||