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

Types

General type for complex numbers

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

Specs

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

General type for complex numbers

Link to this section Functions

Specs

abs(complex()) :: number()

Returns 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
Link to this function

abs_squared(complex)

Specs

abs_squared(complex()) :: number()

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

new/2, abs/1

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

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}

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}

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}

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.999999999999998, re: 2.000000000000001}

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}

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}
Link to this function

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}

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.9999999999999987}

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}

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}

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}

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}

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}
Link to this function

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}

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}

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}

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}

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}

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}

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}
Link to this function

div(complex1, complex2)

Specs

div(complex(), complex()) :: complex()

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}

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}
Link to this function

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/2imag/0

Examples

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

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 (in radians) of z.

See also

fromPolar/2

Examples

iex> Complex.getPolar( Complex.fromPolar(1,:math.pi/2) )
{1.0, 1.5707963267948966}

Specs

imag() :: complex()

Returns a new complex representing the pure imaginary number sqrt(-1).

See also

new/2fromPolar/2

Examples

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

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}

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}

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}
Link to this function

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

add/2, div/2, sub/2

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}

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}
Link to this function

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/0fromPolar/2

Examples

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

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

Specs

parse(String.t()) :: complex()

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

new/2

Examples

iex> Complex.parse("1.1+2.2i")
%Complex{im: 2.2, re: 1.1}

Specs

phase(complex()) :: float()

Returns the phase angle of the supplied complex, in radians.

See also

new/2fromPolar/2

Examples

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

Specs

pow(complex(), complex()) :: complex()

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}

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}

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}

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}

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}

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}

Specs

square(complex()) :: complex()

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

See also

mult/2

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}
Link to this function

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}

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}

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}