Assign Uniform scaling to Matrix with setindex


#1

Say I have a function

function f(a,x)
    a[:,:] = x
end
a = zeros(2,2)
x = eye(2)
f(a,x) # Works fine
f(a,I) # Does not work
ERROR: MethodError: Cannot `convert` an object of type LinearAlgebra.UniformScaling{Bool} to an object of type Float64

Is there something I can do in f such that it can handle both input arguments of type X::Matrix and x::UniformScaling? My example f is simple, but my real use case involves a more complicated function and it would be nice if it could handle arguments of type UniformScaling
Thanks!


#2

The following works, but is arguably not that pretty

function f(a,x)
    a[:,:]  = x isa AbstractMatrix ? x : Matrix(x)
end

#3

copy!(a, x) (or Compat.copyto!(a, x) to be 0.7-compliant) works.


#4

Thanks! I ended up having to use

copy!(@view(a[:,:,1]), x)

since my actual target matrix was three dimensional.