Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invpow and invlog domains #23

Merged
merged 5 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ function square(x::Real)
end


invpow2(x::Real, p::Integer) = sign(x) * abs(x)^inv(p)
invpow2(x::Real, p::Integer) = x ≥ zero(x) || isodd(p) ? sign(x) * abs(x)^inv(p) : throw(DomainError(x, "inverse for x^$p is not defined at $x"))
aplavin marked this conversation as resolved.
Show resolved Hide resolved
invpow2(x::Real, p::Real) = x ≥ zero(x) ? x^inv(p) : throw(DomainError(x, "inverse for x^$p is not defined at $x"))
invpow2(x, p) = x^inv(p)

invpow1(b, x) = log(abs(b), abs(x))
invpow1(b::Real, x::Real) = b ≥ zero(b) && x ≥ zero(x) ? log(b, x) : throw(DomainError(x, "inverse for $b^x is not defined at $x"))
aplavin marked this conversation as resolved.
Show resolved Hide resolved

invlog1(b::Real, x::Real) = b ≥ zero(b) && x ≥ zero(x) ? b^x : throw(DomainError(x, "inverse for log($b, x) is not defined at $x"))
invlog1(b::Real, x::Real) = b ≥ zero(b) ? b^x : throw(DomainError(x, "inverse for log($b, x) is not defined at $x"))
aplavin marked this conversation as resolved.
Show resolved Hide resolved
invlog1(b, x) = b^x

invlog2(b, x) = x^inv(b)
9 changes: 6 additions & 3 deletions test/test_inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
x = rand()
for f in (
foo, inv_foo, log, log2, log10, log1p, sqrt,
Base.Fix2(^, rand()), Base.Fix2(^, rand([-10:-1; 1:10])), Base.Fix1(^, rand()), Base.Fix1(log, rand()), Base.Fix2(log, rand()),
Base.Fix2(^, rand()), Base.Fix2(^, rand([-10:-1; 1:10])), Base.Fix1(^, rand()), Base.Fix1(log, 5rand()), Base.Fix2(log, rand()),
aplavin marked this conversation as resolved.
Show resolved Hide resolved
)
InverseFunctions.test_inverse(f, x)
end
Expand All @@ -56,9 +56,12 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
@test_throws DomainError inverse(Base.Fix1(*, 0))
@test_throws DomainError inverse(Base.Fix2(^, 0))
@test_throws DomainError inverse(Base.Fix1(log, 2))(-5)
InverseFunctions.test_inverse(Base.Fix1(log, 2), -5 + 0im)
@test inverse(Base.Fix1(log, 2))(-5 + 0im) == 0.03125 + 0im
aplavin marked this conversation as resolved.
Show resolved Hide resolved
@test_throws DomainError inverse(Base.Fix2(^, 0.5))(-5)
InverseFunctions.test_inverse(Base.Fix2(^, 0.5), -5 + 0im)
@test_throws DomainError inverse(Base.Fix2(^, 2))(-5)
@test_throws DomainError inverse(Base.Fix1(^, 2))(-5)
@test_throws DomainError inverse(Base.Fix1(^, -2))(3)
@test inverse(Base.Fix1(^, 2))(0) == -Inf
aplavin marked this conversation as resolved.
Show resolved Hide resolved

A = rand(5, 5)
for f in (
Expand Down