Skip to content

Instantly share code, notes, and snippets.

@MathyFurret
Last active May 15, 2018 18:06
Show Gist options
  • Save MathyFurret/4cf7b6094ab116bddc81 to your computer and use it in GitHub Desktop.
Save MathyFurret/4cf7b6094ab116bddc81 to your computer and use it in GitHub Desktop.
Complex = {}
--constructors
function Complex.new(re, im)
assert(type(re) == 'number' and type(im) == 'number', "Both arguments passed to Complex.new must be numbers")
return setmetatable({re = re, im = im}, Complex)
end
function Complex.polar(r, theta)
assert(type(r) == 'number' and type(theta) == 'number', "Both arguments passed to Complex.new must be numbers")
return Complex.new(r*math.cos(theta), r*math.sin(theta))
end
--metamethods
function Complex.__tostring(tbl)
if tbl.re == 0 then
return tostring(tbl.im) .. "i"
end
if tbl.im < 0 then
return tostring(tbl.re) .. "-" .. tostring(-tbl.im) .. "i"
else
return tostring(tbl.re) .. "+" .. tostring(tbl.im) .. "i"
end
end
function Complex.__unm(tbl)
return Complex.new(-tbl.re, -tbl.im)
end
function Complex.__add(tbl1, tbl2)
if getmetatable(tbl1) == Complex then
if getmetatable(tbl2) == Complex then
return Complex.new(tbl1.re + tbl2.re, tbl1.im + tbl2.im)
elseif type(tbl2) == 'number' then
return Complex.new(tbl1.re + tbl2, tbl1.im)
else
error("Attempt to combine a Complex with an invalid type")
end
else
if type(tbl1) == 'number' then
return Complex.new(tbl1 + tbl2.re, tbl2.im)
else
error("Attempt to combine a Complex with an invalid type")
end
end
end
function Complex.__sub(tbl1, tbl2)
return tbl1 + (-tbl2)
end
function Complex.__mul(tbl1, tbl2)
if getmetatable(tbl1) == Complex then
if getmetatable(tbl2) == Complex then
return Complex.new(tbl1.re*tbl2.re - tbl1.im*tbl2.im, tbl1.re*tbl2.im + tbl2.re*tbl1.im)
elseif type(tbl2) == 'number' then
return Complex.new(tbl1.re * tbl2, tbl1.im * tbl2)
else
error("Attempt to combine a Complex with an invalid type")
end
else
if type(tbl1) == 'number' then
return Complex.new(tbl1 * tbl2.re, tbl1 * tbl2.im)
else
error("Attempt to combine a Complex with an invalid type")
end
end
end
function Complex.__div(tbl1, tbl2)
if getmetatable(tbl1) == Complex then
if getmetatable(tbl2) == Complex then
return (tbl1 * tbl2:conj()) / tbl2:times_conj()
elseif type(tbl2) == 'number' then
return Complex.new(tbl1.re / tbl2, tbl1.im / tbl2)
else
error("Attempt to combine a Complex with an invalid type")
end
else
if type(tbl1) == 'number' then
return (tbl1 / tbl2:times_conj()) * tbl2:conj()
else
error("Attempt to combine a Complex with an invalid type")
end
end
end
function Complex.__pow(tbl1, tbl2)
--do later
end
function Complex.__eq(tbl1, tbl2)
return tbl1.re == tbl2.re and tbl1.im == tbl2.im
end
Complex.__index = Complex
--methods
function Complex.conj(self)
return Complex.new(self.re, -self.im)
end
function Complex.times_conj(self)
return self.re ^ 2 + self.im ^ 2
end
function Complex.to_polar(self)
return (self.re^2 + self.im^2)^.5, math.atan2(self.re, self.im)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment