Dense Matrices


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


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.

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...

Defines

#define EGdMatrixAddColMultiple(dmatrix, dest, ori, num)
 Given a number 'num' and a two rows 'ori', 'dest', set columns 'dest' to 'dest' + 'ori' * 'num'. Note that the number MUST_NOT be stored in column 'dest', and note that columns 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.
#define EGdMatrixAddRowMultiple(dmatrix, dest, ori, num)
 Given a number 'num' and a two rows 'ori', 'dest', set rows 'dest' to 'dest' + 'ori' * 'num'. Note that the number MUST_NOT be stored in row 'dest', and note that rows 'ori' 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, out_file)
 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, col_ind, multiple)
 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, new_nrows, new_ncols)
 Set new dimensions for a dense matrix structure.
#define EGdMatrixSubColMultiple(dmatrix, dest, ori, num)
 Given a number 'num' and a two rows 'ori', 'dest', set columns 'dest' to 'dest' - 'ori' * 'num'. Note that the number MUST_NOT be stored in column 'dest', and note that columns 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.
#define EGdMatrixSubRowMultiple(dmatrix, dest, ori, num)
 Given a number 'num' and a two rows 'ori', 'dest', set rows 'dest' to 'dest' - 'ori' * 'num'. Note that the number MUST_NOT be stored in row 'dest', and note that rows 'ori' 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 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, 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.


Define Documentation

#define EGdMatrixAddColMultiple dmatrix,
dest,
ori,
num   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  const size_t __EGdest = (dest);\
  const size_t __EGori = (ori);\
  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 'ori', 'dest', set columns 'dest' to 'dest' + 'ori' * 'num'. Note that the number MUST_NOT be stored in column 'dest', and note that columns 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
dmatrix dense matrix structure pointer.
'ori' index of the column whose multiple will be added to the 'dest' column.
'dest' column to be replaced by 'dest' + 'ori' * 'num'.
'num' constant to be multiply to the 'ori' 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 243 of file eg_dmatrix.h.

#define EGdMatrixAddRowMultiple dmatrix,
dest,
ori,
num   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  const size_t __EGdest = (dest);\
  const size_t __EGori = (ori);\
  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 'ori', 'dest', set rows 'dest' to 'dest' + 'ori' * 'num'. Note that the number MUST_NOT be stored in row 'dest', and note that rows 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
dmatrix dense matrix structure pointer.
'ori' index of the row whose multiple will be added to the 'dest' row.
'dest' row to be replaced by 'dest' + 'ori' * 'num'.
'num' constant to be multiply to the 'ori' 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 170 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 87 of file eg_dmatrix.h.

#define EGdMatrixDisplay dmatrix,
nat_order,
out_file   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  char* __EGdmstr = 0;\
  size_t __EGdmi, __EGdmj;\
  fprintf(out_file,"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(out_file,"%10s ", __EGdmstr);\
        EGfree(__EGdmstr);\
      }\
      fprintf(out_file,"\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(out_file,"%10s ", __EGdmstr);\
        EGfree(__EGdmstr);\
      }\
      fprintf(out_file,"\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.
out_file pointer to a FILE structure where we want the output to be printed.
Examples:
eg_dmatrix.ex.c.

Definition at line 129 of file eg_dmatrix.h.

#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 79 of file eg_dmatrix.h.

#define EGdMatrixMultiplyCol dmatrix,
col_ind,
multiple   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  const size_t __EGdmi = (col_ind);\
  size_t __EGdmj = __EGdm->row_sz;\
  while(__EGdmj--) EGlpNumMultTo(__EGdm->matrow[__EGdmj][__EGdmi],multiple);\
  } 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.
col_ind index of the column being multiplied, note that we will multiply the column stored in EGdMatrix_t::matrow[*][col_ind], wich is different to say that we multiply the column in the col_ind-th position in the column ordering (to do that, then col_ind should be EGdMatrix_t::col_ord[k]).
multiple constant to be multiply to the column.

Definition at line 292 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 218 of file eg_dmatrix.h.

#define EGdMatrixSetDimension dmatrix,
new_nrows,
new_ncols   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  register unsigned __EGdmi;\
  __EGdm->col_sz = (new_ncols);\
  __EGdm->row_sz = (new_nrows);\
  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 = __EGdm->col_sz;\
  while(__EGdmi--) __EGdm->col_ord[__EGdmi] = __EGdmi;\
  __EGdmi = __EGdm->row_sz;\
  while(__EGdmi--) \
    __EGdm->matrow[__EGdmi] = __EGdm->matval + (__EGdmi * __EGdm->col_sz);\
  __EGdmi = __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.
new_nrows number of rows in the matrix.
new_ncols 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 103 of file eg_dmatrix.h.

#define EGdMatrixSubColMultiple dmatrix,
dest,
ori,
num   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  const size_t __EGdest = (dest);\
  const size_t __EGori = (ori);\
  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 'ori', 'dest', set columns 'dest' to 'dest' - 'ori' * 'num'. Note that the number MUST_NOT be stored in column 'dest', and note that columns 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
dmatrix dense matrix structure pointer.
'ori' index of the column whose multiple will be added to the 'dest' column.
'dest' column to be replaced by 'dest' - 'ori' * 'num'.
'num' constant to be multiply to the 'ori' 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 271 of file eg_dmatrix.h.

#define EGdMatrixSubRowMultiple dmatrix,
dest,
ori,
num   ) 
 

Value:

do{\
  EGdMatrix_t*const __EGdm = (dmatrix);\
  const size_t __EGdest = (dest);\
  const size_t __EGori = (ori);\
  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 'ori', 'dest', set rows 'dest' to 'dest' - 'ori' * 'num'. Note that the number MUST_NOT be stored in row 'dest', and note that rows 'ori' and 'dest' should be different. This is needed because of the way GNU_MP interface works.

Parameters:
dmatrix dense matrix structure pointer.
'ori' index of the row whose multiple will be added to the 'dest' row.
'dest' row to be replaced by 'dest' - 'ori' * 'num'.
'num' constant to be multiply to the 'ori' 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 198 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 49 of file eg_dmatrix.ex.c.


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 78 of file eg_dmatrix.ex.c.

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 67 of file eg_dmatrix.ex.c.

int EGdMatrixGaussianElimination EGdMatrix_t *const   dmatrix,
const unsigned  do_col_perm,
const unsigned  do_row_perm,
unsigned *const   rank,
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.

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.

Definition at line 26 of file eg_dmatrix.c.

int main int  argc,
char **  argv
 

main function

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

Here is the call graph for this function:


Variable Documentation

unsigned dmatrix_hilbert_eigenvalues[HILBERT_TABLE_SIZE] [static]
 

Initial value:

 {
  1,
  12,
  180,
  2800,
  44100,
  698544,
  11099088,
  176679360,
  2815827300
}
table containing 1/eigenvalues of the hilbert matrix.

Examples:
eg_dmatrix.ex.c.

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


Generated on Mon Jan 30 08:55:28 2006 for EGlib by  doxygen 1.4.5