Exemplo n.º 1
0
static int Choleski_decompose(double *X, double *L, int n, int lapack){
	int i,j,error_code;
	char upper = 'U';

	for (i=0; i < n; i++){
		for (j=0; j < n; j++){
			if (i > j)
				L[j*n+i] = 0.0;
			else {
				L[j*n+i] = X[j*n + i];
			}
		}
	}
#if 0
	if (!lapack){
		dpofa_(L,&n,&n,&error_code);
	} else {
		dpotrf_(&upper,&n,L,&n,&error_code);
	}
#endif    
	error_code = LAPACKE_dpotrf( LAPACK_COL_MAJOR, upper, n, L, n );


	return error_code;
}
Exemplo n.º 2
0
// chol2inv: compute inverse of n by n pd symm mat A using Cholesky
// assumes A is stored in non-packed upper triangular format
int chol2inv(int n, double *A, bool do_log_det, double *log_det) {
	char uplo = 'U';
	int  info;

	// compute factorization
	dpotrf_(&uplo, &n, A, &n, &info);
	if (info) {
		MSG("Error with chol(A): info = %d\n", info);
		return(info);
	}

	if (do_log_det) {
		// fill in log determinant
		*log_det = 0;

		for (int i = 0; i < n; i++)
			*log_det += log(A[i + i*n]);

		*log_det *= 2;
	}

	// complete inverse
	dpotri_(&uplo, &n, A, &n, &info);
	if (info) {
		MSG("Error with inv(chol(A)): info = %d\n", info);
		return(info);
	}

	return(0);
}
Exemplo n.º 3
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  double *A, *C, z = 0.0, *Z;
  int n, i, j, q;
  char *U = "U";
  A = mxGetPr(prhs[0]);
  n = mxGetN(prhs[0]);
  if (nrhs != 1 || nlhs > 2) {
    mexWarnMsgTxt("Usage: [invA logdetA] = inv_logdet_pd(A)");
    return;
  }
  if (n != mxGetM(prhs[0])) {
    mexWarnMsgTxt("Error: Argument matrix must be square");
    return;
  }
  n = mxGetN(prhs[0]);
  plhs[0] = mxCreateDoubleMatrix(n, n, mxREAL);
  C = mxGetPr(plhs[0]);
  memcpy(C,A,n*n*sizeof(double));
  dpotrf_(U, &n, C, &n, &q);                                     /* cholesky */
  if (q > 0) {
    mexWarnMsgTxt("Error: Argument matrix must be positive definite");
    return;
  }
  if (nlhs > 1) {                                         /* compute log det */
    plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
    Z = mxGetPr(plhs[1]);
    for (i=0; i<n; i++) z += log(C[i*(n+1)]);
    Z[0] = 2.0*z;
  }
  dpotri_(U, &n, C, &n, &q);                          /* cholesky to inverse */
  for (i=0; i<n; i++) for (j=i+1; j<n; j++) C[j+i*n] = C[i+j*n];
}
Exemplo n.º 4
0
bool my_inverse(const double in[N][N], double out[N][N], double &det)
{
  int errorHandler;
  int     n = N;
  char chU[] = "L";

  for (int j = 0; j < N; j++)
    for (int i = 0; i < N; i++)
      out[j][i] = in[j][i];

  dpotrf_(chU, &n, &out[0][0], &n, &errorHandler);
  assert(errorHandler >= 0);

  if (errorHandler > 0)
    return true;

  det = 1.0;
  for (int i = 0; i < N; i++)
    det *= out[i][i];
  det *= det;
  assert(det > 0.0);


  dpotri_(chU, &n, &out[0][0], &n, &errorHandler);
  assert(0 == errorHandler);
  for (int i = 0; i < N; i++)
    for (int j = i; j < N; j++)
      out[i][j] = out[j][i];

  return false;
}
Exemplo n.º 5
0
int GMRFLib_comp_posdef_inverse(double *matrix, int dim)
{
	/*
	 * overwrite a symmetric MATRIX with its inverse 
	 */
	int info = 0, i, j;

	switch (GMRFLib_blas_level) {
	case BLAS_LEVEL2:
		dpotf2_("L", &dim, matrix, &dim, &info, 1);
		break;
	case BLAS_LEVEL3:
		dpotrf_("L", &dim, matrix, &dim, &info, 1);
		break;
	default:
		GMRFLib_ASSERT(1 == 0, GMRFLib_ESNH);
		break;
	}
	if (info)
		GMRFLib_ERROR(GMRFLib_ESINGMAT);

	dpotri_("L", &dim, matrix, &dim, &info, 1);
	if (info)
		GMRFLib_ERROR(GMRFLib_ESINGMAT);

	for (i = 0; i < dim; i++)			       /* fill the U-part */
		for (j = i + 1; j < dim; j++)
			matrix[i + j * dim] = matrix[j + i * dim];

	return GMRFLib_SUCCESS;
}
Exemplo n.º 6
0
Arquivo: factor.cpp Projeto: caomw/r4r
bool CMatrixFactorization<double>::Cholesky(CDenseArray<double>& A) {

   if(A.NCols()!=A.NRows()) {

    	cout << "ERROR: Matrix is not symmetric." << endl;
    	return 1;

    }

   int lda, info, n;
   lda = A.NRows();
   n = lda;
   info = 0;

   double* a = A.Data().get();

   dpotrf_("U",&n,a,&lda,&info);

   if(info>0) {

   	cout << "ERROR: Cholesky decomposition failed." << endl;
   	return 1;

   }

   return 0;

}
Exemplo n.º 7
0
Arquivo: latools.c Projeto: cran/Bmix
void la_dpotrf(int dim, double **A)
{
  int info;
  char cholTri = 'L'; // return cholesky decomp of col-major A in lower triangle
  dpotrf_(&cholTri,&dim,*A,&dim,&info); 
  assert(info==0); // if info = -i, i'th arg is wrong.  if info > 0, A is not pos-def.
}
Exemplo n.º 8
0
/**
 * Cholesky factorization of a square, symmetric matrix. The function computes
 * an upper triangular matrix <b>Z</b> such that <b>Z</b>'&middot;<b>Z</b> =
 * <b>A</b>.
 * 
 * @param Z
 *          Pointer to output matrix, buffer must be able to hold
 *          <code>nXD</code><sup>2</sup> double values
 * @param A
 *          Pointer to square, symmetric input matrix, must not be equal
 *          <code>Z</code>.
 * @param nXD
 *          Number of rows and columns of <code>A</code> and <code>Z</code>
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 dlm_cholf(FLOAT64* Z, const FLOAT64* A, INT32 nXD) {
  integer info = 0;
  integer n = (integer) nXD;
  char uplo[1] = { 'U' };
#ifdef __MAX_TYPE_32BIT
  extern int slacpy_(char*,integer*,integer*,real*,integer*,real*,integer *ldb);
  extern int spotrf_(char*,integer*,real*,integer*,integer*);
#else
  extern int dlacpy_(char*,integer*,integer*,doublereal*,integer*,doublereal*,integer *ldb);
  extern int dpotrf_(char*,integer*,doublereal*,integer*,integer*);
#endif

  /* Declare variables */
  DLPASSERT(Z != A);                                                            /* Assert input is not output        */
  DLPASSERT(dlp_size(Z) >= nXD * nXD * sizeof(FLOAT64));                        /* Check size of output buffer       */
  DLPASSERT(dlp_size(A) >= nXD * nXD * sizeof(FLOAT64));                        /* Check size of input buffer        */

  /* ... computation ... *//* --------------------------------- */
