General size matrix subtraction (original) (raw)

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

General size matrix subtraction

The routines for matrix subtraction follow the scheme of those for matrix addition, leading to the options

  Gan_Matrix mA, mB, mC; /* declare matrices x, y and z */
  Gan_Matrix *pmC; /* declare matrix z alternatively as pointer */

  /* ... create and fill mA & mB, create mC ... */
  gan_mat_sub_q ( &mA, &mB, &mC ); /* compute C = A - B */
  pmC = gan_mat_sub_s ( &mA, &mB ); /* compute C = A - B */
  gan_mat_sub_i1 ( &mA, &mB ); /* replace A = A - B */
  gan_mat_sub_i2 ( &mA, &mB ); /* replace B = A - B */
  gan_mat_decrement ( &mA, &mB ); /* replace A = A - B */

If one of the input matrices is to be implicitly transposed, use instead

  Gan_Matrix mA, mB, mC; /* declare matrices x, y and z */
  Gan_Matrix *pmC; /* declare matrix z alternatively as pointer */

  /* ... create and fill mA & mB, create mC ... */

  /* here B must have the same number of columns as A has rows, and vice versa */
  gan_mat_subT_q ( &mA, &mB, &mC ); /* compute C = A - B^T */
  pmC = gan_mat_subT_s ( &mA, &mB ); /* compute C = A - B^T */
  gan_mat_decrementT ( &mA, &mB ); /* replace A = A - B^T */

There are equivalent functions for square matrices. Firstly the simple routines for subtracting two matrices:

  Gan_SquMatrix smA, smB, smC, *psmC; /* declare matrices A, B & C */

  /* ... create and fill smA & smB, create smC ... */
  gan_squmat_sub_q ( &smA, &smB, &smC ); /* compute C = A - B, OR */
  gan_squmat_sub_i1 ( &smA, &smB ); /* replace A = A - B, OR */
  gan_squmat_sub_i2 ( &smA, &smB ); /* replace B = A - B, OR */
  gan_squmat_decrement ( &smA, &smB ); /* replace A = A - B, OR */
  psmC = gan_squmat_sub_s ( &smA, &smB ); /* compute C = A - B as new matrix */

Other routines implicitly transpose one of the input matrices:

  Gan_SquMatrix smA, smB, smC, *psmC; /* declare matrices A, B & C */

  /* ... create and fill smA & smB, create smC ... */
  gan_squmat_subT_q ( &smA, &smB, &smC ); /* compute C = A - B^T, OR */
  gan_squmat_decrementT ( &smA, &smB ); /* replace A = A - B^T, OR */
  psmC = gan_squmat_subT_s ( &smA, &smB ); /* compute C = A - B^T as new matrix */

Error detection: NULL is returned and the Gandalf error handler invoked if the matrix addition fails. The most likely failure modes are failing to create/set the result matrix, or size/type incompatibility between the input matrices.


2006-03-17