git.iko.soy/rowtype-yoga/purescript-linear-algebra
on master help sign in
Matrix creation. First tests and travis build
c3cd2a9Krzysztof Langner
.travis.ymlnew file+12
@@ -0,0 +1,12 @@
1+language: node_js
2+sudo: false
3+node_js: stable
4+install:
5+ - npm install
6+ - npm install purescript pulp bower -g
7+ - bower install
8+script:
9+ - pulp build && pulp test
10+notifications:
11+ email: false
12+
bower.json+3−1
@@ -15,9 +15,11 @@
15 ],
15 ],
16 "dependencies": {
16 "dependencies": {
17 "purescript-prelude": "^3.0.0",
17 "purescript-prelude": "^3.0.0",
18− "purescript-console": "^3.0.0"
18+ "purescript-arrays": "^4.0.1"
19 },
19 },
20 "devDependencies": {
20 "devDependencies": {
21+ "purescript-assert": "^3.0.0",
22+ "purescript-console": "^3.0.0",
21 "purescript-psci-support": "^3.0.0"
23 "purescript-psci-support": "^3.0.0"
22 }
24 }
23}
25}
src/LinearAlgebra/Matrix.purs+17−5
@@ -1,16 +1,28 @@
1module LinearAlgebra.Matrix
1module LinearAlgebra.Matrix
2 ( Matrix
2 ( Matrix
3+ , replicate
4+ , zeros
3 ) where
5 ) where
4−
6+
7+import Prelude
8+import Data.Array as A
9+
5
10
6-- | Dense Matrix implementation
11-- | Dense Matrix implementation
7type Matrix a =
12type Matrix a =
8 { nrows :: Int
13 { nrows :: Int
9 , ncols :: Int
14 , ncols :: Int
10− , data :: Array a
15+ , values :: Array a
11 }
16 }
12
17
13
18
14−-- -- | Create Matrix from array of values
19+-- | Create array of given dimmension containing replicated value
15−-- fromArray :: ∀ a. Array a -> Int -> Int -> Maybe (Matrix a)
20+replicate :: ∀ a. Int -> Int -> a -> Matrix a
16−-- fromArray ds rows cols | length ds
21+replicate r c v = {nrows: r, ncols: c, values: vs}
22+ where
23+ vs = A.replicate (r * c) v
24+
25+
26+-- | Create array of given dimmension with all values set to 0
27+zeros :: Int -> Int -> Matrix Number
28+zeros r c = replicate r c 0.0
test/Main.purs+9−3
@@ -2,8 +2,14 @@ module Test.Main where
2
2
3import Prelude
3import Prelude
4import Control.Monad.Eff (Eff)
4import Control.Monad.Eff (Eff)
5−import Control.Monad.Eff.Console (CONSOLE, log)
5+import Control.Monad.Eff.Console (CONSOLE)
6
6
7+import Test.Assert (ASSERT)
8+
9+import Test.LinearAlgebra.Matrix (testMatrix)
10+
11+
7−main :: forall e. Eff (console :: CONSOLE | e) Unit
12+main :: ∀ eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
8main = do
13main = do
9− log "You should add some tests."
14+ testMatrix
15+
test/Test/LinearAlgebra/Matrix.pursnew file+18
@@ -0,0 +1,18 @@
1+module Test.LinearAlgebra.Matrix (testMatrix) where
2+
3+import Prelude
4+import Control.Monad.Eff (Eff)
5+import Control.Monad.Eff.Console (log, CONSOLE)
6+import Test.Assert (assert, ASSERT)
7+import LinearAlgebra.Matrix as M
8+
9+
10+testMatrix :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
11+testMatrix = do
12+
13+ log "Init Matrix test case data"
14+ let z1 = M.zeros 5 4
15+
16+ log "Create zero matrix"
17+ assert $ z1.nrows == 5
18+ assert $ z1.ncols == 4