void DOFVectorBase<double>::assemble(double factor, ElInfo* elInfo, const BoundaryType* bound, Operator* op) { if (!(op || operators.size())) return; set_to_zero(elementVector); bool addVector = false; if (op) { op->getElementVector(elInfo, elementVector); addVector = true; } else { std::vector<double*>::iterator factorIt = operatorFactor.begin(); for (Operator* op : operators) { if (!op->getNeedDualTraverse()) { op->getElementVector(elInfo, elementVector, (*factorIt ? **factorIt : 1.0) ); addVector = true; } ++factorIt; } } if (addVector) addElementVector(factor, this->elementVector, bound, elInfo); }
Requires_t<mtl::traits::is_distributed<VectorT>> initVector(VectorT& x) const { #ifdef HAVE_PARALLEL_MTL4 x.init_distribution(col_distribution(*fullMatrix), num_cols(*fullMatrix)); #endif set_to_zero(x); }
void SolverMatrix<Matrix<DOFMatrix*>>::buildMatrix() const { BlockMapper mapper(*this); matrix.change_dim(mapper.getNumRows(), mapper.getNumCols()); set_to_zero(matrix); MatMap<const SolverMatrix<Matrix<DOFMatrix*>>, BlockMapper> matMap{*this, mapper}; matrix << matMap; }
int main(void) { int i=0; int a[30]; set_to_zero(a, sizeof(a)/4); for(i=0; i<sizeof(a)/4; i++) printf("%d ",a[i]); return 0; }
inline void smat_smat_mult(const MatrixA& a, const MatrixB& b, MatrixC& c, Assign, tag::col_major, // orientation a tag::row_major) // orientation b { if (Assign::init_to_zero) set_to_zero(c); // Average numbers of non-zeros per row double ava= double(a.nnz()) / num_rows(a), avb= double(b.nnz()) / num_rows(b); // Define Updater type corresponding to assign mode typedef typename Collection<MatrixC>::value_type c_value_type; typedef typename operations::update_assign_mode<Assign, c_value_type>::type Updater; // Reserve 20% over the average's product for entries in c matrix::inserter<MatrixC, Updater> ins(c, int( ava * avb * 1.2 )); typename traits::row<MatrixA>::type row_a(a); typename traits::col<MatrixA>::type col_a(a); typename traits::const_value<MatrixA>::type value_a(a); typename traits::row<MatrixB>::type row_b(b); typename traits::col<MatrixB>::type col_b(b); typename traits::const_value<MatrixB>::type value_b(b); typedef typename traits::range_generator<tag::col, MatrixA>::type a_cursor_type; a_cursor_type a_cursor = begin<tag::col>(a), a_cend = end<tag::col>(a); typedef typename traits::range_generator<tag::row, MatrixB>::type b_cursor_type; b_cursor_type b_cursor = begin<tag::row>(b); for (unsigned ca= 0; a_cursor != a_cend; ++ca, ++a_cursor, ++b_cursor) { // Iterate over non-zeros of A's column typedef typename traits::range_generator<tag::nz, a_cursor_type>::type ia_cursor_type; for (ia_cursor_type ia_cursor = begin<tag::nz>(a_cursor), ia_cend = end<tag::nz>(a_cursor); ia_cursor != ia_cend; ++ia_cursor) { typename Collection<MatrixA>::size_type ra= row_a(*ia_cursor); // row of non-zero typename Collection<MatrixA>::value_type va= value_a(*ia_cursor); // value of non-zero // Iterate over non-zeros of B's row typedef typename traits::range_generator<tag::nz, b_cursor_type>::type ib_cursor_type; for (ib_cursor_type ib_cursor = begin<tag::nz>(b_cursor), ib_cend = end<tag::nz>(b_cursor); ib_cursor != ib_cend; ++ib_cursor) { typename Collection<MatrixB>::size_type cb= col_b(*ib_cursor); // column of non-zero typename Collection<MatrixB>::value_type vb= value_b(*ib_cursor); // value of non-zero ins(ra, cb) << va * vb; } } } }
inline void smat_smat_mult(const MatrixA& A, const MatrixB& B, MatrixC& C, Assign, tag::col_major, // orientation A tag::row_major) // orientation B { if (Assign::init_to_zero) set_to_zero(C); // Average numbers of non-zeros per row double ava= double(A.nnz()) / num_rows(A), avb= double(B.nnz()) / num_rows(B); // Define Updater type corresponding to assign mode typedef typename Collection<MatrixC>::value_type C_value_type; typedef typename operations::update_assign_mode<Assign, C_value_type>::type Updater; // Reserve 20% over the average's product for entries in C matrix::inserter<MatrixC, Updater> ins(C, int( ava * avb * 1.2 )); typename traits::row<MatrixA>::type row_A(A); typename traits::col<MatrixA>::type col_A(A); typename traits::const_value<MatrixA>::type value_A(A); typename traits::row<MatrixB>::type row_B(B); typename traits::col<MatrixB>::type col_B(B); typename traits::const_value<MatrixB>::type value_B(B); typedef typename traits::range_generator<tag::col, MatrixA>::type A_cursor_type; A_cursor_type A_cursor = begin<tag::col>(A), A_cend = end<tag::col>(A); typedef typename traits::range_generator<tag::row, MatrixB>::type B_cursor_type; B_cursor_type B_cursor = begin<tag::row>(B); for (unsigned ca= 0; A_cursor != A_cend; ++ca, ++A_cursor, ++B_cursor) { // Iterate over non-zeros of A's column typedef typename traits::range_generator<tag::nz, A_cursor_type>::type ia_cursor_type; for (ia_cursor_type ia_cursor = begin<tag::nz>(A_cursor), ia_cend = end<tag::nz>(A_cursor); ia_cursor != ia_cend; ++ia_cursor) { typename Collection<MatrixA>::size_type ra= row_A(*ia_cursor); // row of non-zero typename Collection<MatrixA>::value_type va= value_A(*ia_cursor); // value of non-zero // Iterate over non-zeros of B's row typedef typename traits::range_generator<tag::nz, B_cursor_type>::type ib_cursor_type; for (ib_cursor_type ib_cursor = begin<tag::nz>(B_cursor), ib_cend = end<tag::nz>(B_cursor); ib_cursor != ib_cend; ++ib_cursor) { typename Collection<MatrixB>::size_type cb= col_B(*ib_cursor); // column of non-zero typename Collection<MatrixB>::value_type vb= value_B(*ib_cursor); // value of non-zero ins(ra, cb) << va * vb; } } } }
float MACD(float signif, int factor, int sp, int lp, int empp, float cu_price, vector <struct historicalprices> prev_price, vector <struct price_entry> prices){ double dlp, dsp, ds; if (factor ==1){ unsigned int num_consid = period_consider(lp,signif); float formatted_price[num_consid-1]; set_to_zero(formatted_price); format_price_days(prev_price, formatted_price); dlp = EMA(lp, signif, cu_price, formatted_price); dsp = EMA(sp, signif, cu_price,formatted_price); dsp -=dlp; } }
inline void smat_smat_mult(const MatrixA& A, const MatrixB& B, MatrixC& C, Assign, tag::row_major, // orientation A tag::row_major) // orientation B { if (Assign::init_to_zero) set_to_zero(C); // Average numbers of non-zeros per row double ava= num_cols(A) ? double(A.nnz()) / num_cols(A) : 0, avb= num_rows(B) ? double(B.nnz()) / num_rows(B) : 0; // Define Updater type corresponding to assign mode typedef typename Collection<MatrixC>::value_type C_value_type; typedef typename operations::update_assign_mode<Assign, C_value_type>::type Updater; // Reserve 20% over the average's product for entries in C matrix::inserter<MatrixC, Updater> ins(C, int( ava * avb * 1.4 )); typename traits::row<MatrixA>::type row_A(A); typename traits::col<MatrixA>::type col_A(A); typename traits::const_value<MatrixA>::type value_A(A); typename traits::col<MatrixB>::type col_B(B); typename traits::const_value<MatrixB>::type value_B(B); typedef typename traits::range_generator<tag::row, MatrixA>::type cursor_type; cursor_type cursor = begin<tag::row>(A), cend = end<tag::row>(A); for (unsigned ra= 0; cursor != cend; ++ra, ++cursor) { // Iterate over non-zeros of each row of A typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type; for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor); icursor != icend; ++icursor) { typename Collection<MatrixA>::size_type ca= col_A(*icursor); // column of non-zero typename Collection<MatrixA>::value_type va= value_A(*icursor); // value of non-zero // Get cursor corresponding to row 'ca' in matrix B typedef typename traits::range_generator<tag::row, MatrixB>::type B_cursor_type; B_cursor_type B_cursor = begin<tag::row>(B); B_cursor+= ca; // Iterate over non-zeros of this row typedef typename traits::range_generator<tag::nz, B_cursor_type>::type ib_cursor_type; for (ib_cursor_type ib_cursor = begin<tag::nz>(B_cursor), ib_cend = end<tag::nz>(B_cursor); ib_cursor != ib_cend; ++ib_cursor) { typename Collection<MatrixB>::size_type cb= col_B(*ib_cursor); // column of non-zero typename Collection<MatrixB>::value_type vb= value_B(*ib_cursor); // value of non-zero ins(ra, cb) << va * vb; } } } }
void Assembler::calculateElementVector(const ElInfo* elInfo, DenseVector<double>& userVec, double factor) { if (remember && factor != 1.0) rememberElVec = true; Element* el = elInfo->getElement(); if ((el != lastMatEl && el != lastVecEl) || !operat->isOptimized()) initElement(elInfo); if (el != lastVecEl || !operat->isOptimized()) { if (rememberElVec) set_to_zero(elementVector); lastVecEl = el; } else { // Only possible in single mesh case when one operator // is used more than twice at dof vector? if (rememberElVec) { userVec += factor * elementVector; return; } } DenseVector<double>& vec = rememberElVec ? elementVector : userVec; if (operat->getUhOld() && remember) { matVecAssemble(elInfo, vec); if (rememberElVec) userVec += factor * elementVector; return; } if (firstOrderAssemblerGrdPsi) firstOrderAssemblerGrdPsi->calculateElementVector(elInfo, vec); if (zeroOrderAssembler) zeroOrderAssembler->calculateElementVector(elInfo, vec); if (rememberElVec) userVec += factor * elementVector; }
inline void laplacian_setup(Matrix& matrix, unsigned m, unsigned n) { matrix.change_dim(m*n, m*n); set_to_zero(matrix); inserter<Matrix> ins(matrix); for (unsigned i= 0; i < m; i++) for (unsigned j= 0; j < n; j++) { typename Collection<Matrix>::value_type four(4.0), minus_one(-1.0); unsigned row= i * n + j; ins(row, row) << four; if (j < n-1) ins(row, row+1) << minus_one; if (i < m-1) ins(row, row+n) << minus_one; if (j > 0) ins(row, row-1) << minus_one; if (i > 0) ins(row, row-n) << minus_one; } }
void Assembler::matVecAssemble(const ElInfo* elInfo, DenseVector<double>& vec) { Element* el = elInfo->getElement(); DenseVector<double> uhOldLoc(operat->getUhOld()->getFeSpace() == rowFeSpace ? nRow : nCol); operat->getUhOld()->getLocalVector(el, uhOldLoc); if (el != lastMatEl) { set_to_zero(elementMatrix); calculateElementMatrix(elInfo, elementMatrix); } vec += elementMatrix * uhOldLoc; }
void Assembler::calculateElementMatrix(const ElInfo* elInfo, ElementMatrix& userMat, double factor) { if (remember && (factor != 1.0 || operat->getUhOld())) rememberElMat = true; Element* el = elInfo->getElement(); if (el != lastMatEl || !operat->isOptimized()) { initElement(elInfo); if (rememberElMat) set_to_zero(elementMatrix); lastMatEl = el; } else { // Only possible in single mesh case when one operator // is used more than twice? if (rememberElMat) { if (&userMat != &elementMatrix) userMat += factor * elementMatrix; return; } } ElementMatrix& mat = rememberElMat ? elementMatrix : userMat; if (secondOrderAssembler) secondOrderAssembler->calculateElementMatrix(elInfo, mat); if (firstOrderAssemblerGrdPsi) firstOrderAssemblerGrdPsi->calculateElementMatrix(elInfo, mat); if (firstOrderAssemblerGrdPhi) firstOrderAssemblerGrdPhi->calculateElementMatrix(elInfo, mat); if (zeroOrderAssembler) zeroOrderAssembler->calculateElementMatrix(elInfo, mat); if (rememberElMat && &userMat != &elementMatrix) userMat += factor * elementMatrix; }
t_flags *init_flags(void) { t_flags *current_flag; if (!(current_flag = (t_flags*)malloc(sizeof(t_flags)))) ft_malloc_error(); current_flag->type = 0; current_flag->nb_max_char = 0; current_flag->nb_c_written = 0; current_flag->nb_min_char = 0; current_flag->plus = 0; current_flag->minus = 0; current_flag->space = 0; current_flag->zero = 0; set_to_zero(current_flag); current_flag->star = 0; current_flag->hash = 0; current_flag->dot_star = 0; return (current_flag); }
void run_tutorial() { typedef mtl::dense2D<ScalarType> MTL4DenseMatrix; typedef mtl::compressed2D<ScalarType> MTL4SparseMatrix; /** * Create and fill dense matrices from the MTL4 library: **/ mtl::dense2D<ScalarType> mtl4_densemat(5, 5); mtl::dense2D<ScalarType> mtl4_densemat2(5, 5); mtl4_densemat(0,0) = 2.0; mtl4_densemat(0,1) = -1.0; mtl4_densemat(1,0) = -1.0; mtl4_densemat(1,1) = 2.0; mtl4_densemat(1,2) = -1.0; mtl4_densemat(2,1) = -1.0; mtl4_densemat(2,2) = -1.0; mtl4_densemat(2,3) = -1.0; mtl4_densemat(3,2) = -1.0; mtl4_densemat(3,3) = 2.0; mtl4_densemat(3,4) = -1.0; mtl4_densemat(4,4) = -1.0; mtl4_densemat(4,4) = -1.0; /** * Create and fill sparse matrices from the MTL4 library: **/ MTL4SparseMatrix mtl4_sparsemat; set_to_zero(mtl4_sparsemat); mtl4_sparsemat.change_dim(5, 5); MTL4SparseMatrix mtl4_sparsemat2; set_to_zero(mtl4_sparsemat2); mtl4_sparsemat2.change_dim(5, 5); { mtl::matrix::inserter< MTL4SparseMatrix > ins(mtl4_sparsemat); typedef typename mtl::Collection<MTL4SparseMatrix>::value_type ValueType; ins(0,0) << ValueType(2.0); ins(0,1) << ValueType(-1.0); ins(1,1) << ValueType(2.0); ins(1,2) << ValueType(-1.0); ins(2,2) << ValueType(-1.0); ins(2,3) << ValueType(-1.0); ins(3,3) << ValueType(2.0); ins(3,4) << ValueType(-1.0); ins(4,4) << ValueType(-1.0); } /** * Create and fill a few vectors from the MTL4 library: **/ mtl::dense_vector<ScalarType> mtl4_rhs(5, 0.0); mtl::dense_vector<ScalarType> mtl4_result(5, 0.0); mtl::dense_vector<ScalarType> mtl4_temp(5, 0.0); mtl4_rhs(0) = 10.0; mtl4_rhs(1) = 11.0; mtl4_rhs(2) = 12.0; mtl4_rhs(3) = 13.0; mtl4_rhs(4) = 14.0; /** * Create the corresponding ViennaCL objects: **/ viennacl::vector<ScalarType> vcl_rhs(5); viennacl::vector<ScalarType> vcl_result(5); viennacl::matrix<ScalarType> vcl_densemat(5, 5); viennacl::compressed_matrix<ScalarType> vcl_sparsemat(5, 5); /** * Directly copy the MTL4 objects to ViennaCL objects **/ viennacl::copy(&(mtl4_rhs[0]), &(mtl4_rhs[0]) + 5, vcl_rhs.begin()); //method 1: via iterator interface (cf. std::copy()) viennacl::copy(mtl4_rhs, vcl_rhs); //method 2: via built-in wrappers (convenience layer) viennacl::copy(mtl4_densemat, vcl_densemat); viennacl::copy(mtl4_sparsemat, vcl_sparsemat); // For completeness: Copy matrices from ViennaCL back to Eigen: viennacl::copy(vcl_densemat, mtl4_densemat2); viennacl::copy(vcl_sparsemat, mtl4_sparsemat2); /** * Run dense matrix-vector products and compare results: **/ mtl4_result = mtl4_densemat * mtl4_rhs; vcl_result = viennacl::linalg::prod(vcl_densemat, vcl_rhs); viennacl::copy(vcl_result, mtl4_temp); mtl4_result -= mtl4_temp; std::cout << "Difference for dense matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl; mtl4_result = mtl4_densemat2 * mtl4_rhs - mtl4_temp; std::cout << "Difference for dense matrix-vector product (MTL4->ViennaCL->MTL4): " << mtl::two_norm(mtl4_result) << std::endl; /** * Run sparse matrix-vector products and compare results: **/ mtl4_result = mtl4_sparsemat * mtl4_rhs; vcl_result = viennacl::linalg::prod(vcl_sparsemat, vcl_rhs); viennacl::copy(vcl_result, mtl4_temp); mtl4_result -= mtl4_temp; std::cout << "Difference for sparse matrix-vector product: " << mtl::two_norm(mtl4_result) << std::endl; mtl4_result = mtl4_sparsemat2 * mtl4_rhs - mtl4_temp; std::cout << "Difference for sparse matrix-vector product (MTL4->ViennaCL->MTL4): " << mtl::two_norm(mtl4_result) << std::endl; }
inline void zero_with_sparse_src(MatrixDest& dest, tag::sparse) { set_to_zero(dest); }
Requires_t<not_<mtl::traits::is_distributed<VectorT>>> initVector(VectorT& x) const { x.change_dim(num_cols(*fullMatrix)); set_to_zero(x); }