-
Notifications
You must be signed in to change notification settings - Fork 4
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
detx: don't catch exceptions and retry with wider type #18
Conversation
This puts `detx` in line with the common Julia conventions (the limited width of the input is the caller's responsibility), while improving performance and type stability.
I'm not seeing how this works. Here's what I tried:
I'm not comfortable accepting this PR. |
Tried to explain everything above already. Basically, there's a very important principle in Julia called "type stability". The idea is that we want the compiler to be able to infer the exact concrete type of the return value of most functions (especially public API, like this one) given the function and the types of the arguments.
julia> using LinearAlgebraX, Test
julia> A = rand(Int,20,20);
julia> @inferred detx(A)
ERROR: return type BigInt does not match inferred return type Union{Int64, BigInt} There's basically three things you can do:
|
Thanks @nsajko. Your explanation makes a lot of sense. While I might not fully grasp the importance of type stability, I get that it's important and understand that I break it with my code. Your suggestion is this: function detx(A::AbstractMatrix{T}) where {T<:IntegerX}
det(A // true)
end For an integer matrix, though, the resulting determinant would be T.(det(A//true)) What do you think? |
The problem with type unstable code is that instabilities often propagate through to other code, causing inefficiency and run time dispatch. For example, imagine some code like this:
You're absolutely right, except without broadcasting, I think, so Now that I think about it, it might make sense to instead promote narrow integers to Or maybe fully embrace option (3) and promote rationals to big rationals, too. This package is specifically meant for exact computation so it could make sense to do that. |
Thanks! I think option (3) makes the most sense and is consistent with how I mean for this package to work. I'll try to make those changes soon. |
OK, then feel free to close this if you don't need it. |
@nsajko I just submitted version 0.2.0 to be registered. I think I fixed the type stability issues at the cost of always returning |
Nice! |
This puts
detx
in line with the common Julia conventions (the limited width of the input is the caller's responsibility), while improving performance and type stability.