Why does backslash behave differently when transposition is used in-line?
Show older comments
Hello, in the process of creating a problem for my students in a numerical methods class I found out that the backslash operation produces different results if you solve A^{T}x=b as "x=(A')\b" when compared to first defining "AT=A'" and then solving as "x=AT\b". An example is given below. Order of operations suggests that in the first case, A should be transposed and then the backslash operation performed, which is forced in the second case. One would expect the results to then be the same. Why is there a difference at all?
n = 15;
m = 2*n+1;
t = fliplr(cos(pi/(n)*(0:n))).';
A = [t.^(0:m);
t.^([0,0:m-1])*diag(max(0,0:m))];
b = (1./((0:m)'+1).*((1).^((0:m)'+1)-(-1).^(((0:m)'+1))));
x = (A')\b;
AT = A';
x2 = AT\b;
difference = norm(x-x2)
2 Comments
This is indepedent of the question about the difference between those two solutions, but you might find the cospi function useful. [Or perhaps not; it can give a different answer than cos(pi*...). Would you expect those two calls to return exactly down-to-the-last-bit (DTTLB) identical answers?]
n = 15;
V = (1/n)*(0:n);
t1 = cos(pi*V);
t2 = cospi(V);
norm(t1-t2)
n = 15;
m = 2*n+1;
t = fliplr(cos(pi/(n)*(0:n))).';
A = [t.^(0:m);
t.^([0,0:m-1])*diag(max(0,0:m))];
cond(A)
b = (1./((0:m)'+1).*((1).^((0:m)'+1)-(-1).^(((0:m)'+1))));
norm(b)
Note that the error seen is exactly as would be expected, when a subtly different order of operations is performed. That is, we would expect to see floating point trash on the order of:
cond(A)*eps(norm(b))
which is quite roughly what you got.
Never presume that two such computations are performed in exactly the same sequence, and if they are not, then you can and often will see floating point trash creep in like this:
0.3 - 0.1 - 0.2
-0.1 - 0.2 + 0.3
Accepted Answer
More Answers (1)
The already given answers explain the effect alreayd. A summary:
Why does backslash behave differently when transposition is used in-line?
To improve performance.
Matlab calls BLAS and LAPACK functions, which consider the state of transposition of the input also.
Instable systems are highly sensitive to tiny variations of the start values and applied methods. This concerns physical systems like a double pendulum and ill-conditioned linear systems.
Either way, a 0.0022 percent difference is too large for my purposes.
The results produced by instable systems are random. If different computers or algorithms produce different results, there is no "better" or "worse" solution. The exact reproduction of a result of an ill-conditioned linear equation, you have to call the same BLAS/LAPACK method on the same CPU. Of course, you can do this with Matlab e.g. by using: https://www.mathworks.com/matlabcentral/fileexchange/16777-lapack . But the general problem remains, that the results will differ on other CPUs, with a different number of threads, with another BLAS/MKL version or used compiler.
You cannot draw meaningful conclusions from results of instable systems. This concerns rolling a dice or computing the sum with limited precision:
[1e16 + 1 - 1e16, 1e16 - 1e16 + 1]
The order of arguments matters. If the difference between the results of an instable system does not match the purpose, this is a problem of the purpose.
5 Comments
Jonah A Reeger
on 28 Apr 2026
@Jonah A Reeger: we are just volunteers on this forum, we cannot change how MATLAB works. If you want to give feedback or make an improvement request to TMW then you can do so here:
You can include a link to this thread in your improvement request.
And, since transposition as an operation is well conditioned, a 0.0022 percent difference when I expected the same operations to be performed in either case is too large.
While the transposition is stable, solving A\b for an ill-conditioned system is not.
I just did not expect different algorithms to be applied to solve the problem in this case.
This means, that your expectations do not match how Matlab computes the solution. With the knowledge, that Matlab applies a JIT-optimization inside code lines and that the computations are forwared to a BLAS function, the behavior is expected.
It is important to know, how a tool works. You do see how it works now. Then it is not useful to insist on expecting a different behavior.
Only being able to discover how it works by reverse engineering is in direct contradiction to the documentation is the issue/problem irrespective of the actual numerics. One certainly has justifcation from the documentation to expect that the parentheses will control order of execution.
It's not likely TMW will choose to change the behavior, but certainly the documentation should somehow make note that optimizations may cause intermediate results to not be explicitly computed despite expressed preference for such with parentheses although as noted, they have adamantly refused to reveal anything about JIT optimizations other than they are implemented. As noted by others MATLAB has always been quirky and only continues to become more so with time. I've commented often in the past that since there is no published formal language standard as with C or Fortran, one has no way to find out what is or is not actually a compliant behavior -- "it does what it does".
I just did not expect different algorithms to be applied to solve the problem in this case.
They are not different algorithms. The solution algorithm as applied with AT\b or with (A')\b are mathematically equivalent. The operational differences that are giving you 0.0022 percent errors are not from the transposition or from differences in the mathematical recipe being used. They are from low level differences in the way the CPU is multithreading things. If you were to run your code on different CPUs, you would see differences of 0.0022 percent, even if you used a pre-transposed matrix AT\b in both cases.
I agree that disregarding parentheses is controversial, but it is not at the heart of the numerical discrepancies you are seeing.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!