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

Accessing the elements of a general size matrix

To read the value of a specific element of a matrix $A$ use
      Gan_Matrix mA; /* matrix A */
      double dEl;

      /* ... create and fill matrix A ... */
      dEl = gan_mat_get_el ( &mA, 1, 2 ); /* returns A[1][2], A = (A[0][0] A[0][1] ... )
                                                                  (A[1][0] A[1][1] ... )
                                                                  (   :       :    ... ) */
This sets dEl to the third element of the second row of matrix $A$. To set an element to a specific value use
      gan_mat_set_el ( &mA, 0, 3, 3.0 ); /* sets A[0][3] to 3.0 */
This sets the fourth element of the first row of $A$ to 3. There are also routines to increment or decrement an element of a matrix:
      gan_mat_inc_el ( &mA, 0, 1, 2.5 ); /* A[0][1] += 2.5 */
      gan_mat_dec_el ( &mA, 3, 2, 5.0 ); /* A[3][2] -= 5.0 */
which respectively increment the second element of the first row of $A$ by 2.5 and subtract 5 from the third element of the fourth row of $A$.

For special square matrices there are equivalent routines which can be illustrated by the following code fragment.

      Gan_SquMatrix smA; /* matrix A */
      double dEl;

      /* ... create and fill matrix A ... */
      dEl = gan_squmat_get_el ( &smA, 1, 2 ); /* returns A[1][2] */
      gan_squmat_set_el ( &smA, 0, 3, 3.0 ); /* sets A[0][3] to 3.0 */
      gan_squmat_inc_el ( &smA, 0, 1, 2.5 ); /* A[0][1] += 2.5 */
      gan_squmat_dec_el ( &smA, 3, 2, 5.0 ); /* A[3][2] -= 5.0 */

Error detection: gan_{squ}mat_set_el(), gan_{squ}mat_inc_el() and gan_{squ}mat_dec_el() all return boolean values, with GAN_FALSE returned on failure, in which case the Gandalf error handler is invoked. The most likely failure modes is accessing an element outside the bounds of the matrix, or seting an illegal element of a square matrix (e.g. an off-diagonal element of a diagonal matrix). If NDEBUG is set then no error checking is done. gan_{squ}mat_get_el() operate similarly, but return DBL_MAX on error.


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