annotate tests/test_matrix.nim @ 776:002d9c576756

did: preparations to refactor material system, still tons to do
author Sam <sam@basx.dev>
date Sun, 23 Jul 2023 19:53:10 +0700
parents 9cfd59c93ae8
children 67081fbaf636
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
1 import random
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
2 import math
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
3
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
4 import semicongine
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
5
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
6
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 493
diff changeset
7 proc echoInfo(v: TVec) =
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
8 echo v
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
9 echo " Length: ", v.length
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
10 echo " Normlized: ", v.normalized
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
11 echo " negated: ", -v
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
12
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
13 proc echoAdd[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
14 echo v1, " + ", v2, " = ", v1 + v2
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
15 proc echoSub[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
16 echo v1, " - ", v2, " = ", v1 - v2
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
17 proc echoMul[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
18 echo v1, " * ", v2, " = ", v1 * v2
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
19 proc echoDiv[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
20 echo v1, " / ", v2, " = ", v1 / v2
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
21 proc echoDot[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
22 echo v1, " o ", v2, " = ", v1.dot(v2)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
23 proc echoCross[T, U](v1: T, v2: U) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
24 echo v1, " x ", v2, " = ", v1.cross(v2)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
25
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
26 proc randVec2I(): auto = newVec2(rand(1 .. 10), rand(1 .. 10))
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
27 proc randVec2F(): auto = newVec2(rand(10'f) + 0.01, rand(10'f) + 0.01)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
28 proc randVec3I(): auto = newVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
29 proc randVec3F(): auto = newVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
30 proc randVec4I(): auto = newVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
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)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
32
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
33
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
34 template withAllIntegerMats(stuff: untyped) =
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
35 stuff(TMat2[int32])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
36 stuff(TMat23[int32])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
37 stuff(TMat32[int32])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
38 stuff(TMat3[int32])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
39 stuff(TMat34[int32])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
40 stuff(TMat43[int32])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
41 stuff(TMat4[int32])
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
42 stuff(TMat2[int64])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
43 stuff(TMat23[int64])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
44 stuff(TMat32[int64])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
45 stuff(TMat3[int64])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
46 stuff(TMat34[int64])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
47 stuff(TMat43[int64])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
48 stuff(TMat4[int64])
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
49
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
50 template withAllFloatMats(stuff: untyped) =
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
51 stuff(TMat2[float32])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
52 stuff(TMat23[float32])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
53 stuff(TMat32[float32])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
54 stuff(TMat3[float32])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
55 stuff(TMat34[float32])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
56 stuff(TMat43[float32])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
57 stuff(TMat4[float32])
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
58 stuff(TMat2[float64])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
59 stuff(TMat23[float64])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
60 stuff(TMat32[float64])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
61 stuff(TMat3[float64])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
62 stuff(TMat34[float64])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
63 stuff(TMat43[float64])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
64 stuff(TMat4[float64])
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
65
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
66 template withAllMats(stuff: untyped) =
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
67 stuff(TMat2[int])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
68 stuff(TMat23[int])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
69 stuff(TMat32[int])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
70 stuff(TMat3[int])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
71 stuff(TMat34[int])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
72 stuff(TMat43[int])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
73 stuff(TMat4[int])
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
74 stuff(TMat2[float])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
75 stuff(TMat23[float])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
76 stuff(TMat32[float])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
77 stuff(TMat3[float])
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
78 stuff(TMat34[float])
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
79 stuff(TMat43[float])
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
80 stuff(TMat4[float])
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
81
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
82 template testTranspose(t: typedesc) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
83 echo "testTranspose: ", t
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
84 let m = t().randomized()
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
85 assert m == m.transposed().transposed()
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
86
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
87 template testAssignI(t: typedesc) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
88 echo "testAssignI: ", t
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
89 var m = t()
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
90 for i in 0 ..< t.data.len:
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
91 m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(0'i32 .. 100'i32)
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
92
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
93 template testAssignF(t: typedesc) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
94 echo "testAssignF: ", t
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
95 var m = t()
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
96 for i in 0 ..< t.data.len:
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
97 m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(100'f)
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
98
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
99 template testRowCols(t: typedesc) =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
100 echo "testRowCols: ", t
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
101 var m = t().randomized()
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
102 for i in 0 ..< t.rowCount:
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
103 echo m.row(i)
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
104 for i in 0 ..< t.columnCount:
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
105 echo m.col(i)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
106
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
107
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
108 proc testMatrix() =
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
109 withAllMats(testTranspose)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
110 withAllIntegerMats(testAssignI)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
111 withAllFloatMats(testAssignF)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
112 withAllMats(testRowCols)
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
113
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
114 echo Unit2
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
115 echo Unit2i
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
116 echo Unit2i8
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
117 echo Unit2i16
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
118 echo Unit2i32
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
119 echo Unit2i64
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
120
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
121 echo Unit3
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
122 echo Unit3i
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
123 echo Unit3i8
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
124 echo Unit3i16
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
125 echo Unit3i32
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
126 echo Unit3i64
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
127
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
128 echo Unit4
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
129 echo Unit4i
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
130 echo Unit4i8
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
131 echo Unit4i16
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
132 echo Unit4i32
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
133 echo Unit4i64
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
134
673
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
135 echo TMat2[float32]().randomized() * One2.randomized()
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
136 echo TMat3[float32]().randomized() * One3.randomized()
9cfd59c93ae8 fix: tests
Sam <sam@basx.dev>
parents: 519
diff changeset
137 echo TMat4[float32]().randomized() * One4.randomized()
477
f226c99b5043 add: matrix multiplications, tests
Sam <sam@basx.dev>
parents: 474
diff changeset
138
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
139 randomize()
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
140 testMatrix()