void row_normalize_system(boost::numeric::ublas::compressed_matrix<NumericT> & A, boost::numeric::ublas::vector<NumericT> & b) { typedef typename boost::numeric::ublas::compressed_matrix<NumericT>::iterator1 Iterator1; typedef typename boost::numeric::ublas::compressed_matrix<NumericT>::iterator2 Iterator2; //preprocessing: scale rows such that ||a_i|| = 1 for (Iterator1 iter1 = A.begin1(); iter1 != A.end1(); ++iter1) { double row_norm = 0.0; for (Iterator2 iter2 = iter1.begin(); iter2 != iter1.end(); ++iter2) { row_norm += *iter2 * *iter2; } row_norm = sqrt(row_norm); if (A(iter1.index1(), iter1.index1()) < 0.0) row_norm *= -1.0; for (Iterator2 iter2 = iter1.begin(); iter2 != iter1.end(); ++iter2) { *iter2 /= row_norm; } b(iter1.index1()) /= row_norm; } }
matrix_map<T, ptrdiff_t> map(const boost::numeric::ublas::compressed_matrix<T, boost::numeric::ublas::row_major> &A) { return matrix_map<T, ptrdiff_t>( A.size1(), A.size2(), // amgcl expects signed type for row and col data. This should work // for decently sized matrices: reinterpret_cast<const ptrdiff_t*>(A.index1_data().begin()), reinterpret_cast<const ptrdiff_t*>(A.index2_data().begin()), A.value_data().begin() ); }
bool readMatrixFromFile(const std::string & filename, boost::numeric::ublas::compressed_matrix<TYPE> & matrix) { std::cout << "Reading ublas matrix" << std::endl; std::ifstream file(filename.c_str()); if (!file) return false; std::string id; file >> id; if (id != "Matrix") return false; unsigned int num_rows, num_columns; file >> num_rows >> num_columns; if (num_rows != num_columns) return false; matrix.resize(num_rows, num_rows, false); for (unsigned int row = 0; row < num_rows; ++row) { int num_entries; file >> num_entries; for (int j = 0; j < num_entries; ++j) { unsigned int column; TYPE element; file >> column >> element; //matrix.insert_element(row, column, element); matrix(row, column) = element; } } return true; }
void axpy( const boost::numeric::ublas::compressed_matrix<real, boost::numeric::ublas::row_major> &A, const boost::numeric::ublas::vector<real> &x, boost::numeric::ublas::vector<real> &y ) { const ptrdiff_t n = A.size1(); const size_t *Arow = A.index1_data().begin(); const size_t *Acol = A.index2_data().begin(); const real *Aval = A.value_data().begin(); #pragma omp parallel for schedule(dynamic, 1024) for(ptrdiff_t i = 0; i < n; ++i) { real buf = 0; for(size_t j = Arow[i], e = Arow[i + 1]; j < e; ++j) buf += Aval[j] * x[Acol[j]]; y[i] = buf; } }
std::tuple< size_t, boost::iterator_range<const size_t*>, boost::iterator_range<const size_t*>, boost::iterator_range<const T*> > map(const boost::numeric::ublas::compressed_matrix<T, boost::numeric::ublas::row_major> &A) { return std::make_tuple( A.size1(), boost::make_iterator_range( A.index1_data().begin(), A.index1_data().end() ), boost::make_iterator_range( A.index2_data().begin(), A.index2_data().end() ), boost::make_iterator_range( A.value_data().begin(), A.value_data().end() ) ); }
void resize(boost::numeric::ublas::compressed_matrix<ScalarType> & matrix, size_t rows, size_t cols) { matrix.resize(rows, cols, false); //Note: omitting third parameter leads to compile time error (not implemented in ublas <= 1.42) }