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
Link to this section Functions
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
Imaginary unit.
When no arguments passed, returns imaginary unit; otherwise returns i*b.
examples
Examples
iex> ib
i
iex> ib(-5)
-5i
Returns true
if term
is a complex number, otherwise returns false
.
Allowed in guard tests.
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
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
Polar coordinates.
Returns polar coordinates of a complex number as {radius, angle}
.
examples
Examples
iex> to_polar(~o(7-9i))
{11.40175425099138, -0.9097531579442097}