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

added missing matrix exponentiation methods #29782

Merged
merged 6 commits into from
Oct 30, 2018
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
1 change: 1 addition & 0 deletions base/mathconstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ catalan
# loop over types to prevent ambiguities for ^(::Number, x)
for T in (AbstractIrrational, Rational, Integer, Number, Complex)
Base.:^(::Irrational{:ℯ}, x::T) = exp(x)
Base.:^(::Irrational{:ℯ}, x::AbstractMatrix{T}) = exp(x)
Copy link
Member

@Keno Keno Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop says it's for ambiguities. Shouldn't we instead have a general
Base.:^(::Irrational{:ℯ}, x) = exp(x) fallback?

Copy link
Contributor Author

@ExpandingMan ExpandingMan Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, wouldn't that still allow for the ambiguities of ^(::Number, x)? I would have thought it can create an ambiguity if we have the fallback, even if we have the specific methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, maybe just add AbstractMatrix to the thing that T loops over?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be defined in LinearAlgebra.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to add AbstractMatrix as T to the loop because I thought it would be safer (in regard to method ambiguities) to ensure that only matrices with these element types have exponentials defined for them. Having said that, I just realized that this should have read AbstractMatrix{<:T} rather than what I have here.

Let me know whether you think this should indeed be fore general AbstractMatrix and I'll move it to LinearAlgebra.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since exp(::AbstractMatrix) is defined in LinearAlgebra this should too.

end
Base.literal_pow(::typeof(^), ::Irrational{:ℯ}, ::Val{p}) where {p} = exp(p)

Expand Down
5 changes: 5 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ end
@test ℯ^2 == exp(2)
@test ℯ^2.4 == exp(2.4)
@test ℯ^(2//3) == exp(2//3)
@test ℯ^(fill(2, (4,4))) == exp(fill(2, (4,4)))
@test ℯ^(fill(2.4, (4,4))) == exp(fill(2.4, (4,4)))
# currently missing `exp` methods for matrices of Rational and Irrational
# @test ℯ^(fill(π, (4,4))) == exp(fill(π, (4,4)))
# @test ℯ^(fill(2//3, (4,4))) == exp(fill(2//3, (4,4)))

@test Float16(3.0) < pi
@test pi < Float16(4.0)
Expand Down