bool vec_basic_eq(const vec_basic &a, const vec_basic &b) { // Can't be equal if # of entries differ: if (a.size() != b.size()) return false; // Loop over elements in "a" and "b": for (size_t i = 0; i < a.size(); i++) { if (neq(*a[i], *b[i])) return false; // values not equal } return true; }
std::string StrPrinter::apply(const vec_basic &d) { std::ostringstream o; for (auto p = d.begin(); p != d.end(); p++) { if (p != d.begin()) { o << ", "; } o << this->apply(*p); } return o.str(); }
void CSRMatrix::csr_sum_duplicates(std::vector<unsigned> &p_, std::vector<unsigned> &j_, vec_basic &x_, unsigned row_) { unsigned nnz = 0; unsigned row_end = 0; unsigned jj = 0, j = 0; RCP<const Basic> x = zero; for (unsigned i = 0; i < row_; i++) { jj = row_end; row_end = p_[i + 1]; while (jj < row_end) { j = j_[jj]; x = x_[jj]; jj++; while (jj < row_end and j_[jj] == j) { x = add(x, x_[jj]); jj++; } j_[nnz] = j; x_[nnz] = x; nnz++; } p_[i + 1] = nnz; } // Resize to discard unnecessary elements j_.resize(nnz); x_.resize(nnz); }
// Create diagonal matrices directly void diag(DenseMatrix &A, vec_basic &v, int k) { SYMENGINE_ASSERT(v.size() > 0); unsigned k_ = std::abs(k); if (k >= 0) { for (unsigned i = 0; i < A.row_; i++) { for (unsigned j = 0; j < A.col_; j++) { if (j != (unsigned)k) { A.m_[i * A.col_ + j] = zero; } else { A.m_[i * A.col_ + j] = v[k - k_]; } } k++; } } else { k = -k; for (unsigned j = 0; j < A.col_; j++) { for (unsigned i = 0; i < A.row_; i++) { if (i != (unsigned)k) { A.m_[i * A.col_ + j] = zero; } else { A.m_[i * A.col_ + j] = v[k - k_]; } } k++; } } }
bool vec_basic_eq_perm(const vec_basic &a, const vec_basic &b) { // Can't be equal if # of entries differ: if (a.size() != b.size()) return false; // Loop over elements in "a" for (size_t i = 0; i < a.size(); i++) { // Find the element a[i] in "b" bool found = false; for (size_t j = 0; j < a.size(); j++) { if (eq(*a[i], *b[j])) { found = true; break; } } // If not found, then a != b if (not found) return false; } // If all elements were found, then a == b return true; }
CSRMatrix CSRMatrix::from_coo(unsigned row, unsigned col, const std::vector<unsigned> &i, const std::vector<unsigned> &j, const vec_basic &x) { // cast is okay, because CSRMatrix indices are unsigned. unsigned nnz = numeric_cast<unsigned>(x.size()); std::vector<unsigned> p_ = std::vector<unsigned>(row + 1, 0); std::vector<unsigned> j_ = std::vector<unsigned>(nnz); vec_basic x_ = vec_basic(nnz); for (unsigned n = 0; n < nnz; n++) { p_[i[n]]++; } // cumsum the nnz per row to get p unsigned temp; for (unsigned i = 0, cumsum = 0; i < row; i++) { temp = p_[i]; p_[i] = cumsum; cumsum += temp; } p_[row] = nnz; // write j, x into j_, x_ unsigned row_, dest_; for (unsigned n = 0; n < nnz; n++) { row_ = i[n]; dest_ = p_[row_]; j_[dest_] = j[n]; x_[dest_] = x[n]; p_[row_]++; } for (unsigned i = 0, last = 0; i <= row; i++) { std::swap(p_[i], last); } // sort indices csr_sort_indices(p_, j_, x_, row); // Remove duplicates csr_sum_duplicates(p_, j_, x_, row); CSRMatrix B = CSRMatrix(row, col, std::move(p_), std::move(j_), std::move(x_)); return B; }
int vec_basic_compare(const vec_basic &A, const vec_basic &B) { if (A.size() != B.size()) return (A.size() < B.size()) ? -1 : 1; auto a = A.begin(); auto b = B.begin(); int cmp; for (; a != A.end(); ++a, ++b) { cmp = (*a)->__cmp__(**b); if (cmp != 0) return cmp; } return 0; }