git.iko.soy/rowtype-yoga/purescript-linear-algebra
on master help sign in
Create Matrix from Array
commit9219fb98c9d5dcd686c7f070e5658199f8afd924 authorKrzysztof Langner <klangner@gmail.com> dateTue 11 Apr 2017, 15:44 (10 years ago) parentc3cd2a9
3 files changed+24−10
README.md+3−3 src/LinearAlgebra/Matrix.purs+12−5 test/Test/LinearAlgebra/Matrix.purs+9−2
README.md+3−3
@@ -1,6 +1,6 @@
11# Linear Algebra for PureScript
22
3−[![Build status](https://travis-ci.org/carldata/purescript-linear-algebra.svg?branch=master)](https://travis-ci.org/carldata/purescript-linear-algebra)
3+[![Build status](https://travis-ci.org/klangner/purescript-linear-algebra.svg?branch=master)](https://travis-ci.org/klangner/purescript-linear-algebra)
44<a href="https://pursuit.purescript.org/packages/purescript-linear-algebra">
55 <img src="https://pursuit.purescript.org/packages/purescript-linear-algebra/badge"
66 alt="purescript-linear-algebra on Pursuit">
@@ -20,7 +20,7 @@ bower install purescript-linear-algebra
2020## Features
2121
2222 * Matrix
23− * [ ] Matrix creation from array
23+ * [x] Matrix creation from Array
2424 * [ ] Matrix addition
2525 * [ ] Multiply by scalar
2626 * Vector
@@ -46,7 +46,7 @@ We are happy to receive bug reports, fixes, documentation enhancements,
4646and other improvements.
4747
4848Please report bugs via the
49−[github issue tracker](http://github.com/carldata/purescript-linear-algebra/issues).
49+[github issue tracker](http://github.com/klangner/purescript-linear-algebra/issues).
5050
5151
5252
src/LinearAlgebra/Matrix.purs+12−5
@@ -1,11 +1,13 @@
11module LinearAlgebra.Matrix
22 ( Matrix
3+ , fromArray
34 , replicate
45 , zeros
56 ) where
67
78import Prelude
89import Data.Array as A
10+import Data.Maybe (Maybe(..))
911
1012
1113-- | Dense Matrix implementation
@@ -17,12 +19,17 @@ type Matrix a =
1719
1820
1921-- | Create array of given dimmension containing replicated value
20−replicate :: ∀ a. Int -> Int -> a -> Matrix a
21−replicate r c v = {nrows: r, ncols: c, values: vs}
22− where
23− vs = A.replicate (r * c) v
22+replicate :: ∀ a. Int -> Int -> a -> Maybe (Matrix a )
23+replicate r c v | r > 0 && c > 0 = Just {nrows: r, ncols: c, values: A.replicate (r * c) v}
24+ | otherwise = Nothing
2425
2526
2627-- | Create array of given dimmension with all values set to 0
27−zeros :: Int -> Int -> Matrix Number
28+zeros :: Int -> Int -> Maybe (Matrix Number)
2829zeros r c = replicate r c 0.0
30+
31+
32+-- | Create Matrix from Array
33+fromArray :: ∀ a. Int -> Int -> Array a -> Maybe (Matrix a)
34+fromArray r c vs | r > 0 && c > 0 && r*c == A.length vs = Just {nrows: r, ncols: c, values: vs}
35+ | otherwise = Nothing
test/Test/LinearAlgebra/Matrix.purs+9−2
@@ -3,7 +3,9 @@ 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)
67import Test.Assert (assert, ASSERT)
8+import Partial.Unsafe (unsafePartial)
79import LinearAlgebra.Matrix as M
810
911
@@ -11,8 +13,13 @@ testMatrix :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
1113testMatrix = do
1214
1315 log "Init Matrix test case data"
14− let z1 = M.zeros 5 4
16+ let z1 = unsafePartial $ fromJust $ M.zeros 5 4
17+ let z2 = unsafePartial $ fromJust $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
1518
16− log "Create zero matrix"
19+ log "Create matrix"
1720 assert $ z1.nrows == 5
1821 assert $ z1.ncols == 4
22+
23+ log "From Array"
24+ 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])