git.iko.soy/rowtype-yoga/purescript-linear-algebra
on master help sign in
Implemented getting elements
commit8bcea2c1d284bf75f5bb5e50472eaf6c3160e32c authorKrzysztof Langner <klangner@gmail.com> dateThu 13 Apr 2017, 12:00 (10 years ago) parentd4ff083
3 files changed+52−4
bower.json+2−1 src/LinearAlgebra/Matrix.purs+37−1 test/Test/LinearAlgebra/Matrix.purs+13−2
bower.json+2−1
@@ -15,7 +15,8 @@
1515 ],
1616 "dependencies": {
1717 "purescript-prelude": "^3.0.0",
18− "purescript-arrays": "^4.0.1"
18+ "purescript-arrays": "^4.0.1",
19+ "purescript-tuples": "^4.0.0"
1920 },
2021 "devDependencies": {
2122 "purescript-assert": "^3.0.0",
src/LinearAlgebra/Matrix.purs+37−1
@@ -1,13 +1,20 @@
11module LinearAlgebra.Matrix
22 ( Matrix
3+ -- * Constructors
34 , fromArray
45 , replicate
56 , zeros
7+ -- * Access data
8+ , column
9+ , element
10+ , row
611 ) where
712
813import Prelude
914import Data.Array as A
1015import Data.Maybe (Maybe(..))
16+import Data.Tuple (Tuple, fst, snd)
17+import LinearAlgebra.Vector (Vector)
1118
1219
1320-- | Dense Matrix implementation
@@ -32,4 +39,33 @@ zeros r c = replicate r c 0.0
3239-- | Create Matrix from Array
3340fromArray :: ∀ a. Int -> Int -> Array a -> Maybe (Matrix a)
3441fromArray r c vs | r > 0 && c > 0 && r*c == A.length vs = Just {nrows: r, ncols: c, values: vs}
35− | otherwise = Nothing
42+ | otherwise = Nothing
43+
44+
45+-- | Get specific column as a vector. Index is 0 based
46+-- | If the index is out of range then return empty vector
47+column :: ∀ a. Int -> Matrix a -> Vector a
48+column c mat = snd <<< A.unzip $ A.filter isInColumn ivalues
49+ where
50+ ivalues :: Array (Tuple Int a)
51+ ivalues = A.zip (A.range 0 (A.length mat.values - 1)) mat.values
52+
53+ isInColumn :: forall b. Tuple Int b -> Boolean
54+ isInColumn v = (fst v) `mod` mat.ncols == c
55+
56+
57+
58+-- | Get specific row as a vector. Index is 0 based
59+-- | If the index is out of range then return empty vector
60+row :: ∀ a. Int -> Matrix a -> Vector a
61+row r mat = A.slice i j mat.values
62+ where
63+ i = if r >=0 && r < mat.nrows then r*mat.ncols else 0
64+ j = if r >=0 && r < mat.nrows then i+mat.ncols else 0
65+
66+
67+-- | Get specific element. Index is 0 based
68+element :: ∀ a. Int -> Int -> Matrix a -> Maybe a
69+element r c mat = A.index mat.values ((r*mat.ncols) + c)
70+
71+
test/Test/LinearAlgebra/Matrix.purs+13−2
@@ -3,7 +3,7 @@ module Test.LinearAlgebra.Matrix (testMatrix) where
33import Prelude
44import Control.Monad.Eff (Eff)
55import Control.Monad.Eff.Console (log, CONSOLE)
6−import Data.Maybe (fromJust, isJust, isNothing)
6+import Data.Maybe (Maybe(..), fromJust, isJust, isNothing)
77import Test.Assert (assert, ASSERT)
88import Partial.Unsafe (unsafePartial)
99import LinearAlgebra.Matrix as M
@@ -22,4 +22,15 @@ testMatrix = do
2222
2323 log "From Array"
2424 assert $ isNothing (M.fromArray 2 3 [1.0])
25− assert $ isJust (M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
25+ assert $ isJust (M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
26+
27+ log "Get specific row"
28+ assert $ M.row 0 z2 == [1.0, 2.0, 3.0]
29+
30+ log "Get specific column"
31+ assert $ M.column 1 z2 == [2.0, 5.0]
32+
33+ log "Get specific value"
34+ assert $ M.element 1 2 z2 == Just 6.0
35+
36+