Example #1
0
int main()
{
    int n = 3;
    std::vector<double> m1 = {25, 15, -5,
                              15, 18,  0,
                              -5,  0, 11};
    std::vector<double> c1 = cholesky(m1, n);
    #ifdef DEBUG
    show_matrix(c1, n);
    std::printf("\n");
    #endif
 
    n = 4;
    std::vector<double> m2 = {18, 22,  54,  42,
                              22, 70,  86,  62,
                              54, 86, 174, 134,
                              42, 62, 134, 106};
    std::vector<double> c2 = cholesky(m2, n);
    #ifdef DEBUG
    show_matrix(c2, n);
    std::printf("\n");
    #endif
 
    return 0;
}
Example #2
0
// comms is an array of communicators of length P.  Only the I and J entries will be valid.  They are the communicators along the given row/column of X.
int cholesky( double *L, int h, int bs, int n, int I, int J, int P, MPI_Comm *comms ) {
  int info1;
  int info2 = 0;
  int bs2 = bs*bs;
  int sbs2 = bs*bs;
  if( I != J )
    sbs2 = 2*sbs2;
  
  // compute the cholesky factorization of the first block  
  info1 = chol( L, bs, n, I, J, P, comms );

  if( h > 1 ) {
    // trsm of the first block column
    double *X = L+bs2;
    tstrsm( X, L, h-1, bs, I, J, P, comms );
    // syrk of the rest of the matrix
    double *Lnew = X+(h-1)*sbs2;
    tssyrk( X, Lnew, h-1, bs, I, J, P, comms );
    // cholesky of the rest of the matrix
    info2 = cholesky( Lnew, h-1, bs, n-P*bs, I, J, P, comms );
  }
  if( info1 )
    return info1;
  return info2;
}
Example #3
0
void invSPDMatrix(double *Matrix, int nrow){

	double tmp, det;
	int i;
	if (nrow == 1){
		Matrix[0] = 1.0/Matrix[0];
	} else if (nrow == 2){
		det = Matrix[0]*Matrix[3] - Matrix[1]*Matrix[2];
		tmp = Matrix[0];
		Matrix[0] = Matrix[3];
		Matrix[3] = tmp;
		Matrix[1] = -Matrix[1];
		Matrix[2] = -Matrix[2];
		for (i=0; i<nrow*nrow; i++) Matrix[i] /= det;
	} else if (nrow > 2){
		double *L = calloc(nrow*nrow, sizeof(double));
		double *tL = calloc(nrow*nrow, sizeof(double));
		cholesky(Matrix, nrow, L, 0);
		invMatrix(L, nrow);
		for (i=0; i<nrow*nrow; i++) tL[i] = L[i];
		tr(tL, nrow, nrow);
		prodMatrix(tL, L, Matrix, nrow, nrow, nrow, nrow);
		free(L); free(tL);
	}
}
Example #4
0
int main(int argc, char* argv[]) {
    int i, j;
    int n = (SIZE);
    double A[(SIZE) * (SIZE)] = {0};
    double *L = NULL;
    FILE *fp = NULL;
    char filename[150];

    sprintf(filename, "%s/matrices/%d", PATH, SIZE);
    fp = fopen(filename, "r");
    if (fp == NULL) {
        return 1;
    }

    for (i = 0; i < n * n; ++i) {
        fscanf(fp, "%lf", A + i);  
    }

    start_clock();
    start_papi();
    L = cholesky(A, n);
    stop_papi();
    stop_clock();

    double checksum = 0.0;
    for (i = 0; i < n; ++i) {
        for (j = 0; j < n; ++j) {
            checksum += i + j + L[INDEX(i, j)];
        }
    }

    printf("Size: %d\n", n);
    printf("Checksum: %lf\n", checksum);
    return 0;
}
Example #5
0
 /**
  * Get the lower triangular part of the Cholesky decomposition 
  * on the covariance matrx Sx_ 
  * \param[out] Sx_Chol_L the lower triangular part of the Choloesky decomposition
  */
 void getCovCholeskyDecompLower( MatType &Sx_Chol_L){
   if(!isValid_Sx_L_){
     ::Eigen::LLT<MatType> cholesky( Sx_ );
     Sx_L_ = cholesky.matrixL();
     isValid_Sx_L_ = true;
   }
   Sx_Chol_L = Sx_L_;
 }
