tstaticvector.nim 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. discard """
  2. matrix: "--mm:orc"
  3. output: '''0
  4. 0
  5. 2
  6. 100
  7. 30.0 TVec[1, system.float32](data: [2.0])
  8. '''
  9. """
  10. type
  11. RectArray*[R, C: static[int], T] = distinct array[R * C, T]
  12. StaticMatrix*[R, C: static[int], T] = object
  13. elements*: RectArray[R, C, T]
  14. StaticVector*[N: static[int], T] = StaticMatrix[N, 1, T]
  15. proc foo*[N, T](a: StaticVector[N, T]): T = 0.T
  16. proc foobar*[N, T](a, b: StaticVector[N, T]): T = 0.T
  17. var a: StaticVector[3, int]
  18. echo foo(a) # OK
  19. echo foobar(a, a) # <--- hangs compiler
  20. # https://github.com/nim-lang/Nim/issues/3112
  21. type
  22. Vector[N: static[int]] = array[N, float64]
  23. TwoVectors[Na, Nb: static[int]] = tuple
  24. a: Vector[Na]
  25. b: Vector[Nb]
  26. var v: TwoVectors[2, 100]
  27. echo v[0].len
  28. echo v[1].len
  29. #let xx = 50
  30. v[1][50] = 0.0
  31. # https://github.com/nim-lang/Nim/issues/1051
  32. type
  33. TMatrix[N,M: static[int], T] = object
  34. data: array[0..M*N-1, T]
  35. TMat4f = TMatrix[4,4,float32]
  36. TVec3f = TMatrix[1,3,float32]
  37. TVec4f = TMatrix[1,4,float32]
  38. TVec[N: static[int]; T] = TMatrix[1,N,T]
  39. proc dot*(a, b: TVec): TVec.T =
  40. #assert(a.data.len == b.data.len)
  41. for i in 1..a.data.len:
  42. result += a.data[i-1] * b.data[i-1]
  43. proc row*(a: TMatrix; i: int): auto =
  44. result = TVec[TMatrix.M, TMatrix.T]()
  45. for idx in 1 .. TMatrix.M:
  46. result.data[idx-1] = a.data[(TMatrix.N * (idx-1)) + (i-1)]
  47. proc col*(a: TMatrix; j: int): auto =
  48. result = TVec[TMatrix.N, TMatrix.T]()
  49. for idx in 0 ..< TMatrix.N:
  50. result.data[idx] = a.data[(TMatrix.N * (idx)) + (j-1)]
  51. proc mul*(a: TMat4f; b: TMat4f): TMat4f =
  52. for i in 1..4:
  53. for j in 1..4:
  54. result.data[(4 * (j-1)) + (i-1)] = dot(row(a,i), col(b,j))
  55. var test = TVec4f(data: [1.0'f32, 2.0'f32, 3.0'f32, 4.0'f32])
  56. echo dot(test,test), " ", repr(col(test, 2))