Backslash operator "\" works differently in REPL versus in function?

I have two Array{Float32,2} variables, X1 and X2. Both are 5 by 949.

In the REPL, this works:

Julia> X2\X1
949x949 Array{Float32,2}:
<lots of stuff>

which is what I expect and want.

but doing the exact same operation inside a function:

function mydiv(X1,X2)

    K=X2\X1
end

gives

ERROR: DimensionMismatch("second dimension of A, 949, does not match length of x, 5")
Stacktrace:
 [1] gemv!(::Array{Float32,1}, ::Char, ::Array{Float32,2}, ::Array{Float32,1}, ::Bool, ::Bool) at /build/julia-98cBbp/julia-1.4.1+dfsg/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:456
 [2] mul! at /build/julia-98cBbp/julia-1.4.1+dfsg/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:66 [inlined]
 [3] mul! at /build/julia-98cBbp/julia-1.4.1+dfsg/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:208 [inlined]
 [4] *(::Array{Float32,2}, ::Array{Float32,1}) at /build/julia-98cBbp/julia-1.4.1+dfsg/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:47

What am I doing wrong here?

Is that the full stacktrace? Also, 1.4 is quite an old version of Julia: do you see the same problem in 1.6?

For some reason it appears that you are trying to operate with one 1-dimensional array and one 2-dimensional array.
[4] *(::Array{Float32,2}, ::Array{Float32,1}) at /build/julia-98cBbp/julia-1.4.1+dfsg/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:47

Could you triple check your inputs or provide the code that fails?

This works for me on v1.4.0:

julia> a = rand(5,949);

julia> b = rand(5,949);

julia> K = a\b;

julia> function mydiv(X1,X2)
           K=X2\X1
       end
mydiv (generic function with 1 method)

julia> K2 = mydiv(b,a);

julia> K == K2
true

2 Likes