int GP::infer_diag(int Ns, double* Xs, int* Ds, double* R){ //populate Kxs //printf("x%d",maxinfer); if (Ns>=maxinfer){ maxinfer = 2*Ns; //printf("resize %d ",maxinfer); Ksx.resize(N*maxinfer); Ksx_T.resize(N*maxinfer); } double smodel=0; for (int i=0; i<Ns; i++){ //printf("[%f %f]\n",Xs[i*D],Xs[i*D+1]); for (int j=0; j<N; j++){ Ksx_T[i+j*Ns] = Ksx[i*N+j] = kern[K](&X[j*D],&Xs[i*D],Dx[j],Ds[i],D,&ih[0],&smodel); //printf("[%f]\n",Ksx[i*N+j]); } } int c = LAPACKE_dpotrs(LAPACK_ROW_MAJOR,'L',N,Ns,&Kxx[0],N,&Ksx_T[0],Ns); for (int i=0; i<Ns; i++){ R[i] = cblas_ddot(N,&Y[0],1,&Ksx[i*N],1); R[Ns+i] = kern[K](&Xs[i*D],&Xs[i*D],Ds[i],Ds[i],D,&ih[0],&smodel) - cblas_ddot(N,&Ksx_T[i],Ns,&Ksx[i*N],1); //printf("(%f %f) ",R[i],R[i+Ns]); } //printf("\n"); return c; }
int GP::infer_full(int Ns, double* Xs, int* Ds, double* R){ //populate Kxs if (Ns>=maxinfer){ maxinfer = 2*Ns; //printf("resize %d ",maxinfer); Ksx.resize(N*maxinfer); Ksx_T.resize(N*maxinfer); } double smodel=0; for (int i=0; i<Ns; i++){ for (int j=0; j<N; j++){ Ksx_T[i+j*Ns] = Ksx[i*N+j] = kern[K](&X[j*D],&Xs[i*D],Dx[j],Ds[i],D,&ih[0],&smodel); } R[Ns+Ns*i+i] = kern[K](&Xs[i*D],&Xs[i*D],Ds[i],Ds[i],D,&ih[0],&smodel); for (int h=i+1; h<Ns; h++){ R[Ns+Ns*h+i]=R[Ns+Ns*i+h] = kern[K](&Xs[h*D],&Xs[i*D],Ds[h],Ds[i],D,&ih[0],&smodel); } } //I'm unconvinced this is the best way of doing it int c = LAPACKE_dpotrs(LAPACK_ROW_MAJOR,'L',N,Ns,&Kxx[0],N,&Ksx_T[0],Ns); cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,Ns,Ns,N,-1.,&Ksx[0],N,&Ksx_T[0],Ns,1.,&R[Ns],Ns); for (int i=0; i<Ns; i++){ R[i] = cblas_ddot(N,&Y[0],1,&Ksx[i*N],1); } return c; }
int dense_vector_solve_cholesky(Dense *A, DenseUPLO uplo, DenseVector* b) { lapack_int status; enum CBLAS_ORDER order; char ul; /* todo: check A and b have same order */ order = (A->order == DENSE_COLUMN_MAJOR)? CblasColMajor:CblasRowMajor; ul = (uplo==DENSE_LO)?'L':'U'; status = LAPACKE_dpotrs(order,ul,A->rows,1,A->data,A->tda,b->data,b->size); return PM_SUCCESS; }
GP_LKonly::GP_LKonly(int d, int n, double* Xin, double* Yin, double* Sin, int* Din, int kindex, double* hyp, double* R){ D=d; N=n; K=kindex; //lk = 0.; //hyp process std::vector<double> ih = std::vector<double>(numhyp(kindex,d)); hypconvert(&hyp[0], K, D, &ih[0]); //buildK std::vector<double>Kxx = std::vector<double>(N*N); double smodel = 0; for (int i=0; i<N; i++){ Kxx[i*N+i] = kern[K](&Xin[i*D], &Xin[i*D],Din[i],Din[i],D,&ih[0],&smodel); for (int j=0; j<i; j++){ Kxx[i*N+j] = Kxx[i+N*j] = kern[K](&Xin[i*D], &Xin[j*D],Din[i],Din[j],D,&ih[0],&smodel); //if (i<10){printf("%f %f %d %d %d %f %f %f %f _ ",Xin[i*D], Xin[j*D],Din[i],Din[j],D,ih[0],ih[1],ih[2],k(&Xin[i*D], &Xin[j*D],Din[i],Din[j],D,&ih[0]));} } Kxx[i*N+i]+=Sin[i]+smodel; } //cho factor int c = LAPACKE_dpotrf(LAPACK_ROW_MAJOR,'L',N,&Kxx[0],N); if (c!=0){ printf("failed to cho fac Kxx %d with hyp [",c); for (int i=0; i<numhyp(kindex,d);i++){printf("%f ",hyp[i]);} printf("]"); R[0]=-1e22; return; } std::vector<double>Yd = std::vector<double>(N); for (int i=0; i<N; i++){ Yd[i]= Yin[i]; } //solve agains Y LAPACKE_dpotrs(LAPACK_ROW_MAJOR,'L',N,1,&Kxx[0],N,&Yd[0],1); //calc the llk R[0] = - 0.5*N*L2PI; //printf("\n1 %f\n",R[0]); for (int i=0; i<N; i++){ R[0]-=log(Kxx[i*(N+1)]); } //printf("2 %f\n",R[0]); R[0] -= 0.5*cblas_ddot(N,&Yin[0],1,&Yd[0],1); //printf("3 %f\n",R[0]); return; }
int CholeskyFactorization::solve(Matrix& rhs, Matrix& solution) { if (m_matrix_type == Matrix::MATRIX_SPARSE) { cholmod_dense *x; /* cast the RHS as a cholmod_dense b = rhs */ if (rhs.m_type == Matrix::MATRIX_DENSE) { cholmod_dense *b; b = cholmod_allocate_dense(rhs.m_nrows, rhs.m_ncols, rhs.m_nrows, CHOLMOD_REAL, Matrix::cholmod_handle()); b->x = rhs.m_data; /* Solve - rhs is dense*/ x = cholmod_solve(CHOLMOD_A, m_factor, b, Matrix::cholmod_handle()); solution = Matrix(rhs.m_nrows, rhs.m_ncols); solution.m_delete_data = false; memcpy(solution.m_data, static_cast<double*> (x->x), rhs.m_nrows * rhs.m_ncols * sizeof (double)); cholmod_free_dense(&x, Matrix::cholmod_handle()); } else if (rhs.m_type == Matrix::MATRIX_SPARSE) { // still untested! cholmod_sparse * rhs_sparse; if (rhs.m_sparse == NULL) { const_cast<Matrix&> (rhs)._createSparse(); } rhs_sparse = rhs.m_sparse; cholmod_sparse * result = cholmod_spsolve(CHOLMOD_LDLt, m_factor, rhs_sparse, Matrix::cholmod_handle()); solution = Matrix(rhs.m_nrows, rhs.m_ncols, Matrix::MATRIX_SPARSE); solution.m_sparse = result; solution._createTriplet(); } else { throw std::logic_error("Not supported"); } return ForBESUtils::STATUS_OK; } else { /* the matrix to be factorized is not sparse */ int info = ForBESUtils::STATUS_UNDEFINED_FUNCTION; solution = Matrix(rhs); if (m_matrix_type == Matrix::MATRIX_DENSE) { info = LAPACKE_dpotrs(LAPACK_COL_MAJOR, 'L', m_matrix_nrows, rhs.m_ncols, m_L, m_matrix_nrows, solution.m_data, m_matrix_nrows); } else if (m_matrix_type == Matrix::MATRIX_SYMMETRIC) { info = LAPACKE_dpptrs(LAPACK_COL_MAJOR, 'L', m_matrix_nrows, rhs.m_ncols, m_L, solution.m_data, m_matrix_nrows); } else { throw std::invalid_argument("This matrix type is not supported - only DENSE, SPARSE and SYMMETRIC are supported"); } return info; } }
DLLEXPORT lapack_int d_cholesky_solve_factored(lapack_int n, lapack_int nrhs, double a[], double b[]) { return LAPACKE_dpotrs(LAPACK_COL_MAJOR, 'L', n, nrhs, a, n, b, n); }
int GP::presolv(){ int c = this->build_K(); c = this->fac(); if (c!=0){printf("failed to cho fac Kxx %d",c);} return LAPACKE_dpotrs(LAPACK_ROW_MAJOR,'L',N,1,&Kxx[0],N,&Y[0],1); }