Lua модуль для работы с комплексными числами

Опубликовано в QLua

Lua модуль для работы с комплексными числами. Например, потребуется для расчета FFT -  Fast Fourier Transform


 

  1. -------------------------------------------------------------
  2. -- complex.lua — модуль комплексных чисел (OOP + метаметоды)
  3.  
  4. complex = {
  5. -- Сложение: a + b
  6. __add = function(a, b)
  7.    return complex(a.re + b.re, a.im + b.im)
  8. end,
  9. -- Вычитание: a - b
  10. __sub = function(a, b)
  11.    return complex(a.re - b.re, a.im - b.im)
  12. end,
  13. -- Умножение: a * b
  14. __mul = function(a, b)
  15.    return complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
  16. end,
  17. -- Деление: a / b
  18. __div = function(a, b)
  19. local denom = b.re * b.re + b.im * b.im
  20.    return complex((a.re * b.re + a.im * b.im) / denom, (a.im * b.re - a.re * b.im) / denom)
  21. end,
  22. -- Унарный минус: -a
  23. __unm = function(a)
  24.    return complex(-a.re, -a.im)
  25. end,
  26. -- Оператор длины (#a): модуль числа
  27. __len = function(a)
  28.    return math.sqrt(a.re ^ 2 + a.im ^ 2)
  29. end,
  30. -- Преобразование в строку: tostring(a)
  31. __tostring = function(a)
  32.    return string.format("%.4f %+ .4fi", a.re, a.im)
  33. end,
  34. ---------------------------------------------------------
  35. -- Методы экземпляра
  36. ---------------------------------------------------------
  37.  
  38. -- Модуль (амплитуда)
  39. abs = function(self)
  40.    return math.sqrt(self.re * self.re + self.im * self.im)
  41. end,
  42. -- Аргумент (фаза)
  43. arg = function(self)
  44.    return math.atan2(self.im, self.re)
  45. end,
  46. -- Сопряжённое число
  47. conj = function(self)
  48.    return complex(self.re, -self.im)
  49. end,
  50. -- Экспонента e^{i?}
  51. exp = function(theta)
  52.    return complex(math.cos(theta), math.sin(theta))
  53. end,
  54. -- Копия числа
  55. clone = function(self)
  56.    return complex(self.re, self.im)
  57. end
  58. }
  59.  
  60. -------------------------------------------------------------
  61. -- Метатаблица: конструктор complex(re, im)
  62. -------------------------------------------------------------
  63. complex.__index = complex
  64. local complex =
  65. setmetatable(
  66. complex,
  67. {
  68. __call = function(_, re, im)
  69. return setmetatable(
  70. {
  71. re = re or 0.0,
  72. im = im or 0.0
  73. },
  74. complex
  75. )
  76. end
  77. }
  78. )
  79.  
  80. -------------------------------------------------------------
  81.  
  82. -- local function log(label, value)
  83. -- print(string.format("%-18s = %s", label, tostring(value)))
  84. -- end
  85.  
  86. -----------------------------------------------------------
  87. -- local a = complex(2, 3)
  88. -- local b = complex(1, -1)
  89.  
  90. -- print("=== Complex Numbers Test ===\n")
  91.  
  92. -- log("a", a)
  93. -- log("b", b)
  94.  
  95. -----------------------------------------------------------
  96. -- print("\n=== Arithmetic Operations ===")
  97.  
  98. -- log("a + b", a + b)
  99. -- log("a - b", a - b)
  100. -- log("a * b", a * b)
  101. -- log("a / b", a / b)
  102. -- log("-a", -a)
  103.  
  104. -----------------------------------------------------------
  105. -- print("\n=== Metamethods ===")
  106.  
  107. -- log("#a (magnitude)", #a)
  108. -- log("tostring(a)", tostring(a))
  109.  
  110. -----------------------------------------------------------
  111. -- print("\n=== Instance Methods ===")
  112.  
  113. -- log("a:abs()", a:abs())
  114. -- log("a:arg()", a:arg())
  115. -- log("a:conj()", a:conj())
  116. -- log("a:clone()", a:clone())
  117.  
  118. -----------------------------------------------------------
  119. -- print("\n=== Complex Exponential ===")
  120.  
  121. -- local theta = math.pi / 4
  122. -- log("exp(i?/4)", complex.exp(theta))
  123.  

Архив QLua