#ifdef __MAX_TYPE_32BIT
  spotrf_(uplo, &n, (real*)A, &n, &info);
  slacpy_(uplo, &n, &n, (real*)A, &n, (real*)Z, &n);
#else
  dpotrf_(uplo, &n, (doublereal*)A, &n, &info);
  dlacpy_(uplo, &n, &n, (doublereal*)A, &n, (doublereal*)Z, &n);
#endif
  return (info == 0) ? O_K : NOT_EXEC; /* All done successfully             */
}
Exemplo n.º 9
0
// assume that A, which is triangular, is stored in recursive L; that means that the square block is stored in recursive backwards N
void chol( double *A, int n ) {
  // base case
  if( n <= nmin ) {
    // probably we want to copy into full, since there doesn't seem to be a blocked packed cholesky in lapack; but the easy version for now
    int info = 0;
    //char L = 'L';
    //dpotrf_( &L, &size, Afull, &size, &info);
    //dpptrf_( &L, &n, A, &info);
    //A[0] = sqrt(A[0]);
    // this uses the unpacked, but blocked version.
    double *temp = (double*) malloc( n*n*sizeof(double) );
    double *Ap = A;
    for( int c = 0; c < n; c++ )
      for( int r = c; r < n; r++ )
	temp[c*n+r] = *(Ap++);
    char L = 'L', N = 'N';
    double none = -1., one = 1.;
    dpotrf_( &L, &n, temp, &n, &info);
    Ap = A;
    for( int c = 0; c < n; c++ )
      for( int r = c; r < n; r++ )
	*(Ap++) = temp[c*n+r];
    free(temp);
    return;
  }
  int nhalf = n/2;
  double *A11 = A;
  double *A21 = A+nhalf*(nhalf+1)/2;
  double *A22 = A21+nhalf*nhalf;
  chol(A11,nhalf);
  trsm(A21,A11,nhalf);
  syrk(A22,A21,nhalf);
  chol(A22,nhalf);
}
Exemplo n.º 10
0
Arquivo: calcs.c Projeto: sakov/enkf-c
/** Calculates inverse of a symmetric matrix via Cholesky decomposition.
 * @param m - matrix size
 * @param S - input: matrix; output: S^-1
 * @return lapack_info from dgesvd_()
 */
