19
$\begingroup$

In MATLAB (and Octave), the \ (or mldivide) and \. operators are provided with the exact same functionality as / (or mrdivide) and /., except their arguments are commuted.

I'm looking for any background to this unusual design.

$\endgroup$
6
  • 3
    $\begingroup$ There are other languages supporting rdiv and rsub; it's not just Matlab. $\endgroup$ Jul 21, 2023 at 10:56
  • 1
    $\begingroup$ I may have been thinking of processor instructions (or virtual processor bytecode); no references come to mind. Python allows objects to implement reverse-division and reverse-subtraction protocols - but that's invoked by the ordinary (forward) operators when the first operand isn't suited, so not the same thing. $\endgroup$ Jul 21, 2023 at 14:12
  • 1
    $\begingroup$ \. or .\? $\endgroup$ Jul 22, 2023 at 9:33
  • 1
    $\begingroup$ If you want to know why Python does not have that operator, you can read PEP 211. TL;DR: they were afraid users would mix up b \ A and A \ b. $\endgroup$ Jul 24, 2023 at 13:06
  • 1
    $\begingroup$ Besides linear algebra, this kind of thing makes sense in any non-commutative residuated lattice. The left residual of division on the right, B / A, is like an implication A β†’ B: informally it’s the least you can pair with A to get up to B, without going above B. The right residual of left division, A \ B, is like a difference B βˆ’ A, or the most you can remove from B without going below A. $\endgroup$
    – Jon Purdy
    Jul 24, 2023 at 19:14

3 Answers 3

39
$\begingroup$

Non-commutativity of multiplication

a/b calculates $a \cdot b^{-1}$, whereas b\a calculates $b^{-1} \cdot a$. Since matrix multiplication is not commutative, those are different operations.

$\endgroup$
6
  • 3
    $\begingroup$ Correct, and since systems of linear equations are usually presented like $Ax=b$, then solving by computing $x=A^{-1}b$ is actually the standard operation, so \ makes sense in that context. $\endgroup$
    – Chris Haug
    Jul 22, 2023 at 10:30
  • $\begingroup$ @ChrisHaug I thought the standard operation was Gaussian elimination $\endgroup$ Jul 22, 2023 at 10:31
  • 4
    $\begingroup$ @RodrigodeAzevedo Gaussian elimination is a specific algorithm for finding 𝐴⁻¹ 𝑏. $\endgroup$
    – xigoi
    Jul 22, 2023 at 10:59
  • 2
    $\begingroup$ @RodrigodeAzevedo I didn't say anything specific about how it's computed (in particular, I did not say that you first compute the inverse, and then the product). Symbolically, yes, the standard problem is to compute $A^{-1}b$, as opposed to something like $bA^{-1}$, which is what you would use / for. $\endgroup$
    – Chris Haug
    Jul 22, 2023 at 15:15
  • 4
    $\begingroup$ @RodrigodeAzevedo Matlab needs to handle a number of cases including overdetermined, underdetermined, Hessenberg (almost-triangular), and sparse systems, and as a result the operator automatically selects from a number of algorithms. The textbook Gaussian elimination, a.k.a. LU, is used when favorable. $\endgroup$
    – user71659
    Jul 23, 2023 at 17:37
10
$\begingroup$

Solving linear systems is better than inverting

The main reason is the one in xigoi's answer, noncommutativity, but let me add that a separate operator is handy to have for matrix operations: computing the matrix K = inverse(A) and then multiplying x = K * b is inferior from the point of view of both stability and computational cost. So considering A \ b as a single operation with two operands is more effective than thinking about it as "first invert $A$, then multiply by $b$".

Proxy object

An alternative design choice that solves the same problem and has some advantages is having a method K = factorize(A) which computes an LU factorization (or another more convenient factorization) of the matrix $A$, and returns an "inverse-like" object K for which a method K * b is defined. This choice is very convenient because it allows one to separate and reuse the most expensive part of linear system solution, which is the $O(n^3)$ factorization. So you can write the clearer and more efficient

K = factorize(A); % 2/3*n^3 + O(n^2) for a general nxn matrix A
x1 = K * b1;  % O(n^2) if b1 is a length-n vector
x2 = K * b2;  % O(n^2) if b2 is a length-n vector

rather than

x1 = A \ b1;  % 2/3*n^3 + O(n^2)
x2 = A \ b2;  % 2/3*n^3 + O(n^2)
% total cost 4/3*n^3: the factorization is computed twice.
% Also, with this syntax the factorization cannot be precomputed
% before knowing b1, b2

or

K = inv(A);  % 2*n^3 + O(n^2)
x1 = K * b1; % O(n^2)
x2 = K * b2; % O(n^2)
% x1, x2 computed in this way typically have a higher
% numerical error than A \ b, in floating point.

This "implicit inverse" trick is implemented in Matlab's decomposition, and (with more generality and more methods defined) in Julia's factorize.

In a new language. I can see arguments for going even further and calling this function inverse(A), making it the preferred syntax to perform matrix inversion and linear system solution.

$\endgroup$
0
1
$\begingroup$

Overloading

I'm not familiar with MATLAB/Octave, but this could be used for operator overloading in custom classes (if that's supported in MATLAB/Octave).

Simplifying syntax

If you have something like x = y/x, this could be shortened to x \= y with a left division operator. This might not come up much, but it's still useful to have.

$\endgroup$
8
  • 2
    $\begingroup$ MatLab does indeed support overloading, and yes, / and \ can be overloaded separately. $\endgroup$
    – Adám
    Jul 21, 2023 at 8:37
  • 9
    $\begingroup$ Why would you answer if you're not familiar with the language and haven't searched much about the topic? Why not write a comment instead? Please see the other answer, written on the same day and already with more than 7x more upvotes. $\endgroup$ Jul 22, 2023 at 11:03
  • 8
    $\begingroup$ No your answer is totally wrong. I haven't downvoted because I believe in commenting first and giving people a chance to improve or remove their answers first. The answer has nothing to do with custom classes nor simplifying expressions. The \= in your answer has nothing to do with ldivide. Also, if it's just a guess, why not write a comment rather than an answer? $\endgroup$ Jul 22, 2023 at 11:23
  • 7
    $\begingroup$ The question was not "what are the generic benefits of including a left divide operator in an arbitrary unspecified language". The only thing correct in the first part is the part in parentheses, which OP confirmed (because you started that part with an "if"). You still haven't answered why you didn't write your guess as a comment rather than an answer, but I won't explicitly ask it for a third time. I'll also leave you alone now :) $\endgroup$ Jul 22, 2023 at 11:33
  • 1
    $\begingroup$ @NikeDattani I thought it was a good enough guess to be an answer, and four people have agreed with me. Anyway, bye :) $\endgroup$
    – The Thonnu
    Jul 22, 2023 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .