Lua модуль для работы с комплексными числами
Lua модуль для работы с комплексными числами. Например, потребуется для расчета FFT - Fast Fourier Transform
-
-------------------------------------------------------------
-
-- complex.lua — модуль комплексных чисел (OOP + метаметоды)
-
-
complex = {
-
-- Сложение: a + b
-
__add = function(a, b)
-
return complex(a.re + b.re, a.im + b.im)
-
end,
-
-- Вычитание: a - b
-
__sub = function(a, b)
-
return complex(a.re - b.re, a.im - b.im)
-
end,
-
-- Умножение: a * b
-
__mul = function(a, b)
-
return complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
-
end,
-
-- Деление: a / b
-
__div = function(a, b)
-
local denom = b.re * b.re + b.im * b.im
-
return complex((a.re * b.re + a.im * b.im) / denom, (a.im * b.re - a.re * b.im) / denom)
-
end,
-
-- Унарный минус: -a
-
__unm = function(a)
-
return complex(-a.re, -a.im)
-
end,
-
-- Оператор длины (#a): модуль числа
-
__len = function(a)
-
return math.sqrt(a.re ^ 2 + a.im ^ 2)
-
end,
-
-- Преобразование в строку: tostring(a)
-
__tostring = function(a)
-
return string.format("%.4f %+ .4fi", a.re, a.im)
-
end,
-
---------------------------------------------------------
-
-- Методы экземпляра
-
---------------------------------------------------------
-
-
-- Модуль (амплитуда)
-
abs = function(self)
-
return math.sqrt(self.re * self.re + self.im * self.im)
-
end,
-
-- Аргумент (фаза)
-
arg = function(self)
-
return math.atan2(self.im, self.re)
-
end,
-
-- Сопряжённое число
-
conj = function(self)
-
return complex(self.re, -self.im)
-
end,
-
-- Экспонента e^{i?}
-
exp = function(theta)
-
return complex(math.cos(theta), math.sin(theta))
-
end,
-
-- Копия числа
-
clone = function(self)
-
return complex(self.re, self.im)
-
end
-
}
-
-
-------------------------------------------------------------
-
-- Метатаблица: конструктор complex(re, im)
-
-------------------------------------------------------------
-
complex.__index = complex
-
local complex =
-
setmetatable(
-
complex,
-
{
-
__call = function(_, re, im)
-
return setmetatable(
-
{
-
re = re or 0.0,
-
im = im or 0.0
-
},
-
complex
-
)
-
end
-
}
-
)
-
-
-------------------------------------------------------------
-
-
-- local function log(label, value)
-
-- print(string.format("%-18s = %s", label, tostring(value)))
-
-- end
-
-
-----------------------------------------------------------
-
-- local a = complex(2, 3)
-
-- local b = complex(1, -1)
-
-
-- print("=== Complex Numbers Test ===\n")
-
-
-- log("a", a)
-
-- log("b", b)
-
-
-----------------------------------------------------------
-
-- print("\n=== Arithmetic Operations ===")
-
-
-- log("a + b", a + b)
-
-- log("a - b", a - b)
-
-- log("a * b", a * b)
-
-- log("a / b", a / b)
-
-- log("-a", -a)
-
-
-----------------------------------------------------------
-
-- print("\n=== Metamethods ===")
-
-
-- log("#a (magnitude)", #a)
-
-- log("tostring(a)", tostring(a))
-
-
-----------------------------------------------------------
-
-- print("\n=== Instance Methods ===")
-
-
-- log("a:abs()", a:abs())
-
-- log("a:arg()", a:arg())
-
-- log("a:conj()", a:conj())
-
-- log("a:clone()", a:clone())
-
-
-----------------------------------------------------------
-
-- print("\n=== Complex Exponential ===")
-
-
-- local theta = math.pi / 4
-
-- log("exp(i?/4)", complex.exp(theta))
-