static int invm(int m, double** S)
{
    char uplo = 'U';
    int lapack_info;
    int i, j;

    dpotrf_(&uplo, &m, S[0], &m, &lapack_info);
    if (lapack_info != 0)
        return lapack_info;
    dpotri_(&uplo, &m, S[0], &m, &lapack_info);
    if (lapack_info != 0)
        return lapack_info;
#if 0
    for (j = 1; j < m; ++j)
        for (i = 0; i < j; ++i)
            S[i][j] = S[j][i];
#else
    for (j = 1; j < m; ++j) {
        double* colj_rowi = S[j];
        double* rowj_coli = &S[0][j];

        for (i = 0; i < j; ++i, colj_rowi++, rowj_coli += m)
            *rowj_coli = *colj_rowi;
    }
#endif
    return 0;
}
Exemplo n.º 11
0
int posymatinv(int size,double **A,double (*determinant))
{
    int i, j,INFO,N,LDA ;
    char uplo='L';
    double *AT;  /* AT=transpose vectorized matrix (to accomodate Fortran) */

    MAKE_VECTOR(AT,size*size);
    for (i=0; i<size; i++)		/* to call a Fortran routine from C */
    {   /* have to transform the matrix */
        for(j=0; j<size; j++) AT[j+size*i]=A[j][i];
    }

    N=size;
    LDA=size;

    dpotrf_ (&uplo, &N, AT, &LDA, &INFO);
    /* LAPACK routine DPOTRF computes an Cholesky decomposition of
       a symmetric positive definite matrix A.
       Parameters in the order as they appear in the function call:
       uplo="U" indicates that the strictly lower triangular part of
       A will be ignored, N is the order of the matrix A, the
       matrix A, the leading dimension of A, and the flag for the
       result. On exit, the upper triangle of A contains U.*/
    if (INFO==0) {
        int i;
        (*determinant)=1.0;
        for (i=0; i<N; i++) {
            (*determinant)*=AT[i+i*N]*AT[i+i*N];
        }
        dpotri_ (&uplo, &N, AT, &LDA, &INFO);
        /* LAPACK routine DPOTRI computes the inverse of a matrix A
           using the output of DPOTRF. This method inverts U using the
           Cholesky factorization of A.
           Parameters in the order as they appear in the function call:
           uplo="U" indicates that the strictly lower triangular part of
           A will be ignored, c1 is the order of the matrix A, the
           matrix A, the leading dimension of A, and the flag for the
           result. On exit, the upper triangle of A contains U.*/
        if (INFO!=0) {
            /* Marked by Wei-Chen Chen on 2009/06/07.
            *     printf("Problem in posymatinv: dpotri error %d\n",INFO);
            */
        }
    }
    else {
        /* Marked by Wei-Chen Chen on 2009/06/07.
        *   printf("Problem in posymatinv: dpotrf error %d\n",INFO);
        */
    }

    for (i=0; i<size; i++) {    /*transform the matrix back*/
        for(j=i; j<size; j++) {
            A[j][i]=AT[j+size*i];
            A[i][j]=AT[j+size*i];
        }
    }
    FREE_VECTOR(AT);
    return 0;
}
void dpotrf(const char *in,
                   int &n,
                   double *a,
                   int &lda,
                   int *info)

