I port the algorithm that works in Matlab to numpy and have observed strange behavior. Corresponding code segment
P = eye(4)*1e20;
A = [1 -0.015 -0.025 -0.035; 0.015 1 0.035 -0.025; 0.025 -0.035 1 0.015; 0.035 0.025 -0.015 1];
V1 = A*(P*A')
V2 = (A*P)*A'
This code, when I run with Matlab, provides the following matrices:
V1 = 1.0021e+20 0 -8.0000e+00 0
0 1.0021e+20 0 0
-8.0000e+00 0 1.0021e+20 0
0 0 0 1.0021e+20
V2 = 1.0021e+20 0 -8.0000e+00 0
0 1.0021e+20 0 0
-8.0000e+00 0 1.0021e+20 0
0 0 0 1.0021e+20
Note that V1 and V2 are the same as expected.
When the same code runs on Octave, it provides:
V1 = 1.0021e+20 4.6172e+01 -1.3800e+02 1.8250e+02
-4.6172e+01 1.0021e+20 -1.8258e+02 -1.3800e+02
1.3801e+02 1.8239e+02 1.0021e+20 -4.6125e+01
-1.8250e+02 1.3800e+02 4.6125e+01 1.0021e+20
V2 = 1.0021e+20 -4.6172e+01 1.3801e+02 -1.8250e+02
4.6172e+01 1.0021e+20 1.8239e+02 1.3800e+02
-1.3800e+02 -1.8258e+02 1.0021e+20 4.6125e+01
1.8250e+02 -1.3800e+02 -4.6125e+01 1.0021e+20
In numpy segment becomes
from numpy import array, dot, eye
A = numpy.array([[1, -0.015, -0.025, -0.035],[0.015, 1, 0.035, -0.025],[0.025, -0.035, 1, 0.015],[0.035, 0.025, -0.015, 1]])
P = numpy.eye(4)*1e20
print numpy.dot(A,numpy.dot(P,A.transpose()))
print numpy.dot(numpy.dot(A,P),A.transpose())
which outputs
[[ 1.00207500e+20 4.61718750e+01 -1.37996094e+02 1.82500000e+02]
[ -4.61718750e+01 1.00207500e+20 -1.82582031e+02 -1.38000000e+02]
[ 1.38011719e+02 1.82386719e+02 1.00207500e+20 -4.61250000e+01]
[ -1.82500000e+02 1.38000000e+02 4.61250000e+01 1.00207500e+20]]
[[ 1.00207500e+20 -4.61718750e+01 1.38011719e+02 -1.82500000e+02]
[ 4.61718750e+01 1.00207500e+20 1.82386719e+02 1.38000000e+02]
[ -1.37996094e+02 -1.82582031e+02 1.00207500e+20 4.61250000e+01]
[ 1.82500000e+02 -1.38000000e+02 -4.61250000e+01 1.00207500e+20]]
So both Octave and numpy give the same answer, but it is very different from Matlab. The first point is that V1! = V2, which seems wrong. Another thing is that although the off-diagonal elements are many orders of magnitude smaller than the diagonal ones, this, apparently, causes some problem in my algorithm.
Does anyone know how numpy and Octave behave this way?