Mercurial > games > semicongine
annotate tests/test_matrix.nim @ 518:5d406c17bbcb
did: refactor Vector names
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 16:13:32 +0700 |
parents | 680c4b8ca28a |
children | 8287a91e5d56 |
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 |
518 | 4 import semicongine/math/vector |
5 import semicongine/math/matrix | |
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
518 | 8 proc echoInfo(v: TVec) = |
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 echo v |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 echo " Length: ", v.length |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 echo " Normlized: ", v.normalized |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 echo " negated: ", -v |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 proc echoAdd[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 echo v1, " + ", v2, " = ", v1 + v2 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 proc echoSub[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 echo v1, " - ", v2, " = ", v1 - v2 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 proc echoMul[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 echo v1, " * ", v2, " = ", v1 * v2 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 proc echoDiv[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 echo v1, " / ", v2, " = ", v1 / v2 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 proc echoDot[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 echo v1, " o ", v2, " = ", v1.dot(v2) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 proc echoCross[T, U](v1: T, v2: U) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 echo v1, " x ", v2, " = ", v1.cross(v2) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 template withAllIntegerMats(stuff: untyped) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 stuff(Mat22[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 stuff(Mat23[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 stuff(Mat32[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 stuff(Mat33[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 stuff(Mat34[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 stuff(Mat43[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 stuff(Mat44[int32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 stuff(Mat22[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 stuff(Mat23[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 stuff(Mat32[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 stuff(Mat33[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 stuff(Mat34[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 stuff(Mat43[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 stuff(Mat44[int64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 template withAllFloatMats(stuff: untyped) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 stuff(Mat22[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 stuff(Mat23[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 stuff(Mat32[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 stuff(Mat33[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 stuff(Mat34[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 stuff(Mat43[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 stuff(Mat44[float32]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 stuff(Mat22[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 stuff(Mat23[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 stuff(Mat32[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 stuff(Mat33[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 stuff(Mat34[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 stuff(Mat43[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 stuff(Mat44[float64]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 template withAllMats(stuff: untyped) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 stuff(Mat22[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 stuff(Mat23[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 stuff(Mat32[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 stuff(Mat33[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 stuff(Mat34[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 stuff(Mat43[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 stuff(Mat44[int]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 stuff(Mat22[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 stuff(Mat23[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 stuff(Mat32[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 stuff(Mat33[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 stuff(Mat34[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 stuff(Mat43[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 stuff(Mat44[float]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 template testTranspose(t: typedesc) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 echo "testTranspose: ", t |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 let m = t().randomized() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 assert m == m.transposed().transposed() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 template testAssignI(t: typedesc) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 echo "testAssignI: ", t |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 var m = t() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 for i in 0 ..< t.data.len: |
477 | 92 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
|
93 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 template testAssignF(t: typedesc) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 echo "testAssignF: ", t |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 var m = t() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 for i in 0 ..< t.data.len: |
477 | 98 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
|
99 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 template testRowCols(t: typedesc) = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 echo "testRowCols: ", t |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 var m = t().randomized() |
477 | 103 for i in 0 ..< t.rowCount: |
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 echo m.row(i) |
477 | 105 for i in 0 ..< t.columnCount: |
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 echo m.col(i) |
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 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 proc testMatrix() = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 withAllMats(testTranspose) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 withAllIntegerMats(testAssignI) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 withAllFloatMats(testAssignF) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 withAllMats(testRowCols) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 |
477 | 115 echo Unit22 |
116 echo Unit22i | |
117 echo Unit22i8 | |
118 echo Unit22i16 | |
119 echo Unit22i32 | |
120 echo Unit22i64 | |
121 | |
122 echo Unit33 | |
123 echo Unit33i | |
124 echo Unit33i8 | |
125 echo Unit33i16 | |
126 echo Unit33i32 | |
127 echo Unit33i64 | |
128 | |
129 echo Unit44 | |
130 echo Unit44i | |
131 echo Unit44i8 | |
132 echo Unit44i16 | |
133 echo Unit44i32 | |
134 echo Unit44i64 | |
135 | |
136 echo Mat22[float]().randomized() * One2.randomized() | |
137 echo Mat33[float]().randomized() * One3.randomized() | |
138 echo Mat44[float]().randomized() * One4.randomized() | |
139 | |
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
140 randomize() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
141 testMatrix() |