{
	dpotrf_(in,n,a,lda,info);
}
Exemplo n.º 13
0
int main( int argc, char **argv ) {
  int size = 256;
  // generate a symmetric, positive definite matrix
  double *M = (double *) malloc( size*size*sizeof(double) );
  fill( M, size*size);  
  double *Afull = (double *) malloc( size*size*sizeof(double) );
  char T = 'T', N = 'n';
  double one = 1., zero = 0.;
  dgemm_( &T, &N, &size, &size, &size, &one, M, &size, M, &size, &zero, Afull, &size );
  double *A = (double *) malloc( size*(size+1)/2*sizeof(double) );
  double *Acopy = (double *) malloc( size*(size+1)/2*sizeof(double) );
  for( int r = 0; r < size; r++ )
    for( int c = 0; c <= r; c++ ) {
      setEntry(A, size, r, c, Afull[r*size+c]);
      setEntry(Acopy, size, r, c, Afull[r*size+c], size);
    }
  //printMatrix(Afull,size);
  double startTime = read_timer();
  chol(A,size);
  double endTime = read_timer();
  printf("Time %f Gflop/s %f\n", endTime-startTime, size*size*size/3./(endTime-startTime)/1.e9); 
  int info = 0;
  char L = 'L';
  startTime = read_timer();
  dpptrf_( &L, &size, Acopy, &info);
  endTime = read_timer();
  printf("dpptrf Time %f Gflop/s %f\n", endTime-startTime, size*size*size/3./(endTime-startTime)/1.e9); 
  printf("info is %d, size is %d\n", info, size );
  startTime = read_timer();
  dpotrf_( &L, &size, Afull, &size, &info);
  endTime = read_timer();
  printf("dpotrf Time %f Gflop/s %f\n", endTime-startTime, size*size*size/3./(endTime-startTime)/1.e9); 
  //printMatrix(Afull,size);

  double maxDev = 0.;
  /*
  for( int r = 0; r < size; r++ )
    for( int c = 0; c <= r; c++ ) {
      maxDev = max(maxDev,fabs(Afull[r*size+c]-getEntry(A,size,r,c)));
      Afull[r*size+c] = getEntry(A,size,r,c);
    }
  */
  for( int r = 0; r < size; r++ )
    for( int c = 0; c < size; c++ ) {
      maxDev = max(maxDev,fabs(getEntry(Acopy,size,r,c,size)-getEntry(A,size,r,c)));
      //      Afull[r*size+c] = getEntry(Acopy,size,r,c,size);
    }
  //printMatrix(Afull,size);
  printf("Max deviation: %f\n", maxDev);
  //for( int r = 0; r < size; r++ )
  //  for( int c = 0; c < size; c++ ) {
  //    Afull[r*size+c] = getEntry(A,size,r,c);
  //  }
  //printMatrix(Afull,size);
  return 0;
}
Exemplo n.º 14
0
void Test_dpotrf(double *A,int N)
{
/*   Quark *quark=QUARK_New(OOC_NTHREADS);
     Quark_Task_Flags tflags=Quark_Task_Flags_Initializer;
     QUARK_incore_dpotrf(quark,&tflags,(int)'L',A,N);
     QUARK_Delete(quark); */
     int info;
     dpotrf_("L", &N, A, &N, &info); //computation on host
     assert(info == 0);
} 
Exemplo n.º 15
0
void IllCondDetector::Debug()
{
	/* 3x3 matrix A
	* 76 25 11
	* 27 89 51
	* 18 60 32
	*/
	double A[9] = { 76, 27, 18, 25, 89, 60, 11, 51, 32 };
	double b[3] = { 10, 7, 43 };

	int N = 3;
	int nrhs = 1;
	int lda = 3;
	int ipiv[3];
	int ldb = 3;
	int info;

	//dgesv_(&N, &nrhs, A, &lda, ipiv, b, &ldb, &info);

	//if (info == 0) /* succeed */
	//	printf("The solution is %lf %lf %lf\n", b[0], b[1], b[2]);
	//else
	//	fprintf(stderr, "dgesv_ fails %d\n", info);
	char uplo[] = "U";

	dpotrf_(uplo, &N, A, &lda, &info);

	if (0 == info)
	{
		cout << "cholesky succeed." << endl;
	}
	else
	{
		cout << "cholesky dead." << endl;
	}

	double *workcon = (double*)malloc(3 * N * sizeof(double));
	int    *lworkcon = (int*)malloc(N * sizeof(int));

	dpocon_(uplo, &N, A, &lda, &Anorm_, &rcond_num_, workcon, lworkcon, &info);

	if (0 == info)
	{
		/* succeed */
		printf("condition number calculation succeed in IllConditionDetecter.\n");
		cout << "Condition number: " << 1 / rcond_num_ << endl;
	}
	else
	{
		printf("condition number calculation in IllConditionDetecter.");
	}

	assert(info == 0);

}
Exemplo n.º 16
0
void ProtoMol::Lapack::dpotrf(char *transA, int *n, double *A, int *lda,
                              int *info) {
  FAHCheckIn();
#if defined(HAVE_LAPACK)
  dpotrf_(transA, n, A, lda, info);
#elif defined(HAVE_MKL_LAPACK)
  DPOTRF(transA, n, A, lda, info);
#else
  THROW(std::string(__func__) + " not supported");
#endif
}
Exemplo n.º 17
0
int GMRFLib_comp_chol_general(double **chol, double *matrix, int dim, double *logdet, int ecode)
{
	/*
	 * return a malloc'ed cholesky factorisation of MATRIX in *chol and optional the log(determinant). if fail return
	 * `ecode'
	 * 
	 */
	int info = 0, i, j;
	double *a = NULL, det;

	if (dim == 0) {
		*chol = NULL;
		return GMRFLib_SUCCESS;
	}

	a = Calloc(ISQR(dim), double);
	memcpy(a, matrix, ISQR(dim) * sizeof(double));

	switch (GMRFLib_blas_level) {
	case BLAS_LEVEL2:
		dpotf2_("L", &dim, a, &dim, &info, 1);
		break;
	case BLAS_LEVEL3:
		dpotrf_("L", &dim, a, &dim, &info, 1);
		break;
	default:
		GMRFLib_ASSERT(1 == 0, GMRFLib_ESNH);
		break;
	}

	if (info) {
		Free(a);
		*chol = NULL;

		return ecode;
	}

	if (logdet) {
		for (det = 0.0, i = 0; i < dim; i++) {
			det += log(a[i + i * dim]);
		}
		*logdet = 2.0 * det;
	}

	for (i = 0; i < dim; i++) {			       /* set to zero the upper part */
		for (j = i + 1; j < dim; j++) {
			a[i + j * dim] = 0.0;
		}
	}

	*chol = a;
	return GMRFLib_SUCCESS;
}
Exemplo n.º 18
0
/* Cholesky factorization */
void THLapack_(potrf)(char uplo, int n, real *a, int lda, int *info)
{
#ifdef  USE_LAPACK
#if defined(TH_REAL_IS_DOUBLE)
  dpotrf_(&uplo, &n, a, &lda, info);
#else
  spotrf_(&uplo, &n, a, &lda, info);
#endif
#else
  THError("potrf : Lapack library not found in compile time\n");
#endif
}
Exemplo n.º 19
0
	DLLEXPORT int d_cholesky_factor(int n, double* a){
		char uplo = 'L';
		int info = 0;
		dpotrf_(&uplo, &n, a, &n, &info);
		for (int i = 0; i < n; ++i)
		{
			int index = i * n;
			for (int j = 0; j < n && i > j; ++j)
			{
				a[index + j] = 0;
			}
		}
		return info;
	}
