summaryrefslogtreecommitdiff
path: root/lib/Numeric/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/Matrix.hs')
-rw-r--r--lib/Numeric/Matrix.hs27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Numeric/Matrix.hs b/lib/Numeric/Matrix.hs
index 8397911..e285ff2 100644
--- a/lib/Numeric/Matrix.hs
+++ b/lib/Numeric/Matrix.hs
@@ -28,6 +28,8 @@ module Numeric.Matrix (
28------------------------------------------------------------------- 28-------------------------------------------------------------------
29 29
30import Numeric.Container 30import Numeric.Container
31import qualified Data.Monoid as M
32import Data.List(partition)
31 33
32------------------------------------------------------------------- 34-------------------------------------------------------------------
33 35
@@ -69,3 +71,28 @@ instance (Floating a, Container Vector a, Floating (Vector a), Fractional (Matri
69 (**) = liftMatrix2Auto (**) 71 (**) = liftMatrix2Auto (**)
70 sqrt = liftMatrix sqrt 72 sqrt = liftMatrix sqrt
71 pi = (1><1) [pi] 73 pi = (1><1) [pi]
74
75--------------------------------------------------------------------------------
76
77isScalar m = rows m == 1 && cols m == 1
78
79adaptScalarM f1 f2 f3 x y
80 | isScalar x = f1 (x @@>(0,0) ) y
81 | isScalar y = f3 x (y @@>(0,0) )
82 | otherwise = f2 x y
83
84instance (Container Vector t, Eq t, Num (Vector t), Product t) => M.Monoid (Matrix t)
85 where
86 mempty = 1
87 mappend = adaptScalarM scale mXm (flip scale)
88
89 mconcat xs = work (partition isScalar xs)
90 where
91 work (ss,[]) = product ss
92 work (ss,ms) = scale' (product ss) (optimiseMult ms)
93 scale' x m
94 | isScalar x && x00 == 1 = m
95 | otherwise = scale x00 m
96 where
97 x00 = x @@> (0,0)
98