annotate tests/test_matrix.nim @ 58:8287a91e5d56

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