complex_num v1.1.0 ComplexNum.Cartesian
A simple Complex Number in the form of a + b*i
.
a
and b
are allowed to be any type that implements the Numeric
behaviour.
This means Integer and Float, as well as custom-built data types like Decimal, Ratio, ???.
Do note that certain kinds of operations (especially the conversion of Cartesian <-> Polar) require the calculation of square roots.
Computers are not able to calculate any square root with infinite precision in finite time.
This is exactly the reason that e.g. Decimal and Ratio do not support sqrt
.
Therefore, the only way to manage this, is to explicitly convert a (high- or infinite-precision) data type that does not support square roots
to a data type that does support it (like Floats), in which case precision will be lost.
Summary
Functions
The absolute value of a Cartesian Complex Number is a real part containing the magnitude of the number, and an imaginary part of 0
Adds two Complex Numbers in Cartesian form together
Returns the angle (counter-clockwise in relation to the ‘Real’ axis of the Argand plane) of a Complex number in cartesian form
Returns the Complex Conjugate of a Complex number in Cartesian form
Divides a Complex Number in Cartesian form by another
Extracts the ‘imaginary’ part from a Complex number
Calculates the magnitude of the Cartesian Complex Number. As this is done using Pythagoras, i.e. c = sqrt(a² + b²), this is a lossy operation where (a²+b²) is converted to a float, so the square root can be calculated
Returns the square of the magnitude of the Cartesian Complex Number. Because it is not necessary to calculate a square root, this is a precise operation
Negates the complex number
Multiplies two Complex Numbers in Cartesian form
Creates a new Cartesian Complex Number
Integer power function
Extracts the ‘real’ part from a Complex number
The reciprocal of a Complex number (a + bi) is identical to 1 / (a + bi)
Adds one Complex Numbers in Cartesian form from another
Converts a Complex Number in Cartesian form to a Complex Number in Polar form
Functions
The absolute value of a Cartesian Complex Number is a real part containing the magnitude of the number, and an imaginary part of 0.
This is a lossy operation, as calculating the magnitude (see magnitude/1
) of a Cartesian Complex number is a lossy operation.
Adds two Complex Numbers in Cartesian form together.
This is a precise operation. Note that this function expects both arguments to be Complex numbers in Cartesian form. (optionally, one of the arguments might be an Integer or Float).
If you want to be able to add Complex numbers in Cartesian form and Polar form together,
use ComplexNum.add/2
instead.
Returns the angle (counter-clockwise in relation to the ‘Real’ axis of the Argand plane) of a Complex number in cartesian form.
Note that this is a lossy operation, as the trigonometric function atan2(b, a)
is used,
which is only available for built-in Floats.
Note that when called with 0 + 0i
there are infinitely many solutions,
and thus the result is formally undefined.
By keeping with the convention most practical implementations follow however,
instead of creating an exceptional situation,
the solution angle(0 + 0i) = 0
is returned.
Returns the Complex Conjugate of a Complex number in Cartesian form.
For a + bi
, this is a - bi
.
This is a precise operation.
Divides a Complex Number in Cartesian form by another.
This is a precise operation (but slower than division of numbers in Polar form). Note that this function expects both arguments to be Complex numbers in Cartesian form. (optionally, one of the arguments might be an Integer or Float).
If you want to be able to multiply Complex numbers in Cartesian form and Polar form together,
use ComplexNum.div/2
instead.
Extracts the ‘imaginary’ part from a Complex number.
For a number in the Cartesian form a + bi
, this is b
.
Calculates the magnitude of the Cartesian Complex Number. As this is done using Pythagoras, i.e. c = sqrt(a² + b²), this is a lossy operation where (a²+b²) is converted to a float, so the square root can be calculated.
Returns the square of the magnitude of the Cartesian Complex Number. Because it is not necessary to calculate a square root, this is a precise operation.
Negates the complex number.
This means that both the real and the imaginary part are negated.
Multiplies two Complex Numbers in Cartesian form.
This is a precise operation (but slower than multiplication of numbers in Polar form). Note that this function expects both arguments to be Complex numbers in Cartesian form. (optionally, one of the arguments might be an Integer or Float).
If you want to be able to multiply Complex numbers in Cartesian form and Polar form together,
use ComplexNum.mult/2
instead.
Creates a new Cartesian Complex Number.
real
and imaginary
can be Integer, Float or any custom struct that implements the Numeric behaviour. (defined by The Numbers package)
If a custom Numeric type is used, the other argument is converted to that type automatically.
Integer power function.
The result is calculated using the Exponentiation by Squaring algorithm, which means that it performs log(n) multiplications to calculate (a + bi)^n.
Note that only integers are accepted as exponent, so you cannot calculate roots using this function.
Extracts the ‘real’ part from a Complex number.
For a number in the Cartesian form a + bi
, this is a
.
Adds one Complex Numbers in Cartesian form from another.
This is a precise operation. Note that this function expects both arguments to be Complex numbers in Cartesian form. (optionally, one of the arguments might be an Integer or Float).
If you want to be able to subtract Complex numbers in Cartesian form and Polar form together,
use ComplexNum.sub/2
instead.
Converts a Complex Number in Cartesian form to a Complex Number in Polar form.
This is a lossy operation, as both magnitude/1
and angle/1
need to be called,
which are both lossy operations (requiring the use of a square root and atan2, respectively).