diff options
author | Alberto Ruiz <aruiz@um.es> | 2012-03-14 12:27:09 +0100 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2012-03-14 12:27:09 +0100 |
commit | 69b0f9c46f09dc9fc5c25e706efb8a7da51d2480 (patch) | |
tree | 84bfa82d634605e7ce6f2c297257301ebb51d474 /examples | |
parent | 819b1d83ec3eabc62974b19cb09f2f30fc20a6a9 (diff) |
new module Numeric.LinearAlgebra.Real
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Real.hs | 113 |
1 files changed, 0 insertions, 113 deletions
diff --git a/examples/Real.hs b/examples/Real.hs deleted file mode 100644 index 9083b87..0000000 --- a/examples/Real.hs +++ /dev/null | |||
@@ -1,113 +0,0 @@ | |||
1 | |||
2 | -- Alternative interface and utilities for creation of real arrays, useful to work in interactive mode. | ||
3 | |||
4 | module Real( | ||
5 | module Numeric.LinearAlgebra, | ||
6 | (<>), (*>), (<*), (<\>), (\>), | ||
7 | vector, | ||
8 | eye, | ||
9 | zeros, ones, | ||
10 | diagl, | ||
11 | row, | ||
12 | col, | ||
13 | (#),(&), (//), blocks, | ||
14 | rand, randn | ||
15 | ) where | ||
16 | |||
17 | import Numeric.LinearAlgebra hiding ((<>), (<\>)) | ||
18 | import System.Random(randomIO) | ||
19 | |||
20 | infixl 7 <> | ||
21 | -- | Matrix product ('multiply') | ||
22 | (<>) :: Field t => Matrix t -> Matrix t -> Matrix t | ||
23 | (<>) = multiply | ||
24 | |||
25 | infixl 7 *> | ||
26 | -- | matrix x vector | ||
27 | (*>) :: Field t => Matrix t -> Vector t -> Vector t | ||
28 | m *> v = flatten $ m <> (asColumn v) | ||
29 | |||
30 | infixl 7 <* | ||
31 | -- | vector x matrix | ||
32 | (<*) :: Field t => Vector t -> Matrix t -> Vector t | ||
33 | v <* m = flatten $ (asRow v) <> m | ||
34 | |||
35 | |||
36 | -- | Least squares solution of a linear system for several right-hand sides, similar to the \\ operator of Matlab\/Octave. (\<\\\>) = 'linearSolveSVD'. | ||
37 | (<\>) :: (Field a) => Matrix a -> Matrix a -> Matrix a | ||
38 | infixl 7 <\> | ||
39 | (<\>) = linearSolveSVD | ||
40 | |||
41 | -- | Least squares solution of a linear system for a single right-hand side. See '(\<\\\>)'. | ||
42 | (\>) :: (Field a) => Matrix a -> Vector a -> Vector a | ||
43 | infixl 7 \> | ||
44 | m \> v = flatten (m <\> reshape 1 v) | ||
45 | |||
46 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
47 | randm :: RandDist | ||
48 | -> Int -- ^ rows | ||
49 | -> Int -- ^ columns | ||
50 | -> IO (Matrix Double) | ||
51 | randm d r c = do | ||
52 | seed <- randomIO | ||
53 | return (reshape c $ randomVector seed d (r*c)) | ||
54 | |||
55 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
56 | rand :: Int -> Int -> IO (Matrix Double) | ||
57 | rand = randm Uniform | ||
58 | |||
59 | -- | Pseudorandom matrix with normal elements | ||
60 | randn :: Int -> Int -> IO (Matrix Double) | ||
61 | randn = randm Gaussian | ||
62 | |||
63 | -- | Real identity matrix. | ||
64 | eye :: Int -> Matrix Double | ||
65 | eye = ident | ||
66 | |||
67 | -- | Create a real vector from a list. | ||
68 | vector :: [Double] -> Vector Double | ||
69 | vector = fromList | ||
70 | |||
71 | -- | Create a real diagonal matrix from a list. | ||
72 | diagl :: [Double] -> Matrix Double | ||
73 | diagl = diag . vector | ||
74 | |||
75 | -- | Create a matrix or zeros. | ||
76 | zeros :: Int -- ^ rows | ||
77 | -> Int -- ^ columns | ||
78 | -> Matrix Double | ||
79 | zeros r c = konst 0 (r,c) | ||
80 | |||
81 | -- | Create a matrix or ones. | ||
82 | ones :: Int -- ^ rows | ||
83 | -> Int -- ^ columns | ||
84 | -> Matrix Double | ||
85 | ones r c = konst 1 (r,c) | ||
86 | |||
87 | -- | Concatenation of real vectors. | ||
88 | infixl 9 # | ||
89 | (#) :: Vector Double -> Vector Double -> Vector Double | ||
90 | a # b = join [a,b] | ||
91 | |||
92 | -- | Horizontal concatenation of real matrices. | ||
93 | infixl 8 & | ||
94 | (&) :: Matrix Double -> Matrix Double -> Matrix Double | ||
95 | a & b = fromBlocks [[a,b]] | ||
96 | |||
97 | -- | Vertical concatenation of real matrices. | ||
98 | infixl 7 // | ||
99 | (//) :: Matrix Double -> Matrix Double -> Matrix Double | ||
100 | a // b = fromBlocks [[a],[b]] | ||
101 | |||
102 | -- | Real block matrix from a rectangular list of lists. | ||
103 | blocks :: [[Matrix Double]] -> Matrix Double | ||
104 | blocks = fromBlocks | ||
105 | |||
106 | -- | A real matrix with a single row, create from a list of elements. | ||
107 | row :: [Double] -> Matrix Double | ||
108 | row = asRow . vector | ||
109 | |||
110 | -- | A real matrix with a single column, created from a list of elements. | ||
111 | col :: [Double] -> Matrix Double | ||
112 | col = asColumn . vector | ||
113 | |||