diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 8922e51..9a7e65f 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -16,11 +16,15 @@ Basic optimized operations on vectors and matrices. | |||
16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
17 | 17 | ||
18 | module Numeric.LinearAlgebra.Linear ( | 18 | module Numeric.LinearAlgebra.Linear ( |
19 | -- * Linear Algebra Typeclasses | ||
19 | Vectors(..), | 20 | Vectors(..), |
20 | Linear(..) | 21 | Linear(..), |
22 | -- * Creation of numeric vectors | ||
23 | constant, linspace | ||
21 | ) where | 24 | ) where |
22 | 25 | ||
23 | import Data.Packed.Internal.Vector | 26 | import Data.Packed.Internal.Vector |
27 | import Data.Packed.Internal.Matrix | ||
24 | import Data.Packed.Matrix | 28 | import Data.Packed.Matrix |
25 | import Data.Complex | 29 | import Data.Complex |
26 | import Numeric.GSL.Vector | 30 | import Numeric.GSL.Vector |
@@ -29,6 +33,7 @@ import Control.Monad(ap) | |||
29 | 33 | ||
30 | -- | basic Vector functions | 34 | -- | basic Vector functions |
31 | class Num e => Vectors a e where | 35 | class Num e => Vectors a e where |
36 | -- the C functions sumX are twice as fast as using foldVector | ||
32 | vectorSum :: a e -> e | 37 | vectorSum :: a e -> e |
33 | euclidean :: a e -> e | 38 | euclidean :: a e -> e |
34 | absSum :: a e -> e | 39 | absSum :: a e -> e |
@@ -153,3 +158,29 @@ instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where | |||
153 | divide = liftMatrix2 divide | 158 | divide = liftMatrix2 divide |
154 | equal a b = cols a == cols b && flatten a `equal` flatten b | 159 | equal a b = cols a == cols b && flatten a `equal` flatten b |
155 | scalar x = (1><1) [x] | 160 | scalar x = (1><1) [x] |
161 | |||
162 | |||
163 | ---------------------------------------------------- | ||
164 | |||
165 | {- | creates a vector with a given number of equal components: | ||
166 | |||
167 | @> constant 2 7 | ||
168 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | ||
169 | -} | ||
170 | constant :: Element a => a -> Int -> Vector a | ||
171 | -- constant x n = runSTVector (newVector x n) | ||
172 | constant = constantD -- about 2x faster | ||
173 | |||
174 | {- | Creates a real vector containing a range of values: | ||
175 | |||
176 | @\> linspace 5 (-3,7) | ||
177 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | ||
178 | |||
179 | Logarithmic spacing can be defined as follows: | ||
180 | |||
181 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
182 | -} | ||
183 | linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e | ||
184 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | ||
185 | where s = (b-a)/fromIntegral (n-1) | ||
186 | |||