Data Structures | Files | Defines | Typedefs | Functions | Variables

Dense Matrices

Data Structures

struct  EGdMatrix_t
 structure to hold a dense matrix, we choose a row representation of the matrix, and we allow row and column permutations. All actual values in the matrix are stored in EGdMatrix_t::matval, and the rows in EGdMatrix_t::matrow. More...

Files

file  eg_dmatrix.c
file  eg_dmatrix.ex.c
 

This file shows some examples of the use of EGdMatrix_t structure and functions. as a test-set we use the Hilber Matrix wich is a matrix with coefficients $H^n_{i,j} = \frac1{i+j-1}$. It is well known that the Hilbert matrix is extremmaly ill conditioned, and is very hard to invert, here are some values for it's determinant for different dimmensions $n$.

  • $\mathrm{det}(H_1) = 1 $.
  • $\mathrm{det}(H_2) = \frac1{12} $.
  • $\mathrm{det}(H_3) = \frac1{2160} $.
  • $\mathrm{det}(H_4) = \frac1{604800} $.
  • $\mathrm{det}(H_5) = \frac1{266716800000} $. Since a lot is known about the Hilber Matrix, some accuracy tests are possible. For more details see MathWorld This test program takes as input the number of columns and rows for the hilbert matrix, and so some operations on it and display it to the standard output.

file  eg_dmatrix.h
 

This file provide the user interface and function definitions for Dense Matrices.


Defines

#define EGdMatrixAddColMultiple(__dmatrix, __dest, __orig, __num)
 Given a number '__num' and a two rows '__orig', '__dest', set columns '__dest' to '__dest' + '__orig' * '__num'. Note that the number MUST_NOT be stored in column '__dest', and note that columns '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.
#define EGdMatrixAddRowMultiple(__dmatrix, __dest, __orig, __num)
 Given a number '__num' and a two rows '__orig', '__dest', set rows '__dest' to '__dest' + '__orig' * '__num'. Note that the number MUST_NOT be stored in row '__dest', and note that rows '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.
#define EGdMatrixClear(__dmatrix)
 Clear a dense matrix structure, i.e. free all internally allocated data of the structure. Note that no further use of the structure can be made unless it is re-initialized and set to a suitable size.
#define EGdMatrixDisplay(__dmatrix, __nat_order, __ofile)
 Display a given EGdMatrix_t structure contents.
#define EGdMatrixInit(__dmatrix)   memset(__dmatrix,0,sizeof(EGdMatrix_t))
 Initialize (as a dense matrix of dimension 0x0) an EGdMatrix_t structure.
#define EGdMatrixMultiplyCol(__dmatrix, __colind, __mult)
 Given a number and a column, multiply the complete column by the given number. Note that the number MUST_NOT be stored in the column being multiplied, this is because of the way GNU_MP interface works.
#define EGdMatrixMultiplyRow(__dmatrix, row_ind, multiple)
 Given a number and a row, multiply the complete row by the given number. Note that the number MUST_NOT be stored in the row being multiplied, this is because of the way GNU_MP interface works.
#define EGdMatrixSetDimension(__dmatrix, __nnewrows, __nnewcols)
 Set new dimensions for a dense matrix structure.
#define EGdMatrixSubColMultiple(__dmatrix, __dest, __orig, __num)
 Given a number '__num' and a two rows '__orig', '__dest', set columns '__dest' to '__dest' - '__orig' * '__num'. Note that the number MUST_NOT be stored in column '__dest', and note that columns '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.
#define EGdMatrixSubRowMultiple(__dmatrix, __dest, __orig, __num)
 Given a number '__num' and a two rows '__orig', '__dest', set rows '__dest' to '__dest' - '__orig' * '__num'. Note that the number MUST_NOT be stored in row '__dest', and note that rows '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.
#define HILBERT_TABLE_SIZE   9U
 size of the table of eigenvalues of the hilbert matrix

Typedefs

typedef struct EGdMatrix_t EGdMatrix_t
 structure to hold a dense matrix, we choose a row representation of the matrix, and we allow row and column permutations. All actual values in the matrix are stored in EGdMatrix_t::matval, and the rows in EGdMatrix_t::matrow.

