View Source Complex (qcomplex v1.0.0)

Elixir library implementing complex numbers and math.

The library adds new type of complex/0 numbers and basic math operations for them. Complex numbers can also interact with integers and floats. Actually this library expands existing functions, so they can work with complex numbers too. Number operations available:

  • addition
  • subtraction
  • multiplication
  • division
  • exponentiation
  • absolute value
  • trigonometric functions

some-examples

Some examples

iex> use Complex
Complex

iex> ~o(1+2i)
1.0+2.0i

iex> ~o(1+2i) * ib
-2.0+1.0i

iex> Complex.Trig.sin(~o(-11-2i))
3.762158846210887-0.016051388809949604i

iex> to_polar(~o(7-9i))
{11.40175425099138, -0.9097531579442097}

iex> ~o(1+2i) ** ~o(3+4i)
0.129009594074467+0.03392409290517014i

Link to this section Summary

Functions

Complex conjugate.

Imaginary unit.

Returns true if term is a complex number, otherwise returns false.

Parses a string into a complex number.

Handles the sigil ~o for complex numbers.

Polar coordinates.

Link to this section Types

@type complex() :: %Complex{im: float(), re: float()}

Link to this section Functions

@spec conj(complex()) :: complex()

Complex conjugate.

Returns the complex conjugate of a complex number.

examples

Examples

iex> conj(~o(5+7i))
5.0-7.0i

iex> conj(~o(5-7i))
5.0+7.0i
@spec ib(number()) :: %Complex{im: 1, re: number()}

Imaginary unit.

When no arguments passed, returns imaginary unit; otherwise returns i*b.

examples

Examples

iex> ib
i

iex> ib(-5)
-5i
Link to this macro

is_complex(term)

View Source (macro)

Returns true if term is a complex number, otherwise returns false.

Allowed in guard tests.

@spec parse(String.t()) :: complex() | float() | :error

Parses a string into a complex number.

If successful, returns either a complex/0 or float/0; otherwise returns :error.

examples

Examples

iex> Complex.parse "1+13i"
1.0+13.0i

iex> Complex.parse "2.3-1i"
2.3-1.0i

iex> Complex.parse "42"
:error
Link to this function

sigil_o(string, modifiers)

View Source
@spec sigil_o(String.t(), list()) :: complex() | float() | :error

Handles the sigil ~o for complex numbers.

It returns a complex/0 or float/0.

examples

Examples

iex> ~o(1+4i)
1.0+4.0i

iex> ~o(-3.1+5i)
-3.1+5.0i
@spec to_polar(complex()) :: {float(), float()}

Polar coordinates.

Returns polar coordinates of a complex number as {radius, angle}.

examples

Examples

iex> to_polar(~o(7-9i))
{11.40175425099138, -0.9097531579442097}