<p>In MATLAB (and Octave), the <code>\</code> (or <a href="https://www.mathworks.com/help/matlab/ref/mldivide.html" rel="noreferrer"><code>mldivide</code></a>) and <code>\.</code> operators are provided with the exact same functionality as <code>/</code> (or <a href="https://www.mathworks.com/help/matlab/ref/mrdivide.html" rel="noreferrer"><code>mrdivide</code></a>) and <code>/.</code>, except their arguments are commuted.</p>
<p>I'm looking for any background to this unusual design.</p>

<operators><math><division><matlab-octave><history>
Why does MATLAB have left division/solve?
CC BY-SA 4.0
2023-07-21T10:56:57.370Z
There are other languages supporting rdiv and rsub; it's not just Matlab.
Toby Speight
CC BY-SA 4.0
2023-07-21T13:47:15.860Z
@TobySpeight I didn't know that. If you list them here, I can include them in the question.
Adám
CC BY-SA 4.0
2023-07-21T14:12:08.457Z
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.
Toby Speight
CC BY-SA 4.0
2023-07-22T09:33:29.750Z
`\.` or [```.\```](https://www.mathworks.com/help/matlab/ref/ldivide.html)?
Rodrigo de Azevedo
CC BY-SA 4.0
2023-07-24T13:06:48.910Z
If you want to know why Python does *not* have that operator, you can read [PEP 211](https://peps.python.org/pep-0211/#background). TL;DR: they were afraid users would mix up `b \ A` and `A \ b`.
Federico Poloni
CC BY-SA 4.0
2023-07-24T19:14:20.697Z
Besides linear algebra, this kind of thing makes sense in any non-commutative [residuated lattice](https://en.wikipedia.org/wiki/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*.
Jon Purdy
CC BY-SA 4.0
2024-05-29T08:13:21.097Z
@JonPurdy I have seen a suggestion on the Julia forum to use this operator to remove an initial or final substring: since Julia already uses `*` for string concatenation, `"Hello " * "world" == "Hello world"`; so it would make sense to define `"Hello " \ "Hello world" == "world"`, and `"Hello world" / "world" == "Hello "`.
Federico Poloni
<h2>Overloading</h2>
<p>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).</p>
<h2>Simplifying syntax</h2>
<p>If you have something like <code>x = y/x</code>, this could be shortened to <code>x \= y</code> with a left division operator. This might not come up much, but it's still useful to have.</p>

CC BY-SA 4.0
2023-07-21T08:21:25.150Z
2023-07-22T10:03:40.123Z
2023-07-22T10:03:40.123Z
The Thonnu
CC BY-SA 4.0
2023-07-21T08:37:59.237Z
MatLab does indeed support overloading, and yes, `/` and ``\`` can be overloaded separately.
Adám
CC BY-SA 4.0
2023-07-22T11:03:59.977Z
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.
Nike Dattani
CC BY-SA 4.0
2023-07-22T11:05:56.220Z
@NikeDattani they're both valid answers, it's just that the other answer is better than this one. Just because I don't know MATLAB doesn't mean I can't make an educated guess (which I did correctly). It doesn't make this answer any less valid.
The Thonnu
CC BY-SA 4.0
2023-07-22T11:23:20.137Z
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?
Nike Dattani
CC BY-SA 4.0
2023-07-22T11:28:05.400Z
@NikeDattani alright, think of that as if the question wasn't only about MATLAB, and was just generically "what are the benefits of including a left divide operator?" Also, what's wrong with the first part? It's a reasonable suggestion. There isn't only one answer to this question.
The Thonnu
CC BY-SA 4.0
2023-07-22T11:33:05.777Z
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 :)
Nike Dattani
CC BY-SA 4.0
2023-07-22T11:46:02.617Z
@NikeDattani I thought it was a good enough guess to be an answer, and four people have agreed with me. Anyway, bye :)
The Thonnu
<h2>Solving linear systems is better than inverting</h2>
<p>The main reason is the one in <a href="https://langdev.stackexchange.com/a/2489/1245">xigoi's answer</a>, noncommutativity, but let me add that a separate operator is handy to have for matrix operations: computing the matrix <code>K = inverse(A)</code> and then multiplying <code>x = K * b</code> is inferior from the point of view of both stability and computational cost. So considering <code>A \ b</code> as a single operation with two operands is more effective than thinking about it as &quot;first invert $A$, then multiply by $b$&quot;.</p>
<h2>Proxy object</h2>
<p>An alternative design choice that solves the same problem and has some advantages is having a method <code>K = factorize(A)</code> which computes an LU factorization (or another more convenient factorization) of the matrix $A$, and returns an &quot;inverse-like&quot; object <code>K</code> for which a method <code>K * b</code> 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</p>
<pre class="lang-matlab prettyprint-override"><code>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
</code></pre>
<p>rather than</p>
<pre class="lang-matlab prettyprint-override"><code>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
</code></pre>
<p>or</p>
<pre class="lang-matlab prettyprint-override"><code>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.
</code></pre>
<p>This &quot;implicit inverse&quot; trick is implemented in Matlab's <a href="https://it.mathworks.com/help/matlab/ref/decomposition.html" rel="noreferrer"><code>decomposition</code></a>, and (with more generality and more methods defined) in Julia's <a href="https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.factorize" rel="noreferrer"><code>factorize</code></a>.</p>
<p>In a new language. I can see arguments for going even further and calling this function <code>inverse(A)</code>, making it the preferred syntax to perform matrix inversion and linear system solution.</p>