Functions

int dmatrix_parseargs (int argc, char **argv, size_t *n, size_t *m)
 parse the input argumenbts for the program
void dmatrix_usage (char *program)
 display the usage message for this program
int EGdMatrixGaussianElimination (EGdMatrix_t *const __dmatrix, const unsigned do_col_perm, const unsigned do_row_perm, unsigned *const rank, const EGlpNum_t zero_tol, int *const status)
 This function performs gaussian elimination to the given matrix, depending on the given options it may do row/columns permutations allong the way to improve numerical stabillity.
int main (int argc, char **argv)
 main function

Variables

static unsigned dmatrix_hilbert_eigenvalues [HILBERT_TABLE_SIZE]
 table containing 1/eigenvalues of the hilbert matrix.

Detailed Description

Here we define a common interface for dense matrices (i.e. a structure), and some common operations over dense matrices. The definition uses EGlpNum as reference number type, this allow for template initializations.

History:
Revision 0.0.2
  • 2005-10-27
    • First implementation.

Define Documentation

#define EGdMatrixAddColMultiple (   __dmatrix,
  __dest,
  __orig,
  __num 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdest = (__dest);\
  const size_t __EGori = (__orig);\
  size_t __EGdmj = __EGdm->row_sz;\
  while(__EGdmj--) \
    EGlpNumAddInnProdTo(__EGdm->matrow[__EGdmj][__EGdest],\
                        __EGdm->matrow[__EGdmj][__EGori],__num);\
  } while(0)

Given a number '__num' and a two rows '__orig', '__dest', set columns '__dest' to '__dest' + '__orig' * '__num'. Note that the number MUST_NOT be stored in column '__dest', and note that columns '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
'__orig' index of the column whose multiple will be added to the '__dest' column.
'__dest' column to be replaced by '__dest' + '__orig' * '__num'.
'__num' constant to be multiply to the '__orig' and be added to the '__dest' column.
Note:
The index of the column are taken as internal index, i.e. if we give column 'k' we will use the column stored in EGdMatrix_t::matrow[*][k], wich does not mean that we will access the k-th column in the matrix (wich would need to use as index the value EGdMatrix_t::row_ord[k] instead). Note that we don't test wether the given multiple is zero or not. we always perform the operation.

Definition at line 238 of file eg_dmatrix.h.

#define EGdMatrixAddRowMultiple (   __dmatrix,
  __dest,
  __orig,
  __num 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdest = (size_t)(__dest);\
  const size_t __EGori = (size_t)(__orig);\
  size_t __EGdmj = __EGdm->col_sz;\
  while(__EGdmj--) \
    EGlpNumAddInnProdTo(__EGdm->matrow[__EGdest][__EGdmj],\
                        __EGdm->matrow[__EGori][__EGdmj],__num);\
  } while(0)

Given a number '__num' and a two rows '__orig', '__dest', set rows '__dest' to '__dest' + '__orig' * '__num'. Note that the number MUST_NOT be stored in row '__dest', and note that rows '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
'__orig' index of the row whose multiple will be added to the '__dest' row.
'__dest' row to be replaced by '__dest' + '__orig' * '__num'.
'__num' constant to be multiply to the '__orig' and be added to the '__dest' row.
Note:
The index of the row are taken as internal index, i.e. if we give row 'k' we will use the row stored in EGdMatrix_t::matrow[k], wich does not mean that we will access the k-th row in the matrix (wich would need to use as index the value EGdMatrix_t::row_ord[k] instead). Note that we don't test wether the given multiple is zero or not. we always perform the operation.

Definition at line 165 of file eg_dmatrix.h.

#define EGdMatrixClear (   __dmatrix  ) 
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  EGlpNumFreeArray(__EGdm->matval);\
  EGfree(__EGdm->matrow);\
  int_EGlpNumFreeArray(__EGdm->col_ord);\
  int_EGlpNumFreeArray(__EGdm->row_ord);} while(0)

