I translating some code from octave/matlab to Julia. However, the code does not work. I have isolated the problem in the enclosed octave/julia files. The octave code produces the correct result.
The problem is in the calculation of :
G = ((A’*A)^-1)*A’ # Octave
G = inv(transpose(A)*A)*transpose(A) # Julia
My system is :
uname --all
Linux optiplex 5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
The problems are observed on julia-1.5.0, however it is also observed in 1.4.2.
Please advise. I assume that I have misunderstood the Julia syntax.
the condition number of A'*A seems to be ~infinite? That would mean, both the Julia and the Octave results are wrong.
By the way, you can use the same syntax in Julia: G = ((A'*A)^-1)*A'
Check condition number: using LinearAlgebra
Use more precision: B = BigFloat.(A) cond(B'*B)
As others have observed, the matrix is badly conditioned. MATLAB actually tells you this explicitely in the warning message:
>> G = ((A'*A)^-1)*A'
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.015773e-18.
Also, inv is really rarely needed, and is not always more accurate than the back-solve \ operator. So, you may write this elegantly in Julia like this:
When I constructed the unit test for the function I just picked a very “regular” patter that turned out to be poorly conditioned. I assumed that the octave results were correct as I have used the function for many years without problems ( with proper well-conditioned data).