next up previous contents
Next: Transposing a general size Up: General size matrices Previous: Accessing the elements of   Contents

Copying a general size matrix

To copy a matrix $A$ to 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_copy_q ( &mA, &mB ); /* set B = A, 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 a copy of a matrix from scratch:

      Gan_Matrix *pmB; /* declare matrix B */

      pmB = gan_mat_copy_s ( &mA ); /* create B and set B = A */

For special square matrices, use one of the functions

      Gan_SquMatrix smA, smB, *psmB; /* declare matrices A & B */

      /* ... create A & B using e.g. gan_diagmat_form(), and initialise A using
             e.g. gan_diagmat_fill_va() ...  */
      gan_squmat_copy_q ( &smA, &smB ); /* set B = A, reallocating B if necessary, OR */
      psmB = gan_squmat_copy_s(&smA); /* set B = A, creating B */

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



2006-03-17