diff options
Diffstat (limited to 'lalge/tests')
-rw-r--r-- | lalge/tests/nim.cfg | 1 | ||||
-rw-r--r-- | lalge/tests/tests.nim | 144 |
2 files changed, 145 insertions, 0 deletions
diff --git a/lalge/tests/nim.cfg b/lalge/tests/nim.cfg new file mode 100644 index 0000000..85bf6c4 --- /dev/null +++ b/lalge/tests/nim.cfg @@ -0,0 +1 @@ +--path:"../src/" diff --git a/lalge/tests/tests.nim b/lalge/tests/tests.nim new file mode 100644 index 0000000..e798c39 --- /dev/null +++ b/lalge/tests/tests.nim @@ -0,0 +1,144 @@ +import std/math +import types, vectors, matrices + +let A: Matrix = @[ + @[10.0, 2.0, 3.0, 5.0, 6.0], + @[8.0, 7.0, 6.0, 4.0, 3.0], + @[4.0, 6.0, 0.0, 9.0, 0.0], + @[6.0, 7.0, 9.0, 3.0, 9.0], + @[3.0, 0.0, 7.0, 9.0, 9.0], +] +let B: Matrix = @[ + @[1.0, 2.0, 3.0, 4.0, 5.0], + @[5.0, 3.0, 1.0, 22.0, 3.0], + @[5.0, 21.0, 4.0, 6.0, 3.0], + @[12.0, 1.0, 5.0, 0.0, 9.0], + @[6.0, 7.0, 1.0, 3.0, 5.0], +] + +let L: Matrix = @[ + @[1.0, 0.0, 0.0, 0.0, 0.0, 0.0], + @[-1.0, 1.0, 0.0, 0.0, 0.0, 0.0], + @[0.0, -1.0, 1.0, 0.0, 0.0, 0.0], + @[0.0, 0.0, -1.0, 1.0, 0.0, 0.0], + @[0.0, 0.0, 0.0, -1.0, 1.0, 0.0], + @[0.0, 0.0, 0.0, 0.0, -1.0, 1.0], +] +let U: Matrix = @[ + @[1.0, -1.0, 0.0, 0.0, 0.0, 0.0], + @[0.0, 1.0, -1.0, 0.0, 0.0, 0.0], + @[0.0, 0.0, 1.0, -1.0, 0.0, 0.0], + @[0.0, 0.0, 0.0, 1.0, -1.0, 0.0], + @[0.0, 0.0, 0.0, 0.0, 1.0, -1.0], + @[0.0, 0.0, 0.0, 0.0, 0.0, 1.0], +] + +let + P13: Matrix = @[ + @[0.0, 0.0, 1.0], + @[0.0, 1.0, 0.0], + @[1.0, 0.0, 0.0], + ] + P23: Matrix = @[ + @[1.0, 0.0, 0.0], + @[0.0, 0.0, 1.0], + @[0.0, 1.0, 0.0], + ] + +let foo = @[ + @[1.0, 1.0, 1.0], + @[3.0, 4.0, 5.0], + @[40.0, 51.0, 12.0] +] + +let eliminationMatrix: Matrix = @[ + @[1.0, 0.0, 0.0], + @[-2.0, 1.0, 0.0], + @[0.0, 0.0, 1.0] +] + +let + ones: Matrix = @[ + @[1.0, 2.0, 3.0], + @[4.0, 5.0, 6.0] + ] + twos: Matrix = @[ + @[7.0, 8.0], + @[9.0, 10.0], + @[11.0, 12.0] + ] + +# Addition and subtraction +assert I2 + I2 == 2 * I2 +assert I3-I3 == gen(3, 3, 0.0) + +# Num. of rows / columns +assert rows(I2) == 2 +assert I2.rows() == 2 +assert I2.rows == 2 +assert cols(I3) == 3 +assert I3.cols() == 3 +assert I3.cols == 3 + +# Identity Matrices +assert I1 == @[ + @[1.0], +] +assert I2 == @[ + @[1.0, 0.0], + @[0.0, 1.0], +] +assert I3 == @[ + @[1.0, 0.0, 0.0], + @[0.0, 1.0, 0.0], + @[0.0, 0.0, 1.0], +] +assert I4 == @[ + @[1.0, 0.0, 0.0, 0.0], + @[0.0, 1.0, 0.0, 0.0], + @[0.0, 0.0, 1.0, 0.0], + @[0.0, 0.0, 0.0, 1.0], +] +assert I5 == @[ + @[1.0, 0.0, 0.0, 0.0, 0.0], + @[0.0, 1.0, 0.0, 0.0, 0.0], + @[0.0, 0.0, 1.0, 0.0, 0.0], + @[0.0, 0.0, 0.0, 1.0, 0.0], + @[0.0, 0.0, 0.0, 0.0, 1.0], +] + +assert det(@[ + @[2.0, -1.0, 0.0, -1.0], + @[-1.0, 2.0, -1.0, 0.0], + @[0.0, -1.0, 2.0, -1.0], + @[-1.0, 0.0, -1.0, 2.0], +]) == 0 + +assert det(A * B) == (det(A) * det(B)) +assert det(I3) == 1 +assert det(2*I4) == 16 + +# Vectors + +let + alice: Vector = @[1.0, 1.0, 1.0] + bob = @[3.0, 4.0, 5.0] + carol = @[40.0, 51.0, 12.0] + dan: Vector = @[10.0, 20.0, 30.0, 40.0] + eve = @[2.0, 3.0, 4.0] + +assert alice + bob == bob + alice +assert alice + carol == carol + alice +assert carol - eve != eve - carol +assert alice.length() == sqrt(3.0) + +# echo transpose(ones) +# echo P13 * 8 +# echo P13 + P23 +# echo ones * twos +# assert col(eliminationMatrix, 0) == col(eliminationMatrix, 0) +# echo identity(0) +# echo identity(2) +# echo (1/0) +# echo L*U +# echo U*L |