Exemplo n.º 20
0
/* Cholesky decomposition */
void THLapack_(gpotrf)(char uplo, int n, real *a, int lda, int *info)
{
#ifdef  USE_LAPACK
#if defined(TH_REAL_IS_DOUBLE)
    extern void dpotrf_(char *uplo, int *n, double *a, int *lda, int *info);
    dpotrf_(&uplo, &n, a, &lda, info);
#else
    extern void spotrf_(char *uplo, int *n, float *a, int *lda, int *info);
    spotrf_(&uplo, &n, a, &lda, info);
#endif
#else
    THError("gpotrf : Lapack library not found in compile time\n");
#endif
}
Exemplo n.º 21
0
	DLLEXPORT MKL_INT d_cholesky_factor(MKL_INT n, double* a){
		char uplo = 'L';
		MKL_INT info = 0;
		dpotrf_(&uplo, &n, a, &n, &info);
		for (MKL_INT i = 0; i < n; ++i)
		{
			MKL_INT index = i * n;
			for (MKL_INT j = 0; j < n && i > j; ++j)
			{
				a[index + j] = 0;
			}
		}
		return info;
	}
Exemplo n.º 22
0
void cholesky_(char &UPLO, int* N, double*& A, int* LDA, int* INFO){
  dpotrf_(&UPLO, N, A, LDA, INFO);
  // fill the lower part with zeroes
  if ('U' == UPLO){
    for (int i = 1; i < *N; i++){
      for (int j = 0; j < i; j++)
	A[j * (*N) + i] = 0.0;
    }
  } else { // or the upper part if it's 'L'
    for (int i = 0; i < (*N - 1); i++){
      for (int j = i + 1; j < *N; j++)
	A[j * (*N) + i] = 0.0;
    }
  }
}
Exemplo n.º 23
0
int main()
{
    // Call dpotrf_() Lapack routine (Cholesky factorization)
    int sn_size = 0;
    int lda = 1;
    int info;
    dpotrf_((char*)"LOWER",
            &sn_size,
            NULL,
            &lda,
            &info);

    std::cout << "ok for ATLAS" << std::endl;

    return 0;
}
Exemplo n.º 24
0
	DLLEXPORT int d_cholesky_solve(int n, int nrhs, double a[], double b[])
	{
		double* clone = new double[n*n];
		memcpy(clone, a, n*n*sizeof(double));
		char uplo = 'L';
		int info = 0;
		dpotrf_(&uplo, &n, clone, &n, &info);

		if (info != 0){
			delete[] clone;
			return info;
		}

		dpotrs_(&uplo, &n, &nrhs, clone, &n, b, &n, &info);
		return info;
	}
