I am trying to use the lusolve
function from mathjs 3.8.0 to solve a linear system. However, I have some trouble interpreting the returned result with respect to the input.
I will explain based upon the example from the docs: The example sources begin
var m = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]; var x = math.lusolve(m, [-1, -1, -1, -1]); // x = [[-1], [-0.5], [-1/3], [-0.25]]
So far, so good - this represents the system
1a = -1
2b = -1
3c = -1
4d = -1
The solution is, obviously
a = -1
b = -0.5
c = -1/3
d = -0.25
as stated in the comment from the original example.
The actual return value is an array with the values in the same order as the input vectors, i.e. [[-1], [-0.5], [-1/3], [-0.25]]
.
However, now I try to switch to of the input vectors:
var m = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 0, 4], [0, 0, 3, 0]];
var x = math.lusolve(m, [-1, -1, -1, -1]);
I thought this should represent the system
1a = -1
2b = -1
3d = -1
4c = -1
If so, the solution should be
a = -1
b = -0.5
c = -0.25
d = -1/3
or [[-1], [-0.5], [-0.25], [-1/3]]
in JavaScript.
However, the actual return value of the function for this input is still [[-1],[-0.5],[-1/3],[-0.25]]
, like with the original input vector ordering.
Why is that? How does the ordering of returned coefficients match with the ordering of input vectors?
LU decomposition method returns the upper and lower triangular matrices which then might be used to solve the linear system. This means that the matrices will be reordered to have this shape (zeros under the diagonal and up of it).
For the example you give, you can see how this looks like doing:
math.lup([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
See math.lusolve
source for more specific info.
Also note how the matrices map to a vector
http://mathjs.org/docs/datatypes/matrices.html#creation
there you will see an example matrix of
math.matrix([[0, 1], [2, 3], [4, 5]]); /* Matrix of size [3, 2] */
which means that each inner vector is a row not a column. That is:
[[0, 1], [2, 3], [4, 5]]
is written as
0 1
2 3
4 5