Complex (complex v0.4.2)

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

Link to this type

non_finite_number()

@type non_finite_number() :: :infinity | :neg_infinity | :nan
@type t() :: %Complex{im: number(), re: number()}

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

Link to this section Functions

@spec abs(t() | number() | non_finite_number()) :: number() | non_finite_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() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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: 2.9999999999999996, re: 1.9999999999999993}
@spec acoth(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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: 3.0, re: 1.9999999999999993}
@spec acsch(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.9999999999999984}
Link to this function

add(left, right)

@spec add(t() | number() | non_finite_number(), t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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: 2.9999999999999996, re: 2.0}
@spec atanh(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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: 3.0, re: 1.9999999999999993}
@spec conjugate(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.962299212953233e-16, re: 0.45765755436028577}
@spec coth(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.8619978115303644e-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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number(),
  t() | number() | non_finite_number()
) :: t() | number() | non_finite_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}
@spec erf(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_number()

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
@spec erf_inv(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_number()

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
@spec erfc(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_number()

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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | non_finite_number(), number() | non_finite_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() | non_finite_number(), number() | non_finite_number()) ::
  t()
@spec imag(t() | number() | non_finite_number()) :: number() | non_finite_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() | number() | non_finite_number()) :: t() | number() | non_finite_number()

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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number(),
  t() | number() | non_finite_number()
) :: t() | number() | non_finite_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}

iex> Complex.multiply(-2, Complex.new(:infinity, :neg_infinity))
%Complex{im: :infinity, re: :neg_infinity}
@spec negate(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | non_finite_number(), number() | non_finite_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}

iex> Complex.new(:infinity)
%Complex{im: 0.0, re: :infinity}

iex> Complex.new(:nan, :neg_infinity)
%Complex{im: :neg_infinity, re: :nan}
@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}, ""}

iex> Complex.parse("NaN+Infi")
{%Complex{im: :infinity, re: :nan}, ""}

iex> Complex.parse("-Inf+NaNi")
{%Complex{im: :nan, re: :neg_infinity}, ""}

iex> Complex.parse("Inf-NaNi")
{%Complex{im: :nan, re: :infinity}, ""}

iex> Complex.parse("Inf")
{%Complex{im: 0.0, re: :infinity}, ""}
@spec phase(t() | number() | non_finite_number()) :: float() | non_finite_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() | non_finite_number() | number(),
  t() | non_finite_number() | number()
) :: t() | non_finite_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() | non_finite_number()) :: number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.26580222883407967}
@spec sin(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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() | number() | non_finite_number(),
  t() | number() | non_finite_number()
) :: t() | number() | non_finite_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() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.4143199004457915e-15, re: 2.185039863261519}
@spec tanh(t() | number() | non_finite_number()) ::
  t() | number() | non_finite_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.7304461302709575e-17, re: -0.9640275800758169}
@spec to_polar(t() | number() | non_finite_number()) ::
  {float() | non_finite_number(), float() | non_finite_number()}

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)

@spec to_string(t()) :: String.t()

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