next up previous contents
Next: Fixed size matrix addition Up: Fixed size matrices Previous: Definitions of fixed size   Contents

Creating and accessing fixed size matrices

Single fixed size matrices are such simple objects, it makes sense to normally use declare structure variables directly, rather than use pointers to structures created by malloc(). So to create a double precision $3\times 4$ matrix, use the declaration
      Gan_Matrix34 m34A;
From now on, we shall describe the routines for double precision matrices only. Single precision functions are very similar and will be explained below. Setting the coordinates of a $3\times 4$ matrix can be achieved by one of
  1. Initialising the $3\times 4$ matrix when it is created, as in
          Gan_Matrix34 m34A = {1.0,  2.0,  3.0,  4.0,
                               5.0,  6.0,  7.0,  8.0,
                               9.0, 10.0, 11.0, 12.0};
    

  2. Accessing the structure elements directly:
          m34A.xx = 1.0; m34A.xy = 2.0; m34A.xz = 3.0; /* etc. */
    
  3. Using the macro call
          gan_mat34_fill_q ( &m34A, 1.0,  2.0,  3.0,  4.0,
                                    5.0,  6.0,  7.0,  8.0,
                                    9.0, 10.0, 11.0, 12.0 );
    
    Note that the Gnu C compiler prints a warning when the above call is compiled, and also for most other similar calls in the linear algebra package. This warning can be avoided by inserting an initial (void) cast:
          (void)gan_mat34_fill_q ( &m34A, 1.0,  2.0,  3.0,  4.0,
                                          5.0,  6.0,  7.0,  8.0,
                                          9.0, 10.0, 11.0, 12.0 );
    
    We omit this cast in the following to keep the exposition simple.
  4. The equivalent function call
          m34A = gan_mat34_fill_s ( 1.0,  2.0,  3.0,  4.0,
                                    5.0,  6.0,  7.0,  8.0,
                                    9.0, 10.0, 11.0, 12.0 );
    

The methods of initialising a $3\times 3$ matrix follow those listed above for $3\times 4$ matrices, for instance

      Gan_Matrix33 m33A;

      gan_mat33_fill_q ( &m33A, 1.0, 2.0, 3.0,
                                4.0, 5.0, 6.0,
                                7.0, 8.0, 9.0 ); /* OR */
      m33A = gan_mat33_fill_s ( 1.0, 2.0, 3.0,
                                4.0, 5.0, 6.0,
                                7.0, 8.0, 9.0 );
For a symmetric or lower triangular Gan_SquMatrix33 matrix, direct initialisation (options 1 and 2 above) is not advisable, because of the type field of the structure whose presence depends on NDEBUG, Instead use the macro calls
      Gan_SquMatrix33 sm33S, sm33L;

      /* symmetric matrix */
      gan_symmat33_fill_q ( &sm33S, 1.0,
                                    2.0, 3.0,
                                    4.0, 5.0, 6.0 );

      /* lower triangular matrix */
      gan_ltmat33_fill_q ( &sm33L, 1.0,
                                   2.0, 3.0,
                                   4.0, 5.0, 6.0 );
The first of these fills the matrix without specifying the values above the diagonal, and actually builds the matrix

\begin{displaymath}S = \left(\!\!\begin{array}{ccc} 1 & 2 & 4 \ 2 & 3 & 5 \ 4 & 5 & 6\end{array}\!\!\right)
\end{displaymath}

The second builds the lower triangular matrix

\begin{displaymath}L = \left(\!\!\begin{array}{ccc} 1 & 0 & 0 \ 2 & 3 & 0 \ 4 & 5 & 6\end{array}\!\!\right)
\end{displaymath}

Setting a fixed-size matrix to zero can be accomplished using one of the calls
      gan_mat34_zero_q ( &m34A ); /* OR */ m34A = gan_mat34_zero_s();
      gan_mat33_zero_q ( &m33A ); /* OR */ m33A = gan_mat33_zero_s();
      gan_symmat33_zero_q ( &sm33S ); /* OR */ sm33S = gan_symmat33_zero_s();
      gan_ltmat33_zero_q ( &sm33L ); /* OR */ sm33L = gan_ltmat33_zero_s();

Setting a square matrix to identity is achieved using

      gan_mat33_ident_q ( &m33A ); /* OR */ m33A = gan_mat33_ident_s();
      gan_symmat33_ident_q ( &sm33S ); /* OR */ sm33S = gan_symmat33_ident_s();
      gan_ltmat33_ident_q ( &sm33L ); /* OR */ sm33L = gan_ltmat33_ident_s();

Copying $3\times 4$ matrices can be accomplished either by direct assignment

      Gan_Matrix34 m34B;

      m34B = m34A;
or by use of one of the routines
      gan_mat34_copy_q ( &m34A, &m34B ); /* macro, OR */
      m34B = gan_mat34_copy_s ( &m34A ); /* function call */
The methods of copying general, symmetric and lower triangular matrices follow that of $3\times 4$ matrices.


next up previous contents
Next: Fixed size matrix addition Up: Fixed size matrices Previous: Definitions of fixed size   Contents
2006-03-17