Skip to content

Commit

Permalink
fix: hyperbolic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
polvalente committed Jan 9, 2025
1 parent 0facf27 commit 92cd083
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions lib/complex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,26 @@ defmodule Complex do
def exp(:nan), do: :nan
def exp(n) when is_number(n), do: :math.exp(n)

def exp(%Complex{re: :neg_infinity, im: _}), do: 0
def exp(%Complex{re: :neg_infinity, im: _}), do: new(0, 0)
def exp(%Complex{re: :infinity, im: :nan}), do: new(:infinity, :nan)

def exp(%Complex{re: :infinity, im: im}) when is_number(im),
do: new(:infinity, multiply(:infinity, im))
def exp(%Complex{re: :infinity, im: im}) when is_number(im) do
re =
case :math.cos(im) do
x when x > 0 -> :infinity
x when x < 0 -> :neg_infinity
_ -> :nan
end

im =
case :math.sin(im) do
x when x > 0 -> :infinity
x when x < 0 -> :neg_infinity
_ -> :nan
end

new(re, im)
end

def exp(%Complex{re: _, im: :neg_infinity}), do: new(:nan, :nan)
def exp(%Complex{re: _, im: :infinity}), do: new(:nan, :nan)
Expand Down Expand Up @@ -1618,10 +1633,12 @@ defmodule Complex do
def sinh(n) when is_number(n), do: :math.sinh(n)

def sinh(z = %Complex{}) do
z
|> exp()
|> subtract(exp(negate(z)))
|> divide(2)
%Complex{re: re, im: im} =
z
|> exp()
|> subtract(exp(negate(z)))

new(divide(re, 2), divide(im, 2))
end

@doc """
Expand Down Expand Up @@ -1686,12 +1703,15 @@ defmodule Complex do
def cosh(:infinity), do: :infinity
def cosh(:neg_infinity), do: :infinity
def cosh(:nan), do: :nan
def cosh(n) when is_number(n), do: :math.cosh(n)

def cosh(z) do
z
|> exp()
|> add(exp(negate(z)))
|> divide(2)
%Complex{re: re, im: im} =
z
|> exp()
|> add(exp(negate(z)))

new(divide(re, 2), divide(im, 2))
end

@doc """
Expand Down

0 comments on commit 92cd083

Please sign in to comment.