next up previous contents
Next: Determinant, trace, norms of Up: Fixed size matrices Previous: Fixed size matrix/matrix multiplication   Contents

Fixed size matrix inverse

All types of square matrices can be inverted in Gandalf. If the matrix is singular, NULL is normally returned and an error condition set using gan_err_register() (see Section 2.9). The routines to invert a $3\times 3$ matrix are
      Gan_Matrix33 m33A, m33B;

      /* ... set up m33A using e.g. gan_mat33_fill_q() ... */
      gan_mat33_invert_q ( &m33A, &m33B ); /* B = A^-1, OR */
      m33B = gan_mat33_invert_s ( &m33A ); /* B = A^-1, OR */
      gan_mat33_invert_i ( &m33A ); /* A = A^-1, in-place in A */
Note that the routine gan_mat33_invert_s() returns the structure, rather than a pointer value, so cannot return an error condition. Do not use it unless you are SURE that your matrix is non-singular! The basic routines gan_mat33_invert_[qs]() are implemented as functions rather than macros, because they require temporary variables. There are similar routines for symmetric matrices
      Gan_SquMatrix33 sm33Sa, sm33Sb;

      /* ... set up sm33Sa using e.g. gan_symmat33_fill_q() ... */
      gan_symmat33_invert_q ( &sm33Sa, &sm33Sb ); /* Sb = Sa^-1, OR */
      sm33Sb = gan_symmat33_invert_s ( &sm33Sa ); /* Sb = Sa^-1, OR */
      gan_symmat33_invert_i ( &sm33Sa ); /* Sa = Sa^-1, in-place in Sa */
and for triangular matrices
      Gan_SquMatrix33 sm33La, sm33Lb;

      /* ... set up sm33La using e.g. gan_ltmat33_fill_q() ... */
      gan_ltmat33_invert_q ( &sm33La, &sm33Lb ); /* Lb = La^-1, OR */
      sm33Lb = gan_ltmat33_invert_s ( &sm33La ); /* Lb = La^-1, OR */
      gan_ltmat33_invert_i ( &sm33La ); /* La = La^-1, in-place in La */

If you don't want to invoke the error package when inversion is attempted on a singular matrix, there is a set of routines which allows to instead to return the error condition as part of the result. For instance the code fragment

      Gan_Matrix33 m33A, m33B;
      int error_code;

      /* ... set up m33A using e.g. gan_mat33_fill_q() ... */
      if ( gan_mat33_invert ( &m33A, &m33B, &error_code ) == NULL )
      {
         /* error found, act on it ... */
      }

      /* no error found */
attempts to invert matrix $A$, and if an error is found, returns NULL, with the error condition returned in the error_code variable. For singular matrices the error condition is GAN_ERROR_SINGULAR_MATRIX. There are similar routines gan_symmat33_invert() and gan_ltmat33_invert() for symmetric and triangular matrices respectively.


next up previous contents
Next: Determinant, trace, norms of Up: Fixed size matrices Previous: Fixed size matrix/matrix multiplication   Contents
2006-03-17