complex v0.2.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 n 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}

Summary

Types

General type for complex numbers

Functions

Returns a new complex that is the magnitude (length)) 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

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 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

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

Returns the phase angle of the supplied complex

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 difference of the provided complex numbers

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

Types

complex :: %Complex{re: number, im: number}

General type for complex numbers

Functions

abs(complex)

Specs

abs(complex) :: number

Returns a new complex that is the magnitude (length)) of the provided complex number.

See also

new/2, phase/1

Examples

iex> Complex.abs( Complex.fromPolar(1, :math.pi/2) )
1.0
acos(complex)

Specs

acos(complex) :: complex

Returns a new complex that is the inverse cosine (i.e., arccosine) of the provided parameter.

See also

cos/1

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(complex)

Specs

acosh(complex) :: complex

Returns a new complex that is the inverse hyperbolic cosine (i.e., arccosh) of the provided parameter.

See also

cosh/1

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(complex)

Specs

acot(complex) :: complex

Returns a new complex that is the inverse cotangent (i.e., arccotangent) of the provided parameter.

See also

cot/1

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(complex)

Specs

acoth(complex) :: complex

Returns a new complex that is the inverse hyperbolic cotangent (i.e., arccoth) of the provided parameter.

See also

coth/1

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.999999999999999, re: 2.0}
acsc(complex)

Specs

acsc(complex) :: complex

Returns a new complex that is the inverse cosecant (i.e., arccosecant) of the provided parameter.

See also

sec/1

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(complex)

Specs

acsch(complex) :: complex

Returns a new complex that is the inverse hyperbolic cosecant (i.e., arccsch) of the provided parameter.

See also

csch/1

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(complex1, complex2)

Specs

Returns a new complex that is the sum of the provided complex numbers.

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}
asec(complex)

Specs

asec(complex) :: complex

Returns a new complex that is the inverse secant (i.e., arcsecant) of the provided parameter.

See also

sec/1

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.9999999999999984}
asech(complex)

Specs

asech(complex) :: complex

Returns a new complex that is the inverse hyperbolic secant (i.e., arcsech) of the provided parameter.

See also

sech/1

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(complex)

Specs

asin(complex) :: complex

Returns a new complex that is the inverse sine (i.e., arcsine) of the provided parameter.

See also

sin/1

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(complex)

Specs

asinh(complex) :: complex

Returns a new complex that is the inverse hyperbolic sine (i.e., arcsinh) of the provided parameter.

See also

sinh/1

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(complex)

Specs

atan(complex) :: complex

Returns a new complex that is the inverse tangent (i.e., arctangent) of the provided parameter.

See also

tan/1

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(complex)

Specs

atanh(complex) :: complex

Returns a new complex that is the inverse hyperbolic tangent (i.e., arctanh) of the provided parameter.

See also

tanh/1

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

conjugate(complex) :: complex

Returns a new complex that is the complex conjugate of the provided complex number.

See also

abs/2, phase/1

Examples

iex> Complex.conjugate( Complex.new(1,2) )
%Complex{im: -2, re: 1}
cos(complex)

Specs

cos(complex) :: complex

Returns a new complex that is the cosine of the provided parameter.

See also

sin/1, tan/1

Examples

iex> Complex.cos( Complex.fromPolar(2,:math.pi) )
%Complex{im: 2.2271363664699914e-16, re: -0.4161468365471424}
cosh(complex)

Specs

cosh(complex) :: complex

Returns a new complex that is the hyperbolic cosine of the provided parameter.

See also

sinh/1, tanh/1

Examples

iex> Complex.cosh( Complex.fromPolar(2,:math.pi) )
%Complex{im: -8.883245978848233e-16, re: 3.7621956910836314}
cot(complex)

Specs

cot(complex) :: complex

Returns a new complex that is the cotangent of the provided parameter.

See also

sin/1, cos/1, tan/1

Examples

iex> Complex.cot( Complex.fromPolar(2,:math.pi) )
%Complex{im: -2.9622992129532336e-16, re: 0.45765755436028577}
coth(complex)

Specs

coth(complex) :: complex

Returns a new complex that is the hyperbolic cotangent of the provided parameter.

See also

sinh/1, cosh/1, tanh/1

Examples

iex> Complex.coth( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.8619978115303632e-17, re: -1.037314720727548}
csc(complex)

Specs

csc(complex) :: complex

Returns a new complex that is the cosecant of the provided parameter.

See also

sec/1, sin/1, cos/1, tan/1

Examples

iex> Complex.csc( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.2327514463765779e-16, re: -1.0997501702946164}
csch(complex)

Specs

csch(complex) :: complex

Returns a new complex that is the hyperbolic cosecant of the provided parameter.

See also

sinh/1, cosh/1, tanh/1

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

add/2, mult/2, sub/2

Examples

iex> Complex.div( Complex.fromPolar(1, :math.pi/2), Complex.fromPolar(1, :math.pi/2) )
%Complex{im: 0.0, re: 1.0}
exp(complex)

Specs

exp(complex) :: complex

Returns a new complex that is the complex exponential of the provided complex number. That is, e raised to the power z.

See also

ln/1

Examples

iex> Complex.exp( Complex.fromPolar(2,:math.pi) )
%Complex{im: 3.3147584285483636e-17, re: 0.1353352832366127}
fromPolar(r, phi)

