Go to the documentation of this file.
56 unsigned int AnalyzeKernel( unsigned int nr, unsigned int nc, double *mT,
57 unsigned int dof, double epsilon,
58 boolean computeRank, boolean checkRank, boolean getT, boolean getBase,
59 boolean *singular, unsigned int *rank, boolean **IR, double **T);
76 void PrintGSLVector(FILE *f, char *label,gsl_vector *v);
88 void PrintGSLPermutation(FILE *f, char *label,gsl_permutation *p);
100 void PrintGSLMatrix(FILE *f, char *label,gsl_matrix *M);
106 unsigned int AnalyzeKernel( unsigned int nr, unsigned int nc, double *mT,
107 unsigned int dof, double epsilon,
108 boolean computeRank, boolean checkRank, boolean getT, boolean getBase,
109 boolean *singular, unsigned int *rank, boolean **IR, double **T)
114 gsl_set_error_handler_off();
128 NEW(*T,nc*dof, double);
129 vT=gsl_matrix_view_array(*T,nc,dof);
130 gsl_matrix_set_identity (&(vT.matrix));
135 if ((getBase)&&(nr>0))
157 At=gsl_matrix_view_array(mT,nc,nr);
160 tau=gsl_vector_alloc((nr<nc?nr:nc));
161 p=gsl_permutation_alloc(nr);
162 norm=gsl_vector_alloc(nr);
166 err=gsl_linalg_QRPT_decomp(&(At.matrix),tau,p,&signum,norm);
215 *singular=((nc-dof)!=*rank);
220 Error( "Null 'dof' parameter in AnalyzeKernel");
222 Error( "We can not have more 'dof' than variables");
228 *singular=(r<epsilon);
243 if ((dof>0)&&(!computeRank)&&(getT)&&(!getBase))
246 for(k=*rank;k<mrc;k++)
264 NEW(*T,nc*dof, double);
265 for(k=*rank,i=0;k<nc;k++,i++)
267 vT=gsl_vector_view_array_with_stride(&((*T)[ RC2INDEX(0,i,nc,dof)]),(ROW_MAJOR?dof:1),nc);
268 gsl_vector_set_basis(&(vT.vector),k);
269 gsl_linalg_QR_Qvec(&(At.matrix),tau,&(vT.vector));
281 (*IR)[gsl_permutation_get(p,k)]= TRUE;
286 gsl_vector_free(norm);
287 gsl_vector_free(tau);
288 gsl_permutation_free(p);
293 void PrintGSLVector(FILE *f, char *label,gsl_vector *v)
298 fprintf(f, "%s=",label);
300 for(i=0;i<v->size;i++)
301 fprintf(f, "%.16f ",gsl_vector_get(v,i));
305 void PrintGSLPermutation(FILE *f, char *label,gsl_permutation *p)
310 fprintf(f, "%s=",label);
312 for(i=0;i<p->size;i++)
313 fprintf(f, "%lu ",gsl_permutation_get(p,i));
317 void PrintGSLMatrix(FILE *f, char *label,gsl_matrix *M)
322 fprintf(f, "%s=",label);
324 for(i=0;i<M->size1;i++)
326 for(j=0;j<M->size2;j++)
327 fprintf(f, "%.16f ",gsl_matrix_get(M,i,j));
344 gsl_set_error_handler_off();
349 p=gsl_permutation_alloc(n);
350 m=gsl_matrix_view_array(A,n,n);
352 gsl_linalg_LU_decomp(&(m.matrix),p,&s);
354 s=gsl_linalg_LU_sgndet(&(m.matrix),s);
356 gsl_permutation_free(p);
366 gsl_set_error_handler_off();
377 d=A[0]*A[3]-A[1]*A[2];
380 d= A[0]*A[4]*A[8]+A[2]*A[3]*A[7]+A[1]*A[5]*A[6]
381 -A[2]*A[4]*A[6]-A[1]*A[3]*A[8]-A[0]*A[5]*A[7];
389 p=gsl_permutation_alloc(n);
390 m=gsl_matrix_view_array(A,n,n);
392 gsl_linalg_LU_decomp(&(m.matrix),p,&s);
394 d=gsl_linalg_LU_det(&(m.matrix),s);
396 gsl_permutation_free(p);
402 void InitNewton( unsigned int nr, unsigned int nc,TNewton *n)
406 gsl_set_error_handler_off();
412 NEW(n->Ab,nr*nc, double);
413 NEW(n->bb,nr, double);
415 n->A=gsl_matrix_view_array(n->Ab,n->nr,n->nc);
416 n->b=gsl_vector_view_array(n->bb,n->nr);
418 n->w=gsl_vector_alloc(nc);
422 n->V=gsl_matrix_calloc(nc,nc);
423 n->S=gsl_vector_calloc(nc);
424 n->tmp=gsl_vector_alloc(nr<nc?nr:nc);
432 n->VT=gsl_matrix_submatrix(n->V,0,0,nc,nr);
433 n->UT=gsl_matrix_submatrix(&(n->A.matrix),0,0,nr,nr);
434 n->ST=gsl_vector_subvector(n->S,0,nr);
438 int NewtonStep( double nullSingularValue, double *x, double *dif,TNewton *n)
456 gsl_matrix_set(&(n->VT.matrix),j,i,n->Ab[ RC2INDEX(i,j,n->nr,n->nc)]);
459 err=gsl_linalg_SV_decomp(&(n->VT.matrix),&(n->UT.matrix),&(n->ST.vector),n->tmp);
465 err=gsl_linalg_SV_decomp(&(n->A.matrix),n->V,n->S,n->tmp);
473 if (gsl_vector_get(n->S,i)<nullSingularValue)
474 gsl_vector_set(n->S,i,0.0);
481 err=gsl_linalg_SV_solve(&(n->A.matrix),n->V,n->S,&(n->b.vector),n->w);
489 d=gsl_vector_get(n->w,i);
509 gsl_vector_free(n->w);
511 gsl_matrix_free(n->V);
512 gsl_vector_free(n->S);
513 gsl_vector_free(n->tmp);
516 void InitLS( unsigned int nr, unsigned int nc,TLinearSystem *ls)
519 gsl_set_error_handler_off();
523 Error( "The linear system must be well-constrained or over-constrained");
528 NEW(ls->Ab,nr*nc, double);
529 NEW(ls->bb,nr, double);
530 NEW(ls->xb,nc, double);
532 ls->A=gsl_matrix_view_array(ls->Ab,nr,nc);
533 ls->b=gsl_vector_view_array(ls->bb,nr);
534 ls->x=gsl_vector_view_array(ls->xb,nc);
537 ls->p=gsl_permutation_alloc(nr);
541 ls->tau=gsl_vector_alloc(nr);
542 ls->res=gsl_vector_alloc(nr);
556 err=gsl_linalg_LU_decomp(&(ls->A.matrix),ls->p,&s);
558 err=gsl_linalg_LU_solve(&(ls->A.matrix),ls->p,&(ls->b.vector),&(ls->x.vector));
563 err=gsl_linalg_QR_decomp(&(ls->A.matrix),ls->tau);
566 err=gsl_linalg_QR_lssolve(&(ls->A.matrix),ls->tau,&(ls->b.vector),&(ls->x.vector),ls->res);
579 gsl_permutation_free(ls->p);
582 gsl_vector_free(ls->tau);
583 gsl_vector_free(ls->res);
595 unsigned int AnalyzeKernel( unsigned int nr, unsigned int nc, double *mT,
596 unsigned int dof, double epsilon,
597 boolean computeRank, boolean checkRank, boolean getT, boolean getBase,
598 boolean *singular, unsigned int *rank, boolean **IR, double **T)
613 NEWZ(*T,nc*dof, double);
620 if ((getBase)&&(nr>0))
636 double *tau,wOpt,*work;
639 NEW(tau,(nr<nc?nr:nc), double);
649 info=(int)LAPACKE_dgeqp3_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
650 (lapack_int)nc,(lapack_int)nr,mT,(lapack_int)nc,
651 (lapack_int*)p,tau,&wOpt,(lapack_int)lwork);
653 dgeqp3_(( int*)&nc,( int*)&nr,mT,( int*)&nc,p,tau,&wOpt,&lwork,&info);
656 NEW(work,lwork, double);
661 info=(int)LAPACKE_dgeqp3_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
662 (lapack_int)nc,(lapack_int)nr,mT,(lapack_int)nc,
663 (lapack_int*)p,tau,work,(lapack_int)lwork);
665 dgeqp3_(( int*)&nc,( int*)&nr,mT,( int*)&nc,p,tau,work,&lwork,&info);
717 *singular=((nc-dof)!=*rank);
722 Error( "Null 'dof' parameter in AnalyzeKernel");
724 Error( "We can not have more 'dof' than variables");
730 *singular=(r<epsilon);
745 if ((dof>0)&&(!computeRank)&&(getT)&&(!getBase))
748 for(k=*rank;k<mrc;k++)
767 NEWZ(*T,nc*dof, double);
768 for(k=*rank,i=0;k<nc;k++,i++)
773 info=(int)LAPACKE_dormqr_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),left,noTrans,
774 (lapack_int)nc,(lapack_int)dof,(lapack_int)(nr<nc?nr:nc),
775 mT,(lapack_int)nc,tau,*T,(lapack_int)nc,work,(lapack_int)lwork);
777 dormqr_(&left,&noTrans,( int*)&nc,( int*)&dof,(nr<nc?( int*)&nr:( int*)&nc),mT,( int*)&nc,tau,*T,( int*)&nc,work,&lwork,&info);
813 info=(int)LAPACKE_dgetrf_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
814 (lapack_int)n,(lapack_int)n,A,(lapack_int)n,(lapack_int*)p);
816 dgetrf_(( int*)&n,( int*)&n,A,( int*)&n,p,&info);
826 if ((p[i]-1)!=i) s=-s;
829 for(i=0;((s!=0)&&(i<n));i++)
832 if (fabs(v)<epsilon) s=0;
859 d=A[0]*A[3]-A[1]*A[2];
862 d= A[0]*A[4]*A[8]+A[2]*A[3]*A[7]+A[1]*A[5]*A[6]
863 -A[2]*A[4]*A[6]-A[1]*A[3]*A[8]-A[0]*A[5]*A[7];
875 info=(int)LAPACKE_dgetrf_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
876 (lapack_int)n,(lapack_int)n,A,(lapack_int)n,(lapack_int*)p);
878 dgetrf_(( int*)&n,( int*)&n,A,( int*)&n,p,&info);
887 if ((p[i]-1)!=i) d=-d;
902 void InitNewton( unsigned int nr, unsigned int nc,TNewton *n)
910 NEW(n->Ab,nr*nc, double);
912 NEW(n->bb,(nr>nc?nr:nc), double);
914 NEW(n->s,(nr<nc?nr:nc), double);
920 info=(int)LAPACKE_dgelss_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
921 (lapack_int)n->nr,(lapack_int)n->nc,one,n->Ab,
922 (lapack_int)n->nr,n->bb,(lapack_int)(n->nr>n->nc?n->nr:n->nc),
923 n->s,e,(lapack_int*)&rank,&w,n->lwork);
925 dgelss_(&n->nr,&n->nc,&one,n->Ab,&n->nr,n->bb,(n->nr>n->nc?&n->nr:&n->nc),n->s,&e,&rank,&w,&n->lwork,&info);
929 Error( "Clapack error in InitNewton");
932 NEW(n->work,n->lwork, double);
935 int NewtonStep( double nullSingularValue, double *x, double *dif,TNewton *n)
946 info=(int)LAPACKE_dgelss_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
947 (lapack_int)n->nr,(lapack_int)n->nc,one,n->Ab,
948 (lapack_int)n->nr,n->bb,(lapack_int)(n->nr>n->nc?n->nr:n->nc),
949 n->s,nullSingularValue,(lapack_int*)&rank,n->work,n->lwork);
951 dgelss_(&n->nr,&n->nc,&one,n->Ab,&n->nr,n->bb,(n->nr>n->nc?&n->nr:&n->nc),n->s,&nullSingularValue,&rank,n->work,&n->lwork,&info);
957 *dif= Norm(n->nc,n->bb);
977 void InitLS( unsigned int nr, unsigned int nc,TLinearSystem *ls)
980 Error( "The linear system must be well-constrained or over-constrained");
985 NEW(ls->Ab,nr*nc, double);
986 NEW(ls->bb,nr, double);
1008 info=(int)LAPACKE_dgels_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),noTrans,
1009 (lapack_int)ls->nr,(lapack_int)ls->nc,one,ls->Ab,
1010 (lapack_int)ls->nr,ls->bb,(lapack_int)ls->nr,&w,ls->lwork);
1012 dgels_(&noTrans,&ls->nr,&ls->nc,&one,ls->Ab,&ls->nr,ls->bb,&ls->nr,&w,&ls->lwork,&info);
1016 Error( "Clapack error in InitLS");
1019 NEW(ls->work,ls->lwork, double);
1024 int LSSolve(TLinearSystem *ls)
1036 info=(int)LAPACKE_dgesv_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),
1037 (lapack_int)ls->nr,one,ls->Ab,(lapack_int)ls->nr,
1038 (lapack_int*)ls->p,ls->bb,(lapack_int)ls->nr);
1040 dgesv_(&ls->nr,&one,ls->Ab,&ls->nr,ls->p,ls->bb,&ls->nr,&info);
1049 info=(int)LAPACKE_dgels_work((ROW_MAJOR?LAPACK_ROW_MAJOR:LAPACK_COL_MAJOR),noTrans,
1050 (lapack_int)ls->nr,(lapack_int)ls->nc,one,ls->Ab,
1051 (lapack_int)ls->nr,ls->bb,(lapack_int)ls->nr,ls->work,ls->lwork);
1053 dgels_(&noTrans,&ls->nr,&ls->nc,&one,ls->Ab,&ls->nr,ls->bb,&ls->nr,ls->work,&ls->lwork,&info);
1084 unsigned int FindRank( double epsilon, unsigned int nr, unsigned int nc, double *mT)
1093 &singular,&rank,&IR,&T);
1098 unsigned int FindKernel( unsigned int nr, unsigned int nc, double *mT,
1099 unsigned int dof, boolean check, double epsilon, double **T)
1108 &singular,&rank,&IR,T);
1113 unsigned int dof, double epsilon, boolean *singular,
1114 boolean **IR, double **T)
1121 singular,&rank,IR,T);
1138 n->Ab[ RC2INDEX(i,j,n->nr,n->nc)]=v;
1161 inline void LSSetMatrix( unsigned int i, unsigned int j, double v,TLinearSystem *ls)
1163 ls->Ab[ RC2INDEX(i,j,ls->nr,ls->nc)]=v;
1166 inline void LSSetRH( unsigned int i, double v,TLinearSystem *ls)
int MatrixDeterminantSgn(double epsilon, unsigned int n, double *A) Sign of the determinant of a matrix.
unsigned int AnalyzeKernel(unsigned int nr, unsigned int nc, double *mT, unsigned int dof, double epsilon, boolean computeRank, boolean checkRank, boolean getT, boolean getBase, boolean *singular, unsigned int *rank, boolean **IR, double **T) Analyzes the kernel of a matrix.
double * GetLSSolutionBuffer(TLinearSystem *ls) Buffer to store the linear system solution.
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.
#define NEW(_var, _n, _type) Allocates memory space.
#define RC2INDEX(i, j, nr, nc) Index in a vector of a matrix element.
double MatrixDeterminant(unsigned int n, double *A) Determinant of a matrix.
#define NEWZ(_var, _n, _type) Allocates and cleans memory space.
CBLAS_INLINE double Norm(unsigned int s, double *v) Computes the norm of a vector.
void LSSetRH(unsigned int i, double v, TLinearSystem *ls) Defines the vector being used in a linear system.
double * GetNewtonRHBuffer(TNewton *n) Buffer to store the Newton right hand.
void Error(const char *s) General error function.
int NewtonStep(double nullSingularValue, double *x, double *dif, TNewton *n) One step in a Newton iteration.
unsigned int FindRank(double epsilon, unsigned int nr, unsigned int nc, double *mT) Determines the row-rank of a matrix.
void DeleteNewton(TNewton *n) Releases a Newton structure.
void InitLS(unsigned int nr, unsigned int nc, TLinearSystem *ls) Defines a linear system structure.
void NewtonSetMatrix(unsigned int i, unsigned int j, double v, TNewton *n) Defines the matrix being used in a Newton step.
#define NO_UINT Used to denote an identifier that has not been initialized.
CBLAS_INLINE void SubtractVector(unsigned int s, double *v1, double *v2) Substracts a vector from another vector.
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.
void DeleteLS(TLinearSystem *ls) Releases a linear system structure.
double * GetLSRHBuffer(TLinearSystem *ls) Buffer to store the linear system right hand (RH).
int LSSolve(TLinearSystem *ls) Solves the linear sytem.
void LSSetMatrix(unsigned int i, unsigned int j, double v, TLinearSystem *ls) Defines the matrix being used in a linear system.
double * GetLSMatrixBuffer(TLinearSystem *ls) Buffer to store the A matrix.
CBLAS_INLINE double NormWithStride(unsigned int s, unsigned int st, double *v) Computes the norm of a vector.
void NewtonSetRH(unsigned int i, double v, TNewton *n) Defines the vector being used in a Newton step.
double * GetNewtonMatrixBuffer(TNewton *n) Buffer to store the Newton matrix.
void InitNewton(unsigned int nr, unsigned int nc, TNewton *n) Defines a Newton structure.
|
Follow us!