void test_boostmultiprec()
{
  typedef Matrix<Real,Dynamic,Dynamic> Mat;
  typedef Matrix<std::complex<Real>,Dynamic,Dynamic> MatC;

  std::cout << "NumTraits<Real>::epsilon()         = " << NumTraits<Real>::epsilon() << std::endl;
  std::cout << "NumTraits<Real>::dummy_precision() = " << NumTraits<Real>::dummy_precision() << std::endl;
  std::cout << "NumTraits<Real>::lowest()          = " << NumTraits<Real>::lowest() << std::endl;
  std::cout << "NumTraits<Real>::highest()         = " << NumTraits<Real>::highest() << std::endl;
  std::cout << "NumTraits<Real>::digits10()        = " << NumTraits<Real>::digits10() << std::endl;

  // chekc stream output
  {
    Mat A(10,10);
    A.setRandom();
    std::stringstream ss;
    ss << A;
  }
  {
    MatC A(10,10);
    A.setRandom();
    std::stringstream ss;
    ss << A;
  }

  for(int i = 0; i < g_repeat; i++) {
    int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);

    CALL_SUBTEST_1( cholesky(Mat(s,s)) );

    CALL_SUBTEST_2( lu_non_invertible<Mat>() );
    CALL_SUBTEST_2( lu_invertible<Mat>() );
    CALL_SUBTEST_2( lu_non_invertible<MatC>() );
    CALL_SUBTEST_2( lu_invertible<MatC>() );

    CALL_SUBTEST_3( qr(Mat(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
    CALL_SUBTEST_3( qr_invertible<Mat>() );

    CALL_SUBTEST_4( qr<Mat>() );
    CALL_SUBTEST_4( cod<Mat>() );
    CALL_SUBTEST_4( qr_invertible<Mat>() );

    CALL_SUBTEST_5( qr<Mat>() );
    CALL_SUBTEST_5( qr_invertible<Mat>() );

    CALL_SUBTEST_6( selfadjointeigensolver(Mat(s,s)) );

    CALL_SUBTEST_7( eigensolver(Mat(s,s)) );

    CALL_SUBTEST_8( generalized_eigensolver_real(Mat(s,s)) );

    TEST_SET_BUT_UNUSED_VARIABLE(s)
  }

  CALL_SUBTEST_9(( jacobisvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
  CALL_SUBTEST_10(( bdcsvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
}
Example #7
0
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
// Calculating Ts = chol( solve( Ds ) ) for the BDMCMC sampling algorithm
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
void get_Ts( double Ds[], double Ts[], double inv_Ds[], double copy_Ds[], int *p )
{
	int dim = *p; 

	memcpy( &copy_Ds[0], Ds, sizeof( double ) * dim * dim );
	
	inverse( &copy_Ds[0], &inv_Ds[0], &dim );	

	cholesky( &inv_Ds[0], Ts, &dim );	
}
Example #8
0
double det(const dsymatrix & A)
{
      dgematrix L;
      cholesky(A,L);
      double d=1.;
      int i,N=A.n;
      for(i=0;i<N;i++)
	    d*=L(i,i)*L(i,i);
      return d;
}
Example #9
0
// solve a symmetric, positive definite system
static void solve_spd(long double *x, long double *A, long double *b, int n)
{
	// factor the matrix A = L * L'
	long double L[n*n];
	cholesky(L, A, n);

	// solve the two triangular systems L * L' * x = b
	long double t[n];
	solve_lower(t, L, b, n);
	solve_upper(x, L, t, n);
}
Example #10
0
void mexFunction
(
 int nout, 
 mxArray *pout[],
 int nin, 
 const mxArray *pin[]
 )
{
  mwSize mrows, ncols;
  double *x, *y, *cols;
  int *p;
  int N;

  if (nin != 3)
    mexErrMsgTxt("Three inputs required.");
  if (nout != 1)
    mexErrMsgTxt("One output required.");

  mrows = mxGetM(pin[0]);
  ncols = mxGetN(pin[0]);

  /*pout[0] = pin[0];*/
  pout[0] = mxCreateDoubleMatrix(mrows, ncols, mxREAL);

  y = mxGetPr(pout[0]);
  x = mxGetPr(pin[0]);
  cols = mxGetPr(pin[1]);

  memcpy(y, x, mrows*ncols*sizeof(double));

  /*
  p = malloc(mrows*sizeof(int));
  for (int m=0; m<mrows; m++) {
    p[m] = (int)x[m] - 1;
  }
  */

  N = (int)mxGetScalar(pin[2]);
  int retval = cholesky(y, cols, mrows, ncols-2, N);
  /*
  int retval = cholesky(y, y+mrows, y+(2*mrows), mrows, ncols-2, N);
  */
  
  /*free(p);*/

  if (retval == -1) 
    mexErrMsgTxt("Symbolic structure not consistent.");

  if (retval == -2)
    mexErrMsgTxt("Matrix not positive definite.");

  return;
}
Example #11
0
void
genmultgauss(double *rvec, int num, int n, double *covar)
// rvec contains num mvg variates
{
  double *cf ;
  ZALLOC(cf, n*n, double) ;
  cholesky(cf, covar, n) ;
  transpose(cf, cf, n, n) ;
  gaussa(rvec, num*n) ;
  mulmat(rvec, rvec, cf, num, n, n) ;
  free(cf) ;
}
Example #12
0
int main() {
    int n = 3;
    double M[] = {25, 15, -5,
                   15, 18,  0,
                   -5,  0, 11};
    double *C = cholesky(M, n);
    affichage(C, n);
    printf("\n");
    free(C);
 
    n = 4;
    double N[] = {18, 22,  54,  42,
                   22, 70,  86,  62,
                   54, 86, 174, 134,
                   42, 62, 134, 106};
    double *D = cholesky(N, n);
    affichage(D, n);
    free(D);
 
    return 0;
}
Example #13
0
//-----------------------------------------------------------------------
void
Kriging1D::krigVector(double * data,
                      double * trend,
                      int     nd,
                      float   dz)
{
  //
  // This class takes the incomplete vector 'data' and fills it using kriging.
  //
  int * index = new int[nd];

  double range = 500.0;
  double power =   1.5;

  int md;
  locateValidData(data, index, nd, md);

  if (md < nd) {
    subtractTrend(data, trend, index, md);

    double ** K;  // Kriging matrix
    double ** C;  // Kriging matrix cholesky decomposed
    double *  k;  // Kriging vector

    allocateSpaceForMatrixEq(K, C, k, md);
    fillKrigingMatrix(K, index, md, range, power, static_cast<double>(dz));
    cholesky(K, C, md);

    for (int krigK = 0 ; krigK < nd ; krigK++) {
      if (data[krigK] == RMISSING) {
        fillKrigingVector(k, index, md, range, power, static_cast<double>(dz), krigK);
        lib_matrAxeqbR(md, C, k); // solve kriging equation

        data[krigK] = 0.0f;
        for (int i = 0 ; i < md ; i++) {
          data[krigK] += static_cast<float>(k[i]) * data[index[i]];
        }
      }
    }
    deAllocateSpaceForMatrixEq(K, C, k, md);

    addTrend(data, trend, nd);

    bool debug = false;
    if (debug) {
      for (int i = 0 ; i < nd ; i++) {
        LogKit::LogFormatted(LogKit::Low," i krigedData[i] : %3d  %.5f\n",i,data[i]);
      }
    }
  }
  delete [] index;
}
Eigen::VectorXd spd_solve(const Eigen::MatrixXd & A,
                          const Eigen::VectorXd & b)
{
    int n = b.size();
    assert(A.rows() == n);
    assert(A.cols() == n);

    Eigen::MatrixXd U = cholesky(A);
    Eigen::VectorXd y = forward_subst(U.transpose(), b);
    Eigen::VectorXd x = backward_subst(U, y);

    return x;
}
Example #15
0
int multivariate_normal_draw(dcovector & X, gsl_rng * r, const dsymatrix & Q){
      X.resize(Q.n);
      dgematrix L;
      int i;
      for (i=0;i<X.l;i++)
	    X(i)=gsl_ran_gaussian(r,1.);
      if(  cholesky(Q,L)){
	    X.zero();
	    return 1;
      }
      X=L*X;
      return 0;
}
Example #16
0
int main (int argc, char *argv[])
{
#ifdef _DIST_
    CnC::dist_cnc_init< cholesky_context > dc_init;
#endif
    int n;
    int b;
    dist_type dt = BLOCKED_ROWS;
    const char *fname = NULL;
    const char *oname = NULL;
    const char *mname = NULL;
    int argi;

    // Command line: cholesky n b filename [out-file]
    if (argc < 3 || argc > 7) {
        fprintf(stderr, "Incorrect number of arguments, epxected N BS [-i infile] [-o outfile] [-w mfile] [-dt disttype]\n");
        return -1;
    }
    argi = 1;
    n = atol(argv[argi++]);
    b = atol(argv[argi++]);
    while( argi < argc ) {
        if( ! strcmp( argv[argi], "-o" ) ) oname = argv[++argi];
        else if( ! strcmp( argv[argi], "-i" ) ) fname = argv[++argi];
        else if( ! strcmp( argv[argi], "-w" ) ) mname = argv[++argi];
        else if( ! strcmp( argv[argi], "-dt" ) ) dt = static_cast< dist_type >( atoi( argv[++argi] ) );
        ++argi;
    }

#ifdef USE_MKL
    if( mname == NULL ) {
        mkl_set_num_threads( 1 );
        omp_set_num_threads( 1 );
    }
#endif

    if(n % b != 0) {
        fprintf(stderr, "The tile size is not compatible with the given matrix\n");
        exit(0);
    }

    double * A = new double[n*n];

    matrix_init( A, n, fname );
    if( mname ) matrix_write( A, n, mname );
    else cholesky(A, n, b, oname, dt);     

    delete [] A;

    return 0;
}
Example #17
0
double raninvwis(double *wis, int t, int d, double *s) 
// inverse Wishart:  t d.o.f. d dimension S data matrix  
// Ref Liu: Monte Carlo Strategies pp 40-41

{
   double *b, *n, *v, *cf, *ww, y ;
   int i, j ;

   if (t < d)  { 
     fatalx("(raninvwis) d.o.f. too small %d %d\n", t, d) ;
   }

   ZALLOC(b, d*d, double) ;
   ZALLOC(n, d*d, double) ;
   ZALLOC(v, d, double) ;
   ZALLOC(cf, d*d, double) ;
   ZALLOC(ww, d*d, double) ;

   for (i=0; i<d; i++) { 
    v[i] = ranchi(t-i) ;
    for (j=0; j<i; j++) {  
     n[i*d+j] = n[j*d+i] = gauss() ;
    }
   }

   y = b[0] = v[0] ;  
   for (j=1; j<d; j++)  {  
    b[j*d+0] = b[j] = sqrt(y)*n[j] ;
   }
   for (j=1; j<d; j++)  {  
    b[j*d+j] = v[j]  ;
    for (i=0; i<j; i++) {  
     y = n[i*d+j] ;
     b[j*d+j] += y*y ;
     b[j*d+i] = b[i*d+j] = y*sqrt(v[i]) + vdot(n+i*d, n+j*d, i-1) ;
    }
   }

   cholesky(cf, s, d) ;
   mulmat(ww, cf, b, d, d, d) ;
   transpose(cf, cf, d, d) ;
   mulmat(wis, ww, cf, d, d, d) ;

   free(b) ; 
   free(n) ;
   free(v) ;
   free(cf) ;
   free(ww) ;
}
Example #18
0
/*----------------------------------------------------------------------------*/
double *inverse (double *A, int n, double *tempo) {
    double t1 = timestamp();

    double *L = cholesky (A, n);

    double *L_inv = forward_subs (L, n);

    double *A_inv = mult_at_a (L_inv, n);
    
    free(L);
    free(L_inv);

    *tempo = timestamp()-t1;
    return A_inv;
}
Example #19
0
/*
 * Computer problem 4.2 #2
 */
void problem42_2()
{
  // variables
  struct matrix mat,chol,cholT;
  struct matrix4 eq = {{
      {0.05,0.07,0.06,0.05},
      {0.07,0.10,0.08,0.07},
      {0.06,0.08,0.10,0.09},
      {0.05,0.07,0.09,0.10}
    }};
  struct array ans = {{0.23,0.32,0.33,0.31}};
  struct array y,x;

  // setup
  mat.m4 = eq;
  mat.size = NUM4;

  // print
  printf("Problem 4.2 #2\n");
  printf("Solve this system by Cholesky method:\n");
  printf("0.05x1 + 0.07x2 + 0.06x3 + 0.05x4 = 0.23\n");
  printf("0.07x1 + 0.10x2 + 0.08x3 + 0.07x4 = 0.32\n");
  printf("0.06x1 + 0.08x2 + 0.10x3 + 0.09x4 = 0.33\n");
  printf("0.05x1 + 0.07x2 + 0.09x3 + 0.10x4 = 0.31\n");

  // do the work
  chol = cholesky(mat);
  // transpose
  cholT = transposeMatrix(chol);
  // solve Ly = b (for y)
  y = forwardSubstitution(chol,ans);
  // solve L^Tx = y (for x)
  x = backSubstitution(cholT,y);

  // print out results
  printf("\nCholesky\n");
  printMatrix(chol);
  printf("\nTranspose \n");
  printMatrix(cholT);
  printf("\nLy = b (for y)\n");
  printArray(y,"y");
  printf("L^Tx = y (for x)\n");
  printArray(x,"x");
  printf("\n");
  return;
}
Example #20
0
double det(const dgematrix & A){
      int N=A.m;
      dgematrix L;
      dsymatrix S(N);
      
      double d=1.;
      int i;
      int j;
      for (j=0;j<N;j++)
	    for(i=j;i<N;i++)
		  S(i,j)=A(i,j);
      
      cholesky(S,L);
      for(i=0;i<N;i++)
	    d*=L(i,i)*L(i,i);
      return d;
}
Example #21
0
double runAlgorithmCholesky(double **inputMatrix, double *b, int size, double *x) {
	double **factorizedMatrix = allocate2DArray(size, size);
	double **factorizedMatrixTrans = allocate2DArray(size, size);
	double *y = allocade1DArray(size);
	double timeConsumed = 0;


	time_t start = time(NULL);//POMIAR CZASU START

	cholesky(size, inputMatrix, factorizedMatrix);

    //printf("Sfaktoryzowana  macierz Choleskiego (U): \n");
    //print2DArray(factorizedMatrix, size, size);


    matrixTransposition(size, factorizedMatrix, factorizedMatrixTrans);

    //printf("Sfaktoryzowana transponowana macierz Choleskiego (U): \n");
    //print2DArray(factorizedMatrixTrans, size, size);


    forwardSolutionPhase(size, factorizedMatrixTrans, b, y);
    //printf("\nWynik po forwardSolutionPhase: \n");
    //print1DArray(y, size);


    backwardSolutionPhase(size, factorizedMatrix, y, x);
    //printf("\nWynik po backwardSolutionPhase: \n");
    //print1DArray(x, size);

    timeConsumed = (double)(time(NULL) - start);//POMIAR CZASU END

    //printf("\nSprawdzenie rozwiązania: \n");
    //checkSolution(inputMatrix, x, b, size);


    deallocate2D(factorizedMatrix,size);
    deallocate2D(factorizedMatrixTrans,size);
    free(y);

    return timeConsumed;
}
Example #22
0
int cg(MAT &A,VEC b,VEC &x,int maxIter,double tol){
	int n = A.dim();
	int k = 0;
	VEC x_next(n);
	VEC r_next(n);
	VEC p_next(n);
	VEC r(n);
	VEC p(n);
	MAT L(n);
    VEC cpr(n);
	double alpha,beta;
	double err;
	double diff;
	r = b - A*x;					//initial condition
	p = r;
	while(k<maxIter){				//conjugate gradient decent
		alpha = (r*r)/(p*(A*p));
		x_next = x + alpha*p;
		r_next = r - alpha*(A*p);
		beta = (r_next*r_next)/(r*r);
		p_next = r_next + beta*p;
		k++;
		x = x_next;					//assign to the x,r,p of the next iteration 
		r = r_next;
		p = p_next;
		err = pow((r*r)/n,0.5);
		if(err<tol)					//see if the error is smaller than the defined tolerance. if so, break out of the loop 
			break;
	}
        diff = 0;
        //the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
                diff = max(diff,fabs(x[i]-cpr[i]));		//use infinity norm to compute the error
        }
        printf("error between hw4 and hw6(infinity-norm): %e\n",diff);

	return k;	
}
Example #23
0
void splineM(int N,VEC &X,VEC &Y,VEC &M){
	M[0]=0;
	M[N]=0;
	VEC h(N);
	h[0] = 0;
	for(int i=1;i<N;i++){			// h is the length of the subinterval 
		h[i] = X[i] - X[i-1];
	}
	VEC MU(N+1);
	VEC LAMBDA(N);
	VEC d(N+1);
	MAT W(N+1);
	MAT L(N+1);
	LAMBDA[0] = 0;
	d[0] = 0;
	MU[N] = 0;
	d[N] = 0;
	for(int i=1;i<N;i++){			//prepare the variable for the tridiagonal matrix
		MU[i] = h[i]/(h[i]+h[i+1]);
		LAMBDA[i] = h[i+1]/(h[i]+h[i+1]);
		d[i] = (6/(h[i]+h[i+1]))*(((Y[i+1]-Y[i])/h[i+1])-((Y[i]-Y[i-1])/h[i]));
	}
	for(int i=0;i<N+1;i++){		//initialization
		for(int j=0;j<N+1;j++){
			W[i][j] = 0;
		}
	}
	for(int i=0;i<N+1;i++){		
		W[i][i] = 2;			//diagonal
		
	}
	for(int i=0;i<N;i++){
		W[i][i+1] = LAMBDA[i];	//above diagonal
	}
	for(int i=1;i<N+1;i++){
		W[i][i-1] = MU[i];	//below diagonal
		
	}
	
	L = cholesky(W);                        //in-place Cholesky Decomposition
    M = choSolve(L,d);                      //solve the linear system by forward and backward substitution
}
Example #24
0
int multivariate_normal_draw(dcovector & X, gsl_rng * r, const dgematrix & Q){
      X.resize(Q.n);
      dgematrix L;
      int i;
      int j;
      int N=Q.m;
      dsymatrix S(N);
      for (i=0;i<X.l;i++)
	    X(i)=gsl_ran_gaussian(r,1.);
      
      for (j=0;j<N;j++)
	    for(i=j;i<N;i++)
		  S(i,j)=Q(i,j);

      if(  cholesky(S,L)){
	    X.zero();
	    return 1;
      }
      X=L*X;
      return 0;
}
int main() 
{
	/* Step 1 : Generate covariance matrix & standard normal random number files */
	int m_size = 3200;
	int d_size = 30;
	int debug_step = 0;

	double *cov_numbers = cov_gen(m_size);
	printf("Step %d completed - generation of a covariance matrix\n", debug_step);

	double *z_numbers = z_gen(m_size, d_size);

	printf("Step %d completed - generation of a Z matrix\n", debug_step+1);

	/* Step 2 : Cholesky Decomposition of Sigma-matrix */
	double *c2 = cholesky(cov_numbers, m_size);

	printf("Step %d completed - Cholesky decomposition\n", debug_step+2);

	/* Step 3 : Correlated Random Number Generator: Matrix Multiplication */
	double *c3 = mat_mul(c2, z_numbers, m_size, d_size);

	printf("Step %d completed - matrix multiplication\n", debug_step+3);

	/* Step 4 : Write result files*/
	system("rm -rf result");
	system("mkdir result");

	file_write("./result/result.txt", m_size, d_size, c3);
	printf("Step %d completed - generation of results\n", debug_step+4);

	free(cov_numbers);
	free(z_numbers);
	free(c2);
	free(c3);

	printf("Step %d completed - memory release\n", debug_step+5);

	return 0;
}
Example #26
0
int sqrtm(const dsymatrix &A, dgematrix & sqrtA)
{
      dgematrix P,D;
      int i;
      
      if (!cholesky(A,sqrtA))
            {
                  return 0;
            }
      if (!eigen_decomposition(A,P,D))
            {
                  for (i=0;i<D.m; i++)
                        {
                              D(i,i)=sqrt(fabs(D(i,i)));
                        }
                  sqrtA = P * D * t(P);
                  return 0;
            }
      
      cerr<<"BFilt :: SQRTM Problem."<<endl;
      return 1;
}
Example #27
0
int main(int argc, char** argv) {
 	magma_init();
 	magma_timestr_t start , end;
 	double gpu_time ;
 	double *c;
 	int dim[] = {20000,30000,40000};
 	int i,n;
 	n = sizeof(dim) / sizeof(dim[0]);
 
 	for(i=0; i < n; i++) {
 		magma_int_t m = dim[i];
 		magma_int_t mm=m*m;
 		magma_err_t err;

 		err = magma_dmalloc_cpu ( &c , mm );
 		//generate random symmetric, positive matrix
 		double *ml = generate_sym_matrix(m);
 
 		start = get_current_time();
 		
 		//find the inverse matrix for MxM symmetric, positive definite matrix using the cholesky decomposition.
 		//Compute GPU cholesky decomposition with CPU interface
 		c = cholesky(ml, m);
 
 		end = get_current_time();
 
 		gpu_time = GetTimerValue(start,end)/1e3;
 
 		printf("gpu time for %dx%d: %7.5f sec\n", m, m, gpu_time);

 		//copy upper diag
 		copy_upper_diag(c,m);

 		free(c);
 	}
 
 	magma_finalize ();
 	return 0;
}
Example #28
0
T randomNormalMulti( T means, U varcovar )
{
/*  
  - Let $u$ a vector of $n$ number, following a centered/reducted
  normal distribution; 
  - let $L$ be the matrix resulting from the Cholesky decomposition
  of $V$; 
  - the vector $y=m+Lu$ follow the multi-normal distribution, 
  with a mean $m$ and a variance-covariance matrix $V$.
*/
  T finalPoint;
  
  // Cholesky decomposition of the variance-covariance matrix
  U popVarCholesky; // low triangular matrix
  popVarCholesky = cholesky( varcovar );

  // Vector with terms in a centered/reducted normal distribution
  T u;
  T mean(means.size(),0.0);
  T variance(means.size(),1.0);
  u = randomNormal( mean,variance );

  // temporary vector for multiplication
  U tempU;
  tempU.push_back(u);
  
  // post multiplication by the u vector
  U tempCompVar;
  tempCompVar = multiply( popVarCholesky, transpose(tempU) );

  // transposition
  T compVar = transpose(tempCompVar)[0];

  // addition to the mean
  finalPoint = addition( means, compVar );
  
  return finalPoint;
}
Example #29
0
  /** 
   * Sample this random vector and write the result over the mean x_;
   */
  void sample(){
    
    VecType x_sample, indep_noise;

    if(!isValid_Sx_L_){
      ::Eigen::LLT<MatType> cholesky( Sx_ );
      Sx_L_ = cholesky.matrixL();
      isValid_Sx_L_ = true;
    }

    if(gen_ == NULL){
      gen_ = new ::boost::variate_generator< ::boost::mt19937, 
					     ::boost::normal_distribution<double> >
	(::boost::mt19937(rand()), ::boost::normal_distribution<double>());
    }
    
    int n = Sx_L_.cols();
    for(int i = 0; i < n; i++){
      indep_noise(i) = (*gen_)();
    }
    x_ += Sx_L_ * indep_noise;

  }
Example #30
0
bool GaussianMixtureModels::estep( const MatrixFloat &data, VectorFloat &u, VectorFloat &v, Float &change ){

	Float tmp,sum,max,oldloglike;
	for(UINT j=0; j<numInputDimensions; j++) u[j] = v[j] = 0;

	oldloglike = loglike;

	for(UINT k=0; k<numClusters; k++){
		Cholesky cholesky( sigma[k] );
		if( !cholesky.getSuccess() ){ return false; }
		lndets[k] = cholesky.logdet();

		for(UINT i=0; i<numTrainingSamples; i++){
			for(UINT j=0; j<numInputDimensions; j++) u[j] = data[i][j] - mu[k][j];
			if( !cholesky.elsolve(u,v) ){ return false; }
			sum=0;
			for(UINT j=0; j<numInputDimensions; j++) sum += SQR(v[j]);
			resp[i][k] = -0.5*(sum + lndets[k]) + log(frac[k]);
		}
	}

	//Compute the overall likelihood of the entire estimated paramter set
	loglike = 0;
	for(UINT i=0; i<numTrainingSamples; i++){
		sum=0;
		max = -99.9e99;
		for(UINT k=0; k<numClusters; k++) if( resp[i][k] > max ) max = resp[i][k];
		for(UINT k=0; k<numClusters; k++) sum += exp( resp[i][k]-max );
		tmp = max + log( sum );
		for(UINT k=0; k<numClusters; k++) resp[i][k] = exp( resp[i][k] - tmp );
		loglike += tmp;
	}
    
    change = (loglike - oldloglike);

	return true;
}