annotate old_tests/test_matrix.nim @ 1319:43db6f5abb61

add: more vector utils
author sam <sam@basx.dev>
date Tue, 13 Aug 2024 15:28:56 +0700
parents 6360c8d17ce0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
1 import random
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
2 import math
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
3
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
4 import semicongine
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
5
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
6
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 32
diff changeset
7 proc echoInfo(v: TVec) =
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
8 echo v
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
9 echo " Length: ", v.length
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
10 echo " Normlized: ", v.normalized
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
11 echo " negated: ", -v
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
12
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
13 proc echoAdd[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
14 echo v1, " + ", v2, " = ", v1 + v2
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
15 proc echoSub[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
16 echo v1, " - ", v2, " = ", v1 - v2
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
17 proc echoMul[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
18 echo v1, " * ", v2, " = ", v1 * v2
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
19 proc echoDiv[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
20 echo v1, " / ", v2, " = ", v1 / v2
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
21 proc echoDot[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
22 echo v1, " o ", v2, " = ", v1.dot(v2)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
23 proc echoCross[T, U](v1: T, v2: U) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
24 echo v1, " x ", v2, " = ", v1.cross(v2)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
25
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
26 proc randVec2I(): auto = NewVec2(rand(1 .. 10), rand(1 .. 10))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
27 proc randVec2F(): auto = NewVec2(rand(10'f) + 0.01, rand(10'f) + 0.01)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
28 proc randVec3I(): auto = NewVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
29 proc randVec3F(): auto = NewVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
30 proc randVec4I(): auto = NewVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
31 proc randVec4F(): auto = NewVec4(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
32
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
33
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
34 template withAllIntegerMats(stuff: untyped) =
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
35 stuff(TMat2[int32])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
36 stuff(TMat23[int32])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
37 stuff(TMat32[int32])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
38 stuff(TMat3[int32])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
39 stuff(TMat34[int32])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
40 stuff(TMat43[int32])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
41 stuff(TMat4[int32])
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
42 stuff(TMat2[int64])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
43 stuff(TMat23[int64])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
44 stuff(TMat32[int64])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
45 stuff(TMat3[int64])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
46 stuff(TMat34[int64])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
47 stuff(TMat43[int64])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
48 stuff(TMat4[int64])
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
49
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
50 template withAllFloatMats(stuff: untyped) =
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
51 stuff(TMat2[float32])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
52 stuff(TMat23[float32])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
53 stuff(TMat32[float32])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
54 stuff(TMat3[float32])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
55 stuff(TMat34[float32])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
56 stuff(TMat43[float32])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
57 stuff(TMat4[float32])
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
58 stuff(TMat2[float64])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
59 stuff(TMat23[float64])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
60 stuff(TMat32[float64])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
61 stuff(TMat3[float64])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
62 stuff(TMat34[float64])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
63 stuff(TMat43[float64])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
64 stuff(TMat4[float64])
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
65
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
66 template withAllMats(stuff: untyped) =
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
67 stuff(TMat2[int])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
68 stuff(TMat23[int])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
69 stuff(TMat32[int])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
70 stuff(TMat3[int])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
71 stuff(TMat34[int])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
72 stuff(TMat43[int])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
73 stuff(TMat4[int])
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
74 stuff(TMat2[float])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
75 stuff(TMat23[float])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
76 stuff(TMat32[float])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
77 stuff(TMat3[float])
58
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
78 stuff(TMat34[float])
8287a91e5d56 did: refactor Matrix names
Sam <sam@basx.dev>
parents: 57
diff changeset
79 stuff(TMat43[float])
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
80 stuff(TMat4[float])
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
81
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
82 template testTranspose(t: typedesc) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
83 echo "testTranspose: ", t
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
84 let m = t().Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
85 assert m == m.Transposed().Transposed()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
86
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
87 template testInversed(t: typedesc) =
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
88 echo "testTranspose: ", t
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
89 let m = t().Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
90 var unit = t()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
91 for i in unit.RowCount:
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
92 unit[i][i] = 1
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
93 assert m.Transposed() * m == unit
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
94
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
95 template testAssignI(t: typedesc) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
96 echo "testAssignI: ", t
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
97 var m = t()
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
98 for i in 0 ..< t.data.len:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
99 m[rand(0 ..< t.RowCount), rand(0 ..< t.ColumnCount)] = rand(0'i32 .. 100'i32)
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
100
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
101 template testAssignF(t: typedesc) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
102 echo "testAssignF: ", t
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
103 var m = t()
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
104 for i in 0 ..< t.data.len:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
105 m[rand(0 ..< t.RowCount), rand(0 ..< t.ColumnCount)] = rand(100'f)
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
106
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
107 template testRowCols(t: typedesc) =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
108 echo "testRowCols: ", t
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
109 var m = t().Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
110 for i in 0 ..< t.RowCount:
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
111 echo m.Row(i)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
112 for i in 0 ..< t.ColumnCount:
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
113 echo m.Col(i)
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
114
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
115
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
116 proc testMatrix() =
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
117 withAllMats(testTranspose)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
118 withAllIntegerMats(testAssignI)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
119 withAllFloatMats(testAssignF)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
120 withAllMats(testRowCols)
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
121
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
122 echo Unit2
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
123 echo Unit2i
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
124 echo Unit2i8
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
125 echo Unit2i16
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
126 echo Unit2i32
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
127 echo Unit2i64
16
617c6dcddbe2 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 13
diff changeset
128
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
129 echo Unit3
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
130 echo Unit3i
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
131 echo Unit3i8
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
132 echo Unit3i16
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
133 echo Unit3i32
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
134 echo Unit3i64
16
617c6dcddbe2 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 13
diff changeset
135
212
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
136 echo Unit4
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
137 echo Unit4i
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
138 echo Unit4i8
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
139 echo Unit4i16
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
140 echo Unit4i32
77755701bf49 fix: tests
Sam <sam@basx.dev>
parents: 58
diff changeset
141 echo Unit4i64
16
617c6dcddbe2 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 13
diff changeset
142
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
143 echo TMat2[float32]().Randomized() * One2.Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
144 echo TMat3[float32]().Randomized() * One3.Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
145 echo TMat4[float32]().Randomized() * One4.Randomized()
16
617c6dcddbe2 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 13
diff changeset
146
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
147 echo float32(rand(1'f32)) * TMat2[float32]().Randomized()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
148 echo TMat2[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
149 echo TMat2[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
150 echo TMat23[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
151 echo TMat23[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
152 echo TMat3[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
153 echo TMat34[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
154 echo TMat43[float]().Randomized() * rand(1'f)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 348
diff changeset
155 echo TMat4[float]().Randomized() * rand(1'f)
348
67081fbaf636 did: a ton of small improvments
Sam <sam@basx.dev>
parents: 212
diff changeset
156
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
157 randomize()
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
158 testMatrix()