How to resolve the algorithm Arithmetic/Complex step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Complex step by step in the Lua programming language

Table of Contents

Problem Statement

A   complex number   is a number which can be written as:

a + b × i

{\displaystyle a+b\times i}

(sometimes shown as:

b + a × i

{\displaystyle b+a\times i}

where

a

{\displaystyle a}

and

b

{\displaystyle b}

are real numbers,   and

i

{\displaystyle i}

is   √ -1

Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part",   where the imaginary part is the number to be multiplied by

i

{\displaystyle i}

.

By definition, the   complex conjugate   of

a + b i

{\displaystyle a+bi}

is

a − b i

{\displaystyle a-bi}

Some languages have complex number libraries available.   If your language does, show the operations.   If your language does not, also show the definition of this type.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic/Complex step by step in the Lua programming language

Source code in the lua programming language

--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to strgs.
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__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)
    if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. 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,
  }
  return operations[index] and operations[index](u)
end,
__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 .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)


  

You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Set step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Java programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the Scala programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the REXX programming language