summaryrefslogtreecommitdiff
path: root/lalge/tests/tests.nim
blob: e798c396e5da83cf6780d69d224af54e55377e0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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