Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/LUA/complex.lua.html -- http://angg.twu.net/LUA/complex.lua -- (find-angg "LUA/complex.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/complex.lua")) Complex = Class { type = "Complex", new = function (a, b) return Complex {re=a, im=b} end, from = function (o) if type(o) == "number" then return Complex.new(o, 0) end if otype(o) == "Complex" then return o end error("Can't convert to complex") end, -- __add = function (z, w) z, w = Complex.from(z), Complex.from(w) local a,b, c,d = z.re,z.im, w.re,w.im return Complex.new(a+c, b+d) end, __mul = function (z, w) z, w = Complex.from(z), Complex.from(w) local a,b, c,d = z.re,z.im, w.re,w.im return Complex.new(a*c-b*d, a*d+b*c) end, __unm = function (z) return z*-1 end, __sub = function (z, w) return z+(-w) end, __tostring = function (o) return o:tostring() end, -- __index = { tostring = function (o) if o.im == 0 then return pformat("%s", o.re) end if o.re == 0 then return pformat("%si", o.im) end local optplus = (o.im > 0) and "+" or "" return pformat("%s%s%si", o.re, optplus, o.im) end, }, } --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "complex.lua" z = Complex.new(2,3) PP(z) = z:tostring() = z = z*z = z*z*z = z*z*z*z --]] -- Local Variables: -- coding: utf-8-unix -- End: