Expand/Shrink

complex

The file builtins\complex.e (not an autoinclude) contains a basic implementation of complex number arithmetic.

A complex number contains a real and imaginary part, which simplify some calculations. For a proper explanation see https://en.wikipedia.org/wiki/Complex_number.

These routines are fully supported by pwa/p2js.

Example:

include complex.e

--constant complex i = complex_new(0,1)  -- NB: 'i' is not provided as standard
--constant complex i = {0,1}             --     (equivalent, if you prefer)

complex a = complex_new(5,3),
        b = complex_new(4,-3),
        x = complex_add(a,b),
        y = complex_mul(a,b),
        z = complex_div(y,b)
string sa = complex_sprint(a),
       sb = complex_sprint(b),
       sx = complex_sprint(x),
       sy = complex_sprint(y),
       sz = complex_sprint(z)
printf(1,"(%s) + (%s) = %s\n",{sa,sb,sx})   -- (5+3i) + (4-3i) = 9
printf(1,"(%s) * (%s) = %s\n",{sa,sb,sy})   -- (5+3i) * (4-3i) = 29-3i
printf(1,"(%s) / (%s) = %s\n",{sy,sb,sz})   -- (29-3i) / (4-3i) = 5+3i

bool res = 
complex(object o) - the standard complex type.
The private type complexn allows the following routines to accept either complex or atom arguments.
An atom argument is usually immediately converted to {a,0} [ie an imaginary part of 0].
complex res = 
complex_new(atom real, imag) - the standard constructor, simply returns {real,imag}.
atom res = 
complex_real(complexn a) - returns the real part.
atom res = 
complex_imag(complexn a) - returns the imaginary part.
atom res = 
complex_norm(complexn a) - returns the norm - which is a2 + b2 in this library, agreeing with wikipedia.
atom res = 
complex_abs(complexn a) - returns the magnitude, aka modulus, ie the square root of the complex_norm().
complex res = 
complex_add(complexn a, b) - returns a + b.
complex res = 
complex_neg(complexn a) - returns - a.
complex res = 
complex_sub(complexn a, b) - returns a - b.
complex res = 
complex_conjugate(complexn a) - returns the conjugate, ie {complex_real(a),-complex_imag(a)}.
complex res = 
complex_mul(complexn a, b) - returns a * b.
complex res = 
complex_inv(complexn a) - returns the reciprocal.
complex res = 
complex_div(complexn a, b) - returns a / b.
atom res = 
complex_arg(complexn a) - returns the derived polar angle for polar form.
atom res = 
complex_theta(complexn a) - as complex_arg, but normalized to 0 <= theta < 2*PI.
atom res = 
complex_rho(complexn a) - returns the derived polar magnitude rho for polar form. (==complex_abs)
complex res = 
from_polar(atom rho, theta) - returns Euler’s polar-to-Cartesian complex conversion.
complex res = 
with_theta(complexn this, atom theta) - creates new complex with same magnitude but different angle.
complex res = 
with_rho(complexn this, atom rho) - creates new complex with same angle but different magnitude.
complex res = 
complex_log(complexn a) - returns the logarithm of a.
complex_ln() is a simple alias of this function.
complex res = 
complex_exp(complexn a) - returns the exponent of a.
complex res = 
complex_power(complexn a, p) - returns a raised to the power p.
complex res = 
complex_sqrt(complexn a) - returns the square root of a.
complex res = 
complex_sinh(complexn a) - returns the sinh of a.
complex res = 
complex_cosh(complexn a) - returns the cosh of a.
complex res = 
complex_sin(complexn a) - returns the sin of a.
complex res = 
complex_cos(complexn a) - returns the cos of a.
complex res = 
complex_round(complexn a, atom inverted_precision=1) - as per round().
string res = 
complex_sprint(complexn a, bool both=false) - returns a string representation of a.
When both is false (the default), can return say "5", "3i" or "0", when true returns "5+0i", "0+3i" and "0+0i" for the same inputs.