Home

Awesome

ComplexNum

hex.pm version

ComplexNum allows you to work with Complex Numbers in Elixir.

Cartesian vs. Polar

There are two kinds of representaions for Complex Numbers:

Polar form is very useful to perform fast multiplications, division and integer powers with. Also, it obviously allows for O(1) precise computation of the magnitude and the angle.

Cartesian form on the other hand, allows besides multiplication and division, precise addition and subtraction. Also, it obviously allows for O(1) precise computation of the real and imaginary parts.

Conversions between these two representations is possible, but lossy: This involves trigonometry and square roots, which means that precision is lost. (CompexNum converts the internal data types to floats and back to perform these oprations.)

Internal data types

ComplexNum uses the Numbers library, which means that the real/imaginary (resp. magnitude/angle) can be of any data type that implements Numbers' Numeric behaviour. This means that Integers, Floats, arbitrary precision decimals defined by the Decimals package, rationals defined by the Ratio package, etc. can be used.

ComplexNum itself also follows the Numeric behaviour, which means that it can be used inside any container that uses Numbers. (Including inside ComplexNum itself, but who would do such a thing?)

Installation

The package can be installed as:

  1. Add complex_num to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:complex_num, "~> 1.0.0"}]
end
```

2. Ensure complex_num is started before your application:

```elixir
def application do
  [applications: [:complex_num]]
end
```

Changelog