I assume from your screenshot that you want to multiply the matrix

` 1 0 0 1`

0 1 0 1

0 0 1 0

0 0 0 1

with the vector [2, 3, 4, 1].

Using SciLab it can be shown that this product is [3, 4, 4, 1]:

`M =`

1.0000 0.0000 0.0000 1.0000

0.0000 1.0000 0.0000 1.0000

0.0000 0.0000 1.0000 0.0000

0.0000 0.0000 0.0000 1.0000

v =

2.0000

3.0000

4.0000

1.0000

M*v =

3.0000

4.0000

4.0000

1.0000

And here matrix.pas:

program Project1;

{$mode objfpc}{$H+}

uses

Matrix;

var

M: TMatrix4_double;

v: TVector4_double;

procedure PrintMatrix(Caption: String; M: TMatrix4_double);

var

i, j: Integer;

begin

WriteLn(Caption, ' =');

for i := 0 to 3 do begin

for j := 0 to 3 do

Write(M.Data[i, j]:10:4);

WriteLn;

end;

WriteLn;

end;

procedure PrintVector(Caption: String; V: TVector4_double);

var

i: Integer;

begin

WriteLn(Caption, ' =');

for i := 0 to 3 do

WriteLn(V.Data[i]:10:4);

WriteLn;

end;

begin

M.Init(

1, 0, 0, 1,

0, 1, 0, 1,

0, 0, 1, 0,

0, 0, 0, 1

);

v.Init(

2,

3,

4,

1

);

PrintMatrix('M', M);

PrintVector('v', v);

PrintVector('M*v', M*v);

ReadLn;

end.

Output:

`M =`

1.0000 0.0000 0.0000 1.0000

0.0000 1.0000 0.0000 1.0000

0.0000 0.0000 1.0000 0.0000

0.0000 0.0000 0.0000 1.0000

v =

2.0000

3.0000

4.0000

1.0000

M*v =

3.0000

4.0000

4.0000

1.0000

The same result! This confirms that the calculation of the matrix unit is correct.

So, what is wrong with your example?

I see the discrepancy between the screenshot (which shows the matrix above) and the instructions how you create it:

m1.init_identity;

m1.data[3,0]:=1;

m1.data[3,1]:=1;

You start with an identity matrix and put "1" into the elements [3,0] and [3,1]. However, according to math conventions the first index of a matrix is the index of the row and the second index is the index of the column. Therefore you do not add the 1's to the upper right corner of the matrix, but to the lower left corner! And this, effectively, is the transpose of the matrix that you want. According to SciLab, the product of the transposed matrix with the vector is [2, 3, 4, 6]. This is the result that you show in the lower part of the screenshot.

What's wrong with the screenshot? You did the same error that I did in my previous post: confused the order of the index. As already said math identifies the rows with the first index and the columns with the second index, but the stringgrid uses the first index for the column and the second index for the row; therefore, in your code the matrix indexes i, j show appear in reverse order for the grid cells. Since they are in the same order you effectively draw the transposed matrix!

Another issue: You do have a "m1.Transpose" in your code (I don't know why), but this is useless, because Transpose is a function: it does not in-place transpose the matrix data but transfers the transposed data to another matrix - but you do not assign the result of the function to a variable; therefore you discard the result of the operation.