Matrixmultiplikation

Aus DGL Wiki
Version vom 7. Januar 2009, 14:22 Uhr von Flash (Diskussion | Beiträge) (Emres Code)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Emre lieferte im Forum diesen kleinen Codeschnipsel für die Matrixmultiplikation ab:

Type
  TSMatrix = Array of Array of Single;

//sMatrix := sMatrix * Matrix
procedure pSMatrixMatrixProduct( var sMatrix: TSMatrix; const Matrix: TSMatrix );
var
  m, n,
  o : Integer;
  Res  : TSMatrix;
begin
  {Matrices can only be multiplicated, if the row count of the matrix#1 is the same
  as the column of the second matrix:}
  if High(sMatrix) <> (High(Matrix[0])) then
    Exit;
  {if a k*l matrix is multiplicated by a m*n matrix,
   the result matrix will have a k*n dimension:}
  SetLength( Res, Length(sMatrix), Length(Matrix[0]) );
  for m := 0 to High(Res) do
    for n := 0 to High(Res[m]) do
      for o := 0 to High(Matrix) do
        incS( Res[m,n], sMatrix[m,o]*Matrix[o,n] );
  sMatrix := Res;
end;