next up previous contents
Next: General size matrix addition Up: General size matrices Previous: Copying a general size   Contents

Transposing a general size matrix

Gandalf supports implicit matrix transpose across all relevant routines, so it is not often necessary to explicitly transpose a matrix. Nonetheless, like matrix inverse it sometimes cannot be avoided. To transpose a matrix $A$ into another matrix $B$, both matrices must have been created, and $A$ should be filled with values. $B$ can be created with arbitrary initial dimensions (for instance zero), since Gandalf will if necessary reallocate $B$ to the same size as $A$. So for instance the following code is perfectly valid:
      Gan_Matrix mA, mB; /* declare matrices A & B */

      gan_mat_form ( &mA, 0, 0 ); /* create matrix A */
      gan_mat_form ( &mB, 0, 0 ); /* create matrix B */

      /* reallocate & initialise A */
      gan_mat_fill_va ( &mA, 2, 3, 11.0, 9.0, 7.0,
                                    5.0, 3.0, 1.0 );
      gan_mat_tpose_q ( &mA, &mB ); /* set B = A^T, reallocating B */
The last two lines reallocate first $A$ and then $B$, because both were created with zero size. Note that $B$ may have previosly been filled with other values, which are now lost.

There is also a version that creates the transpose of a matrix from scratch:

      Gan_Matrix *pmB; /* declare matrix B */

      pmB = gan_mat_tpose_s ( &mA ); /* create B and set B = A */
If in this case matrix $A$ happens to be square, Gandalf supports in-place transpose:
      /* A have the same number of rows and columns */
      gan_mat_tpose_i ( &mA ); /* replace A = A^T */

There is no explicit transpose implemented in Gandalf for special square matrices. With the current matrix types supported by Gandalf, it would only be relevant anyway for triangular matrices. Implicit transpose can handle every practical situation.

Error detection: The matrix transpose routines return NULL and invoke the Gandalf error handler upon failure.


next up previous contents
Next: General size matrix addition Up: General size matrices Previous: Copying a general size   Contents
2006-03-17