Exemplo n.º 25
0
	DLLEXPORT MKL_INT d_cholesky_solve(MKL_INT n, MKL_INT nrhs, double a[], double b[])
	{
		double* clone = new double[n*n];
		std::memcpy(clone, a, n*n*sizeof(double));
		char uplo = 'L';
		MKL_INT info = 0;
		dpotrf_(&uplo, &n, clone, &n, &info);

		if (info != 0){
			delete[] clone;
			return info;
		}

		dpotrs_(&uplo, &n, &nrhs, clone, &n, b, &n, &info);
		delete[] clone;
		return info;
	}
Exemplo n.º 26
0
/* Compute Cholesky decomposition of an nxn matrix */
void dpotrf_driver(int n, double *A, double *U) {
    double *AT;
    char uplo = 'U';
    int lda = n;
    int info;

    /* Transpose A */
    AT = (double *)malloc(sizeof(double) * n * n);
    matrix_transpose(n, n, A, AT);
    
    /* Call lapack routine */
    dpotrf_(&uplo, &n, AT, &lda, &info);

    /* Transpose AT */
    matrix_transpose(n, n, AT, U);

    free(AT);
}
Exemplo n.º 27
0
    //! Computes Cholesky decomposition and returns true if matrix is pos.def.
    bool cholesky()
    {
      char uplo = 'U';
      int info = -1, len = length(), offset = stride();

      // xx is upper triangular, cholesky l will also be
      dpotrf_(&uplo, &len, data_, &offset, &info);
      if (info != 0){
        assert(info > 0);
        return false;
        /*if (info < 0){
            throw std::logic_error("Invalid argument to chol");
          } else {
            return false;
          }*/
      }
      return true;
    }
