Arithmetic/Complex: Difference between revisions

Line 994:
__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,
__concat = function(u, v)
strgif = functiontype(u) == "table" then return u.real .. " + " .. u.imag .. "i" end.. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
strg = function(u) return u.real .. " + " .. u.imag .. "i" end
}
return operations[index] and operations[index](u)
Line 1,009 ⟶ 1,012:
local i, j = complex(2, 3), complex(1, 1)
 
print(i.strg .. " + " .. j.strg .. " = " .. (i+j).strg)
print(i.strg .. " - " .. j.strg .. " = " .. (i-j).strg)
print(i.strg .. " * " .. j.strg .. " = " .. (i*j).strg)
print(i.strg .. " / " .. j.strg .. " = " .. (i/j).strg)
print("|" .. i.strg .. "| = " .. math.sqrt(i.norm))
print(i.strg .. "* = " .. i.conj.strg)
</lang>
 
Anonymous user