StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Feature request: reduce usage of arithmetic_closure

Open marius311 opened this issue 3 years ago • 0 comments

I'm probably missing a detail since I'm not super familiar with this code base, but whats the reasoning behind all the use of arithmetic_closure ? E.g. for determinant we have:

@inline function det(A::StaticMatrix)
    T = eltype(A)
    S = arithmetic_closure(T)
    A_S = convert(similar_type(A,S),A)
    _det(Size(A_S),A_S)
end

@inline function _det(::Size{(2,2)}, A::StaticMatrix)
    @inbounds return A[1]*A[4] - A[3]*A[2]
end

But why do the promotion up front via arithmetic_closure, why not just let the compiler infer and promote as needed as it works though A[1]*A[4] - A[3]*A[2]?

The presence of arithmetic_closure makes it so that lots of objects that might have otherwise worked instead break because they either need arithmetic_closure defined or one and zero, in addition to just * and - which should be enough for determinant. As an example that comes up alot for me, block matrices as SMatrices of matrices:

a,b,c,d = (rand(2,2) for i=1:4)
A = @SMatrix[a b; c d]
det(A) # error
StaticArrays._det(StaticArrays.Size(2,2), A) # fine

Any chance to get rid of it in as many places as possible?

marius311 avatar Jan 20 '23 07:01 marius311