Arithmetic/Complex: Difference between revisions
Content added Content deleted
Line 994: | Line 994: | ||
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end, |
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end, |
||
__unm = function(u) return complex(-u.real, -u.imag) end, |
__unm = function(u) return complex(-u.real, -u.imag) end, |
||
__concat = function(u, v) |
|||
⚫ | |||
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i" |
|||
end end, |
|||
__index = function(u, index) |
__index = function(u, index) |
||
local operations = { |
local operations = { |
||
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end, |
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end, |
||
conj = function(u) return complex(u.real, -u.imag) end, |
conj = function(u) return complex(u.real, -u.imag) end, |
||
⚫ | |||
} |
} |
||
return operations[index] and operations[index](u) |
return operations[index] and operations[index](u) |
||
Line 1,009: | Line 1,012: | ||
local i, j = complex(2, 3), complex(1, 1) |
local i, j = complex(2, 3), complex(1, 1) |
||
print(i |
print(i .. " + " .. j .. " = " .. (i+j)) |
||
print(i |
print(i .. " - " .. j .. " = " .. (i-j)) |
||
print(i |
print(i .. " * " .. j .. " = " .. (i*j)) |
||
print(i |
print(i .. " / " .. j .. " = " .. (i/j)) |
||
print("|" .. i |
print("|" .. i .. "| = " .. math.sqrt(i.norm)) |
||
print(i |
print(i .. "* = " .. i.conj) |
||
</lang> |
</lang> |
||