Specs

fromPolar(number, number) :: complex

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

new/2 imag/0

Examples

iex> Complex.fromPolar(1, :math.pi/2)
%Complex{im: 1.0, re: 6.123233995736766e-17}
getPolar(complex)

Specs

getPolar(complex) :: {float, float}

Returns the polar coordinates of the supplied complex. That is, the returned tuple {r,phi} is the magnitude and phase of z.

See also

fromPolar/2

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

new/2 fromPolar/2

Examples

iex> Complex.imag()
%Complex{im: 1.0, re: 0.0}
ln(complex)

Specs

ln(complex) :: complex

Returns a new complex that is the complex natural log of the provided complex number. That is, log base e of z.

See also

exp/1

Examples

iex> Complex.ln( Complex.fromPolar(2,:math.pi) )
%Complex{im: 3.141592653589793, re: 0.6931471805599453}
log10(complex)

Specs

log10(complex) :: complex

Returns a new complex that is the complex log base 10 of the provided complex number.

See also

ln/1

Examples

iex> Complex.log10( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.3643763538418412, re: 0.30102999566398114}
log2(complex)

Specs

log2(complex) :: complex

Returns a new complex that is the complex log base 2 of the provided complex number.

See also

ln/1, log10/1

Examples

iex> Complex.log2( Complex.fromPolar(2,:math.pi) )
%Complex{im: 4.532360141827194, re: 1.0}
mult(complex1, complex2)

Specs

mult(complex, complex) :: complex

Returns a new complex that is the product of the provided complex numbers.

See also

add/2, div/2, sub/2

Examples

iex> Complex.mult( Complex.fromPolar(1, :math.pi/2), Complex.fromPolar(1, :math.pi/2) )
%Complex{im: 1.2246467991473532e-16, re: -1.0}

iex> Complex.mult( Complex.imag(), Complex.imag() )
%Complex{im: 0.0, re: -1.0}
neg(complex)

Specs

neg(complex) :: complex

Returns a new complex that is the “negation” of the provided parameter. That is, the real and imaginary parts are negated.

See also

neq/2, imag/0

Examples

iex> Complex.neg( Complex.new(3,5) )
%Complex{im: -5, re: -3}
new(re, im \\ 0)

Specs

new(number, number) :: complex

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

imag/0 fromPolar/2

Examples

iex> Complex.new(3, 4)
%Complex{im: 4, re: 3}

iex> Complex.new(2)
%Complex{im: 0, re: 2}
phase(complex)

Specs

phase(complex) :: float

Returns the phase angle of the supplied complex.

See also

new/2 fromPolar/2

Examples

iex> Complex.phase( Complex.fromPolar(1,:math.pi/2) )
1.5707963267948966
pow(complex1, complex2)

Specs

Returns a new complex that is the provided parameter a raised to the complex power b.

See also

ln/1, log10/1

Examples

iex> Complex.pow( Complex.fromPolar(2,:math.pi), Complex.imag() )
%Complex{im: 0.027612020368333014, re: 0.03324182700885666}
sec(complex)

Specs

sec(complex) :: complex

Returns a new complex that is the secant of the provided parameter.

See also

sin/1, cos/1, tan/1

Examples

iex> Complex.sec( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.2860374461837126e-15, re: -2.402997961722381}
sech(complex)

Specs

sech(complex) :: complex

Returns a new complex that is the hyperbolic secant of the provided parameter.

See also

sinh/1, cosh/1, tanh/1

Examples

iex> Complex.sech( Complex.fromPolar(2,:math.pi) )
%Complex{im: 6.27608655779184e-17, re: 0.2658022288340797}
sin(complex)

Specs

sin(complex) :: complex

Returns a new complex that is the sine of the provided parameter.

See also

cos/1, tan/1

Examples

iex> Complex.sin( Complex.fromPolar(2,:math.pi) )
%Complex{im: -1.0192657827055095e-16, re: -0.9092974268256817}
sinh(complex)

Specs

sinh(complex) :: complex

Returns a new complex that is the hyperbolic sine of the provided parameter.

See also

cosh/1, tanh/1

Examples

iex> Complex.sinh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 9.214721821703068e-16, re: -3.626860407847019}
sqrt(complex)

Specs

sqrt(complex) :: complex

Returns a new complex that is the complex square root of the provided complex number.

See also

abs/2, phase/1

Examples

iex> Complex.sqrt( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.4142135623730951, re: 8.659560562354933e-17}
sub(complex1, complex2)

Specs

Returns a new complex that is the difference of the provided complex numbers.

See also

add/2, div/2, mult/2

Examples

iex> Complex.sub( Complex.fromPolar(1, :math.pi/2), Complex.fromPolar(1, :math.pi/2) )
%Complex{im: 0.0, re: 0.0}
tan(complex)

Specs

tan(complex) :: complex

Returns a new complex that is the tangent of the provided parameter.

See also

sin/1, cos/1

Examples

iex> Complex.tan( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.4143199004457917e-15, re: 2.185039863261519}
tanh(complex)

Specs

tanh(complex) :: complex

Returns a new complex that is the hyperbolic tangent of the provided parameter.

See also

sinh/1, cosh/1

Examples

iex> Complex.tanh( Complex.fromPolar(2,:math.pi) )
%Complex{im: 1.7304461302709572e-17, re: -0.964027580075817}