tstaticvector.nim 1.7 KB

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