Clear a dense matrix structure, i.e. free all internally allocated data of the structure. Note that no further use of the structure can be made unless it is re-initialized and set to a suitable size.

Parameters:
__dmatrix dense matrix structure pointer.
Examples:
eg_dmatrix.ex.c.

Definition at line 82 of file eg_dmatrix.h.

Referenced by main().

#define EGdMatrixDisplay (   __dmatrix,
  __nat_order,
  __ofile 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  char* __EGdmstr = 0;\
  size_t __EGdmi, __EGdmj;\
  fprintf(__ofile,"Matrix %p\nDimensions: %zd rows, %zd columns\n", (void*)__EGdm, __EGdm->row_sz, __EGdm->col_sz);\
  if(__nat_order){\
    for(__EGdmi = 0 ; __EGdmi < __EGdm->row_sz ; __EGdmi++){\
      for(__EGdmj = 0 ; __EGdmj < __EGdm->col_sz ; __EGdmj++){\
        __EGdmstr = EGlpNumGetStr(__EGdm->matrow[__EGdmi][__EGdmj]);\
        fprintf(__ofile,"%10s ", __EGdmstr);\
        EGfree(__EGdmstr);\
      }\
      fprintf(__ofile,"\n");}\
  } else {\
    for(__EGdmi = 0 ; __EGdmi < __EGdm->row_sz ; __EGdmi++){\
      for(__EGdmj = 0 ; __EGdmj < __EGdm->col_sz ; __EGdmj++){\
        __EGdmstr = EGlpNumGetStr(__EGdm->matrow[__EGdm->row_ord[__EGdmi]][__EGdm->col_ord[__EGdmj]]);\
        fprintf(__ofile,"%10s ", __EGdmstr);\
        EGfree(__EGdmstr);\
      }\
      fprintf(__ofile,"\n");}\
  }} while(0)

Display a given EGdMatrix_t structure contents.

Parameters:
__dmatrix dense matrix structure pointer.
__nat_order if set to one, display the matrix using the natural internal order, i.e. we discard the order of columns and rows as defined in EGdMatrix_t::col_ord and EGdMatrix_t::row_ord. Otherwise, use such orders.
__ofile pointer to a FILE structure where we want the output to be printed.
Examples:
eg_dmatrix.ex.c.

Definition at line 124 of file eg_dmatrix.h.

Referenced by main().

#define EGdMatrixInit (   __dmatrix  )     memset(__dmatrix,0,sizeof(EGdMatrix_t))

Initialize (as a dense matrix of dimension 0x0) an EGdMatrix_t structure.

Parameters:
__dmatrix dense matrix structure pointer.
Examples:
eg_dmatrix.ex.c.

Definition at line 74 of file eg_dmatrix.h.

Referenced by main().

#define EGdMatrixMultiplyCol (   __dmatrix,
  __colind,
  __mult 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdmi = (__colind);\
  size_t __EGdmj = __EGdm->row_sz;\
  while(__EGdmj--) EGlpNumMultTo(__EGdm->matrow[__EGdmj][__EGdmi],__mult);\
  } while(0)

Given a number and a column, multiply the complete column by the given number. Note that the number MUST_NOT be stored in the column being multiplied, this is because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
__colind index of the column being multiplied, note that we will multiply the column stored in EGdMatrix_t::matrow[*][__colind], wich is different to say that we multiply the column in the __colind-th position in the column ordering (to do that, then __colind should be EGdMatrix_t::col_ord[k]).
__mult constant to be multiply to the column.

Definition at line 287 of file eg_dmatrix.h.

#define EGdMatrixMultiplyRow (   __dmatrix,
  row_ind,
  multiple 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdmi = (row_ind);\
  size_t __EGdmj = __EGdm->col_sz;\
  while(__EGdmj--) EGlpNumMultTo(__EGdm->matrow[__EGdmi][__EGdmj],multiple);\
  } while(0)

