typename Cholesky<Matrix>::DMatrix Cholesky<Matrix>::doSolve( const AbstractMatrix& b ) { if ( this->rowNum() != b.rows() ) { std::cerr<<"The order of matrix is "<<this->rowNum()<<" and the dimension of vector is "<<b.rows()<<std::endl; throw COException("Cholesky solving error: the size if not consistent!"); } DMatrix result(b); copt_lapack_potrs('U',this->rowNum(),b.cols(),__a,this->lda(),result.dataPtr(),result.lda(),&__info); if ( __info != 0 ) std::cerr<<"Warning in Cholesky solver: solving is wrong!"<<std::endl; return result; }
void QRDecomposition<T>::operator()(const AbstractMatrix<T>& aMatrix) { q = GenericMatrix<T>(aMatrix.rows()); r = TriangularMatrix<T>(aMatrix.rows(), TRIANGLE_TYPE::UPPER); T normResult; TwoNorm<T> norm; /*for(int i = 0; i < aMatrix.rows(); i++) { for(int j = 0; j < aMatrix.rows(); j++) { q[i][j] = 0; r[i][j] = 0; //q(i, j) = 0; //r(i, j) = 0; } }*/ int rows = static_cast<int>(aMatrix.rows()); for(int i = 0; i < rows; i++) { if(i == 0) { normResult = norm(aMatrix.getColumn(0)); if(normResult == 0) { throw std::domain_error("Error in QRDecomposition: Attempted division by zero"); } for(int j = 0; j < rows; j++) { q[j][0] = aMatrix[j][0] * (1 / normResult); } } else { AlgebraVector<T> temp(aMatrix[0]); temp = r[0][i] * q.getColumn(0); //temp = r(0, i) * q.getColumn(0); for(int k = 1; k < i; k++) { temp += r[k][i] * q.getColumn(k); //temp += r(k, i) * q.getColumn(k); } temp = aMatrix.getColumn(i) - temp; r[i][i] = norm(temp); //r(i, i) = norm(temp); //if(r(i, i) == 0) if(r[i][i] == 0) { throw std::domain_error("QRDecomposition: Attempted division by zero"); } for(int j = 0; j < rows; j++) { q[j][i] = (1 / r[i][i]) * temp[j]; //q(j, i) = (1 / r(i, i)) * temp[j]; } } for(int j = 0; j < rows; j++) { r[i][j] = aMatrix.getColumn(j) * q.getColumn(i); //r(i, j) = aMatrix.getColumn(j) * q.getColumn(i); } } }
void operator()(AbstractMatrix<M>& m) const { for (int i=0; i<m.rows(); ++i) for (int j=0; j<m.cols(); ++j) m(i,j) = rand() * a - b; }
void operator()(AbstractMatrix<M>& m) const { assert_square(m); fill(m, 0); for (int i=0; i<m.rows(); ++i) m(i,i) = 1; }