Skip to content

Commit 8a46bfe

Browse files
authored
feat: add cbrt/1 (#23)
* feat: add cbrt/1 * docs: typos * docs: arity
1 parent c3506d8 commit 8a46bfe

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

lib/complex.ex

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ defmodule Complex do
740740
@spec conjugate(t | number | non_finite_number) :: t | number | non_finite_number
741741
def conjugate(z)
742742

743-
def conjugate(n) when is_number(n), do: n
743+
def conjugate(n) when is_number(n) or is_non_finite_number(n), do: n
744744

745745
def conjugate(%Complex{re: r, im: :neg_infinity}), do: new(r, :infinity)
746746
def conjugate(%Complex{re: r, im: :infinity}), do: new(r, :neg_infinity)
@@ -756,7 +756,7 @@ defmodule Complex do
756756
757757
### See also
758758
759-
`abs/2`, `phase/1`
759+
`abs/1`, `cbrt/1`, `phase/1`
760760
761761
### Examples
762762
@@ -767,7 +767,7 @@ defmodule Complex do
767767
@spec sqrt(t | number | non_finite_number) :: t | number | non_finite_number
768768
def sqrt(z)
769769
def sqrt(:infinity), do: :infinity
770-
def sqrt(:neg_infinity), do: Complex.new(0, :infinity)
770+
def sqrt(:neg_infinity), do: :nan
771771
def sqrt(:nan), do: :nan
772772
def sqrt(n) when is_number(n), do: :math.sqrt(n)
773773

@@ -807,6 +807,51 @@ defmodule Complex do
807807
end
808808
end
809809

810+
@doc """
811+
Returns a new number that is the complex cube root of the provided
812+
number.
813+
814+
Returns the principal branch of the cube root for complex inputs.
815+
816+
### See also
817+
818+
`abs/1`, `phase/1`, `sqrt/1`
819+
820+
### Examples
821+
822+
iex> Complex.cbrt(-8)
823+
-2.0
824+
825+
When a negative number is given as a complex input,
826+
the output now changes. Instead of still giving a
827+
negative number, we now get a number with phase
828+
$\\frac{\\pi}{3}$
829+
830+
iex> z = Complex.cbrt(Complex.new(-8, 0))
831+
%Complex{re: 1.0000000000000002, im: 1.7320508075688772}
832+
iex> Complex.abs(z)
833+
2.0
834+
iex> Complex.phase(z)
835+
1.0471975511965976
836+
iex> :math.pi() / 3
837+
1.0471975511965976
838+
839+
"""
840+
@spec cbrt(t | number | non_finite_number) :: t | float() | non_finite_number
841+
def cbrt(z)
842+
def cbrt(:infinity), do: :infinity
843+
def cbrt(:neg_infinity), do: :neg_infinity
844+
def cbrt(:nan), do: :nan
845+
def cbrt(n) when is_number(n) and n >= 0, do: :math.pow(n, 1 / 3)
846+
def cbrt(n) when is_number(n), do: -1 * cbrt(abs(n))
847+
848+
def cbrt(z) do
849+
r = abs(z)
850+
theta = phase(z)
851+
852+
from_polar(cbrt(r), theta / 3)
853+
end
854+
810855
@doc """
811856
Returns a new complex that is the complex exponential of the provided
812857
complex number: $exp(z) = e^z$.

test/complex_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ defmodule ComplexTest do
555555

556556
test "sqrt (non-finite)" do
557557
assert Complex.sqrt(:infinity) == :infinity
558-
assert Complex.sqrt(:neg_infinity) == Complex.new(0, :infinity)
558+
assert Complex.sqrt(:neg_infinity) == :nan
559559
assert Complex.sqrt(:nan) == :nan
560560

561561
assert Complex.sqrt(Complex.new(:infinity, 1)) == Complex.new(:infinity)

0 commit comments

Comments
 (0)