Given a number and a row, multiply the complete row by the given number. Note that the number MUST_NOT be stored in the row being multiplied, this is because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
row_ind index of the row being multiplied, note that we will multiply the row stored in EGdMatrix_t::matrow[row_ind], wich is different to say that we multiply the row in the row_ind-th position in the row ordering (to do that, then row_ind should be EGdMatrix_t::row_ord[k]).
multiple constant to be multiply to the row.

Definition at line 213 of file eg_dmatrix.h.

#define EGdMatrixSetDimension (   __dmatrix,
  __nnewrows,
  __nnewcols 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  register int __EGdmi;\
  __EGdm->col_sz = (__nnewcols);\
  __EGdm->row_sz = (__nnewrows);\
  EGlpNumReallocArray(&(__EGdm->matval),__EGdm->col_sz * __EGdm->row_sz);\
  EGrealloc(__EGdm->matrow,__EGdm->row_sz * sizeof(EGlpNum_t*));\
  int_EGlpNumReallocArray(&(__EGdm->col_ord),__EGdm->col_sz);\
  int_EGlpNumReallocArray(&(__EGdm->row_ord),__EGdm->row_sz);\
  __EGdmi = (int)(__EGdm->col_sz);\
  while(__EGdmi--) __EGdm->col_ord[__EGdmi] = __EGdmi;\
  __EGdmi = (int)(__EGdm->row_sz);\
  while(__EGdmi--) \
    __EGdm->matrow[__EGdmi] = __EGdm->matval + ((size_t)(__EGdmi) * __EGdm->col_sz);\
  __EGdmi = (int)(__EGdm->row_sz);\
  while(__EGdmi--) __EGdm->row_ord[__EGdmi] = __EGdmi;} while(0)

Set new dimensions for a dense matrix structure.

Parameters:
__dmatrix dense matrix structure pointer.
__nnewrows number of rows in the matrix.
__nnewcols number of columns in the matrix.
Note:
Take care that the values stored in the matrix are not initialized to any particular number. Also the ordering (for both column and row) is reset to the standard ordering 0,....,n.
Examples:
eg_dmatrix.ex.c.

Definition at line 98 of file eg_dmatrix.h.

Referenced by main().

#define EGdMatrixSubColMultiple (   __dmatrix,
  __dest,
  __orig,
  __num 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdest = (size_t)(__dest);\
  const size_t __EGori = (size_t)(__orig);\
  size_t __EGdmj = __EGdm->row_sz;\
  while(__EGdmj--) \
    EGlpNumSubInnProdTo(__EGdm->matrow[__EGdmj][__EGdest],\
                        __EGdm->matrow[__EGdmj][__EGori],__num);\
  } while(0)

Given a number '__num' and a two rows '__orig', '__dest', set columns '__dest' to '__dest' - '__orig' * '__num'. Note that the number MUST_NOT be stored in column '__dest', and note that columns '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
'__orig' index of the column whose multiple will be added to the '__dest' column.
'__dest' column to be replaced by '__dest' - '__orig' * '__num'.
'__num' constant to be multiply to the '__orig' and be added to the '__dest' column.
Note:
The index of the column are taken as internal index, i.e. if we give column 'k' we will use the column stored in EGdMatrix_t::matrow[*][k], wich does not mean that we will access the k-th column in the matrix (wich would need to use as index the value EGdMatrix_t::col_ord[k] instead). Note that we don't test wether the given multiple is zero or not. we always perform the operation.

Definition at line 266 of file eg_dmatrix.h.

#define EGdMatrixSubRowMultiple (   __dmatrix,
  __dest,
  __orig,
  __num 
)
Value:
do{\
  EGdMatrix_t*const __EGdm = (__dmatrix);\
  const size_t __EGdest = (__dest);\
  const size_t __EGori = (__orig);\
  size_t __EGdmj = __EGdm->col_sz;\
  while(__EGdmj--) \
    EGlpNumSubInnProdTo(__EGdm->matrow[__EGdest][__EGdmj],\
                        __EGdm->matrow[__EGori][__EGdmj],__num);\
  } while(0)

