comparison tests/test_matrix.nim @ 477:f226c99b5043

add: matrix multiplications, tests
author Sam <sam@basx.dev>
date Fri, 30 Dec 2022 15:56:17 +0700
parents d4750f2e0760
children 9edca5dc4e93
comparison
equal deleted inserted replaced
476:6862ca8c1324 477:f226c99b5043
28 proc randVec2F(): auto = newVec2(rand(10'f) + 0.01, rand(10'f) + 0.01) 28 proc randVec2F(): auto = newVec2(rand(10'f) + 0.01, rand(10'f) + 0.01)
29 proc randVec3I(): auto = newVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10)) 29 proc randVec3I(): auto = newVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
30 proc randVec3F(): auto = newVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01) 30 proc randVec3F(): auto = newVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01)
31 proc randVec4I(): auto = newVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10)) 31 proc randVec4I(): auto = newVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10))
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) 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)
33
34
35 proc testVector() =
36 echoInfo(randVec2I())
37 echoInfo(randVec2F())
38 echoInfo(randVec3I())
39 echoInfo(randVec3F())
40 echoInfo(randVec4I())
41 echoInfo(randVec4F())
42
43 # test math operations vector-vector
44 echoAdd(randVec2I(), randVec2I())
45 echoAdd(randVec2F(), randVec2F())
46 echoAdd(randVec3I(), randVec3I())
47 echoAdd(randVec3F(), randVec3F())
48 echoAdd(randVec4I(), randVec4I())
49 echoAdd(randVec4F(), randVec4F())
50 echoSub(randVec2I(), randVec2I())
51 echoSub(randVec2F(), randVec2F())
52 echoSub(randVec3I(), randVec3I())
53 echoSub(randVec3F(), randVec3F())
54 echoSub(randVec4I(), randVec4I())
55 echoSub(randVec4F(), randVec4F())
56 echoMul(randVec2I(), randVec2I())
57 echoMul(randVec2F(), randVec2F())
58 echoMul(randVec3I(), randVec3I())
59 echoMul(randVec3F(), randVec3F())
60 echoMul(randVec4I(), randVec4I())
61 echoMul(randVec4F(), randVec4F())
62 echoDiv(randVec2I(), randVec2I())
63 echoDiv(randVec2F(), randVec2F())
64 echoDiv(randVec3I(), randVec3I())
65 echoDiv(randVec3F(), randVec3F())
66 echoDiv(randVec4I(), randVec4I())
67 echoDiv(randVec4F(), randVec4F())
68 echoDot(randVec2I(), randVec2I())
69 echoDot(randVec2F(), randVec2F())
70 echoDot(randVec3I(), randVec3I())
71 echoDot(randVec3F(), randVec3F())
72 echoDot(randVec4I(), randVec4I())
73 echoDot(randVec4F(), randVec4F())
74 echoCross(randVec3I(), randVec3I())
75 echoCross(randVec3F(), randVec3F())
76
77
78 # test math operations vector-scalar
79 echoAdd(randVec2I(), rand(1 .. 10))
80 echoAdd(randVec2F(), rand(10'f))
81 echoAdd(randVec3I(), rand(1 .. 10))
82 echoAdd(randVec3F(), rand(10'f))
83 echoAdd(randVec4I(), rand(1 .. 10))
84 echoAdd(randVec4F(), rand(10'f))
85 echoSub(randVec2I(), rand(1 .. 10))
86 echoSub(randVec2F(), rand(10'f))
87 echoSub(randVec3I(), rand(1 .. 10))
88 echoSub(randVec3F(), rand(10'f))
89 echoSub(randVec4I(), rand(1 .. 10))
90 echoSub(randVec4F(), rand(10'f))
91 echoMul(randVec2I(), rand(1 .. 10))
92 echoMul(randVec2F(), rand(10'f))
93 echoMul(randVec3I(), rand(1 .. 10))
94 echoMul(randVec3F(), rand(10'f))
95 echoMul(randVec4I(), rand(1 .. 10))
96 echoMul(randVec4F(), rand(10'f))
97 echoDiv(randVec2I(), rand(1 .. 10))
98 echoDiv(randVec2F(), rand(10'f))
99 echoDiv(randVec3I(), rand(1 .. 10))
100 echoDiv(randVec3F(), rand(10'f))
101 echoDiv(randVec4I(), rand(1 .. 10))
102 echoDiv(randVec4F(), rand(10'f))
103
104 # test math operations scalar-vector
105 echoAdd(rand(1 .. 10), randVec2I())
106 echoAdd(rand(10'f), randVec2F())
107 echoAdd(rand(1 .. 10), randVec3I())
108 echoAdd(rand(10'f), randVec3F())
109 echoAdd(rand(1 .. 10), randVec4I())
110 echoAdd(rand(10'f), randVec4F())
111 echoSub(rand(1 .. 10), randVec2I())
112 echoSub(rand(10'f), randVec2F())
113 echoSub(rand(1 .. 10), randVec3I())
114 echoSub(rand(10'f), randVec3F())
115 echoSub(rand(1 .. 10), randVec4I())
116 echoSub(rand(10'f), randVec4F())
117 echoMul(rand(1 .. 10), randVec2I())
118 echoMul(rand(10'f), randVec2F())
119 echoMul(rand(1 .. 10), randVec3I())
120 echoMul(rand(10'f), randVec3F())
121 echoMul(rand(1 .. 10), randVec4I())
122 echoMul(rand(10'f), randVec4F())
123 echoDiv(rand(1 .. 10), randVec2I())
124 echoDiv(rand(10'f), randVec2F())
125 echoDiv(rand(1 .. 10), randVec3I())
126 echoDiv(rand(10'f), randVec3F())
127 echoDiv(rand(1 .. 10), randVec4I())
128 echoDiv(rand(10'f), randVec4F())
129
130 # test attribute syntax sugar
131 echo "float2int ", to[int](randVec2F())
132 echo "int2float ", to[float](randVec2I())
133 echo "float2int ", to[int](randVec3F())
134 echo "int2float ", to[float](randVec3I())
135 echo "float2int ", to[int](randVec3F())
136 echo "int2float ", to[float](randVec3I())
137
138 echo "V2I.xx: ", randVec2I().xx
139 echo "V2I.yx: ", randVec2I().xy
140 echo "V2F.xx: ", randVec2F().xx
141 echo "V2F.yx: ", randVec2F().yx
142 echo "V2I.rr: ", randVec2I().rr
143 echo "V2I.gr: ", randVec2I().gr
144 echo "V2F.rr: ", randVec2F().rr
145 echo "V2F.gr: ", randVec2F().gr
146
147 echo "V3I.yyy: ", randVec3I().yyy
148 echo "V3I.yxz: ", randVec3I().xyz
149 echo "V3F.yyy: ", randVec3F().yyy
150 echo "V3F.yxz: ", randVec3F().yxz
151 echo "V3I.ggg: ", randVec3I().ggg
152 echo "V3I.grb: ", randVec3I().grb
153 echo "V3F.ggg: ", randVec3F().ggg
154 echo "V3F.grb: ", randVec3F().grb
155
156 echo "V4I.zzzz: ", randVec4I().zzzz
157 echo "V4I.yxzw: ", randVec4I().xyzw
158 echo "V4F.zzzz: ", randVec4F().zzzz
159 echo "V4F.yxzw: ", randVec4F().yxzw
160 echo "V4I.bbbb: ", randVec4I().bbbb
161 echo "V4I.grba: ", randVec4I().grba
162 echo "V4F.bbbb: ", randVec4F().bbbb
163 echo "V4F.grba: ", randVec4F().grba
164
165 echo "X: ", X
166 echo "Y: ", Y
167 echo "Z: ", Z
168 echo "X: ", Xi
169 echo "Y: ", Yi
170 echo "Z: ", Zi
171 33
172 34
173 template withAllIntegerMats(stuff: untyped) = 35 template withAllIntegerMats(stuff: untyped) =
174 stuff(Mat22[int32]) 36 stuff(Mat22[int32])
175 stuff(Mat23[int32]) 37 stuff(Mat23[int32])
225 87
226 template testAssignI(t: typedesc) = 88 template testAssignI(t: typedesc) =
227 echo "testAssignI: ", t 89 echo "testAssignI: ", t
228 var m = t() 90 var m = t()
229 for i in 0 ..< t.data.len: 91 for i in 0 ..< t.data.len:
230 m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(0'i32 .. 100'i32) 92 m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(0'i32 .. 100'i32)
231 93
232 template testAssignF(t: typedesc) = 94 template testAssignF(t: typedesc) =
233 echo "testAssignF: ", t 95 echo "testAssignF: ", t
234 var m = t() 96 var m = t()
235 for i in 0 ..< t.data.len: 97 for i in 0 ..< t.data.len:
236 m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(100'f) 98 m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(100'f)
237 99
238 template testRowCols(t: typedesc) = 100 template testRowCols(t: typedesc) =
239 echo "testRowCols: ", t 101 echo "testRowCols: ", t
240 var m = t().randomized() 102 var m = t().randomized()
241 for i in 0 ..< m.rowCount: 103 for i in 0 ..< t.rowCount:
242 echo m.row(i) 104 echo m.row(i)
243 for i in 0 ..< m.columnCount: 105 for i in 0 ..< t.columnCount:
244 echo m.col(i) 106 echo m.col(i)
245 107
246 108
247 proc testMatrix() = 109 proc testMatrix() =
248 withAllMats(testTranspose) 110 withAllMats(testTranspose)
249 withAllIntegerMats(testAssignI) 111 withAllIntegerMats(testAssignI)
250 withAllFloatMats(testAssignF) 112 withAllFloatMats(testAssignF)
251 withAllMats(testRowCols) 113 withAllMats(testRowCols)
252 114
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
253 randomize() 140 randomize()
254 testVector()
255 testMatrix() 141 testMatrix()