Where is backslash (\) implemented?

Hi, I’m new to Julia. Recently, I’ve used the backslash (\) operator to solve a linear system. However, I don’t know how it is implemented. I wanted to know it so I traced the code.

I am able to locate to line 1110 at julia/stdlib/LinearAlgebra/src/generic.jl at master · JuliaLang/julia (github.com)

However, I don’t know what code will do next. For example, what will be done in line 1126? I don’t see any definition of backslash operator (\) there.

To find the specific code run in a function call you can simply use julia> @edit myfun(arg1, arg2) and then your favorite editor will pop up at the correct file and line.

Edit: I realized that I may have misunderstood your question. The short answer to “what happens when I do A\B?” is “it depends on what A and B are”. You can see that the function starting in line 1110 starts checking some different cases where better solutions exists or else uses the default fallback of qr(A)\B or lu(A) \ B for square matrices. So you’ll need to trace the calls further by for example julia> @edit qr(A)\B

5 Likes

Welcome to the Julia Discourse!

In 1126 the qr decomposition is computed and for that (with multiple dispatch) this method

is called.

3 Likes

Line 1116

return Diagonal(A) \ B

you find in
Line 455 in https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/diagonal.jl

julia> @which Diagonal([1 1;2 2])\[2 2 ; 2 2]
\(D::Diagonal, B::AbstractMatrix)
     @ LinearAlgebra C:\Users\Oli\.julia\juliaup\julia-1.9.3+0.x64.w64.mingw32\share\julia\stdlib\v1.9\LinearAlgebra\src\diagonal.jl:412

Local on my installed version its in line 412.

Just as an example. You find the other implementations/methods in a similar way.

4 Likes

In general, a subroutine in a LAPACK library outside julia may ultimately be called to solve the system: the default library that comes with julia is OpenBLAS.

4 Likes

Shameless plug: You can try TraceFuns.jl to see which methods are actually hit

@trace [1 2; 3 4] \ [1, 2] Base.:\
# Or for an even larger picture
import LinearAlgebra
@trace [1 2; 3 4] \ [1, 2] LinearAlgebra
8 Likes

Thank you. I think I know how to trace the code now.

1 Like

Thank you. It is an interesting tool.

1 Like

Thank you. I know how to find the backslash for qr now.

2 Likes

I try to use @edit qr(A)\b. However, it only opens the LinearAlgebra.jl. I still don’t know where I can find the implementation.

1 Like