tcomplex.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/[complex, math]
  5. import std/assertions
  6. proc `=~`[T](x, y: Complex[T]): bool =
  7. result = abs(x.re-y.re) < 1e-6 and abs(x.im-y.im) < 1e-6
  8. proc `=~`[T](x: Complex[T]; y: T): bool =
  9. result = abs(x.re-y) < 1e-6 and abs(x.im) < 1e-6
  10. let
  11. z: Complex64 = complex(0.0, 0.0)
  12. oo: Complex64 = complex(1.0, 1.0)
  13. a: Complex64 = complex(1.0, 2.0)
  14. b: Complex64 = complex(-1.0, -2.0)
  15. m1: Complex64 = complex(-1.0, 0.0)
  16. i: Complex64 = complex(0.0, 1.0)
  17. one: Complex64 = complex(1.0, 0.0)
  18. tt: Complex64 = complex(10.0, 20.0)
  19. ipi: Complex64 = complex(0.0, -PI)
  20. doAssert(a/2.0 =~ complex(0.5, 1.0))
  21. doAssert(a == a)
  22. doAssert((a-a) == z)
  23. doAssert((a+b) == z)
  24. doAssert((a+b) =~ 0.0)
  25. doAssert((a/b) == m1)
  26. doAssert((1.0/a) =~ complex(0.2, -0.4))
  27. doAssert((a*b) == complex(3.0, -4.0))
  28. doAssert(10.0*a == tt)
  29. doAssert(a*10.0 == tt)
  30. doAssert(tt/10.0 == a)
  31. doAssert(oo+(-1.0) == i)
  32. doAssert( (-1.0)+oo == i)
  33. doAssert(abs(oo) == sqrt(2.0))
  34. doAssert(conjugate(a) == complex(1.0, -2.0))
  35. doAssert(sqrt(m1) == i)
  36. doAssert(exp(ipi) =~ m1)
  37. doAssert(pow(a, b) =~ complex(-3.72999124927876, -1.68815826725068))
  38. doAssert(pow(z, a) =~ complex(0.0, 0.0))
  39. doAssert(pow(z, z) =~ complex(1.0, 0.0))
  40. doAssert(pow(a, one) =~ a)
  41. doAssert(pow(a, m1) =~ complex(0.2, -0.4))
  42. doAssert(pow(a, 2.0) =~ complex(-3.0, 4.0))
  43. doAssert(pow(a, 2) =~ complex(-3.0, 4.0))
  44. doAssert(not(pow(a, 2.0) =~ a))
  45. doAssert(ln(a) =~ complex(0.804718956217050, 1.107148717794090))
  46. doAssert(log10(a) =~ complex(0.349485002168009, 0.480828578784234))
  47. doAssert(log2(a) =~ complex(1.16096404744368, 1.59727796468811))
  48. doAssert(sin(a) =~ complex(3.16577851321617, 1.95960104142161))
  49. doAssert(cos(a) =~ complex(2.03272300701967, -3.05189779915180))
  50. doAssert(tan(a) =~ complex(0.0338128260798967, 1.0147936161466335))
  51. doAssert(cot(a) =~ 1.0 / tan(a))
  52. doAssert(sec(a) =~ 1.0 / cos(a))
  53. doAssert(csc(a) =~ 1.0 / sin(a))
  54. doAssert(arcsin(a) =~ complex(0.427078586392476, 1.528570919480998))
  55. doAssert(arccos(a) =~ complex(1.14371774040242, -1.52857091948100))
  56. doAssert(arctan(a) =~ complex(1.338972522294494, 0.402359478108525))
  57. doAssert(arccot(a) =~ complex(0.2318238045004031, -0.402359478108525))
  58. doAssert(arcsec(a) =~ complex(1.384478272687081, 0.3965682301123288))
  59. doAssert(arccsc(a) =~ complex(0.1863180541078155, -0.3965682301123291))
  60. doAssert(cosh(a) =~ complex(-0.642148124715520, 1.068607421382778))
  61. doAssert(sinh(a) =~ complex(-0.489056259041294, 1.403119250622040))
  62. doAssert(tanh(a) =~ complex(1.1667362572409199, -0.243458201185725))
  63. doAssert(sech(a) =~ 1.0 / cosh(a))
  64. doAssert(csch(a) =~ 1.0 / sinh(a))
  65. doAssert(coth(a) =~ 1.0 / tanh(a))
  66. doAssert(arccosh(a) =~ complex(1.528570919480998, 1.14371774040242))
  67. doAssert(arcsinh(a) =~ complex(1.469351744368185, 1.06344002357775))
  68. doAssert(arctanh(a) =~ complex(0.173286795139986, 1.17809724509617))
  69. doAssert(arcsech(a) =~ arccosh(1.0/a))
  70. doAssert(arccsch(a) =~ arcsinh(1.0/a))
  71. doAssert(arccoth(a) =~ arctanh(1.0/a))
  72. doAssert(phase(a) == 1.1071487177940904)
  73. let t = polar(a)
  74. doAssert(rect(t.r, t.phi) =~ a)
  75. doAssert(rect(1.0, 2.0) =~ complex(-0.4161468365471424, 0.9092974268256817))
  76. doAssert(almostEqual(a, a + complex(1e-16, 1e-16)))
  77. doAssert(almostEqual(a, a + complex(2e-15, 2e-15), unitsInLastPlace = 5))
  78. let
  79. i64: Complex32 = complex(0.0f, 1.0f)
  80. a64: Complex32 = 2.0f*i64 + 1.0.float32
  81. b64: Complex32 = complex(-1.0'f32, -2.0'f32)
  82. doAssert(a64 == a64)
  83. doAssert(a64 == -b64)
  84. doAssert(a64 + b64 =~ 0.0'f32)
  85. doAssert(not(pow(a64, b64) =~ a64))
  86. doAssert(pow(a64, 0.5f) =~ sqrt(a64))
  87. doAssert(pow(a64, 2) =~ complex(-3.0'f32, 4.0'f32))
  88. doAssert(sin(arcsin(b64)) =~ b64)
  89. doAssert(cosh(arccosh(a64)) =~ a64)
  90. doAssert(phase(a64) - 1.107149f < 1e-6)
  91. let t64 = polar(a64)
  92. doAssert(rect(t64.r, t64.phi) =~ a64)
  93. doAssert(rect(1.0f, 2.0f) =~ complex(-0.4161468f, 0.90929742f))
  94. doAssert(sizeof(a64) == 8)
  95. doAssert(sizeof(a) == 16)
  96. doAssert 123.0.im + 456.0 == complex64(456, 123)
  97. let localA = complex(0.1'f32)
  98. doAssert localA.im is float32