Matrix Transpose

In the next example, the matrix transpose will be implemented.

defprimitive
{
  name        { Transpose_M }
  domain      { DE }
  desc
  {
    Transpose a floating-point matrix
  }

  input
  {
    name { Input }
    type { float_matrix_env }
  }

  output
  {
    name { Output }
    type { float_matrix_env }
  }

  ccinclude { "kernel/Matrix.h" }

  go
  {
    // Functional Primitive: pass timestamp without change
    completionTime = arrivalTime;

    // Extract the matrix on the input port
    Envelope tXPkt;
    Input.get().getMessage(tXPkt);
    const FloatMatrix& tXMatrix = *(const FloatMatrix *)tXPkt.myData();

    // Create a copy of the input matrix
    FloatMatrix& tXTrans = *(new FloatMatrix(tXMatrix));

    // Transpose the matrix
    tXTrans.transpose();

    // Send the matrix result to the output port
    Output.put(completionTime) << tXTrans;
  }
}

The key difference between creating an identity matrix and taking a matrix transpose in the DE domain is the conversion of the input data to a matrix. The input data comes in the form of an envelope which is essentially an instance of a class embedded in a message particle. To extract the contents of the message, the message is first extracted from the input envelope. Then, the data field is taken from the message and cast to a FloatMatrix. Just as in the previous example, the dynamic memory is needed to be allocated to hold the value of the matrix to be output. In this case, the programmer does not have to code the transpose operation since it is already built into the matrix classes.