summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/LinearAlgebra/Instances.hs25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs
index b4a769e..1992db0 100644
--- a/lib/Numeric/LinearAlgebra/Instances.hs
+++ b/lib/Numeric/LinearAlgebra/Instances.hs
@@ -24,10 +24,11 @@ import Numeric.GSL.Vector
24import Data.Packed.Matrix 24import Data.Packed.Matrix
25import Data.Complex 25import Data.Complex
26import Data.List(transpose,intersperse) 26import Data.List(transpose,intersperse)
27import Foreign(Storable)
28-- import Data.Monoid
29import Data.Packed.Internal.Vector 27import Data.Packed.Internal.Vector
30-- import Control.Parallel.Strategies 28
29#ifndef VECTOR
30import Foreign(Storable)
31#endif
31 32
32------------------------------------------------------------------ 33------------------------------------------------------------------
33 34
@@ -43,9 +44,13 @@ dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unw
43 pad n str = replicate (n - length str) ' ' ++ str 44 pad n str = replicate (n - length str) ' ' ++ str
44 unwords' = concat . intersperse ", " 45 unwords' = concat . intersperse ", "
45 46
47#ifndef VECTOR
48
46instance (Show a, Storable a) => (Show (Vector a)) where 49instance (Show a, Storable a) => (Show (Vector a)) where
47 show v = (show (dim v))++" |> " ++ show (toList v) 50 show v = (show (dim v))++" |> " ++ show (toList v)
48 51
52#endif
53
49------------------------------------------------------------------ 54------------------------------------------------------------------
50 55
51instance (Element a, Read a) => Read (Matrix a) where 56instance (Element a, Read a) => Read (Matrix a) where
@@ -55,12 +60,23 @@ instance (Element a, Read a) => Read (Matrix a) where
55 cs = read . init . fst. breakAt ')' . snd . breakAt '<' $ dims 60 cs = read . init . fst. breakAt ')' . snd . breakAt '<' $ dims
56 rs = read . snd . breakAt '(' .init . fst . breakAt '>' $ dims 61 rs = read . snd . breakAt '(' .init . fst . breakAt '>' $ dims
57 62
63#ifdef VECTOR
64
65instance (Element a, Read a) => Read (Vector a) where
66 readsPrec _ s = [(fromList . read $ listnums, rest)]
67 where (thing,trest) = breakAt ']' s
68 (dims,listnums) = breakAt ' ' (dropWhile (==' ') thing)
69 rest = drop 31 trest
70#else
71
58instance (Element a, Read a) => Read (Vector a) where 72instance (Element a, Read a) => Read (Vector a) where
59 readsPrec _ s = [((d |>) . read $ listnums, rest)] 73 readsPrec _ s = [((d |>) . read $ listnums, rest)]
60 where (thing,rest) = breakAt ']' s 74 where (thing,rest) = breakAt ']' s
61 (dims,listnums) = breakAt '>' thing 75 (dims,listnums) = breakAt '>' thing
62 d = read . init . fst . breakAt '|' $ dims 76 d = read . init . fst . breakAt '|' $ dims
63 77
78#endif
79
64breakAt c l = (a++[c],tail b) where 80breakAt c l = (a++[c],tail b) where
65 (a,b) = break (==c) l 81 (a,b) = break (==c) l
66 82
@@ -71,10 +87,13 @@ adaptScalar f1 f2 f3 x y
71 | dim y == 1 = f3 x (y@>0) 87 | dim y == 1 = f3 x (y@>0)
72 | otherwise = f2 x y 88 | otherwise = f2 x y
73 89
90#ifndef VECTOR
74 91
75instance Linear Vector a => Eq (Vector a) where 92instance Linear Vector a => Eq (Vector a) where
76 (==) = equal 93 (==) = equal
77 94
95#endif
96
78instance Num (Vector Double) where 97instance Num (Vector Double) where
79 (+) = adaptScalar addConstant add (flip addConstant) 98 (+) = adaptScalar addConstant add (flip addConstant)
80 negate = scale (-1) 99 negate = scale (-1)