Aidan Rosswood
Aidan Rosswood

Reputation: 1212

What does Julia's backslash (\) operator do on two matrices?

In the context of matrices, if we have A\B what mathematical operation is being executed on A and B. The documentation seems to state that it is division but I thought division was an invalid operation for matrices.

Upvotes: 2

Views: 2280

Answers (2)

Fredrik Bagge
Fredrik Bagge

Reputation: 1381

You can figure out which method is called by using @which (or @edit)

A = randn(10,2)
b = randn(10)
@which A\b

which leads to the implementation

function (\)(A::AbstractMatrix, B::AbstractVecOrMat)
    require_one_based_indexing(A, B)
    m, n = size(A)
    if m == n
        if istril(A)
            if istriu(A)
                return Diagonal(A) \ B
            else
                return LowerTriangular(A) \ B
            end
        end
        if istriu(A)
            return UpperTriangular(A) \ B
        end
        return lu(A) \ B
    end
    return qr(A,Val(true)) \ B
end

where you can see that what method is used depends on the structure of the matrices. In the absence of any useful structure, a QR factorization is performed, using which the linear system is solved.

Upvotes: 3

Aidan Rosswood
Aidan Rosswood

Reputation: 1212

Julia documentation for linear algebra has the explanation here. "For input matrices A and B, the result X is such that A*X == B".

In the context of a square matrix A, A\B returns inverse(A) * B.

Upvotes: 3

Related Questions