CC BY-SA 4.0
2023-07-24T13:21:17.650Z
2023-07-25T06:58:42.030Z
2023-07-25T06:58:42.030Z
Federico Poloni
<h1>Non-commutativity of multiplication</h1>
<p><code>a/b</code> calculates <span class="math-container">$a \cdot b^{-1}$</span>, whereas <code>b\a</code> calculates <span class="math-container">$b^{-1} \cdot a$</span>. Since matrix multiplication is not commutative, those are different operations.</p>

CC BY-SA 4.0
2023-07-21T08:27:54.727Z
2023-08-11T16:23:35.957Z
2023-08-11T16:23:35.957Z
xigoi
CC BY-SA 4.0
2023-07-22T10:30:13.300Z
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.
Chris Haug
CC BY-SA 4.0
2023-07-22T10:31:52.993Z
@ChrisHaug I thought the standard operation was Gaussian elimination
Rodrigo de Azevedo
CC BY-SA 4.0
2023-07-22T10:59:47.877Z
@RodrigodeAzevedo Gaussian elimination is a specific algorithm for finding ⁻¹ .
xigoi
CC BY-SA 4.0
2023-07-22T11:00:53.990Z
@xigoi Have you counted the FLOPs?
Rodrigo de Azevedo
CC BY-SA 4.0
2023-07-22T15:15:49.443Z
@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.
Chris Haug
CC BY-SA 4.0
2023-07-23T17:37:43.903Z
@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](https://www.mathworks.com/help/matlab/ref/mldivide.html#bt4jslc-6) of algorithms. The textbook Gaussian elimination, a.k.a. LU, is used when favorable.
user71659
# Why does MATLAB have left division/solve?
Posted by: username_0 on 2023-07-21
In MATLAB (and Octave), the `\` (or [`mldivide`](https://www.mathworks.com/help/matlab/ref/mldivide.html)) and `\.` operators are provided with the exact same functionality as `/` (or [`mrdivide`](https://www.mathworks.com/help/matlab/ref/mrdivide.html)) and `/.`, except their arguments are commuted.

I'm looking for any background to this unusual design.
**Tags:** operators math division matlab-octave history
> **Comments:**
> **username_1**: There are other languages supporting rdiv and rsub; it's not just Matlab.  
> **username_0**: @TobySpeight I didn't know that. If you list them here, I can include them in the question.  
> **username_1**: 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.  
> **username_2**: `\.` or [```.\```](https://www.mathworks.com/help/matlab/ref/ldivide.html)?  
> **username_3**: If you want to know why Python does *not* have that operator, you can read [PEP 211](https://peps.python.org/pep-0211/#background). TL;DR: they were afraid users would mix up `b \ A` and `A \ b`.  
> **username_4**: Besides linear algebra, this kind of thing makes sense in any non-commutative [residuated lattice](https://en.wikipedia.org/wiki/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*.  
> **username_3**: @JonPurdy I have seen a suggestion on the Julia forum to use this operator to remove an initial or final substring: since Julia already uses `*` for string concatenation, `"Hello " * "world" == "Hello world"`; so it would make sense to define `"Hello " \ "Hello world" == "world"`, and `"Hello world" / "world" == "Hello "`.  
---
## 3 answers
### Answer by username_7 on 2023-07-21. Score: 39
# 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.
> **Comments:**
> **username_8** (2023-07-22): 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.  
> **username_2** (2023-07-22): @ChrisHaug I thought the standard operation was Gaussian elimination  
> **username_7** (2023-07-22): @RodrigodeAzevedo Gaussian elimination is a specific algorithm for finding ⁻¹ .  
> **username_2** (2023-07-22): @xigoi Have you counted the FLOPs?  
> **username_8** (2023-07-22): @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.  
> **username_9** (2023-07-23): @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](https://www.mathworks.com/help/matlab/ref/mldivide.html#bt4jslc-6) of algorithms. The textbook Gaussian elimination, a.k.a. LU, is used when favorable.  
---
### Answer by username_3 on 2023-07-24. Score: 10
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.
---
### Answer by username_5 on 2023-07-21. Score: 1
## 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.
> **Comments:**
> **username_0** (2023-07-21): MatLab does indeed support overloading, and yes, `/` and ``\`` can be overloaded separately.  
> **username_6** (2023-07-22): 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.  
> **username_5** (2023-07-22): @NikeDattani they're both valid answers, it's just that the other answer is better than this one. Just because I don't know MATLAB doesn't mean I can't make an educated guess (which I did correctly). It doesn't make this answer any less valid.  
> **username_6** (2023-07-22): 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?  
> **username_5** (2023-07-22): @NikeDattani alright, think of that as if the question wasn't only about MATLAB, and was just generically "what are the benefits of including a left divide operator?" Also, what's wrong with the first part? It's a reasonable suggestion. There isn't only one answer to this question.  
> **username_6** (2023-07-22): 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 :)  
> **username_5** (2023-07-22): @NikeDattani I thought it was a good enough guess to be an answer, and four people have agreed with me. Anyway, bye :)  
---
