complex v0.3.0 Complex
Complex is a library for types and mathematical functions for complex numbers.
Each complex number is represented as a structure holding the real and imaginary part. There are functions for creation and manipulation of them. Unfortunately since there is no operator overloading in Elixir the math functions (add, subtract, etc.) are implemented as add/2, sub/2, etc.
Examples
iex> Complex.new(3, 4)
%Complex{im: 4, re: 3}
iex> Complex.imag()
%Complex{im: 1.0, re: 0.0}
Link to this section Summary
Functions
Returns the magnitude (length) of the provided complex number.
Returns the square of the magnitude of the provided complex number.
Returns a new complex that is the inverse cosine (i.e., arccosine) of the provided parameter.
Returns a new complex that is the inverse hyperbolic cosine (i.e., arccosh) of the provided parameter.
Returns a new complex that is the inverse cotangent (i.e., arccotangent) of the provided parameter.
Returns a new complex that is the inverse hyperbolic cotangent (i.e., arccoth) of the provided parameter.
Returns a new complex that is the inverse cosecant (i.e., arccosecant) of the provided parameter.
Returns a new complex that is the inverse hyperbolic cosecant (i.e., arccsch) of the provided parameter.
Returns a new complex that is the sum of the provided complex numbers. Also supports a mix of complex and number.
Returns a new complex that is the inverse secant (i.e., arcsecant) of the provided parameter.
Returns a new complex that is the inverse hyperbolic secant (i.e., arcsech) of the provided parameter.
Returns a new complex that is the inverse sine (i.e., arcsine) of the provided parameter.
Returns a new complex that is the inverse hyperbolic sine (i.e., arcsinh) of the provided parameter.
Returns a new complex that is the inverse tangent (i.e., arctangent) of the provided parameter.
Returns a new complex that is the inverse hyperbolic tangent (i.e., arctanh) of the provided parameter.
Returns a new complex that is the complex conjugate of the provided complex number.
Returns a new complex that is the cosine of the provided parameter.
Returns a new complex that is the hyperbolic cosine of the provided parameter.
Returns a new complex that is the cotangent of the provided parameter.
Returns a new complex that is the hyperbolic cotangent of the provided parameter.
Returns a new complex that is the cosecant of the provided parameter.
Returns a new complex that is the hyperbolic cosecant of the provided parameter.
Returns a new complex that is the ratio (division) of the provided complex numbers.
Returns a new complex that is the complex exponential of the provided complex number. That is, e raised to the power z.
Returns a new complex number described by the supplied polar coordinates.
That is, the complex (real and imaginary) will have radius (magnitude) r
and angle (phase) phi.
Returns the polar coordinates of the supplied complex. That is, the returned tuple {r,phi} is the magnitude and phase (in radians) of z.
Returns a new complex representing the pure imaginary number sqrt(-1).
Returns a new complex that is the complex natural log of the provided complex number. That is, log base e of z.
Returns a new complex that is the complex log base 10 of the provided complex number.
Returns a new complex that is the complex log base 2 of the provided complex number.
Returns a new complex that is the product of the provided complex numbers. Also supports a mix of complex and number.
Returns a new complex that is the "negation" of the provided parameter. That is, the real and imaginary parts are negated.
Returns a new complex with specified real and imaginary components. The imaginary part defaults to zero so a "real" number can be created with new/1
Parses a complex number from a string. The values of the real and imaginary parts must be represented by a float, including decimal and at least one trailing digit (e.g. 1.2, 0.4).
Returns the phase angle of the supplied complex, in radians.
Returns a new complex that is the provided parameter a raised to the complex power b.
Returns a new complex that is the secant of the provided parameter.
Returns a new complex that is the hyperbolic secant of the provided parameter.
Returns a new complex that is the sine of the provided parameter.
Returns a new complex that is the hyperbolic sine of the provided parameter.
Returns a new complex that is the complex square root of the provided complex number.
Returns a new complex that is the square of the provided complex number.
Returns a new complex that is the difference of the provided complex numbers. Also supports a mix of complex and number.
Returns a new complex that is the tangent of the provided parameter.
Returns a new complex that is the hyperbolic tangent of the provided parameter.
Link to this section Types
complex()
Specs
General type for complex numbers
Link to this section Functions
abs(complex)
Specs
Returns the magnitude (length) of the provided complex number.
See also
Examples
iex> Complex.abs( Complex.fromPolar(1, :math.pi/2) )
1.0
abs_squared(complex)
Specs
Returns the square of the magnitude of the provided complex number.
The square of the magnitude is faster to compute---no square roots!
See also
Examples
iex> Complex.abs_squared( Complex.fromPolar(1, :math.pi/2) )
1.0
iex> Complex.abs_squared( Complex.fromPolar(2, :math.pi/2) )
4.0
acos(z)
Specs
Returns a new complex that is the inverse cosine (i.e., arccosine) of the provided parameter.
See also
Examples
iex> Complex.acos( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.3169578969248164, re: -3.141592653589793}
iex> Complex.cos( Complex.acos(Complex.new(2,3)) )
%Complex{im: 3.0, re: 2.0000000000000004}
acosh(z)
Specs
Returns a new complex that is the inverse hyperbolic cosine (i.e., arccosh) of the provided parameter.
See also
Examples
iex> Complex.acosh( Complex.fromPolar(2,:math.pi) )
%Complex{im: -3.141592653589793, re: -1.3169578969248164}
iex> Complex.cosh( Complex.acosh(Complex.new(2,3)) )
%Complex{im: 3.0, re: 2.0}
acot(z)
Specs
Returns a new complex that is the inverse cotangent (i.e., arccotangent) of the provided parameter.
See also
Examples
iex> Complex.acot( Complex.fromPolar(2,:math.pi) )
%Complex{im: -9.71445146547012e-17, re: -0.46364760900080615}
iex> Complex.cot( Complex.acot(Complex.new(2,3)) )
%Complex{im: 2.9999999999999996, re: 1.9999999999999991}
acoth(z)
Specs
Returns a new complex that is the inverse hyperbolic cotangent (i.e., arccoth) of the provided parameter.
See also
Examples
iex> Complex.acoth( Complex.fromPolar(2,:math.pi) )
%Complex{im: -8.164311994315688e-17, re: -0.5493061443340548}
iex> Complex.coth( Complex.acoth(Complex.new(2,3)) )
%Complex{im: 2.999999999999998, re: 2.000000000000001}
acsc(z)
Specs
Returns a new complex that is the inverse cosecant (i.e., arccosecant) of the provided parameter.
See also
Examples
iex> Complex.acsc( Complex.fromPolar(2,:math.pi) )
%Complex{im: 0.0, re: -0.5235987755982988}
iex> Complex.csc( Complex.acsc(Complex.new(2,3)) )
%Complex{im: 2.9999999999999996, re: 1.9999999999999993}
acsch(z)
Specs
Returns a new complex that is the inverse hyperbolic cosecant (i.e., arccsch) of the provided parameter.
See also
Examples
iex> Complex.acsch( Complex.fromPolar(2,:math.pi) )
%Complex{im: -5.4767869826420256e-17, re: -0.48121182505960336}
iex> Complex.csch( Complex.acsch(Complex.new(2,3)) )
%Complex{im: 3.0000000000000018, re: 1.9999999999999982}
add(left, right)
Specs
add(complex(), complex()) :: complex()
add(number(), complex()) :: complex()
add(complex(), number()) :: complex()
Returns a new complex that is the sum of the provided complex numbers. Also supports a mix of complex and number.
#### See also div/2, mult/2, sub/2
#### Examples
iex> Complex.add( Complex.fromPolar(1, :math.pi/2), Complex.fromPolar(1, :math.pi/2) )
%Complex{im: 2.0, re: 1.2246467991473532e-16}
iex> Complex.add( Complex.new(4, 4), 1 )
%Complex{im: 4, re: 5}
iex> Complex.add( 2, Complex.new(4, 3) )
%Complex{im: 3, re: 6}
iex> Complex.add( 2, 3 )
%Complex{im: 0, re: 5}
asec(z)
Specs
Returns a new complex that is the inverse secant (i.e., arcsecant) of the provided parameter.
See also
Examples
iex> Complex.asec( Complex.fromPolar(2,:math.pi) )
%Complex{im: 0.0, re: 2.0943951023931957}
iex> Complex.sec( Complex.asec(Complex.new(2,3)) )
%Complex{im: 2.9999999999999987, re: 1.9999999999999987}
asech(z)
Specs
Returns a new complex that is the inverse hyperbolic secant (i.e., arcsech) of the provided parameter.
See also
Examples
iex> Complex.asech( Complex.fromPolar(2,:math.pi) )
%Complex{im: -2.0943951023931953, re: 0.0}
iex> Complex.sech( Complex.asech(Complex.new(2,3)) )
%Complex{im: 2.999999999999999, re: 2.0}
asin(z)
Specs
Returns a new complex that is the inverse sine (i.e., arcsine) of the provided parameter.
See also
Examples
iex> Complex.asin( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.3169578969248164, re: -1.5707963267948963}
iex> Complex.sin( Complex.asin(Complex.new(2,3)) )
%Complex{im: 3.000000000000001, re: 1.9999999999999991}
asinh(z)
Specs
Returns a new complex that is the inverse hyperbolic sine (i.e., arcsinh) of the provided parameter.
See also
Examples
iex> Complex.asinh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.0953573965284052e-16, re: -1.4436354751788099}
iex> Complex.sinh( Complex.asinh(Complex.new(2,3)) )
%Complex{im: 3.0, re: 2.000000000000001}
atan(z)
Specs
Returns a new complex that is the inverse tangent (i.e., arctangent) of the provided parameter.
See also
Examples
iex> Complex.atan( Complex.fromPolar(2,:math.pi) )
%Complex{im: 0.0, re: -1.1071487177940904}
iex> Complex.tan( Complex.atan(Complex.new(2,3)) )
%Complex{im: 3.0, re: 2.0}
atanh(z)
Specs
Returns a new complex that is the inverse hyperbolic tangent (i.e., arctanh) of the provided parameter.
See also
Examples
iex> Complex.atanh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.5707963267948966, re: -0.5493061443340549}
iex> Complex.tanh( Complex.atanh(Complex.new(2,3)) )
%Complex{im: 2.999999999999999, re: 1.9999999999999987}
conjugate(complex)
Specs
Returns a new complex that is the complex conjugate of the provided complex number.
See also
Examples
iex> Complex.conjugate( Complex.new(1,2) )
%Complex{im: -2, re: 1}
cos(z)
Specs
Returns a new complex that is the cosine of the provided parameter.
See also
Examples
iex> Complex.cos( Complex.fromPolar(2,:math.pi) )
%Complex{im: 2.2271363664699914e-16, re: -0.4161468365471424}
cosh(z)
Specs
Returns a new complex that is the hyperbolic cosine of the provided parameter.
See also
Examples
iex> Complex.cosh( Complex.fromPolar(2,:math.pi) )
%Complex{im: -8.883245978848233e-16, re: 3.7621956910836314}
cot(z)
Specs
Returns a new complex that is the cotangent of the provided parameter.
See also
Examples
iex> Complex.cot( Complex.fromPolar(2,:math.pi) )
%Complex{im: -2.9622992129532336e-16, re: 0.45765755436028577}
coth(z)
Specs
Returns a new complex that is the hyperbolic cotangent of the provided parameter.
See also
Examples
iex> Complex.coth( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.8619978115303632e-17, re: -1.037314720727548}
csc(z)
Specs
Returns a new complex that is the cosecant of the provided parameter.
See also
Examples
iex> Complex.csc( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.2327514463765779e-16, re: -1.0997501702946164}
csch(z)
Specs
Returns a new complex that is the hyperbolic cosecant of the provided parameter.
See also
Examples
iex> Complex.csch( Complex.fromPolar(2,:math.pi) )
%Complex{im: -7.00520014334671e-17, re: -0.2757205647717832}
div(complex1, complex2)
Specs
Returns a new complex that is the ratio (division) of the provided complex numbers.
See also
Examples
iex> Complex.div( Complex.fromPolar(1, :math.pi/2), Complex.fromPolar(1, :math.pi/2) )
%Complex{im: 0.0, re: 1.0}
exp(z)
Specs
Returns a new complex that is the complex exponential of the provided complex number. That is, e raised to the power z.
See also
Examples
iex> Complex.exp( Complex.fromPolar(2,:math.pi) )
%Complex{im: 3.3147584285483636e-17, re: 0.1353352832366127}
fromPolar(r, phi)
Specs
Returns a new complex number described by the supplied polar coordinates.
That is, the complex (real and imaginary) will have radius (magnitude) r
and angle (phase) phi.
See also
Examples
iex> Complex.fromPolar(1, :math.pi/2)
%Complex{im: 1.0, re: 6.123233995736766e-17}
getPolar(z)
Specs
Returns the polar coordinates of the supplied complex. That is, the returned tuple {r,phi} is the magnitude and phase (in radians) of z.
See also
Examples
iex> Complex.getPolar( Complex.fromPolar(1,:math.pi/2) )
{1.0, 1.5707963267948966}
imag()
Specs
imag() :: complex()
Returns a new complex representing the pure imaginary number sqrt(-1).
See also
Examples
iex> Complex.imag()
%Complex{im: 1.0, re: 0.0}
ln(z)
Specs
Returns a new complex that is the complex natural log of the provided complex number. That is, log base e of z.
See also
Examples
iex> Complex.ln( Complex.fromPolar(2,:math.pi) )
%Complex{im: 3.141592653589793, re: 0.6931471805599453}
log10(z)
Specs
Returns a new complex that is the complex log base 10 of the provided complex number.
See also
Examples
iex> Complex.log10( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.3643763538418412, re: 0.30102999566398114}
log2(z)
Specs
Returns a new complex that is the complex log base 2 of the provided complex number.
See also
Examples
iex> Complex.log2( Complex.fromPolar(2,:math.pi) )
%Complex{im: 4.532360141827194, re: 1.0}
mult(left, right)
Specs
mult(complex(), complex()) :: complex()
mult(number(), complex()) :: complex()
mult(complex(), number()) :: complex()
Returns a new complex that is the product of the provided complex numbers. Also supports a mix of complex and number.
See also
Examples
iex> Complex.mult( Complex.new(1,2), Complex.new(3,4) )
%Complex{im: 10, re: -5}
iex> Complex.mult( Complex.imag(), Complex.imag() )
%Complex{im: 0.0, re: -1.0}
iex> Complex.mult(Complex.new(1, 2), 3 )
%Complex{im: 6, re: 3}
iex> Complex.mult( 3, Complex.new(1, 2) )
%Complex{im: 6, re: 3}
neg(z)
Specs
Returns a new complex that is the "negation" of the provided parameter. That is, the real and imaginary parts are negated.
See also
Examples
iex> Complex.neg( Complex.new(3,5) )
%Complex{im: -5, re: -3}
new(re, im \\ 0)
Specs
Returns a new complex with specified real and imaginary components. The imaginary part defaults to zero so a "real" number can be created with new/1
See also
Examples
iex> Complex.new(3, 4)
%Complex{im: 4, re: 3}
iex> Complex.new(2)
%Complex{im: 0, re: 2}
parse(str)
Specs
Parses a complex number from a string. The values of the real and imaginary parts must be represented by a float, including decimal and at least one trailing digit (e.g. 1.2, 0.4).
See also
Examples
iex> Complex.parse("1.1+2.2i")
%Complex{im: 2.2, re: 1.1}
phase(z)
Specs
Returns the phase angle of the supplied complex, in radians.
See also
Examples
iex> Complex.phase( Complex.fromPolar(1,:math.pi/2) )
1.5707963267948966
pow(x, y)
Specs
Returns a new complex that is the provided parameter a raised to the complex power b.
See also
Examples
iex> Complex.pow( Complex.fromPolar(2,:math.pi), Complex.imag() )
%Complex{im: 0.027612020368333014, re: 0.03324182700885666}
sec(z)
Specs
Returns a new complex that is the secant of the provided parameter.
See also
Examples
iex> Complex.sec( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.2860374461837126e-15, re: -2.402997961722381}
sech(z)
Specs
Returns a new complex that is the hyperbolic secant of the provided parameter.
See also
Examples
iex> Complex.sech( Complex.fromPolar(2,:math.pi) )
%Complex{im: 6.27608655779184e-17, re: 0.2658022288340797}
sin(z)
Specs
Returns a new complex that is the sine of the provided parameter.
See also
Examples
iex> Complex.sin( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.0192657827055095e-16, re: -0.9092974268256817}
sinh(z)
Specs
Returns a new complex that is the hyperbolic sine of the provided parameter.
See also
Examples
iex> Complex.sinh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 9.214721821703068e-16, re: -3.626860407847019}
sqrt(z)
Specs
Returns a new complex that is the complex square root of the provided complex number.
See also
Examples
iex> Complex.sqrt( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.4142135623730951, re: 8.659560562354933e-17}
square(z)
Specs
Returns a new complex that is the square of the provided complex number.
See also
Examples
iex> Complex.square( Complex.new(2.0, 0.0) )
%Complex{im: 0.0, re: 4.0}
iex> Complex.square( Complex.imag() )
%Complex{im: 0.0, re: -1.0}
sub(left, right)
Specs
sub(complex(), complex()) :: complex()
sub(number(), complex()) :: complex()
sub(complex(), number()) :: complex()
Returns a new complex that is the difference of the provided complex numbers. Also supports a mix of complex and number.
#### See also add/2, div/2, mult/2
#### Examples
iex> Complex.sub( Complex.new(1,2), Complex.new(3,4) )
%Complex{im: -2, re: 0-2}
iex> Complex.sub( Complex.new(1, 2), 3 )
%Complex{im: 2, re: -2}
iex> Complex.sub( 10, Complex.new(1, 2) )
%Complex{im: -2, re: 9}
tan(z)
Specs
Returns a new complex that is the tangent of the provided parameter.
See also
Examples
iex> Complex.tan( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.4143199004457917e-15, re: 2.185039863261519}
tanh(z)
Specs
Returns a new complex that is the hyperbolic tangent of the provided parameter.
See also
Examples
iex> Complex.tanh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.7304461302709572e-17, re: -0.964027580075817}