Exemplo n.º 28
0
template <typename fptype> static inline int
lapack_Cholesky(fptype* a, size_t a_step, int m, fptype* b, size_t b_step, int n, bool* info)
{
    int lapackStatus;
    int lda = a_step / sizeof(fptype);
    char L[] = {'L', '\0'};

    if(b)
    {
        if(n == 1 && b_step == sizeof(fptype))
        {
            if(typeid(fptype) == typeid(float))
                sposv_(L, &m, &n, (float*)a, &lda, (float*)b, &m, &lapackStatus);
            else if(typeid(fptype) == typeid(double))
                dposv_(L, &m, &n, (double*)a, &lda, (double*)b, &m, &lapackStatus);
        }
        else
        {
            int ldb = b_step / sizeof(fptype);
            fptype* tmpB = new fptype[m*n];
            transpose(b, ldb, tmpB, m, m, n);

            if(typeid(fptype) == typeid(float))
                sposv_(L, &m, &n, (float*)a, &lda, (float*)tmpB, &m, &lapackStatus);
            else if(typeid(fptype) == typeid(double))
                dposv_(L, &m, &n, (double*)a, &lda, (double*)tmpB, &m, &lapackStatus);

            transpose(tmpB, m, b, ldb, n, m);
            delete[] tmpB;
        }
    }
    else
    {
        if(typeid(fptype) == typeid(float))
            spotrf_(L, &m, (float*)a, &lda, &lapackStatus);
        else if(typeid(fptype) == typeid(double))
            dpotrf_(L, &m, (double*)a, &lda, &lapackStatus);
    }

    if(lapackStatus == 0) *info = true;
    else *info = false; //in opencv Cholesky function false means error

    return CV_HAL_ERROR_OK;
}
Exemplo n.º 29
0
int main( int argc, char *argv[] ) {
  int n, i, j, info;
  double *A;

  double t1, t2;

  if( argc<2 ) {
    fprintf(stderr,"usage: %s n\n",argv[0]);
    exit(-1);
  }
  sscanf(argv[1],"%d",&n);

  if( ( A = (double*) malloc(n*n*sizeof(double)) ) == NULL ) {
    fprintf(stderr,"Error reserving suficient memory for the matrix A\n");
    exit(-1);
  }

  // Fill the Matrix with random content
  for( j=0; j<n; j++ ) {
    for( i=j; i<n; i++ ) {
      A[j*n+i] = ((double) rand()) / RAND_MAX;
    }
    A[j*n+j] += n;
  }

  t1 = omp_get_wtime();
  #pragma omp parallel
  #pragma omp single
  chol_escalar(A,n);
  t2 = omp_get_wtime();
  
  printf("\n");
  printf("Time: %f\n\n", t2-t1);

  // Check result
  dpotrf_( "L", &n, A, &n, &info ); 
  
  if( info != 0 ) {
    fprintf(stderr,"Error = %d in the Cholesky decomposition. \n",info);
    exit(-1);
  }

  free(A);
}
/**
 * Compute the cholesky factorization of the adjacency submatrix that is 
 * everything but the last two rows and columns.
 * \param[inout] adjacency_matrix The adjacency represenation of the graph.
 */
static void factorize_submatrix (std::vector<double>& adjacency_matrix) {
  int matrix_dimension = static_cast<int>(sqrt (adjacency_matrix.size()));
  int submatrix_dimension = matrix_dimension-2;
  int result=0;
  char UPLO='U';

  assert (0<submatrix_dimension);

  dpotrf_(&UPLO, /* Store the upper triangle of the factorized matrix */
          &submatrix_dimension, /* dimension of the matrix */
          &(adjacency_matrix[0]), /* first element of the buffer */
          &matrix_dimension, /* stride or leading dimension */
          &result); /* result variable; 0 == SUCCESS */

  if (0!=result) {
    fprintf (stderr, "Cholesky factorization failed with %d\n", result);
    exit (1);
  }
}