Results 1 to 3 of 3

Thread: Is this 3x3 matrix multiply function right?

  1. #1

    Is this 3x3 matrix multiply function right?

    Hello!

    A simple question. I translated this 4x4 matrix multiply code from a C function:

    Code:
    function gr_Multiply(
      const M1, M2: gr_aMatrix4x4
      ): gr_aMatrix4x4;
    
      var
        I, J, K: Integer;
    
    begin
      Result := gr_Matrix4x4;
      for I := 0 to 3 do
        for J := 0 to 3 do
          for K := 0 to 3 do
            Result[I,J] := Result[I,J]+M1[I,K]*M2[K,J];
    end;
    Now I need a 3x3 matrix multiply, so I just reduced the size of the For loops:

    Code:
    function gr_Multiply(
      const M1, M2: gr_aMatrix3x3
      ): gr_aMatrix3x3;
    
      var
        I, J, K: Integer;
    
    begin
      Result := gr_Matrix3x3;
      for I := 0 to 2 do
        for J := 0 to 2 do
          for K := 0 to 2 do
            Result[I,J] := Result[I,J]+M1[I,K]*M2[K,J];
    end;
    Is this code ok? :?

  2. #2

    Is this 3x3 matrix multiply function right?

    I don't know i use functions from GLScene:

    [pascal]
    // MatrixMultiply (4x4, func)
    //
    function MatrixMultiply(const M1, M2: TMatrix): TMatrix;
    begin
    begin
    Result[X, X] := M1[X, X] * M2[X, X] + M1[X, Y] * M2[Y, X] + M1[X, Z] * M2[Z, X] + M1[X, W] * M2[W, X];
    Result[X, Y] := M1[X, X] * M2[X, Y] + M1[X, Y] * M2[Y, Y] + M1[X, Z] * M2[Z, Y] + M1[X, W] * M2[W, Y];
    Result[X, Z] := M1[X, X] * M2[X, Z] + M1[X, Y] * M2[Y, Z] + M1[X, Z] * M2[Z, Z] + M1[X, W] * M2[W, Z];
    Result[X, W] := M1[X, X] * M2[X, W] + M1[X, Y] * M2[Y, W] + M1[X, Z] * M2[Z, W] + M1[X, W] * M2[W, W];
    Result[Y, X] := M1[Y, X] * M2[X, X] + M1[Y, Y] * M2[Y, X] + M1[Y, Z] * M2[Z, X] + M1[Y, W] * M2[W, X];
    Result[Y, Y] := M1[Y, X] * M2[X, Y] + M1[Y, Y] * M2[Y, Y] + M1[Y, Z] * M2[Z, Y] + M1[Y, W] * M2[W, Y];
    Result[Y, Z] := M1[Y, X] * M2[X, Z] + M1[Y, Y] * M2[Y, Z] + M1[Y, Z] * M2[Z, Z] + M1[Y, W] * M2[W, Z];
    Result[Y, W] := M1[Y, X] * M2[X, W] + M1[Y, Y] * M2[Y, W] + M1[Y, Z] * M2[Z, W] + M1[Y, W] * M2[W, W];
    Result[Z, X] := M1[Z, X] * M2[X, X] + M1[Z, Y] * M2[Y, X] + M1[Z, Z] * M2[Z, X] + M1[Z, W] * M2[W, X];
    Result[Z, Y] := M1[Z, X] * M2[X, Y] + M1[Z, Y] * M2[Y, Y] + M1[Z, Z] * M2[Z, Y] + M1[Z, W] * M2[W, Y];
    Result[Z, Z] := M1[Z, X] * M2[X, Z] + M1[Z, Y] * M2[Y, Z] + M1[Z, Z] * M2[Z, Z] + M1[Z, W] * M2[W, Z];
    Result[Z, W] := M1[Z, X] * M2[X, W] + M1[Z, Y] * M2[Y, W] + M1[Z, Z] * M2[Z, W] + M1[Z, W] * M2[W, W];
    Result[W, X] := M1[W, X] * M2[X, X] + M1[W, Y] * M2[Y, X] + M1[W, Z] * M2[Z, X] + M1[W, W] * M2[W, X];
    Result[W, Y] := M1[W, X] * M2[X, Y] + M1[W, Y] * M2[Y, Y] + M1[W, Z] * M2[Z, Y] + M1[W, W] * M2[W, Y];
    Result[W, Z] := M1[W, X] * M2[X, Z] + M1[W, Y] * M2[Y, Z] + M1[W, Z] * M2[Z, Z] + M1[W, W] * M2[W, Z];
    Result[W, W] := M1[W, X] * M2[X, W] + M1[W, Y] * M2[Y, W] + M1[W, Z] * M2[Z, W] + M1[W, W] * M2[W, W];
    end;
    end;

    // MatrixMultiply (3x3 func)
    //
    function MatrixMultiply(const M1, M2: TAffineMatrix): TAffineMatrix;
    begin
    begin
    Result[X, X] := M1[X, X] * M2[X, X] + M1[X, Y] * M2[Y, X] + M1[X, Z] * M2[Z, X];
    Result[X, Y] := M1[X, X] * M2[X, Y] + M1[X, Y] * M2[Y, Y] + M1[X, Z] * M2[Z, Y];
    Result[X, Z] := M1[X, X] * M2[X, Z] + M1[X, Y] * M2[Y, Z] + M1[X, Z] * M2[Z, Z];
    Result[Y, X] := M1[Y, X] * M2[X, X] + M1[Y, Y] * M2[Y, X] + M1[Y, Z] * M2[Z, X];
    Result[Y, Y] := M1[Y, X] * M2[X, Y] + M1[Y, Y] * M2[Y, Y] + M1[Y, Z] * M2[Z, Y];
    Result[Y, Z] := M1[Y, X] * M2[X, Z] + M1[Y, Y] * M2[Y, Z] + M1[Y, Z] * M2[Z, Z];
    Result[Z, X] := M1[Z, X] * M2[X, X] + M1[Z, Y] * M2[Y, X] + M1[Z, Z] * M2[Z, X];
    Result[Z, Y] := M1[Z, X] * M2[X, Y] + M1[Z, Y] * M2[Y, Y] + M1[Z, Z] * M2[Z, Y];
    Result[Z, Z] := M1[Z, X] * M2[X, Z] + M1[Z, Y] * M2[Y, Z] + M1[Z, Z] * M2[Z, Z];
    end;
    end;
    [/pascal]

    http://glscene.cvs.sourceforge.net/g...49&view=markup

    That it?

  3. #3

    Is this 3x3 matrix multiply function right?

    Hehe, exactly in this moment I'm translating this Java functions:

    http://www.euclideanspace.com/maths/...ourD/index.htm

    It's interesting the other functions in the book uses For loops for the calculations (and C++'s For which are less optimal than Pascal's For), and at the same time the author pretends to fully optimize the code, HAH! :lol:

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •