void SpectralClustering::MakeSimilarityMatrix(Vector<float*> &points, UINT dimension, float sigma, bool zeroDiagonal, DenseMatrix<double> &result) { const UINT pointCount = points.Length(); result.Allocate(pointCount, pointCount); const double expScale = -1.0f / (2.0f * sigma * sigma); for(UINT outerPointIndex = 0; outerPointIndex < pointCount; outerPointIndex++) { float *outerPoint = points[outerPointIndex]; for(UINT innerPointIndex = 0; innerPointIndex < pointCount; innerPointIndex++) { float *innerPoint = points[innerPointIndex]; float sum = 0.0f; for(UINT dimensionIndex = 0; dimensionIndex < dimension; dimensionIndex++) { float diff = innerPoint[dimensionIndex] - outerPoint[dimensionIndex]; sum += diff * diff; } result(outerPointIndex, innerPointIndex) = exp(sum * expScale); } } if(zeroDiagonal) { for(UINT pointIndex = 0; pointIndex < pointCount; pointIndex++) { result(pointIndex, pointIndex) = 0.0; } } }
void SolveLDLt (const DenseMatrix & l, const Vector & d, const Vector & g, Vector & p) { double val; int n = l.Height(); p = g; for (int i = 0; i < n; i++) { val = 0; for (int j = 0; j < i; j++) val += p(j) * l(i,j); p(i) -= val; } for (int i = 0; i < n; i++) p(i) /= d(i); for (int i = n-1; i >= 0; i--) { val = 0; for (int j = i+1; j < n; j++) val += p(j) * l(j, i); p(i) -= val; } }
void EigenSparseMatrix<T>::add_matrix(const DenseMatrix<T> & dm, const std::vector<numeric_index_type> & rows, const std::vector<numeric_index_type> & cols) { libmesh_assert (this->initialized()); unsigned int n_rows = cast_int<unsigned int>(rows.size()); unsigned int n_cols = cast_int<unsigned int>(cols.size()); libmesh_assert_equal_to (dm.m(), n_rows); libmesh_assert_equal_to (dm.n(), n_cols); for (unsigned int i=0; i<n_rows; i++) for (unsigned int j=0; j<n_cols; j++) this->add(rows[i],cols[j],dm(i,j)); }
/* * Calculates the matrix that transforms a B-spline basis to a power basis * Input: p (degree of basis) * Output: M (power matrix) */ DenseMatrix getBSplineToPowerBasisMatrix1D(unsigned int p) { assert(p > 0); // m = n+p+1 // M is a lower triangular matrix of size (p+1)x(p+1) DenseMatrix M; M.setZero(p+1,p+1); for (unsigned int j = 0; j <= p; j++) // cols { for (unsigned int i = j; i <= p; i++) // rows { M(i,j) = pow(-1,i-j)*binomialCoeff(p,j)*binomialCoeff(p-j,i-j); } } return M; }
// ------------------------------------------------------------------------ // // Init function - call prior to start of iterations // // ------------------------------------------------------------------------ void Init(const Matrix<T>& A, DenseMatrix<T>& W, DenseMatrix<T>& H) { WtW.Resize(W.Width(), W.Width()); WtA.Resize(W.Width(), A.Width()); HHt.Resize(H.Height(), H.Height()); AHt.Resize(A.Height(), H.Height()); ScaleFactors.Resize(H.Height(), 1); // compute the kxk matrix WtW = W' * W Gemm(TRANSPOSE, NORMAL, T(1), W, W, T(0), WtW); // compute the kxn matrix WtA = W' * A Gemm(TRANSPOSE, NORMAL, T(1), W, A, T(0), WtA); }
void transpose(const DenseMatrix &A, DenseMatrix &Atranspose) { Atranspose.resize(A.n, A.m); for(int j=0; j<A.n; ++j) for(int i=0; i<A.m; ++i){ Atranspose(j,i)=A(i,j); } }
int TransposeDenseTestObj() { DenseMatrix b{3}; b(0) = 6; b(1) = -4; b(2) = 27; b.transpose(); DenseMatrix c = transposed(b); DenseMatrix d = transposed(move(c)); assertEqual(b.getData(), d.getData(), 3) return 1; }
int ResidJacEval::evalJacobian(const doublereal t, const doublereal delta_t, doublereal cj, const doublereal* const y, const doublereal* const ydot, DenseMatrix& J, doublereal* const resid) { doublereal* const* jac_colPts = J.colPts(); return evalJacobianDP(t, delta_t, cj, y, ydot, jac_colPts, resid); }
double ElasticEnergyCoefficient::Eval(ElementTransformation &T, const IntegrationPoint &ip) { model.SetTransformation(T); x.GetVectorGradient(T, J); // return model.EvalW(J); // in reference configuration return model.EvalW(J)/J.Det(); // in deformed configuration }
/// Construct a time-independent square matrix coefficient from a C-function MatrixFunctionCoefficient(int dim, void (*F)(const Vector &, DenseMatrix &), Coefficient *q = NULL) : MatrixCoefficient(dim), Q(q) { Function = F; TDFunction = NULL; mat.SetSize(0); }
//==================================================================================================================== int invert(DenseMatrix& A, size_t nn) { integer n = static_cast<int>(nn != npos ? nn : A.nRows()); int info=0; ct_dgetrf(n, n, A.ptrColumn(0), static_cast<int>(A.nRows()), &A.ipiv()[0], info); if (info != 0) { if (A.m_printLevel) { writelogf("invert(DenseMatrix& A, int nn): DGETRS returned INFO = %d\n", info); } if (! A.m_useReturnErrorCode) { throw CELapackError("invert(DenseMatrix& A, int nn)", "DGETRS returned INFO = "+int2str(info)); } return info; } vector_fp work(n); integer lwork = static_cast<int>(work.size()); ct_dgetri(n, A.ptrColumn(0), static_cast<int>(A.nRows()), &A.ipiv()[0], &work[0], lwork, info); if (info != 0) { if (A.m_printLevel) { writelogf("invert(DenseMatrix& A, int nn): DGETRS returned INFO = %d\n", info); } if (! A.m_useReturnErrorCode) { throw CELapackError("invert(DenseMatrix& A, int nn)", "DGETRI returned INFO="+int2str(info)); } } return info; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { using namespace FortranLinalg; char * filename = mxArrayToString(prhs[0]); DenseMatrix<Precision> in = LinalgIO<Precision>::readMatrix(filename); mxArray *lhs[1]; lhs[0] = mxCreateNumericMatrix(in.M(), in.N(), CLASS, mxREAL); Precision *out = (Precision*) mxGetPr(lhs[0]); memcpy(out, in.data(), in.M()*in.N()*sizeof(Precision)); plhs[0] = lhs[0]; in.deallocate(); mxFree(filename); return; }
void MinFunction :: ApproximateHesse (const Vector & x, DenseMatrix & hesse) const { int n = x.Size(); int i, j; static Vector hx; hx.SetSize(n); double eps = 1e-6; double f, f11, f12, f21, f22; for (i = 1; i <= n; i++) { for (j = 1; j < i; j++) { hx = x; hx.Elem(i) = x.Get(i) + eps; hx.Elem(j) = x.Get(j) + eps; f11 = Func(hx); hx.Elem(i) = x.Get(i) + eps; hx.Elem(j) = x.Get(j) - eps; f12 = Func(hx); hx.Elem(i) = x.Get(i) - eps; hx.Elem(j) = x.Get(j) + eps; f21 = Func(hx); hx.Elem(i) = x.Get(i) - eps; hx.Elem(j) = x.Get(j) - eps; f22 = Func(hx); hesse.Elem(i, j) = hesse.Elem(j, i) = (f11 + f22 - f12 - f21) / (2 * eps * eps); } hx = x; f = Func(x); hx.Elem(i) = x.Get(i) + eps; f11 = Func(hx); hx.Elem(i) = x.Get(i) - eps; f22 = Func(hx); hesse.Elem(i, i) = (f11 + f22 - 2 * f) / (eps * eps); } // (*testout) << "hesse = " << hesse << endl; }
int main(int argc, char *argv[]) { // tests(); // srand(time(10)); srand(10); if(argc < 4) { std::cout << "Input arguments:\n\t1: Filename for A matrix (as in A ~= WH)\n\t2: New desired dimension\n\t3: Max NMF iterations\n\t4: Max NNLS iterations\n\t5 (optional): delimiter (space is default)\n"; } else { std::string filename = argv[1]; int newDimension = atoi(argv[2]); int max_iter_nmf = atoi(argv[3]); int max_iter_nnls = atoi(argv[4]); char delimiter = (argc > 5) ? *argv[5] : ' '; DenseMatrix* A = readMatrix(filename,delimiter); A->copyColumnToRow(); printf("Sparsity of A: %f\n",sparsity(A)); DenseMatrix W = DenseMatrix(A->rows,newDimension); DenseMatrix H = DenseMatrix(newDimension,A->cols); NMF_Input input = NMF_Input(&W,&H,A,max_iter_nmf,max_iter_nnls); std::cout << "Starting NMF computation." << std::endl; std::clock_t start = std::clock(); double duration; // nmf_cpu(input); nmf_cpu_profile(input); duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cout << "NMF computation complete. Time: " << duration << " s." << std::endl; W.copyColumnToRow(); dtype AF = FrobeniusNorm(A); dtype WH_AF = Fnorm(W,H,*A); printf("Objective value: %f\n",WH_AF/AF); // DenseMatrix z1 = DenseMatrix(A->rows,newDimension); // DenseMatrix z2 = DenseMatrix(newDimension,A->cols); // printf("Calculated solution approximate Frobenius norm: %f\n",Fnorm(z1,z2,*A)); // printcolmajor(H.colmajor,H.rows,H.cols); if(A) delete A; } return 0; }
void DenseMatrix<T>::right_multiply_transpose (const DenseMatrix<T>& B) { if (this->use_blas_lapack) this->_multiply_blas(B, RIGHT_MULTIPLY_TRANSPOSE); else { //Check to see if we are doing B*(B^T) if (this == &B) { //libmesh_here(); DenseMatrix<T> A(*this); // Simple but inefficient way // return this->right_multiply_transpose(A); // More efficient, more code // If B is mxn, the result will be a square matrix of Size m x m. const unsigned int n_rows = B.m(); const unsigned int n_cols = B.n(); // resize() *this and also zero out all entries. this->resize(n_rows,n_rows); // Compute the lower-triangular part for (unsigned int i=0; i<n_rows; ++i) for (unsigned int j=0; j<=i; ++j) for (unsigned int k=0; k<n_cols; ++k) // inner products are over n_cols (*this)(i,j) += A(i,k)*A(j,k); // Copy lower-triangular part into upper-triangular part for (unsigned int i=0; i<n_rows; ++i) for (unsigned int j=i+1; j<n_rows; ++j) (*this)(i,j) = (*this)(j,i); } else { DenseMatrix<T> A(*this); this->resize (A.m(), B.m()); libmesh_assert_equal_to (A.n(), B.n()); libmesh_assert_equal_to (this->m(), A.m()); libmesh_assert_equal_to (this->n(), B.m()); const unsigned int m_s = A.m(); const unsigned int p_s = A.n(); const unsigned int n_s = this->n(); // Do it this way because there is a // decent chance (at least for constraint matrices) // that B.transpose(k,j) = 0. for (unsigned int j=0; j<n_s; j++) for (unsigned int k=0; k<p_s; k++) if (B.transpose(k,j) != 0.) for (unsigned int i=0; i<m_s; i++) (*this)(i,j) += A(i,k)*B.transpose(k,j); } } }
int CalcTriangleCenter (const Point3d ** pts, Point3d & c) { static DenseMatrix a(2), inva(2); static Vector rs(2), sol(2); double h = Dist(*pts[0], *pts[1]); Vec3d v1(*pts[0], *pts[1]); Vec3d v2(*pts[0], *pts[2]); rs.Elem(1) = v1 * v1; rs.Elem(2) = v2 * v2; a.Elem(1,1) = 2 * rs.Get(1); a.Elem(1,2) = a.Elem(2,1) = 2 * (v1 * v2); a.Elem(2,2) = 2 * rs.Get(2); if (fabs (a.Det()) <= 1e-12 * h * h) { (*testout) << "CalcTriangleCenter: degenerated" << endl; return 1; } CalcInverse (a, inva); inva.Mult (rs, sol); c = *pts[0]; v1 *= sol.Get(1); v2 *= sol.Get(2); c += v1; c += v2; return 0; }
bool HasNaNs(const DenseMatrix<T>& M) { // returns true if any matrix element is NaN int height = M.Height(); int width = M.Width(); for (int c=0; c<width; ++c) { for (int r=0; r<height; ++r) { T elt = M.Get(r, c); if (std::isnan(elt)) return true; } } return false; }
//! compute transpose of matrix void DenseMatrix::Transpose(DenseMatrix & C) const { C.Resize(m_nCols,m_nRows); for ( unsigned int r = 0; r < m_nRows; ++r ) { for ( unsigned int c = 0; c < m_nCols; ++c ) { M_C(c,r) = M(r,c); } } }
bool SolveDeficit(DenseMatrix< VariableArray2<double> > &A, DenseVector<VariableArray1<double> > &x, DenseVector<VariableArray1<double> > &rhs, double deficitTolerance) { DenseMatrix< VariableArray2<double> > A2=A; DenseVector<VariableArray1<double> > rhs2=rhs; UG_ASSERT(A.num_rows() == rhs.size(), ""); UG_ASSERT(A.num_cols() == x.size(), ""); size_t iNonNullRows; x.resize(A.num_cols()); for(size_t i=0; i<x.size(); i++) x[i] = 0.0; std::vector<size_t> interchange; if(Decomp(A, rhs, iNonNullRows, interchange, deficitTolerance) == false) return false; // A.maple_print("Adecomp"); // rhs.maple_print("rhs decomp"); for(int i=iNonNullRows-1; i>=0; i--) { double d=A(i,i); double s=0; for(size_t k=i+1; k<A.num_cols(); k++) s += A(i,k)*x[interchange[k]]; x[interchange[i]] = (rhs[i] - s)/d; } DenseVector<VariableArray1<double> > f; f = A2*x - rhs2; if(VecNormSquared(f) > 1e-2) { UG_LOGN("iNonNullRows = " << iNonNullRows); UG_LOG("solving was wrong:"); UG_LOGN(CPPString(A2, "Aold")); rhs2.maple_print("rhs"); UG_LOGN(CPPString(A, "Adecomp")); rhs.maple_print("rhsDecomp"); x.maple_print("x"); f.maple_print("f"); } return true; }
void compute(const MatrixType& A, const Index rank) { if(A.cols() == 0 || A.rows() == 0) return; Index r = (rank < A.cols()) ? rank : A.cols(); r = (r < A.rows()) ? r : A.rows(); // Gaussian Random Matrix for A^T DenseMatrix O(A.rows(), r); sample_gaussian(O); // Compute Sample Matrix of A^T DenseMatrix Y = A.transpose() * O; // Orthonormalize Y gram_schmidt(Y); // Range(B) = Range(A^T) DenseMatrix B = A * Y; // Gaussian Random Matrix DenseMatrix P(B.cols(), r); sample_gaussian(P); // Compute Sample Matrix of B DenseMatrix Z = B * P; // Orthonormalize Z gram_schmidt(Z); // Range(C) = Range(B) DenseMatrix C = Z.transpose() * B; Eigen::JacobiSVD<DenseMatrix> svdOfC(C, Eigen::ComputeThinU | Eigen::ComputeThinV); // C = USV^T // A = Z * U * S * V^T * Y^T() m_matrixU = Z * svdOfC.matrixU(); m_vectorS = svdOfC.singularValues(); m_matrixV = Y * svdOfC.matrixV(); }
double DenseMatrix::InnerProduct(const DenseMatrix& mat) const { double res = 0; const std::vector<double>& dataMat = mat.GetValues(); for (int dataId = 0; dataId < mValues.size(); dataId++) { res += mValues.at(dataId) * dataMat.at(dataId); } return res; }
// helper to set submatrix of A repeated vdim times static void SetVDofSubMatrixTranspose(SparseMatrix& A, Array<int>& rows, Array<int>& cols, const DenseMatrix& subm, int vdim) { if (vdim == 1) { A.SetSubMatrixTranspose(rows, cols, subm, 1); } else { int nr = subm.Width(), nc = subm.Height(); for (int d = 0; d < vdim; d++) { Array<int> rows_sub(rows.GetData() + d*nr, nr); // (not owner) Array<int> cols_sub(cols.GetData() + d*nc, nc); // (not owner) A.SetSubMatrixTranspose(rows_sub, cols_sub, subm, 1); } } }
DenseMatrix DenseMatrix::operator * (const DenseMatrix& mat) const { int rightColNum = mat.GetColNum(); DenseMatrix matRes(mRowNum, rightColNum, 0); for (int rid = 0; rid < mRowNum; rid++) { for (int cid = 0; cid < rightColNum; cid++) { double v = 0.0; int rBaseIndex = rid * mColNum; for (int did = 0; did < mColNum; did++) { v += mValues.at(rBaseIndex + did) * mat.at(did, cid); } matRes.at(rid, cid) = v; } } return matRes; }
EmbeddingResult project(const ProjectionResult& projection_result, RandomAccessIterator begin, RandomAccessIterator end, FeatureVectorCallback callback, unsigned int dimension) { timed_context context("Data projection"); DenseVector current_vector(dimension); const DenseSymmetricMatrix& projection_matrix = projection_result.first; DenseMatrix embedding = DenseMatrix::Zero((end-begin),projection_matrix.cols()); for (RandomAccessIterator iter=begin; iter!=end; ++iter) { callback(*iter,current_vector); embedding.row(iter-begin) = projection_matrix.transpose()*current_vector; } return EmbeddingResult(embedding,DenseVector()); }
void Assembly::addJacobianBlock(SparseMatrix<Number> & jacobian, DenseMatrix<Number> & jac_block, const std::vector<dof_id_type> & idof_indices, const std::vector<dof_id_type> & jdof_indices, Real scaling_factor) { if ((idof_indices.size() > 0) && (jdof_indices.size() > 0) && jac_block.n() && jac_block.m()) { std::vector<dof_id_type> di(idof_indices); std::vector<dof_id_type> dj(jdof_indices); _dof_map.constrain_element_matrix(jac_block, di, dj, false); if (scaling_factor != 1.0) { _tmp_Ke = jac_block; _tmp_Ke *= scaling_factor; jacobian.add_matrix(_tmp_Ke, di, dj); } else jacobian.add_matrix(jac_block, di, dj); } }
ConstraintPtr ConstraintBSpline::computeRelaxationConvexHull() { /* * Convex combination model: * * X = [x' y' auxiliary variables]' * * A = [ I -C ] b = [ 0 ] * [ 0 1 ] [ 1 ] * * AX = b, [lb' 0]' <= X <= [ub' inf]' */ // Consider renaming here (control points matrix was transposed in old implementation) int rowsC = bspline.getNumVariables() + 1; int colsC = bspline.getNumControlPoints(); int rowsA = rowsC + 1; int colsA = rowsC + colsC; DenseMatrix I; I.setIdentity(rowsC,rowsC); DenseMatrix zeros; zeros.setZero(1,rowsC); DenseMatrix ones; ones.setOnes(1,colsC); DenseMatrix A(rowsA, colsA); A.block(0, 0, rowsC, rowsC) = I; A.block(0, rowsC, rowsC, colsC) = -bspline.getControlPoints().transpose(); A.block(rowsC, 0, 1, rowsC) = zeros; A.block(rowsC, rowsC, 1, colsC) = ones; zeros.setZero(rowsC,1); ones.setOnes(1,1); DenseVector b(rowsA); b.block(0, 0, rowsC, 1) = zeros; b.block(rowsC, 0, 1, 1) = ones; auto auxVariables = variables; // Number of auxiliary variables equals number of control points for (int i = 0; i < bspline.getNumControlPoints(); i++) auxVariables.push_back(std::make_shared<Variable>(0, 0, 1)); ConstraintPtr relaxedConstraint = std::make_shared<ConstraintLinear>(auxVariables, A ,b, true); relaxedConstraint->setName("B-spline convex hull relaxation"); return relaxedConstraint; }
void sdiff(const DenseMatrix &A, const RCP<const Basic> &x, DenseMatrix &result) { SYMENGINE_ASSERT(A.row_ == result.nrows() and A.col_ == result.ncols()); #pragma omp parallel for for (unsigned i = 0; i < result.row_; i++) { for (unsigned j = 0; j < result.col_; j++) { if (is_a<Symbol>(*x)) { const RCP<const Symbol> x_ = rcp_static_cast<const Symbol>(x); result.m_[i * result.col_ + j] = A.m_[i * result.col_ + j]->diff(x_); } else { // TODO: Use a dummy symbol const RCP<const Symbol> x_ = symbol("_x"); result.m_[i * result.col_ + j] = ssubs( ssubs(A.m_[i * result.col_ + j], {{x, x_}})->diff(x_), {{x_, x}}); } } } }
void DenseMatrix<T>::get_principal_submatrix (unsigned int sub_m, unsigned int sub_n, DenseMatrix<T>& dest) const { libmesh_assert( (sub_m <= this->m()) && (sub_n <= this->n()) ); dest.resize(sub_m, sub_n); for(unsigned int i=0; i<sub_m; i++) for(unsigned int j=0; j<sub_n; j++) dest(i,j) = (*this)(i,j); }
DenseMatrix project(const DenseMatrix& projection_matrix, const DenseVector& mean_vector, RandomAccessIterator begin, RandomAccessIterator end, FeatureVectorCallback callback, IndexType dimension) { timed_context context("Data projection"); DenseVector current_vector(dimension); DenseVector current_vector_subtracted_mean(dimension); DenseMatrix embedding = DenseMatrix::Zero((end-begin),projection_matrix.cols()); for (RandomAccessIterator iter=begin; iter!=end; ++iter) { callback(*iter,current_vector); current_vector_subtracted_mean = current_vector - mean_vector; embedding.row(iter-begin) = projection_matrix.transpose()*current_vector_subtracted_mean; } return embedding; }
int LDLtUpdate (DenseMatrix & l, Vector & d, double a, const Vector & u) { // Bemerkung: Es wird a aus R erlaubt // Rueckgabewert: 0 .. D bleibt positiv definit // 1 .. sonst int i, j, n; n = l.Height(); Vector v(n); double t, told, xi; told = 1; v = u; for (j = 1; j <= n; j++) { t = told + a * sqr (v.Elem(j)) / d.Get(j); if (t <= 0) { (*testout) << "update err, t = " << t << endl; return 1; } xi = a * v.Elem(j) / (d.Get(j) * t); d.Elem(j) *= t / told; for (i = j + 1; i <= n; i++) { v.Elem(i) -= v.Elem(j) * l.Elem(i, j); l.Elem(i, j) += xi * v.Elem(i); } told = t; } return 0; }