@@ -1,25 +1,136 @@
11module Test.LinearAlgebra.Matrix (testMatrix) where
22
33import Prelude
4−import Control.Monad.Eff (Eff)
5−import Control.Monad.Eff.Console (log, CONSOLE)
6−import Data.Maybe (fromJust, isJust, isNothing)
7−import Test.Assert (assert, ASSERT)
8−import Partial.Unsafe (unsafePartial)
4+import Effect (Effect)
5+import Effect.Class.Console (log)
6+import Data.Maybe (Maybe(..), fromMaybe, isJust, isNothing)
7+import Test.Assert (assert)
8+
99import LinearAlgebra.Matrix as M
1010
1111
12−testMatrix :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
12+testMatrix :: Effect Unit
1313testMatrix = do
14+ log "\n# Test Matrix"
15+ testCreate
16+ testFromArray
17+ testGetRow
18+ testRows
19+ testGetValue
20+ testGetCol
21+ testColumns
22+ testSliceColumns
23+ testSliceRows
24+ testInsertColumn
25+ testMultiply
26+ testAdd
27+ testTranspose
28+ testIdentity
29+
1430
15− log "Init Matrix test case data"
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]
31+testCreate :: Effect Unit
32+testCreate = do
33+ log " * Create matrix"
34+ let z1 = M.zeros 5 4
35+ assert $ M.nrows z1 == 5
36+ assert $ M.ncols z1 == 4
1837
19− log "Create matrix"
20− assert $ z1.nrows == 5
21− assert $ z1.ncols == 4
2238
23− log "From Array"
39+testFromArray :: Effect Unit
40+testFromArray = do
41+ log " * From Array"
2442 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])
43+ assert $ isJust (M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
44+
45+
46+testGetRow :: Effect Unit
47+testGetRow = do
48+ log " * Get specific row"
49+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
50+ assert $ M.row 0 mat == [1.0, 2.0, 3.0]
51+
52+
53+testGetCol :: Effect Unit
54+testGetCol = do
55+ log " * Get specific column"
56+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
57+ assert $ M.column 1 mat == [2.0, 5.0]
58+
59+
60+testGetValue :: Effect Unit
61+testGetValue = do
62+ log " * Get specific value"
63+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
64+ assert $ M.element 1 2 mat == Just 6.0
65+
66+
67+testRows :: Effect Unit
68+testRows = do
69+ log " * Get list of rows"
70+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
71+ assert $ M.rows mat == [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
72+
73+
74+testColumns :: Effect Unit
75+testColumns = do
76+ log " * Get list of columns"
77+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
78+ assert $ M.columns mat == [[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
79+
80+
81+testSliceColumns :: Effect Unit
82+testSliceColumns = do
83+ log " * Slice columns"
84+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
85+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 2 2 [1.0, 2.0, 4.0, 5.0]
86+ assert $ M.sliceCols 0 1 mat == expected
87+
88+
89+testSliceRows :: Effect Unit
90+testSliceRows = do
91+ log " * Slice rows"
92+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 3 2 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
93+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 2 2 [1.0, 2.0, 3.0, 4.0]
94+ assert $ M.sliceRows 0 1 mat == expected
95+
96+
97+testInsertColumn :: Effect Unit
98+testInsertColumn = do
99+ log " * Insert column"
100+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 2 [1.0, 2.0, 4.0, 5.0]
101+ let expected1 = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
102+ let expected2 = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [3.0, 1.0, 2.0, 6.0, 4.0, 5.0]
103+ assert $ M.insertCol 2 [3.0, 6.0] mat == expected1
104+ assert $ M.insertCol 0 [3.0, 6.0] mat == expected2
105+
106+
107+testAdd :: Effect Unit
108+testAdd = do
109+ log " * Multiply matrices"
110+ let m1 = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
111+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [2.0, 4.0, 6.0, 8.0, 10.0, 12.0]
112+ assert $ M.add m1 m1 == expected
113+
114+
115+testMultiply :: Effect Unit
116+testMultiply = do
117+ log " * Multiply matrices"
118+ let m1 = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
119+ let m2 = fromMaybe (M.zeros 1 1) $ M.fromArray 3 2 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
120+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 2 2 [22.0, 28.0, 49.0, 64.0]
121+ assert $ M.multiply m1 m2 == expected
122+
123+
124+testTranspose :: Effect Unit
125+testTranspose = do
126+ log " * Transpose matrix"
127+ let mat = fromMaybe (M.zeros 1 1) $ M.fromArray 2 3 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
128+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 3 2 [1.0, 4.0, 2.0, 5.0, 3.0, 6.0]
129+ assert $ M.transpose mat == expected
130+
131+
132+testIdentity :: Effect Unit
133+testIdentity = do
134+ log " * Identity matrix"
135+ let expected = fromMaybe (M.zeros 1 1) $ M.fromArray 3 3 [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
136+ assert $ M.identity 3 == expected