Anonymous user
Arithmetic/Complex: Difference between revisions
→{{header|Lua}}
Line 984:
}</lang>
<lang lua>
--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
Line 999 ⟶ 998:
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
Line 1,005 ⟶ 1,004:
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i.
print(i.
print(i.
print(i.
print("|" .. i.
print(i.
</lang>
|