Say I have the following line of code in Julia
C = A*B
where A
and B
are matrices, i.e.
typeof(A) == typeof(B) <: AbstractMatrix
If A
is a multiple of the appropriately sized identity matrix then it is convenient to represent it as A = UniformScaling(x)
for some scalar x
. That works great except when you need to create A
in one function then pass it into another. UniformScaling
is not a subtype of AbstractArray
(this is the case in Julia 0.5 and on master) so the following code doesn’t work
f(A::AbstractMatrix,B::AbstractMatrix) = A*B
A = UniformScaling(1.0);
B = sprandn(100,100,0.1);
C = f(A,B)
which you can quickly confirm at the REPL:
julia> f(A::AbstractMatrix,B::AbstractMatrix) = A*B
f (generic function with 1 method)
julia> A = UniformScaling(1.0);
julia> B = sprandn(100,100,0.1);
julia> C = f(A,B)
ERROR: MethodError: no method matching f(::UniformScaling{Float64}, ::SparseMatrixCSC{Float64,Int64})
Closest candidates are:
f(::AbstractArray{T,2}, ::AbstractArray{T,2}) at REPL[1]:1
julia>
I’m wondering if that is the intended behaviour or a bug? The one reason I can think of off the top of my head for why UniformScaling shouldn’t be a subtype of AbstractArray is that methods that should be supported for any array type (e.g. size, ndims) don’t exist for UniformScaling. I don’t know enough to know if that’s reason to nix the idea or not.
If the consensus is that making UniformScaling
a subtype of AbstractArray
is a good idea, I’ll make a PR. If not, does anyone know of another way to generate a placeholder for a very large identity matrix with a negligible memory footprint that can be passed to functions that take AbstractMatrix
inputs?