Metatable Help on Complex Number Class

I am currently writing a large module, called Math +. The idea is to create an alternative, more useful, math function table. It will include all the standard math functions, such as math.sin, etc.

I have implemented a system in which users can create complex numbers. It is OOP and its own number “class.” I have successfully implemented addition, subtraction, multiplication, and division of said complex numbers. These all, of course, use metamethods, such as __add, etc. However, I wish to implement modulus and exponentiation, which appear to be more sophisticated than the other operators that I made very easily.

I have tried searching the web for solutions, but I have yet to find an understandable (for me) way to achieve my goal. Essentially I want operators % and ^ to work on the complex numbers.

local n = Complex.new(1,3) -- this would be 1 + 3i

print(n ^ 2) -- should return -8 + 6i
print(n ^ n) -- should return -0.000705958477 - 0.0745795029i
print(n % 2) -- should return 1 + i
print(n % Complex.new(0,4)) -- -3 + 3i

These are just a few examples of the idea behind these operators. I do realize I could use a for loop for a complex number raised to an integer power. However, ideally I could include decimals as powers as well.

I am not in any way asking for someone to write code for me. If anyone knows how I could accomplish this, please provide an explanation, or even a link if you have time to research. Code written for me (if written and used) would be credited in the module forever, which I plan to release publicly once I believe it is sufficiently completed.

Edit: I am heading off for the night. I will respond to any responses in the morning. Thanks!

Thank you for reading. Please help you are able to. Have a great day!

  • Galactiq
1 Like

I mean couldnt you do like complexnumber.mod(anothercomplexnumber) or something?

I believe there are also metamethods for modulus and exponent operators:

Yep, looking at the metamethods, it should be exactly the same process as the _add metamethod not sure where the added complexity for implementing is other than the actual math operation steps required.

__add(table, value)
__mod(table, value) -- % modulo
__pow(table, value) -- ^ operator

Not sure why anyone would ever need to use % modulo on a complex number, searching online will get you some weird theoretical math stuff.

Anyways, someone has already made a complex number class in lua you can just look there for answers.

http://lua-users.org/wiki/ComplexNumbers

Here is a script portion of the pow function:

-- complex.pow( cx, num )
-- get the power of a complex number
function complex.pow( cx,num )
   if math.floor( num ) == num then
      if num < 0 then
         local val = cx[1]^2 + cx[2]^2
         cx = { cx[1]/val,-cx[2]/val }
         num = -num
      end
      local real,imag = cx[1],cx[2]
      for i = 2,num do
         real,imag = real*cx[1] - imag*cx[2],real*cx[2] + imag*cx[1]
      end
      return setmetatable( { real,imag }, complex_meta )
   end
   -- we calculate the polar complex number now
   -- since then we have the versatility to calc any potenz of the complex number
   -- then we convert it back to a carthesic complex number, we loose precision here
   local length,phi = math.sqrt( cx[1]^2 + cx[2]^2 )^num, math.atan2( cx[2], cx[1] )*num
   return setmetatable( { length * math.cos( phi ), length * math.sin( phi ) }, complex_meta )
end
3 Likes

Thank you for your response. It was not the implementation but rather the mathematical portion, which you provided. Once again, thank you. The implementation of all the operations is simply to have a complete class.

Edit: This link you provided has given quite a deal of help. Very useful.