algebra.h
Go to the documentation of this file.
1 #ifndef ALGEBRAH
2 
13 #include "basic_algebra.h"
14 
15 #if !defined(_LAPACK) && !defined(_GSL)
16  #error CuikSuite requires LAPACK or GSL
17 #endif
18 
31 #define DAMPED_NEWTON 0
32 
38 #define DN_F 0.1
39 
45 #define DN_MF 0.95
46 
55 #define DN_W 0.25
56 
57 #ifdef _GSL
58  #include <gsl/gsl_linalg.h>
59 
75  typedef struct {
76  unsigned int nr;
77  unsigned int nc;
79  double *Ab;
80  double *bb;
82  gsl_matrix_view A;
83  gsl_vector_view b;
85  gsl_matrix *V;
86  gsl_vector *S;
87  gsl_vector *tmp;
90  gsl_matrix_view VT;
91  gsl_matrix_view UT;
92  gsl_vector_view ST;
94  gsl_vector *w;
96  #if (DAMPED_NEWTON)
97  double alpha;
98  #endif
99  } TNewton;
100 
109  typedef struct {
110  unsigned int nr;
111  unsigned int nc;
113  double *Ab;
114  double *bb;
115  double *xb;
117  gsl_matrix_view A;
118  gsl_vector_view b;
119  gsl_vector_view x;
121  gsl_permutation *p;
123  gsl_vector *tau;
125  gsl_vector *res;
128  } TLinearSystem;
129 #endif
130 
131 #ifdef _LAPACK
132  #ifdef _LAPACKE
133  #include <lapacke.h>
134  #else
135  /* assuming clapack */
136  #include <clapack.h>
137  #endif
138 
153  typedef struct {
154  int nr;
155  int nc;
157  double *Ab;
158  double *bb;
160  double *s;
162  int lwork;
163  double *work;
165  #if (DAMPED_NEWTON)
166  double alpha;
167  #endif
168  } TNewton;
169 
178  typedef struct {
179  int nr;
180  int nc;
182  double *Ab;
183  double *bb;
184  double *xb;
186  int *p;
188  int lwork;
189  double *work;
191  } TLinearSystem;
192 #endif
193 
194 
195 /*******************************************************************************/
196 /* The functions below have different implemenatation depending on the helper */
197 /* library being used (gsl, eigen, etc). In other words, these are the */
198 /* functions to re-implement to change to another helper library. */
199 
211 int MatrixDeterminantSgn(double epsilon,unsigned int n,double *A);
212 
226 double MatrixDeterminant(unsigned int n,double *A);
227 
246 unsigned int FindRank(double epsilon,unsigned int nr,unsigned int nc,double *mT);
247 
269 unsigned int FindKernel(unsigned int nr,unsigned int nc,double *mT,
270  unsigned int dof,boolean check,double epsilon,double **T);
271 
305 unsigned int FindKernelAndIndependentRows(unsigned int nr,unsigned int nc,double *mT,
306  unsigned int dof,double epsilon,boolean *singular,
307  boolean **IR,double **T);
308 
322 void InitNewton(unsigned int nr,unsigned int nc,TNewton *n);
323 
337 double *GetNewtonMatrixBuffer(TNewton *n);
338 
339 
349 double *GetNewtonRHBuffer(TNewton *n);
350 
363 void NewtonSetMatrix(unsigned int i,unsigned int j,double v,TNewton *n);
364 
376 void NewtonSetRH(unsigned int i,double v,TNewton *n);
377 
378 
401 int NewtonStep(double nullSingularValue,double *x,double *dif,
402  TNewton *n);
403 
414 double NewtonGetResult(unsigned int i,TNewton *n);
415 
423 void DeleteNewton(TNewton *n);
424 
434 void InitLS(unsigned int nr,unsigned int nc,TLinearSystem *ls);
435 
449 double *GetLSMatrixBuffer(TLinearSystem *ls);
450 
460 double *GetLSRHBuffer(TLinearSystem *ls);
461 
471 double *GetLSSolutionBuffer(TLinearSystem *ls);
472 
483 void LSSetMatrix(unsigned int i,unsigned int j,double v,TLinearSystem *ls);
484 
494 void LSSetRH(unsigned int i,double v,TLinearSystem *ls);
495 
496 
507 int LSSolve(TLinearSystem *ls);
508 
516 void DeleteLS(TLinearSystem *ls);
517 
518 #endif
519 
520 #ifdef _LAPACK
521 
522 
523 
524 #endif
int MatrixDeterminantSgn(double epsilon, unsigned int n, double *A)
Sign of the determinant of a matrix.
void LSSetRH(unsigned int i, double v, TLinearSystem *ls)
Defines the vector being used in a linear system.
Definition: algebra.c:1187
double * GetLSMatrixBuffer(TLinearSystem *ls)
Buffer to store the A matrix.
Definition: algebra.c:1167
double * GetNewtonRHBuffer(TNewton *n)
Buffer to store the Newton right hand.
Definition: algebra.c:1152
double MatrixDeterminant(unsigned int n, double *A)
Determinant of a matrix.
unsigned int FindKernel(unsigned int nr, unsigned int nc, double *mT, unsigned int dof, boolean check, double epsilon, double **T)
Computes the kernel of a matrix.
Definition: algebra.c:1119
unsigned int FindKernelAndIndependentRows(unsigned int nr, unsigned int nc, double *mT, unsigned int dof, double epsilon, boolean *singular, boolean **IR, double **T)
Computes the kernel of a matrix and determines the independent rows of this matrix.
Definition: algebra.c:1133
double * GetNewtonMatrixBuffer(TNewton *n)
Buffer to store the Newton matrix.
Definition: algebra.c:1147
double * GetLSRHBuffer(TLinearSystem *ls)
Buffer to store the linear system right hand (RH).
Definition: algebra.c:1172
int NewtonStep(double nullSingularValue, double *x, double *dif, TNewton *n)
One step in a Newton iteration.
double * GetLSSolutionBuffer(TLinearSystem *ls)
Buffer to store the linear system solution.
Definition: algebra.c:1177
void NewtonSetRH(unsigned int i, double v, TNewton *n)
Defines the vector being used in a Newton step.
Definition: algebra.c:1162
void LSSetMatrix(unsigned int i, unsigned int j, double v, TLinearSystem *ls)
Defines the matrix being used in a linear system.
Definition: algebra.c:1182
void DeleteNewton(TNewton *n)
Releases a Newton structure.
double NewtonGetResult(unsigned int i, TNewton *n)
Gets the vector resulting from a Newton step.
void InitLS(unsigned int nr, unsigned int nc, TLinearSystem *ls)
Defines a linear system structure.
unsigned int FindRank(double epsilon, unsigned int nr, unsigned int nc, double *mT)
Determines the row-rank of a matrix.
Definition: algebra.c:1105
void DeleteLS(TLinearSystem *ls)
Releases a linear system structure.
int LSSolve(TLinearSystem *ls)
Solves the linear sytem.
void NewtonSetMatrix(unsigned int i, unsigned int j, double v, TNewton *n)
Defines the matrix being used in a Newton step.
Definition: algebra.c:1157
void InitNewton(unsigned int nr, unsigned int nc, TNewton *n)
Defines a Newton structure.