Complex (complex v0.4.0)

Complex is a library that brings complex number support to Elixir.

Each complex number is represented as a %Complex{} struct, that holds the real and imaginary parts. There are functions for the creation and manipulation of complex numbers.

This module implements mathematical functions such as add/2, subtract/2, divide/2, and multiply/2 that subtitute the +, -, / and * operators.

Operator overloading is provided through Complex.Kernel

examples

Examples

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

iex> Complex.new(0, 1)
%Complex{im: 1.0, re: 0.0}

Link to this section Summary

Types

t()

A complex number represented as a %Complex{} struct.

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.

$atan2(b, a)$ returns the phase of the complex number $a + bi$.

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.

Calculates $erf(z)$ of the argument, as defined by

Calculates $erf^-1(z)$ of the argument, as defined by

Calculates $erfc(z)$ of the argument, as defined by

Returns a new complex that is the complex exponential of the provided complex number: $exp(z) = e^z$.

Turns a representation in polar coordinates $r\phase\phi$, into the complex number $z = r.cos(\phi) + r.sin(\phi) i$

Returns the imaginary part of the provided complex number. If a real number is provided, 0 is returned.

Returns a new complex that is the complex natural log of the provided complex number, $ln(z) = log_e(z)$.

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

Returns a new complex that is the complex log base 10 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 the real part of the provided complex number.

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.

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

Conveniency function that is used to implement the String.Chars and Inspect protocols.

Link to this section Types

@type t() :: %Complex{im: number(), re: number()}

A complex number represented as a %Complex{} struct.

Link to this section Functions

@spec abs(t()) :: number()
@spec abs(number()) :: number()

Returns the magnitude (length) of the provided complex number.

see-also

See also

new/2, phase/1

examples

Examples

iex> Complex.abs(Complex.from_polar(1, :math.pi/2))
1.0
@spec abs_squared(t()) :: number()
@spec abs_squared(number()) :: 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

See also

new/2, abs/1

examples

Examples

iex> Complex.abs_squared(Complex.from_polar(1, :math.pi/2))
1.0

iex> Complex.abs_squared(Complex.from_polar(2, :math.pi/2))
4.0
@spec acos(t()) :: t()
@spec acos(number()) :: number()

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

see-also

See also

cos/1

examples

Examples

iex> Complex.acos(Complex.from_polar(2,:math.pi))
%Complex{im: 1.3169578969248164, re: -3.141592653589793}
@spec acosh(t()) :: t()
@spec acosh(number()) :: number()

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

see-also

See also

cosh/1

examples

Examples

iex> Complex.acosh(Complex.from_polar(2,:math.pi))
%Complex{im: -3.141592653589793, re: -1.3169578969248164}
@spec acot(t()) :: t()
@spec acot(number()) :: number()

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

see-also

See also

cot/1

examples

Examples

iex> Complex.acot(Complex.from_polar(2,:math.pi))
%Complex{im: -9.71445146547012e-17, re: -0.46364760900080615}

iex> Complex.cot(Complex.acot(Complex.new(2,3)))
%Complex{im: 3.0, re: 1.9999999999999991}
@spec acoth(t()) :: t()
@spec acoth(number()) :: number()

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

see-also

See also

coth/1

examples

Examples

