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

Behavior of ^(::Matrix{<:Integer}, ::Integer) inconsistent with behavior of ^(::Integer, ::Integer) #23366

Closed
afniedermayer opened this issue Aug 20, 2017 · 1 comment
Labels
linear algebra Linear algebra

Comments

@afniedermayer
Copy link
Contributor

afniedermayer commented Aug 20, 2017

^(::Matrix{Int64}, ::Int64) is type unstable. See e.g.

fibonacci_Q = [1 1;1 0]
@code_warntype fibonacci_Q^2

which shows Union{Array{Float64,2}, Array{Int64,2}}. The reason seems to be that ^(A::Matrix{Int64}, p::Int64) returns a Matrix{Float64} if p<0.

This is inconsistent with the behavior of ^(x::Int64, p::Int64), which returns an Int64 if p>=0 or if x==1 or x==-1. Otherwise, it throws a DomainError.

@afniedermayer
Copy link
Contributor Author

A related issue (also fixed by #23368 ) is that ^(::AbstractArray{S}, ::T) where S<:Integer, T<:Integer does not promote types the same way as ^(::S, ::T). For example, [1 1;1 0]^big(10000) returns a Matrix{Int64} (which overflows) rather than Matrix{BigInt}. This behavior is different from 2^big(10000), which returns a BigInt.

I'll adjust the title of the issue to reflect this.

@afniedermayer afniedermayer changed the title ^(::Matrix{Int64}, ::Int64) is not type stable Behavior of ^(::Matrix{<:Integer}, ::Integer) inconsistent with behavior of ^(::Integer, ::Integer) Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

No branches or pull requests

2 participants