Mercurial > games > semicongine
changeset 16:617c6dcddbe2
add: matrix multiplications, tests
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 30 Dec 2022 15:56:17 +0700 |
parents | dde536a70483 |
children | b40466fa446a |
files | Makefile src/matrix.nim src/vector.nim tests/test_matrix.nim tests/test_vector.nim |
diffstat | 5 files changed, 279 insertions(+), 247 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Wed Dec 28 20:33:15 2022 +0700 +++ b/Makefile Fri Dec 30 15:56:17 2022 +0700 @@ -53,6 +53,8 @@ rm -rf build # clean thirdparty too? +.PHONY: tests + # tests tests: testament p tests/
--- a/src/matrix.nim Wed Dec 28 20:33:15 2022 +0700 +++ b/src/matrix.nim Fri Dec 30 15:56:17 2022 +0700 @@ -1,4 +1,6 @@ -import random +import std/math +import std/macros +import std/random import std/strutils import std/typetraits @@ -32,21 +34,68 @@ IntegerMat = Mat22[SomeInteger]|Mat33[SomeInteger]|Mat44[SomeInteger]|Mat23[SomeInteger]|Mat32[SomeInteger]|Mat34[SomeInteger]|Mat43[SomeInteger] FloatMat = Mat22[SomeFloat]|Mat33[SomeFloat]|Mat44[SomeFloat]|Mat23[SomeFloat]|Mat32[SomeFloat]|Mat34[SomeFloat]|Mat43[SomeFloat] +func unit22[T: SomeNumber](): auto {.compiletime.} = Mat22[T](data:[ + T(1), T(0), + T(0), T(1), +]) +func unit33[T: SomeNumber](): auto {.compiletime.} = Mat33[T](data:[ + T(1), T(0), T(0), + T(0), T(1), T(0), + T(0), T(0), T(1), +]) +func unit44[T: SomeNumber](): auto {.compiletime.} = Mat44[T](data: [ + T(1), T(0), T(0), T(0), + T(0), T(1), T(0), T(0), + T(0), T(0), T(1), T(0), + T(0), T(0), T(0), T(1), +]) -func rowCount*(m: Mat22): int = 2 -func columnCount*(m: Mat22): int = 2 -func rowCount*(m: Mat23): int = 2 -func columnCount*(m: Mat23): int = 3 -func rowCount*(m: Mat32): int = 3 -func columnCount*(m: Mat32): int = 2 -func rowCount*(m: Mat33): int = 3 -func columnCount*(m: Mat33): int = 3 -func rowCount*(m: Mat34): int = 3 -func columnCount*(m: Mat34): int = 4 -func rowCount*(m: Mat43): int = 4 -func columnCount*(m: Mat43): int = 3 -func rowCount*(m: Mat44): int = 4 -func columnCount*(m: Mat44): int = 4 +# generates constants: Unit +# Also for Y, Z, R, G, B +# not sure if this is necessary or even a good idea... +macro generateAllConsts() = + result = newStmtList() + for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: + var typename = theType[0 .. 0] + if theType[^2].isDigit: + typename = typename & theType[^2] + if theType[^1].isDigit: + typename = typename & theType[^1] + result.add(newConstStmt( + postfix(ident("Unit22" & typename), "*"), + newCall(nnkBracketExpr.newTree(ident("unit22"), ident(theType))) + )) + result.add(newConstStmt( + postfix(ident("Unit33" & typename), "*"), + newCall(nnkBracketExpr.newTree(ident("unit33"), ident(theType))) + )) + result.add(newConstStmt( + postfix(ident("Unit44" & typename), "*"), + newCall(nnkBracketExpr.newTree(ident("unit44"), ident(theType))) + )) + +generateAllConsts() + +const Unit22* = unit22[float]() +const Unit33* = unit33[float]() +const Unit44* = unit44[float]() + +template rowCount*(m: typedesc): int = + when m is Mat22: 2 + elif m is Mat23: 2 + elif m is Mat32: 3 + elif m is Mat33: 3 + elif m is Mat34: 3 + elif m is Mat43: 4 + elif m is Mat44: 4 +template columnCount*(m: typedesc): int = + when m is Mat22: 2 + elif m is Mat23: 3 + elif m is Mat32: 2 + elif m is Mat33: 3 + elif m is Mat34: 4 + elif m is Mat43: 3 + elif m is Mat44: 4 func toString[T](value: T): string = @@ -62,10 +111,10 @@ for i in 0 ..< strvalues.len: let filler = " ".repeat(maxwidth - strvalues[i].len) - if i mod value.columnCount == value.columnCount - 1: + if i mod T.columnCount == T.columnCount - 1: result &= filler & strvalues[i] & "\n" else: - if i mod value.columnCount == 0: + if i mod T.columnCount == 0: result &= " " result &= filler & strvalues[i] & " " result = $T & "\n" & result @@ -78,8 +127,8 @@ func `$`*(v: Mat43[SomeNumber]): string = toString[Mat43[SomeNumber]](v) func `$`*(v: Mat44[SomeNumber]): string = toString[Mat44[SomeNumber]](v) -func `[]`*[T: Mat](m: T, row, col: int): auto = m.data[col + row * m.columnCount] -proc `[]=`*[T: Mat, U](m: var T, row, col: int, value: U) = m.data[col + row * m.columnCount] = value +func `[]`*[T: Mat](m: T, row, col: int): auto = m.data[col + row * T.columnCount] +proc `[]=`*[T: Mat, U](m: var T, row, col: int, value: U) = m.data[col + row * T.columnCount] = value func row*[T: Mat22](m: T, i: 0..1): auto = Vec2([m[i, 0], m[i, 1]]) func row*[T: Mat32](m: T, i: 0..2): auto = Vec2([m[i, 0], m[i, 1]]) @@ -97,6 +146,163 @@ func col*[T: Mat43](m: T, i: 0..2): auto = Vec4([m[0, i], m[1, i], m[2, i], m[3, i]]) func col*[T: Mat44](m: T, i: 0..3): auto = Vec4([m[0, i], m[1, i], m[2, i], m[3, i]]) +proc createMatMatMultiplicationOperator(leftType: typedesc, rightType: typedesc, outType: typedesc): NimNode = + var data = nnkBracket.newTree() + for i in 0 ..< rowCount(leftType): + for j in 0 ..< rightType.columnCount: + data.add(newCall( + ident("sum"), + infix( + newCall(newDotExpr(ident("a"), ident("row")), newLit(i)), + "*", + newCall(newDotExpr(ident("b"), ident("col")), newLit(j)) + ) + )) + + return newProc( + postfix(nnkAccQuoted.newTree(ident("*")), "*"), + params=[ + ident("auto"), + newIdentDefs(ident("a"), ident(leftType.name)), + newIdentDefs(ident("b"), ident(rightType.name)) + ], + body=nnkObjConstr.newTree(ident(outType.name), nnkExprColonExpr.newTree(ident("data"), data)), + procType=nnkFuncDef, + ) + +proc createVecMatMultiplicationOperator(matType: typedesc, vecType: typedesc): NimNode = + var data = nnkBracket.newTree() + for i in 0 ..< matType.rowCount: + data.add(newCall( + ident("sum"), + infix( + ident("v"), + "*", + newCall(newDotExpr(ident("m"), ident("row")), newLit(i)) + ) + )) + + let resultVec = newCall( + nnkBracketExpr.newTree(ident(vecType.name), ident("T")), + data, + ) + let name = postfix(nnkAccQuoted.newTree(ident("*")), "*") + let genericParams = nnkGenericParams.newTree(nnkIdentDefs.newTree(ident("T"), ident("SomeNumber"), newEmptyNode())) + let formalParams = nnkFormalParams.newTree( + ident("auto"), + newIdentDefs(ident("m"), nnkBracketExpr.newTree(ident(matType.name), ident("T"))), + newIdentDefs(ident("v"), nnkBracketExpr.newTree(ident(vecType.name), ident("T"))), + ) + + return nnkFuncDef.newTree( + name, + newEmptyNode(), + genericParams, + formalParams, + newEmptyNode(), + newEmptyNode(), + resultVec + ) + +proc createVecMatMultiplicationOperator1(vecType: typedesc, matType: typedesc): NimNode = + var data = nnkBracket.newTree() + for i in 0 ..< matType.columnCount: + data.add(newCall( + ident("sum"), + infix( + ident("v"), + "*", + newCall(newDotExpr(ident("m"), ident("col")), newLit(i)) + ) + )) + let resultVec = nnkObjConstr.newTree( + nnkBracketExpr.newTree(ident(vecType.name), ident("float")), + nnkExprColonExpr.newTree(ident("data"), data) + ) + + return nnkFuncDef.newTree( + ident("test"), + newEmptyNode(), + newEmptyNode(), + newEmptyNode(), + newEmptyNode(), + newEmptyNode(), + resultVec, + ) + +proc createMatScalarOperator(matType: typedesc, op: string): NimNode = + result = newStmtList() + + var data = nnkBracket.newTree() + for i in 0 ..< matType.rowCount * matType.columnCount: + data.add(infix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op, ident("b"))) + result.add(newProc( + postfix(nnkAccQuoted.newTree(ident(op)), "*"), + params=[ + ident("auto"), + newIdentDefs(ident("a"), ident(matType.name)), + newIdentDefs(ident("b"), ident("SomeNumber")), + ], + body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), + procType=nnkFuncDef, + )) + result.add(newProc( + postfix(nnkAccQuoted.newTree(ident(op)), "*"), + params=[ + ident("auto"), + newIdentDefs(ident("b"), ident("SomeNumber")), + newIdentDefs(ident("a"), ident(matType.name)), + ], + body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), + procType=nnkFuncDef, + )) + if op == "-": + var data2 = nnkBracket.newTree() + for i in 0 ..< matType.rowCount * matType.columnCount: + data2.add(prefix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op)) + result.add(newProc( + postfix(nnkAccQuoted.newTree(ident(op)), "*"), + params=[ + ident("auto"), + newIdentDefs(ident("a"), ident(matType.name)), + ], + body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data2)), + procType=nnkFuncDef, + )) + +macro createAllMultiplicationOperators() = + result = newStmtList() + + for op in ["+", "-", "*", "/"]: + result.add(createMatScalarOperator(Mat22, op)) + result.add(createMatScalarOperator(Mat23, op)) + result.add(createMatScalarOperator(Mat32, op)) + result.add(createMatScalarOperator(Mat33, op)) + result.add(createMatScalarOperator(Mat34, op)) + result.add(createMatScalarOperator(Mat43, op)) + result.add(createMatScalarOperator(Mat44, op)) + + result.add(createMatMatMultiplicationOperator(Mat22, Mat22, Mat22)) + result.add(createMatMatMultiplicationOperator(Mat22, Mat23, Mat23)) + result.add(createMatMatMultiplicationOperator(Mat23, Mat32, Mat22)) + result.add(createMatMatMultiplicationOperator(Mat23, Mat33, Mat23)) + result.add(createMatMatMultiplicationOperator(Mat32, Mat22, Mat32)) + result.add(createMatMatMultiplicationOperator(Mat32, Mat23, Mat33)) + result.add(createMatMatMultiplicationOperator(Mat33, Mat32, Mat32)) + result.add(createMatMatMultiplicationOperator(Mat33, Mat33, Mat33)) + result.add(createMatMatMultiplicationOperator(Mat33, Mat34, Mat34)) + result.add(createMatMatMultiplicationOperator(Mat43, Mat33, Mat43)) + result.add(createMatMatMultiplicationOperator(Mat43, Mat34, Mat44)) + result.add(createMatMatMultiplicationOperator(Mat44, Mat43, Mat43)) + result.add(createMatMatMultiplicationOperator(Mat44, Mat44, Mat44)) + + result.add(createVecMatMultiplicationOperator(Mat22, Vec2)) + result.add(createVecMatMultiplicationOperator(Mat33, Vec3)) + result.add(createVecMatMultiplicationOperator(Mat44, Vec4)) + +createAllMultiplicationOperators() + + func transposed*[T](m: Mat22[T]): Mat22[T] = Mat22[T](data: [ m[0, 0], m[1, 0], m[0, 1], m[1, 1], @@ -133,7 +339,7 @@ m[0, 3], m[1, 3], m[2, 3], m[3, 3], ]) -# call e.g. Mat32[int]().initRandom() to get a random matrix +# call e.g. Mat32[int]().randomized() to get a random matrix template makeRandomInit(mattype: typedesc) = proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = for i in 0 ..< result.data.len:
--- a/src/vector.nim Wed Dec 28 20:33:15 2022 +0700 +++ b/src/vector.nim Fri Dec 30 15:56:17 2022 +0700 @@ -1,3 +1,4 @@ +import std/random import std/math import std/strutils import std/macros @@ -12,6 +13,9 @@ Vec* = Vec2|Vec3|Vec4 # define some often used constants +func ConstOne2[T: SomeNumber](): auto {.compiletime.} = Vec2[T]([T(1), T(1)]) +func ConstOne3[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(1), T(1)]) +func ConstOne4[T: SomeNumber](): auto {.compiletime.} = Vec4[T]([T(1), T(1), T(1), T(1)]) func ConstX[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) func ConstY[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) func ConstZ[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) @@ -20,11 +24,11 @@ func ConstB[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) # generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64 -# Also for Y, Z, R, G, B +# Also for Y, Z, R, G, B and One # not sure if this is necessary or even a good idea... macro generateAllConsts() = result = newStmtList() - for component in ["X", "Y", "Z", "R", "G", "B"]: + for component in ["X", "Y", "Z", "R", "G", "B", "One2", "One3", "One4"]: for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: var typename = theType[0 .. 0] if theType[^2].isDigit: @@ -43,6 +47,9 @@ const X* = ConstX[float]() const Y* = ConstY[float]() const Z* = ConstZ[float]() +const One2* = ConstOne2[float]() +const One3* = ConstOne3[float]() +const One4* = ConstOne4[float]() func newVec2*[T](x, y: T): auto = Vec2([x, y]) func newVec3*[T](x, y, z: T): auto = Vec3([x, y, z]) @@ -201,3 +208,16 @@ result.add(vectorAttributeAccessor(i & j & k & l)) createVectorAttribAccessorFuncs() + +# call e.g. Vec2[int]().randomized() to get a random matrix +template makeRandomInit(mattype: typedesc) = + proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = + for i in 0 ..< result.len: + result[i] = rand(low(typeof(m[0])) .. high(typeof(m[0]))) + proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = + for i in 0 ..< result.len: + result[i] = rand(1.0) + +makeRandomInit(Vec2) +makeRandomInit(Vec3) +makeRandomInit(Vec4)
--- a/tests/test_matrix.nim Wed Dec 28 20:33:15 2022 +0700 +++ b/tests/test_matrix.nim Fri Dec 30 15:56:17 2022 +0700 @@ -32,144 +32,6 @@ proc randVec4F(): auto = newVec4(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01) -proc testVector() = - echoInfo(randVec2I()) - echoInfo(randVec2F()) - echoInfo(randVec3I()) - echoInfo(randVec3F()) - echoInfo(randVec4I()) - echoInfo(randVec4F()) - - # test math operations vector-vector - echoAdd(randVec2I(), randVec2I()) - echoAdd(randVec2F(), randVec2F()) - echoAdd(randVec3I(), randVec3I()) - echoAdd(randVec3F(), randVec3F()) - echoAdd(randVec4I(), randVec4I()) - echoAdd(randVec4F(), randVec4F()) - echoSub(randVec2I(), randVec2I()) - echoSub(randVec2F(), randVec2F()) - echoSub(randVec3I(), randVec3I()) - echoSub(randVec3F(), randVec3F()) - echoSub(randVec4I(), randVec4I()) - echoSub(randVec4F(), randVec4F()) - echoMul(randVec2I(), randVec2I()) - echoMul(randVec2F(), randVec2F()) - echoMul(randVec3I(), randVec3I()) - echoMul(randVec3F(), randVec3F()) - echoMul(randVec4I(), randVec4I()) - echoMul(randVec4F(), randVec4F()) - echoDiv(randVec2I(), randVec2I()) - echoDiv(randVec2F(), randVec2F()) - echoDiv(randVec3I(), randVec3I()) - echoDiv(randVec3F(), randVec3F()) - echoDiv(randVec4I(), randVec4I()) - echoDiv(randVec4F(), randVec4F()) - echoDot(randVec2I(), randVec2I()) - echoDot(randVec2F(), randVec2F()) - echoDot(randVec3I(), randVec3I()) - echoDot(randVec3F(), randVec3F()) - echoDot(randVec4I(), randVec4I()) - echoDot(randVec4F(), randVec4F()) - echoCross(randVec3I(), randVec3I()) - echoCross(randVec3F(), randVec3F()) - - - # test math operations vector-scalar - echoAdd(randVec2I(), rand(1 .. 10)) - echoAdd(randVec2F(), rand(10'f)) - echoAdd(randVec3I(), rand(1 .. 10)) - echoAdd(randVec3F(), rand(10'f)) - echoAdd(randVec4I(), rand(1 .. 10)) - echoAdd(randVec4F(), rand(10'f)) - echoSub(randVec2I(), rand(1 .. 10)) - echoSub(randVec2F(), rand(10'f)) - echoSub(randVec3I(), rand(1 .. 10)) - echoSub(randVec3F(), rand(10'f)) - echoSub(randVec4I(), rand(1 .. 10)) - echoSub(randVec4F(), rand(10'f)) - echoMul(randVec2I(), rand(1 .. 10)) - echoMul(randVec2F(), rand(10'f)) - echoMul(randVec3I(), rand(1 .. 10)) - echoMul(randVec3F(), rand(10'f)) - echoMul(randVec4I(), rand(1 .. 10)) - echoMul(randVec4F(), rand(10'f)) - echoDiv(randVec2I(), rand(1 .. 10)) - echoDiv(randVec2F(), rand(10'f)) - echoDiv(randVec3I(), rand(1 .. 10)) - echoDiv(randVec3F(), rand(10'f)) - echoDiv(randVec4I(), rand(1 .. 10)) - echoDiv(randVec4F(), rand(10'f)) - - # test math operations scalar-vector - echoAdd(rand(1 .. 10), randVec2I()) - echoAdd(rand(10'f), randVec2F()) - echoAdd(rand(1 .. 10), randVec3I()) - echoAdd(rand(10'f), randVec3F()) - echoAdd(rand(1 .. 10), randVec4I()) - echoAdd(rand(10'f), randVec4F()) - echoSub(rand(1 .. 10), randVec2I()) - echoSub(rand(10'f), randVec2F()) - echoSub(rand(1 .. 10), randVec3I()) - echoSub(rand(10'f), randVec3F()) - echoSub(rand(1 .. 10), randVec4I()) - echoSub(rand(10'f), randVec4F()) - echoMul(rand(1 .. 10), randVec2I()) - echoMul(rand(10'f), randVec2F()) - echoMul(rand(1 .. 10), randVec3I()) - echoMul(rand(10'f), randVec3F()) - echoMul(rand(1 .. 10), randVec4I()) - echoMul(rand(10'f), randVec4F()) - echoDiv(rand(1 .. 10), randVec2I()) - echoDiv(rand(10'f), randVec2F()) - echoDiv(rand(1 .. 10), randVec3I()) - echoDiv(rand(10'f), randVec3F()) - echoDiv(rand(1 .. 10), randVec4I()) - echoDiv(rand(10'f), randVec4F()) - - # test attribute syntax sugar - echo "float2int ", to[int](randVec2F()) - echo "int2float ", to[float](randVec2I()) - echo "float2int ", to[int](randVec3F()) - echo "int2float ", to[float](randVec3I()) - echo "float2int ", to[int](randVec3F()) - echo "int2float ", to[float](randVec3I()) - - echo "V2I.xx: ", randVec2I().xx - echo "V2I.yx: ", randVec2I().xy - echo "V2F.xx: ", randVec2F().xx - echo "V2F.yx: ", randVec2F().yx - echo "V2I.rr: ", randVec2I().rr - echo "V2I.gr: ", randVec2I().gr - echo "V2F.rr: ", randVec2F().rr - echo "V2F.gr: ", randVec2F().gr - - echo "V3I.yyy: ", randVec3I().yyy - echo "V3I.yxz: ", randVec3I().xyz - echo "V3F.yyy: ", randVec3F().yyy - echo "V3F.yxz: ", randVec3F().yxz - echo "V3I.ggg: ", randVec3I().ggg - echo "V3I.grb: ", randVec3I().grb - echo "V3F.ggg: ", randVec3F().ggg - echo "V3F.grb: ", randVec3F().grb - - echo "V4I.zzzz: ", randVec4I().zzzz - echo "V4I.yxzw: ", randVec4I().xyzw - echo "V4F.zzzz: ", randVec4F().zzzz - echo "V4F.yxzw: ", randVec4F().yxzw - echo "V4I.bbbb: ", randVec4I().bbbb - echo "V4I.grba: ", randVec4I().grba - echo "V4F.bbbb: ", randVec4F().bbbb - echo "V4F.grba: ", randVec4F().grba - - echo "X: ", X - echo "Y: ", Y - echo "Z: ", Z - echo "X: ", Xi - echo "Y: ", Yi - echo "Z: ", Zi - - template withAllIntegerMats(stuff: untyped) = stuff(Mat22[int32]) stuff(Mat23[int32]) @@ -227,20 +89,20 @@ echo "testAssignI: ", t var m = t() for i in 0 ..< t.data.len: - m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(0'i32 .. 100'i32) + m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(0'i32 .. 100'i32) template testAssignF(t: typedesc) = echo "testAssignF: ", t var m = t() for i in 0 ..< t.data.len: - m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(100'f) + m[rand(0 ..< t.rowCount), rand(0 ..< t.columnCount)] = rand(100'f) template testRowCols(t: typedesc) = echo "testRowCols: ", t var m = t().randomized() - for i in 0 ..< m.rowCount: + for i in 0 ..< t.rowCount: echo m.row(i) - for i in 0 ..< m.columnCount: + for i in 0 ..< t.columnCount: echo m.col(i) @@ -250,6 +112,30 @@ withAllFloatMats(testAssignF) withAllMats(testRowCols) + echo Unit22 + echo Unit22i + echo Unit22i8 + echo Unit22i16 + echo Unit22i32 + echo Unit22i64 + + echo Unit33 + echo Unit33i + echo Unit33i8 + echo Unit33i16 + echo Unit33i32 + echo Unit33i64 + + echo Unit44 + echo Unit44i + echo Unit44i8 + echo Unit44i16 + echo Unit44i32 + echo Unit44i64 + + echo Mat22[float]().randomized() * One2.randomized() + echo Mat33[float]().randomized() * One3.randomized() + echo Mat44[float]().randomized() * One4.randomized() + randomize() -testVector() testMatrix()
--- a/tests/test_vector.nim Wed Dec 28 20:33:15 2022 +0700 +++ b/tests/test_vector.nim Fri Dec 30 15:56:17 2022 +0700 @@ -2,7 +2,6 @@ import math import vector -import matrix proc echoInfo(v: Vec) = @@ -170,86 +169,5 @@ echo "Z: ", Zi -template withAllIntegerMats(stuff: untyped) = - stuff(Mat22[int32]) - stuff(Mat23[int32]) - stuff(Mat32[int32]) - stuff(Mat33[int32]) - stuff(Mat34[int32]) - stuff(Mat43[int32]) - stuff(Mat44[int32]) - stuff(Mat22[int64]) - stuff(Mat23[int64]) - stuff(Mat32[int64]) - stuff(Mat33[int64]) - stuff(Mat34[int64]) - stuff(Mat43[int64]) - stuff(Mat44[int64]) - -template withAllFloatMats(stuff: untyped) = - stuff(Mat22[float32]) - stuff(Mat23[float32]) - stuff(Mat32[float32]) - stuff(Mat33[float32]) - stuff(Mat34[float32]) - stuff(Mat43[float32]) - stuff(Mat44[float32]) - stuff(Mat22[float64]) - stuff(Mat23[float64]) - stuff(Mat32[float64]) - stuff(Mat33[float64]) - stuff(Mat34[float64]) - stuff(Mat43[float64]) - stuff(Mat44[float64]) - -template withAllMats(stuff: untyped) = - stuff(Mat22[int]) - stuff(Mat23[int]) - stuff(Mat32[int]) - stuff(Mat33[int]) - stuff(Mat34[int]) - stuff(Mat43[int]) - stuff(Mat44[int]) - stuff(Mat22[float]) - stuff(Mat23[float]) - stuff(Mat32[float]) - stuff(Mat33[float]) - stuff(Mat34[float]) - stuff(Mat43[float]) - stuff(Mat44[float]) - -template testTranspose(t: typedesc) = - echo "testTranspose: ", t - let m = t().randomized() - assert m == m.transposed().transposed() - -template testAssignI(t: typedesc) = - echo "testAssignI: ", t - var m = t() - for i in 0 ..< t.data.len: - m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(0'i32 .. 100'i32) - -template testAssignF(t: typedesc) = - echo "testAssignF: ", t - var m = t() - for i in 0 ..< t.data.len: - m[rand(0 ..< m.rowCount), rand(0 ..< m.columnCount)] = rand(100'f) - -template testRowCols(t: typedesc) = - echo "testRowCols: ", t - var m = t().randomized() - for i in 0 ..< m.rowCount: - echo m.row(i) - for i in 0 ..< m.columnCount: - echo m.col(i) - - -proc testMatrix() = - withAllMats(testTranspose) - withAllIntegerMats(testAssignI) - withAllFloatMats(testAssignF) - withAllMats(testRowCols) - randomize() testVector() -testMatrix()