iex> Complex.acoth(Complex.from_polar(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}
@spec acsc(t()) :: t()
@spec acsc(number()) :: number()

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

see-also

See also

sec/1

examples

Examples

iex> Complex.acsc(Complex.from_polar(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.9999999999999991}
@spec acsch(t()) :: t()
@spec acsch(number()) :: number()

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

see-also

See also

csch/1

examples

Examples

iex> Complex.acsch(Complex.from_polar(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)

@spec add(t(), t()) :: t()
@spec add(number(), t()) :: t()
@spec add(t(), number()) :: t()
@spec add(number(), number()) :: number()

Returns a new complex that is the sum of the provided complex numbers. Also supports a mix of complex and number.

see-also

See also

div/2, multiply/2, subtract/2

examples

Examples

iex> Complex.add(Complex.from_polar(1, :math.pi/2), Complex.from_polar(1, :math.pi/2))
%Complex{im: 2.0, re: 1.2246467991473532e-16}

iex> Complex.add(Complex.new(4, 4), 1)
%Complex{im: 4.0, re: 5.0}

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

iex> Complex.add(2, 3)
5

iex> Complex.add(2.0, 2)
4.0
@spec asec(t()) :: t()
@spec asec(number()) :: number()

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

see-also

See also

sec/1

examples

Examples

iex> Complex.asec(Complex.from_polar(2,:math.pi))
%Complex{im: 0.0, re: 2.0943951023931957}

iex> Complex.sec(Complex.asec(Complex.new(2,3)))
%Complex{im: 2.9999999999999982, re: 1.9999999999999987}
@spec asech(t()) :: t()
@spec asech(number()) :: number()

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

see-also

See also

sech/1

examples

Examples

iex> Complex.asech(Complex.from_polar(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}
@spec asin(t()) :: t()
@spec asin(number()) :: number()

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

see-also

See also

sin/1

examples

Examples

iex> Complex.asin(Complex.from_polar(2,:math.pi))
%Complex{im: 1.3169578969248164, re: -1.5707963267948966}
@spec asinh(t()) :: t()
@spec asinh(number()) :: number()

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

see-also

See also

sinh/1

examples

Examples

iex> Complex.asinh(Complex.from_polar(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.0000000000000004}

$atan2(b, a)$ returns the phase of the complex number $a + bi$.

see-also

See also

tan/1, atan/1

examples

Examples

iex> phase = Complex.atan2(2, 2)
iex> phase == :math.pi() / 4
true

iex> phase = Complex.atan2(2, Complex.new(0))
iex> phase == Complex.new(:math.pi() / 2, 0)
true
@spec atan(t()) :: t()
@spec atan(number()) :: number()

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

see-also

See also

tan/1, atan2/2

examples

Examples

iex> Complex.atan(Complex.from_polar(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}
@spec atanh(t()) :: t()
@spec atanh(number()) :: number()

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

see-also

See also

tanh/1

examples

Examples

iex> Complex.atanh(Complex.from_polar(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}
@spec conjugate(t()) :: t()
@spec conjugate(number()) :: number()

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

If $z = a + bi$, $conjugate(z) = z^* = a - bi$

see-also

See also

abs/2, phase/1

examples

Examples

iex> Complex.conjugate(Complex.new(1,2))
%Complex{im: -2.0, re: 1.0}
@spec cos(t()) :: t()
@spec cos(number()) :: number()

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

see-also

See also

sin/1, tan/1

examples

Examples

iex> Complex.cos(Complex.from_polar(2,:math.pi))
%Complex{im: 2.2271363664699914e-16, re: -0.4161468365471424}
@spec cosh(t()) :: t()
@spec cosh(number()) :: number()

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

see-also

See also

sinh/1, tanh/1

examples

Examples

iex> Complex.cosh(Complex.from_polar(2,:math.pi))
%Complex{im: -8.883245978848233e-16, re: 3.7621956910836314}
@spec cot(t()) :: t()
@spec cot(number()) :: number()

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

see-also

See also

sin/1, cos/1, tan/1

examples

Examples

iex> Complex.cot(Complex.from_polar(2,:math.pi))
%Complex{im: -2.9622992129532336e-16, re: 0.45765755436028577}
@spec coth(t()) :: t()
@spec coth(number()) :: number()

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

see-also

See also

sinh/1, cosh/1, tanh/1

examples

Examples

iex> Complex.coth(Complex.from_polar(2,:math.pi))
%Complex{im: -1.8619978115303632e-17, re: -1.037314720727548}
@spec csc(t()) :: t()
@spec csc(number()) :: number()

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

see-also

See also

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

examples

Examples

iex> Complex.csc(Complex.from_polar(2,:math.pi))
%Complex{im: 1.2327514463765779e-16, re: -1.0997501702946164}
@spec csch(t()) :: t()
@spec csch(number()) :: number()

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

see-also

See also

sinh/1, cosh/1, tanh/1

examples

Examples

iex> Complex.csch(Complex.from_polar(2,:math.pi))
%Complex{im: -7.00520014334671e-17, re: -0.2757205647717832}
@spec divide(t(), t()) :: t()
@spec divide(number(), t()) :: t()
@spec divide(t(), number()) :: t()
@spec divide(number(), number()) :: number()

Returns a new complex that is the ratio (division) of the provided complex numbers.

see-also

See also

add/2, multiply/2, subtract/2

examples

Examples

iex> Complex.divide(Complex.from_polar(1, :math.pi/2), Complex.from_polar(1, :math.pi/2))
%Complex{im: 0.0, re: 1.0}

Calculates $erf(z)$ of the argument, as defined by:

$$erf(z) = \frac{2}{\sqrt{\pi}} \int_{0}^{z} e^{-t^2}$$

examples

Examples

iex> x = Complex.erf(0.5)
iex> Float.round(x, 5)
0.52050

iex> z = Complex.erf(Complex.new(-0.5))
iex> z.im
0.0
iex> Float.round(z.re, 5)
-0.52050

iex> Complex.erf(Complex.new(1, 1))
** (ArithmeticError) erf not implemented for non-real numbers

Calculates $erf^-1(z)$ of the argument, as defined by:

$$erf(erf^-1(z)) = z$$

examples

Examples

iex> Complex.erf_inv(0.5204998778130465)
0.5000000069276399

iex> Complex.erf_inv(Complex.new(-0.5204998778130465))
%Complex{im: 0.0, re: -0.5000000069276399}

iex> Complex.erf_inv(Complex.new(1, 1))
** (ArithmeticError) erf_inv not implemented for non-real numbers

Calculates $erfc(z)$ of the argument, as defined by:

$$erfc(z) = 1 - erf(z)$$

examples

Examples

iex> x = Complex.erfc(0.5)
iex> Float.round(x, 5)
0.47950

iex> z = Complex.erfc(Complex.new(-0.5))
iex> z.im
0.0
iex> Float.round(z.re, 5)
1.52050

iex> Complex.erfc(Complex.new(1, 1))
** (ArithmeticError) erfc not implemented for non-real numbers
@spec exp(t()) :: t()
@spec exp(number()) :: number()

Returns a new complex that is the complex exponential of the provided complex number: $exp(z) = e^z$.

see-also

See also

ln/1

examples

Examples

iex> Complex.exp(Complex.from_polar(2,:math.pi))
%Complex{im: 3.3147584285483636e-17, re: 0.1353352832366127}
Link to this function

from_polar(arg)

@spec from_polar({number(), number()}) :: t()

Turns a representation in polar coordinates $r\phase\phi$, into the complex number $z = r.cos(\phi) + r.sin(\phi) i$

see-also

See also

new/2

examples

Examples

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

iex> polar = Complex.to_polar(%Complex{re: 6, im: 20})
iex> Complex.from_polar(polar)
%Complex{im: 20.0, re: 6.0}
Link to this function

from_polar(r, phi)

@spec from_polar(number(), number()) :: t()
@spec imag(t()) :: number()
@spec imag(number()) :: number()

Returns the imaginary part of the provided complex number. If a real number is provided, 0 is returned.

see-also

See also

real/1

examples

Examples

iex> Complex.imag(Complex.new(1, 2))
2.0

iex> Complex.imag(1)
0
@spec ln(t()) :: t()

Returns a new complex that is the complex natural log of the provided complex number, $ln(z) = log_e(z)$.

see-also

See also

exp/1

examples

Examples

iex> Complex.ln(Complex.from_polar(2,:math.pi))
%Complex{im: 3.141592653589793, re: 0.6931471805599453}
@spec log2(t()) :: t()
@spec log2(number()) :: number()

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

see-also

See also

ln/1, log10/1

examples

Examples

iex> Complex.log2(Complex.from_polar(2,:math.pi))
%Complex{im: 4.532360141827194, re: 1.0}
@spec log10(t()) :: t()
@spec log10(number()) :: number()

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

see-also

See also

ln/1

examples

Examples

iex> Complex.log10(Complex.from_polar(2,:math.pi))
%Complex{im: 1.3643763538418412, re: 0.30102999566398114}
Link to this function

multiply(left, right)

@spec multiply(t(), t()) :: t()
@spec multiply(number(), t()) :: t()
@spec multiply(t(), number()) :: t()
@spec multiply(number(), number()) :: number()

Returns a new complex that is the product of the provided complex numbers. Also supports a mix of complex and number.

see-also

See also

add/2, div/2, subtract/2

examples

Examples

iex> Complex.multiply(Complex.new(1,2), Complex.new(3,4))
%Complex{im: 10.0, re: -5.0}

iex> Complex.multiply(Complex.new(0, 1), Complex.new(0, 1))
%Complex{im: 0.0, re: -1.0}

iex> Complex.multiply(Complex.new(1, 2), 3)
%Complex{im: 6.0, re: 3.0}

iex> Complex.multiply(3, Complex.new(1, 2))
%Complex{im: 6.0, re: 3.0}
@spec negate(t()) :: t()
@spec negate(number()) :: number()

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

see-also

See also

new/2

examples

Examples

iex> Complex.negate(Complex.new(3,5))
%Complex{im: -5.0, re: -3.0}
Link to this function

new(re, im \\ 0)

@spec new(number(), number()) :: t()

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

See also

from_polar/2

examples

Examples

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

iex> Complex.new(2)
%Complex{im: 0.0, re: 2.0}
@spec parse(String.t()) :: {t(), String.t()} | :error

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

See also

new/2

examples

Examples

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

iex> Complex.parse("1+2i")
{%Complex{im: 2.0, re: 1.0}, ""}

iex> Complex.parse("2-3i")
{%Complex{im: -3.0, re: 2.0}, ""}

iex> Complex.parse("-1.0-3.i")
{%Complex{im: -3.0, re: -1.0}, ""}

iex> Complex.parse("-1.0+3.i 2.2+3.3i")
{%Complex{im: 3.0, re: -1.0}, " 2.2+3.3i"}

iex> Complex.parse("1e-4-3e-3i")
{%Complex{im: -3.0e-3, re: 1.0e-4}, ""}

iex> Complex.parse("2i")
{%Complex{im: 2.0, re: 0.0}, ""}

iex> Complex.parse("-3.0i")
{%Complex{im: -3.0, re: 0.0}, ""}
Link to this function

parse_imag(str)

Link to this function

parse_real(str)

@spec phase(t()) :: float()
@spec phase(number()) :: number()

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

see-also

See also

new/2, from_polar/2

examples

Examples

iex> Complex.phase(Complex.from_polar(1,:math.pi/2))
1.5707963267948966
@spec power(t(), t()) :: t()
@spec power(number(), t()) :: t()
@spec power(t(), number()) :: t()
@spec power(number(), number()) :: number()

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

see-also

See also

ln/1, log10/1

examples

Examples

iex> Complex.power(Complex.from_polar(2,:math.pi), Complex.new(0, 1))
%Complex{im: 0.027612020368333014, re: 0.03324182700885666}
@spec real(t()) :: number()
@spec real(number()) :: number()

Returns the real part of the provided complex number.

see-also

See also

imag/1

examples

Examples

iex> Complex.real(Complex.new(1, 2))
1.0

iex> Complex.real(1)
1
@spec sec(t()) :: t()
@spec sec(number()) :: number()

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

see-also

See also

sin/1, cos/1, tan/1

examples

Examples

iex> Complex.sec(Complex.from_polar(2,:math.pi))
%Complex{im: -1.2860374461837126e-15, re: -2.402997961722381}
@spec sech(t()) :: t()
@spec sech(number()) :: number()

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

see-also

See also

sinh/1, cosh/1, tanh/1

examples

Examples

iex> Complex.sech(Complex.from_polar(2,:math.pi))
%Complex{im: 6.27608655779184e-17, re: 0.2658022288340797}
@spec sin(t()) :: t()
@spec sin(number()) :: number()

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

see-also

See also

cos/1, tan/1

examples

Examples

iex> Complex.sin(Complex.from_polar(2,:math.pi))
%Complex{im: -1.0192657827055095e-16, re: -0.9092974268256817}
@spec sinh(t()) :: t()
@spec sinh(number()) :: number()

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

see-also

See also

cosh/1, tanh/1

examples

Examples

iex> Complex.sinh(Complex.from_polar(2,:math.pi))
%Complex{im: 9.214721821703068e-16, re: -3.626860407847019}
@spec sqrt(t()) :: t()
@spec sqrt(number()) :: number()

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

see-also

See also

abs/2, phase/1

examples

Examples

iex> Complex.sqrt(Complex.from_polar(2,:math.pi))
%Complex{im: 1.4142135623730951, re: 8.659560562354933e-17}
@spec square(t()) :: t()
@spec square(number()) :: number()

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

see-also

See also

multiply/2

examples

Examples

iex> Complex.square(Complex.new(2.0, 0.0))
%Complex{im: 0.0, re: 4.0}

iex> Complex.square(Complex.new(0, 1))
%Complex{im: 0.0, re: -1.0}
Link to this function

subtract(left, right)

@spec subtract(t(), t()) :: t()
@spec subtract(number(), t()) :: t()
@spec subtract(t(), number()) :: t()
@spec subtract(number(), number()) :: number()

Returns a new complex that is the difference of the provided complex numbers. Also supports a mix of complex and number.

see-also

See also

add/2, div/2, multiply/2

examples

Examples

iex> Complex.subtract(Complex.new(1,2), Complex.new(3,4))
%Complex{im: -2.0, re: -2.0}

iex> Complex.subtract(Complex.new(1, 2), 3)
%Complex{im: 2.0, re: -2.0}

iex> Complex.subtract(10, Complex.new(1, 2))
%Complex{im: -2.0, re: 9.0}
@spec tan(t()) :: t()
@spec tan(number()) :: number()

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

see-also

See also

sin/1, cos/1

examples

Examples

iex> Complex.tan(Complex.from_polar(2,:math.pi))
%Complex{im: 1.4143199004457917e-15, re: 2.185039863261519}
@spec tanh(t()) :: t()
@spec tanh(number()) :: number()

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

see-also

See also

sinh/1, cosh/1

examples

Examples

iex> Complex.tanh(Complex.from_polar(2,:math.pi))
%Complex{im: 1.7304461302709572e-17, re: -0.964027580075817}
@spec to_polar(t()) :: {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

See also

from_polar/2

examples

Examples

iex> Complex.to_polar(Complex.from_polar(1,:math.pi/2))
{1.0, 1.5707963267948966}
Link to this function

to_string(complex)

Conveniency function that is used to implement the String.Chars and Inspect protocols.