Given a number '__num' and a two rows '__orig', '__dest', set rows '__dest' to '__dest' - '__orig' * '__num'. Note that the number MUST_NOT be stored in row '__dest', and note that rows '__orig' and '__dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
__dmatrix dense matrix structure pointer.
'__orig' index of the row whose multiple will be added to the '__dest' row.
'__dest' row to be replaced by '__dest' - '__orig' * '__num'.
'__num' constant to be multiply to the '__orig' and be added to the '__dest' row.
Note:
The index of the row are taken as internal index, i.e. if we give row 'k' we will use the row stored in EGdMatrix_t::matrow[k], wich does not mean that we will access the k-th row in the matrix (wich would need to use as index the value EGdMatrix_t::row_ord[k] instead). Note that we don't test wether the given multiple is zero or not. we always perform the operation.

Definition at line 193 of file eg_dmatrix.h.

#define HILBERT_TABLE_SIZE   9U

size of the table of eigenvalues of the hilbert matrix

Examples:
eg_dmatrix.ex.c.

Definition at line 45 of file eg_dmatrix.ex.c.

Referenced by main().


Typedef Documentation

typedef struct EGdMatrix_t EGdMatrix_t

structure to hold a dense matrix, we choose a row representation of the matrix, and we allow row and column permutations. All actual values in the matrix are stored in EGdMatrix_t::matval, and the rows in EGdMatrix_t::matrow.


Function Documentation

int dmatrix_parseargs ( int  argc,
char **  argv,
size_t *  n,
size_t *  m 
)

parse the input argumenbts for the program

Examples:
eg_dmatrix.ex.c.

Definition at line 74 of file eg_dmatrix.ex.c.

References dmatrix_usage().

Referenced by main().

Here is the call graph for this function:

void dmatrix_usage ( char *  program  ) 

display the usage message for this program

Examples:
eg_dmatrix.ex.c.

Definition at line 63 of file eg_dmatrix.ex.c.

Referenced by dmatrix_parseargs().

int EGdMatrixGaussianElimination ( EGdMatrix_t *const   dmatrix,
const unsigned  do_col_perm,
const unsigned  do_row_perm,
unsigned *const   ext_rank,
const EGlpNum_t  zero_tol,
int *const   ext_status 
)

This function performs gaussian elimination to the given matrix, depending on the given options it may do row/columns permutations allong the way to improve numerical stabillity.

Parameters:
__dmatrix dense matrix structure pointer.
do_col_perm if set to one, the try columns permutation to improve numericall stabillity, otherwise, not do column permutations at all.
do_row_perm if set to one, try row permutations to improve numericall stabillity, otherwise, not do row permutations at all.
status pointer to where return an status, if the procedure finish all the way (i.e. the matrix is full rank), then we return EG_ALGSTAT_SUCCESS, if the matrix is found to be partial rank, the status is EG_ALGSTAT_PARTIAL, otherwise, we return EG_ALGSTAT_NUMERROR, wich means that we stoped because a zero pivot was found (after checking for allowed row/collumns permmutations).
rank where to return the (proven) rank of the matrix. This number is accurate if the status is EG_ALGSTAT_SUCCESS, or EG_ALGSTAT_PARTIAL, but is just a lower bound if the status is EG_ALGSTAT_NUMERROR
zero_tol What is the threshold for a value to be considered zero.
Returns:
if no error happen, we return zero, otherwise a non-zero valued is returned. Note that the algorithm status is independent of the return value, non zero values araise only if an error happen during execution, wich is different to say that the algorithm didn't finish correctly.
Examples:
eg_dmatrix.ex.c.

Referenced by main().

int main ( int  argc,
char **  argv 
)

Variable Documentation

unsigned dmatrix_hilbert_eigenvalues[HILBERT_TABLE_SIZE] [static]
Initial value:
 {
  1U,
  12U,
  180U,
  2800U,
  44100U,
  698544U,
  11099088U,
  176679360U,
  2815827300U
}

table containing 1/eigenvalues of the hilbert matrix.

Examples:
eg_dmatrix.ex.c.

Definition at line 49 of file eg_dmatrix.ex.c.

Referenced by main().