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