Loading...
Searching...
No Matches
SymEngine::CSRMatrix Class Reference
+ Inheritance diagram for SymEngine::CSRMatrix:
+ Collaboration diagram for SymEngine::CSRMatrix:

Public Member Functions

 CSRMatrix (unsigned row, unsigned col)
 
 CSRMatrix (unsigned row, unsigned col, const std::vector< unsigned > &p, const std::vector< unsigned > &j, const vec_basic &x)
 
 CSRMatrix (unsigned row, unsigned col, std::vector< unsigned > &&p, std::vector< unsigned > &&j, vec_basic &&x)
 
CSRMatrixoperator= (CSRMatrix &&other)
 
 CSRMatrix (const CSRMatrix &)=default
 
std::tuple< std::vector< unsigned >, std::vector< unsigned >, vec_basicas_vectors () const
 
bool is_canonical () const
 
bool eq (const MatrixBase &other) const override
 
RCP< const Basicget (unsigned i, unsigned j) const override
 
void set (unsigned i, unsigned j, const RCP< const Basic > &e) override
 
unsigned nrows () const override
 
unsigned ncols () const override
 
tribool is_real (const Assumptions *assumptions=nullptr) const override
 
unsigned rank () const override
 
RCP< const Basicdet () const override
 
void inv (MatrixBase &result) const override
 
void add_matrix (const MatrixBase &other, MatrixBase &result) const override
 
void mul_matrix (const MatrixBase &other, MatrixBase &result) const override
 
void elementwise_mul_matrix (const MatrixBase &other, MatrixBase &result) const override
 
void add_scalar (const RCP< const Basic > &k, MatrixBase &result) const override
 
void mul_scalar (const RCP< const Basic > &k, MatrixBase &result) const override
 
void conjugate (MatrixBase &result) const override
 
void transpose (MatrixBase &result) const override
 
CSRMatrix transpose (bool conjugate=false) const
 
void conjugate_transpose (MatrixBase &result) const override
 
void submatrix (MatrixBase &result, unsigned row_start, unsigned col_start, unsigned row_end, unsigned col_end, unsigned row_step=1, unsigned col_step=1) const override
 
void LU (MatrixBase &L, MatrixBase &U) const override
 
void LDL (MatrixBase &L, MatrixBase &D) const override
 
void LU_solve (const MatrixBase &b, MatrixBase &x) const override
 
void FFLU (MatrixBase &LU) const override
 
void FFLDU (MatrixBase &L, MatrixBase &D, MatrixBase &U) const override
 
void QR (MatrixBase &Q, MatrixBase &R) const override
 
void cholesky (MatrixBase &L) const override
 
- Public Member Functions inherited from SymEngine::MatrixBase
bool is_square () const
 
virtual unsigned nrows () const =0
 
virtual unsigned ncols () const =0
 
virtual bool eq (const MatrixBase &other) const
 
virtual tribool is_real (const Assumptions *assumptions=nullptr) const =0
 
virtual RCP< const Basicget (unsigned i, unsigned j) const =0
 
virtual void set (unsigned i, unsigned j, const RCP< const Basic > &e)=0
 
virtual std::string __str__ () const
 
virtual unsigned rank () const =0
 
virtual RCP< const Basicdet () const =0
 
virtual void inv (MatrixBase &result) const =0
 
virtual void add_matrix (const MatrixBase &other, MatrixBase &result) const =0
 
virtual void mul_matrix (const MatrixBase &other, MatrixBase &result) const =0
 
virtual void elementwise_mul_matrix (const MatrixBase &other, MatrixBase &result) const =0
 
virtual void add_scalar (const RCP< const Basic > &k, MatrixBase &result) const =0
 
virtual void mul_scalar (const RCP< const Basic > &k, MatrixBase &result) const =0
 
virtual void conjugate (MatrixBase &result) const =0
 
virtual void transpose (MatrixBase &result) const =0
 
virtual void conjugate_transpose (MatrixBase &result) const =0
 
virtual void submatrix (MatrixBase &result, unsigned row_start, unsigned col_start, unsigned row_end, unsigned col_end, unsigned row_step=1, unsigned col_step=1) const =0
 
virtual void LU (MatrixBase &L, MatrixBase &U) const =0
 
virtual void LDL (MatrixBase &L, MatrixBase &D) const =0
 
virtual void FFLU (MatrixBase &LU) const =0
 
virtual void FFLDU (MatrixBase &L, MatrixBase &D, MatrixBase &U) const =0
 
virtual void QR (MatrixBase &Q, MatrixBase &R) const =0
 
virtual void cholesky (MatrixBase &L) const =0
 
virtual void LU_solve (const MatrixBase &b, MatrixBase &x) const =0
 

Static Public Member Functions

static void csr_sum_duplicates (std::vector< unsigned > &p_, std::vector< unsigned > &j_, vec_basic &x_, unsigned row_)
 
static void csr_sort_indices (std::vector< unsigned > &p_, std::vector< unsigned > &j_, vec_basic &x_, unsigned row_)
 
static bool csr_has_sorted_indices (const std::vector< unsigned > &p_, const std::vector< unsigned > &j_, unsigned row_)
 
static bool csr_has_duplicates (const std::vector< unsigned > &p_, const std::vector< unsigned > &j_, unsigned row_)
 
static bool csr_has_canonical_format (const std::vector< unsigned > &p_, const std::vector< unsigned > &j_, unsigned row_)
 
static CSRMatrix from_coo (unsigned row, unsigned col, const std::vector< unsigned > &i, const std::vector< unsigned > &j, const vec_basic &x)
 
static CSRMatrix jacobian (const vec_basic &exprs, const vec_sym &x, bool diff_cache=true)
 
static CSRMatrix jacobian (const DenseMatrix &A, const DenseMatrix &x, bool diff_cache=true)
 

Private Attributes

std::vector< unsigned > p_
 
std::vector< unsigned > j_
 
vec_basic x_
 
unsigned row_
 
unsigned col_
 

Friends

void csr_matmat_pass1 (const CSRMatrix &A, const CSRMatrix &B, CSRMatrix &C)
 
void csr_matmat_pass2 (const CSRMatrix &A, const CSRMatrix &B, CSRMatrix &C)
 
void csr_diagonal (const CSRMatrix &A, DenseMatrix &D)
 
void csr_scale_rows (CSRMatrix &A, const DenseMatrix &X)
 
void csr_scale_columns (CSRMatrix &A, const DenseMatrix &X)
 
void csr_binop_csr_canonical (const CSRMatrix &A, const CSRMatrix &B, CSRMatrix &C, RCP< const Basic >(&bin_op)(const RCP< const Basic > &, const RCP< const Basic > &))
 

Detailed Description

Definition at line 342 of file matrix.h.

Constructor & Destructor Documentation

◆ CSRMatrix() [1/4]

SymEngine::CSRMatrix::CSRMatrix ( )

Definition at line 14 of file sparse_matrix.cpp.

14{}

◆ CSRMatrix() [2/4]

SymEngine::CSRMatrix::CSRMatrix ( unsigned  row,
unsigned  col 
)

Definition at line 16 of file sparse_matrix.cpp.

16 : row_(row), col_(col)
17{
18 p_ = std::vector<unsigned>(row + 1, 0);
19 SYMENGINE_ASSERT(is_canonical());
20}

◆ CSRMatrix() [3/4]

SymEngine::CSRMatrix::CSRMatrix ( unsigned  row,
unsigned  col,
const std::vector< unsigned > &  p,
const std::vector< unsigned > &  j,
const vec_basic x 
)

Definition at line 22 of file sparse_matrix.cpp.

24 : p_{p}, j_{j}, x_{x}, row_(row), col_(col)
25{
26 SYMENGINE_ASSERT(is_canonical());
27}

◆ CSRMatrix() [4/4]

SymEngine::CSRMatrix::CSRMatrix ( unsigned  row,
unsigned  col,
std::vector< unsigned > &&  p,
std::vector< unsigned > &&  j,
vec_basic &&  x 
)

Definition at line 29 of file sparse_matrix.cpp.

31 : p_{std::move(p)}, j_{std::move(j)}, x_{std::move(x)}, row_(row), col_(col)
32{
33 SYMENGINE_ASSERT(is_canonical());
34}
T move(T... args)

Member Function Documentation

◆ add_matrix()

void SymEngine::CSRMatrix::add_matrix ( const MatrixBase other,
MatrixBase result 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 190 of file sparse_matrix.cpp.

191{
192 throw NotImplementedError("Not Implemented");
193}

◆ add_scalar()

void SymEngine::CSRMatrix::add_scalar ( const RCP< const Basic > &  k,
MatrixBase result 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 211 of file sparse_matrix.cpp.

212{
213 throw NotImplementedError("Not Implemented");
214}

◆ as_vectors()

std::tuple< std::vector< unsigned >, std::vector< unsigned >, vec_basic > SymEngine::CSRMatrix::as_vectors ( ) const

Definition at line 47 of file sparse_matrix.cpp.

48{
49 auto p = p_, j = j_;
50 auto x = x_;
52}
T make_tuple(T... args)

◆ cholesky()

void SymEngine::CSRMatrix::cholesky ( MatrixBase L) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 332 of file sparse_matrix.cpp.

333{
334 throw NotImplementedError("Not Implemented");
335}

◆ conjugate()

void SymEngine::CSRMatrix::conjugate ( MatrixBase result) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 223 of file sparse_matrix.cpp.

224{
225 if (is_a<CSRMatrix>(result)) {
226 auto &r = down_cast<CSRMatrix &>(result);
227 std::vector<unsigned> p(p_), j(j_);
228 vec_basic x(x_.size());
229 for (unsigned i = 0; i < x_.size(); ++i) {
230 x[i] = SymEngine::conjugate(x_[i]);
231 }
232 r = CSRMatrix(col_, row_, std::move(p), std::move(j), std::move(x));
233 } else {
234 throw NotImplementedError("Not Implemented");
235 }
236}
RCP< const Basic > conjugate(const RCP< const Basic > &arg)
Canonicalize Conjugate.
Definition: functions.cpp:149
T size(T... args)

◆ conjugate_transpose()

void SymEngine::CSRMatrix::conjugate_transpose ( MatrixBase result) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 276 of file sparse_matrix.cpp.

277{
278 if (is_a<CSRMatrix>(result)) {
279 auto &r = down_cast<CSRMatrix &>(result);
280 r = this->transpose(true);
281 } else {
282 throw NotImplementedError("Not Implemented");
283 }
284}

◆ csr_has_canonical_format()

bool SymEngine::CSRMatrix::csr_has_canonical_format ( const std::vector< unsigned > &  p_,
const std::vector< unsigned > &  j_,
unsigned  row_ 
)
static

Definition at line 428 of file sparse_matrix.cpp.

431{
432 for (unsigned i = 0; i < row_; i++) {
433 if (p_[i] > p_[i + 1])
434 return false;
435 }
436
437 return csr_has_sorted_indices(p_, j_, row_)
438 and not csr_has_duplicates(p_, j_, row_);
439}

◆ csr_has_duplicates()

bool SymEngine::CSRMatrix::csr_has_duplicates ( const std::vector< unsigned > &  p_,
const std::vector< unsigned > &  j_,
unsigned  row_ 
)
static

Definition at line 402 of file sparse_matrix.cpp.

405{
406 for (unsigned i = 0; i < row_; i++) {
407 for (unsigned j = p_[i]; j + 1 < p_[i + 1]; j++) {
408 if (j_[j] == j_[j + 1])
409 return true;
410 }
411 }
412 return false;
413}

◆ csr_has_sorted_indices()

bool SymEngine::CSRMatrix::csr_has_sorted_indices ( const std::vector< unsigned > &  p_,
const std::vector< unsigned > &  j_,
unsigned  row_ 
)
static

Definition at line 415 of file sparse_matrix.cpp.

418{
419 for (unsigned i = 0; i < row_; i++) {
420 for (unsigned jj = p_[i]; jj < p_[i + 1] - 1; jj++) {
421 if (j_[jj] > j_[jj + 1])
422 return false;
423 }
424 }
425 return true;
426}

◆ csr_sort_indices()

void SymEngine::CSRMatrix::csr_sort_indices ( std::vector< unsigned > &  p_,
std::vector< unsigned > &  j_,
vec_basic x_,
unsigned  row_ 
)
static

Definition at line 372 of file sparse_matrix.cpp.

375{
377
378 for (unsigned i = 0; i < row_; i++) {
379 unsigned row_start = p_[i];
380 unsigned row_end = p_[i + 1];
381
382 temp.clear();
383
384 for (unsigned jj = row_start; jj < row_end; jj++) {
385 temp.push_back(std::make_pair(j_[jj], x_[jj]));
386 }
387
388 std::sort(temp.begin(), temp.end(),
389 [](const std::pair<unsigned, RCP<const Basic>> &x,
390 const std::pair<unsigned, RCP<const Basic>> &y) {
391 return x.first < y.first;
392 });
393
394 for (unsigned jj = row_start, n = 0; jj < row_end; jj++, n++) {
395 j_[jj] = temp[n].first;
396 x_[jj] = temp[n].second;
397 }
398 }
399}
T begin(T... args)
T clear(T... args)
T end(T... args)
T make_pair(T... args)
T push_back(T... args)
T sort(T... args)

◆ csr_sum_duplicates()

void SymEngine::CSRMatrix::csr_sum_duplicates ( std::vector< unsigned > &  p_,
std::vector< unsigned > &  j_,
vec_basic x_,
unsigned  row_ 
)
static

Definition at line 337 of file sparse_matrix.cpp.

340{
341 unsigned nnz = 0;
342 unsigned row_end = 0;
343 unsigned jj = 0, j = 0;
344 RCP<const Basic> x = zero;
345
346 for (unsigned i = 0; i < row_; i++) {
347 jj = row_end;
348 row_end = p_[i + 1];
349
350 while (jj < row_end) {
351 j = j_[jj];
352 x = x_[jj];
353 jj++;
354
355 while (jj < row_end and j_[jj] == j) {
356 x = add(x, x_[jj]);
357 jj++;
358 }
359
360 j_[nnz] = j;
361 x_[nnz] = x;
362 nnz++;
363 }
364 p_[i + 1] = nnz;
365 }
366
367 // Resize to discard unnecessary elements
368 j_.resize(nnz);
369 x_.resize(nnz);
370}
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition: add.cpp:425
T resize(T... args)

◆ det()

RCP< const Basic > SymEngine::CSRMatrix::det ( ) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 180 of file sparse_matrix.cpp.

181{
182 throw NotImplementedError("Not Implemented");
183}

◆ elementwise_mul_matrix()

void SymEngine::CSRMatrix::elementwise_mul_matrix ( const MatrixBase other,
MatrixBase result 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 200 of file sparse_matrix.cpp.

202{
203 if (is_a<CSRMatrix>(result)) {
204 auto &o = down_cast<const CSRMatrix &>(other);
205 auto &r = down_cast<CSRMatrix &>(result);
206 csr_binop_csr_canonical(*this, o, r, mul);
207 }
208}
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition: mul.cpp:352

◆ eq()

bool SymEngine::CSRMatrix::eq ( const MatrixBase other) const
overridevirtual

Reimplemented from SymEngine::MatrixBase.

Definition at line 54 of file sparse_matrix.cpp.

55{
56 unsigned row = this->nrows();
57 if (row != other.nrows() or this->ncols() != other.ncols())
58 return false;
59
60 if (is_a<CSRMatrix>(other)) {
61 const CSRMatrix &o = down_cast<const CSRMatrix &>(other);
62
63 if (this->p_[row] != o.p_[row])
64 return false;
65
66 for (unsigned i = 0; i <= row; i++)
67 if (this->p_[i] != o.p_[i])
68 return false;
69
70 for (unsigned i = 0; i < this->p_[row]; i++)
71 if ((this->j_[i] != o.j_[i]) or neq(*this->x_[i], *(o.x_[i])))
72 return false;
73
74 return true;
75 } else {
76 return this->MatrixBase::eq(other);
77 }
78}
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
Definition: basic-inl.h:29

◆ FFLDU()

void SymEngine::CSRMatrix::FFLDU ( MatrixBase L,
MatrixBase D,
MatrixBase U 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 320 of file sparse_matrix.cpp.

321{
322 throw NotImplementedError("Not Implemented");
323}

◆ FFLU()

void SymEngine::CSRMatrix::FFLU ( MatrixBase LU) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 314 of file sparse_matrix.cpp.

315{
316 throw NotImplementedError("Not Implemented");
317}

◆ from_coo()

CSRMatrix SymEngine::CSRMatrix::from_coo ( unsigned  row,
unsigned  col,
const std::vector< unsigned > &  i,
const std::vector< unsigned > &  j,
const vec_basic x 
)
static

Definition at line 441 of file sparse_matrix.cpp.

445{
446 // cast is okay, because CSRMatrix indices are unsigned.
447 unsigned nnz = numeric_cast<unsigned>(x.size());
450 vec_basic x_ = vec_basic(nnz);
451
452 for (unsigned n = 0; n < nnz; n++) {
453 p_[i[n]]++;
454 }
455
456 // cumsum the nnz per row to get p
457 unsigned temp;
458 for (unsigned i = 0, cumsum = 0; i < row; i++) {
459 temp = p_[i];
460 p_[i] = cumsum;
461 cumsum += temp;
462 }
463 p_[row] = nnz;
464
465 // write j, x into j_, x_
466 unsigned row_, dest_;
467 for (unsigned n = 0; n < nnz; n++) {
468 row_ = i[n];
469 dest_ = p_[row_];
470
471 j_[dest_] = j[n];
472 x_[dest_] = x[n];
473
474 p_[row_]++;
475 }
476
477 for (unsigned i = 0, last = 0; i <= row; i++) {
478 std::swap(p_[i], last);
479 }
480
481 // sort indices
482 csr_sort_indices(p_, j_, x_, row);
483 // Remove duplicates
484 csr_sum_duplicates(p_, j_, x_, row);
485
486 CSRMatrix B
487 = CSRMatrix(row, col, std::move(p_), std::move(j_), std::move(x_));
488 return B;
489}
T swap(T... args)

◆ get()

RCP< const Basic > SymEngine::CSRMatrix::get ( unsigned  i,
unsigned  j 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 91 of file sparse_matrix.cpp.

92{
93 SYMENGINE_ASSERT(i < row_ and j < col_);
94
95 unsigned row_start = p_[i];
96 unsigned row_end = p_[i + 1];
97 unsigned k;
98
99 if (row_start == row_end) {
100 return zero;
101 }
102
103 while (row_start < row_end) {
104 k = (row_start + row_end) / 2;
105 if (j_[k] == j) {
106 return x_[k];
107 } else if (j_[k] < j) {
108 row_start = k + 1;
109 } else {
110 row_end = k;
111 }
112 }
113
114 return zero;
115}

◆ inv()

void SymEngine::CSRMatrix::inv ( MatrixBase result) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 185 of file sparse_matrix.cpp.

186{
187 throw NotImplementedError("Not Implemented");
188}

◆ is_canonical()

bool SymEngine::CSRMatrix::is_canonical ( ) const

Definition at line 80 of file sparse_matrix.cpp.

81{
82 if (p_.size() != row_ + 1 or j_.size() != p_[row_] or x_.size() != p_[row_])
83 return false;
84
85 if (p_[row_] != 0) // Zero matrix is in canonical format
86 return csr_has_canonical_format(p_, j_, row_);
87 return true;
88}

◆ is_real()

tribool SymEngine::CSRMatrix::is_real ( const Assumptions assumptions = nullptr) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 162 of file sparse_matrix.cpp.

163{
164 RealVisitor visitor(assumptions);
165 tribool cur = tribool::tritrue;
166 for (auto &e : x_) {
167 cur = and_tribool(cur, visitor.apply(*e));
168 if (is_false(cur)) {
169 return cur;
170 }
171 }
172 return cur;
173}

◆ jacobian() [1/2]

CSRMatrix SymEngine::CSRMatrix::jacobian ( const DenseMatrix A,
const DenseMatrix x,
bool  diff_cache = true 
)
static

Definition at line 516 of file sparse_matrix.cpp.

518{
519 SYMENGINE_ASSERT(A.col_ == 1);
520 SYMENGINE_ASSERT(x.col_ == 1);
521 vec_sym syms;
522 syms.reserve(x.row_);
523 for (const auto &dx : x.m_) {
524 if (!is_a<Symbol>(*dx)) {
525 throw SymEngineException("'x' must contain Symbols only");
526 }
527 syms.push_back(rcp_static_cast<const Symbol>(dx));
528 }
529 return CSRMatrix::jacobian(A.m_, syms, diff_cache);
530}

◆ jacobian() [2/2]

CSRMatrix SymEngine::CSRMatrix::jacobian ( const vec_basic exprs,
const vec_sym x,
bool  diff_cache = true 
)
static

Definition at line 491 of file sparse_matrix.cpp.

493{
494 const unsigned nrows = static_cast<unsigned>(exprs.size());
495 const unsigned ncols = static_cast<unsigned>(x.size());
496 std::vector<unsigned> p(1, 0), j;
497 vec_basic elems;
498 p.reserve(nrows + 1);
499 j.reserve(nrows);
500 elems.reserve(nrows);
501 for (unsigned ri = 0; ri < nrows; ++ri) {
502 p.push_back(p.back());
503 for (unsigned ci = 0; ci < ncols; ++ci) {
504 auto elem = exprs[ri]->diff(x[ci], diff_cache);
505 if (!is_true(is_zero(*elem))) {
506 p.back()++;
507 j.push_back(ci);
508 elems.emplace_back(std::move(elem));
509 }
510 }
511 }
512 return CSRMatrix(nrows, ncols, std::move(p), std::move(j),
513 std::move(elems));
514}
tribool is_zero(const Basic &b, const Assumptions *assumptions=nullptr)
Check if a number is zero.
T reserve(T... args)

◆ LDL()

void SymEngine::CSRMatrix::LDL ( MatrixBase L,
MatrixBase D 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 302 of file sparse_matrix.cpp.

303{
304 throw NotImplementedError("Not Implemented");
305}

◆ LU()

void SymEngine::CSRMatrix::LU ( MatrixBase L,
MatrixBase U 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 296 of file sparse_matrix.cpp.

297{
298 throw NotImplementedError("Not Implemented");
299}

◆ LU_solve()

void SymEngine::CSRMatrix::LU_solve ( const MatrixBase b,
MatrixBase x 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 308 of file sparse_matrix.cpp.

309{
310 throw NotImplementedError("Not Implemented");
311}

◆ mul_matrix()

void SymEngine::CSRMatrix::mul_matrix ( const MatrixBase other,
MatrixBase result 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 195 of file sparse_matrix.cpp.

196{
197 throw NotImplementedError("Not Implemented");
198}

◆ mul_scalar()

void SymEngine::CSRMatrix::mul_scalar ( const RCP< const Basic > &  k,
MatrixBase result 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 217 of file sparse_matrix.cpp.

218{
219 throw NotImplementedError("Not Implemented");
220}

◆ ncols()

unsigned SymEngine::CSRMatrix::ncols ( ) const
inlineoverridevirtual

Implements SymEngine::MatrixBase.

Definition at line 368 of file matrix.h.

369 {
370 return col_;
371 }

◆ nrows()

unsigned SymEngine::CSRMatrix::nrows ( ) const
inlineoverridevirtual

Implements SymEngine::MatrixBase.

Definition at line 364 of file matrix.h.

365 {
366 return row_;
367 }

◆ operator=()

CSRMatrix & SymEngine::CSRMatrix::operator= ( CSRMatrix &&  other)

Definition at line 36 of file sparse_matrix.cpp.

37{
38 col_ = other.col_;
39 row_ = other.row_;
40 p_ = std::move(other.p_);
41 j_ = std::move(other.j_);
42 x_ = std::move(other.x_);
43 return *this;
44}

◆ QR()

void SymEngine::CSRMatrix::QR ( MatrixBase Q,
MatrixBase R 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 326 of file sparse_matrix.cpp.

327{
328 throw NotImplementedError("Not Implemented");
329}

◆ rank()

unsigned SymEngine::CSRMatrix::rank ( ) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 175 of file sparse_matrix.cpp.

176{
177 throw NotImplementedError("Not Implemented");
178}

◆ set()

void SymEngine::CSRMatrix::set ( unsigned  i,
unsigned  j,
const RCP< const Basic > &  e 
)
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 117 of file sparse_matrix.cpp.

118{
119 SYMENGINE_ASSERT(i < row_ and j < col_);
120
121 unsigned k = p_[i];
122 unsigned row_end = p_[i + 1];
123 unsigned end = p_[i + 1];
124 unsigned mid;
125
126 while (k < end) {
127 mid = (k + end) / 2;
128 if (mid == k) {
129 if (j_[k] < j) {
130 k++;
131 }
132 break;
133 } else if (j_[mid] >= j and j_[mid - 1] < j) {
134 k = mid;
135 break;
136 } else if (j_[mid - 1] >= j) {
137 end = mid - 1;
138 } else {
139 k = mid + 1;
140 }
141 }
142
143 if (!is_true(is_zero(*e))) {
144 if (k < row_end and j_[k] == j) {
145 x_[k] = e;
146 } else { // j_[k] > j or k is the last non-zero element
147 x_.insert(x_.begin() + k, e);
148 j_.insert(j_.begin() + k, j);
149 for (unsigned l = i + 1; l <= row_; l++)
150 p_[l]++;
151 }
152 } else { // e is zero
153 if (k < row_end and j_[k] == j) { // remove existing non-zero element
154 x_.erase(x_.begin() + k);
155 j_.erase(j_.begin() + k);
156 for (unsigned l = i + 1; l <= row_; l++)
157 p_[l]--;
158 }
159 }
160}
T erase(T... args)
T insert(T... args)

◆ submatrix()

void SymEngine::CSRMatrix::submatrix ( MatrixBase result,
unsigned  row_start,
unsigned  col_start,
unsigned  row_end,
unsigned  col_end,
unsigned  row_step = 1,
unsigned  col_step = 1 
) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 287 of file sparse_matrix.cpp.

291{
292 throw NotImplementedError("Not Implemented");
293}

◆ transpose() [1/2]

CSRMatrix SymEngine::CSRMatrix::transpose ( bool  conjugate = false) const

Definition at line 249 of file sparse_matrix.cpp.

250{
251 const auto nnz = j_.size();
252 std::vector<unsigned> p(col_ + 1, 0), j(nnz), tmp(col_, 0);
253 vec_basic x(nnz);
254
255 for (unsigned i = 0; i < nnz; ++i)
256 p[j_[i] + 1]++;
257 std::partial_sum(p.begin(), p.end(), p.begin());
258
259 for (unsigned ri = 0; ri < row_; ++ri) {
260 for (unsigned i = p_[ri]; i < p_[ri + 1]; ++i) {
261 const auto ci = j_[i];
262 const unsigned k = p[ci] + tmp[ci];
263 j[k] = ri;
264 if (conjugate) {
265 x[k] = SymEngine::conjugate(x_[i]);
266 } else {
267 x[k] = x_[i];
268 }
269 tmp[ci]++;
270 }
271 }
272 return CSRMatrix(col_, row_, std::move(p), std::move(j), std::move(x));
273}
T partial_sum(T... args)

◆ transpose() [2/2]

void SymEngine::CSRMatrix::transpose ( MatrixBase result) const
overridevirtual

Implements SymEngine::MatrixBase.

Definition at line 239 of file sparse_matrix.cpp.

240{
241 if (is_a<CSRMatrix>(result)) {
242 auto &r = down_cast<CSRMatrix &>(result);
243 r = this->transpose();
244 } else {
245 throw NotImplementedError("Not Implemented");
246 }
247}

Friends And Related Function Documentation

◆ csr_binop_csr_canonical

void csr_binop_csr_canonical ( const CSRMatrix A,
const CSRMatrix B,
CSRMatrix C,
RCP< const Basic >(&)(const RCP< const Basic > &, const RCP< const Basic > &)  bin_op 
)
friend

Definition at line 689 of file sparse_matrix.cpp.

693{
694 SYMENGINE_ASSERT(A.row_ == B.row_ and A.col_ == B.col_ and C.row_ == A.row_
695 and C.col_ == A.col_);
696
697 // Method that works for canonical CSR matrices
698 C.p_[0] = 0;
699 C.j_.clear();
700 C.x_.clear();
701 unsigned nnz = 0;
702 unsigned A_pos, B_pos, A_end, B_end;
703
704 for (unsigned i = 0; i < A.row_; i++) {
705 A_pos = A.p_[i];
706 B_pos = B.p_[i];
707 A_end = A.p_[i + 1];
708 B_end = B.p_[i + 1];
709
710 // while not finished with either row
711 while (A_pos < A_end and B_pos < B_end) {
712 unsigned A_j = A.j_[A_pos];
713 unsigned B_j = B.j_[B_pos];
714
715 if (A_j == B_j) {
716 RCP<const Basic> result = bin_op(A.x_[A_pos], B.x_[B_pos]);
717 if (!is_true(is_zero(*result))) {
718 C.j_.push_back(A_j);
719 C.x_.push_back(result);
720 nnz++;
721 }
722 A_pos++;
723 B_pos++;
724 } else if (A_j < B_j) {
725 RCP<const Basic> result = bin_op(A.x_[A_pos], zero);
726 if (!is_true(is_zero(*result))) {
727 C.j_.push_back(A_j);
728 C.x_.push_back(result);
729 nnz++;
730 }
731 A_pos++;
732 } else {
733 // B_j < A_j
734 RCP<const Basic> result = bin_op(zero, B.x_[B_pos]);
735 if (!is_true(is_zero(*result))) {
736 C.j_.push_back(B_j);
737 C.x_.push_back(result);
738 nnz++;
739 }
740 B_pos++;
741 }
742 }
743
744 // tail
745 while (A_pos < A_end) {
746 RCP<const Basic> result = bin_op(A.x_[A_pos], zero);
747 if (!is_true(is_zero(*result))) {
748 C.j_.push_back(A.j_[A_pos]);
749 C.x_.push_back(result);
750 nnz++;
751 }
752 A_pos++;
753 }
754 while (B_pos < B_end) {
755 RCP<const Basic> result = bin_op(zero, B.x_[B_pos]);
756 if (!is_true(is_zero(*result))) {
757 C.j_.push_back(B.j_[B_pos]);
758 C.x_.push_back(result);
759 nnz++;
760 }
761 B_pos++;
762 }
763
764 C.p_[i + 1] = nnz;
765 }
766
767 // It's enough to check for duplicates as the column indices
768 // remain sorted after the above operations
769 if (CSRMatrix::csr_has_duplicates(C.p_, C.j_, A.row_))
770 CSRMatrix::csr_sum_duplicates(C.p_, C.j_, C.x_, A.row_);
771}

◆ csr_diagonal

void csr_diagonal ( const CSRMatrix A,
DenseMatrix D 
)
friend

Definition at line 622 of file sparse_matrix.cpp.

623{
624 unsigned N = std::min(A.row_, A.col_);
625
626 SYMENGINE_ASSERT(D.nrows() == N and D.ncols() == 1);
627
628 unsigned row_start;
629 unsigned row_end;
630 RCP<const Basic> diag;
631
632 for (unsigned i = 0; i < N; i++) {
633 row_start = A.p_[i];
634 row_end = A.p_[i + 1];
635 diag = zero;
636 unsigned jj;
637
638 while (row_start <= row_end) {
639 jj = (row_start + row_end) / 2;
640 if (A.j_[jj] == i) {
641 diag = A.x_[jj];
642 break;
643 } else if (A.j_[jj] < i) {
644 row_start = jj + 1;
645 } else {
646 row_end = jj - 1;
647 }
648 }
649
650 D.set(i, 0, diag);
651 }
652}
T min(T... args)

◆ csr_matmat_pass1

void csr_matmat_pass1 ( const CSRMatrix A,
const CSRMatrix B,
CSRMatrix C 
)
friend

Definition at line 532 of file sparse_matrix.cpp.

533{
534 // method that uses O(n) temp storage
535 std::vector<unsigned> mask(A.col_, -1);
536 C.p_[0] = 0;
537
538 unsigned nnz = 0;
539 for (unsigned i = 0; i < A.row_; i++) {
540 // npy_intp row_nnz = 0;
541 unsigned row_nnz = 0;
542
543 for (unsigned jj = A.p_[i]; jj < A.p_[i + 1]; jj++) {
544 unsigned j = A.j_[jj];
545 for (unsigned kk = B.p_[j]; kk < B.p_[j + 1]; kk++) {
546 unsigned k = B.j_[kk];
547 if (mask[k] != i) {
548 mask[k] = i;
549 row_nnz++;
550 }
551 }
552 }
553
554 unsigned next_nnz = nnz + row_nnz;
555
556 // Addition overflow: http://www.cplusplus.com/articles/DE18T05o/
557 if (next_nnz < nnz) {
558 throw std::overflow_error("nnz of the result is too large");
559 }
560
561 nnz = next_nnz;
562 C.p_[i + 1] = nnz;
563 }
564}

◆ csr_matmat_pass2

void csr_matmat_pass2 ( const CSRMatrix A,
const CSRMatrix B,
CSRMatrix C 
)
friend

Definition at line 568 of file sparse_matrix.cpp.

569{
570 std::vector<int> next(A.col_, -1);
571 vec_basic sums(A.col_, zero);
572
573 unsigned nnz = 0;
574
575 C.p_[0] = 0;
576
577 for (unsigned i = 0; i < A.row_; i++) {
578 int head = -2;
579 unsigned length = 0;
580
581 unsigned jj_start = A.p_[i];
582 unsigned jj_end = A.p_[i + 1];
583 for (unsigned jj = jj_start; jj < jj_end; jj++) {
584 unsigned j = A.j_[jj];
585 RCP<const Basic> v = A.x_[jj];
586
587 unsigned kk_start = B.p_[j];
588 unsigned kk_end = B.p_[j + 1];
589 for (unsigned kk = kk_start; kk < kk_end; kk++) {
590 unsigned k = B.j_[kk];
591
592 sums[k] = add(sums[k], mul(v, B.x_[kk]));
593
594 if (next[k] == -1) {
595 next[k] = head;
596 head = k;
597 length++;
598 }
599 }
600 }
601
602 for (unsigned jj = 0; jj < length; jj++) {
603
604 if (!is_true(is_zero(*sums[head]))) {
605 C.j_[nnz] = head;
606 C.x_[nnz] = sums[head];
607 nnz++;
608 }
609
610 unsigned temp = head;
611 head = next[head];
612
613 next[temp] = -1; // clear arrays
614 sums[temp] = zero;
615 }
616
617 C.p_[i + 1] = nnz;
618 }
619}
T next(T... args)

◆ csr_scale_columns

void csr_scale_columns ( CSRMatrix A,
const DenseMatrix X 
)
friend

Definition at line 670 of file sparse_matrix.cpp.

671{
672 SYMENGINE_ASSERT(A.col_ == X.nrows() and X.ncols() == 1);
673
674 const unsigned nnz = A.p_[A.row_];
675 unsigned i;
676
677 for (i = 0; i < A.col_; i++) {
678 if (is_true(is_zero(*X.get(i, 0))))
679 throw SymEngineException("Scaling factor can't be zero");
680 }
681
682 for (i = 0; i < nnz; i++)
683 A.x_[i] = mul(A.x_[i], X.get(A.j_[i], 0));
684}

◆ csr_scale_rows

void csr_scale_rows ( CSRMatrix A,
const DenseMatrix X 
)
friend

Definition at line 656 of file sparse_matrix.cpp.

657{
658 SYMENGINE_ASSERT(A.row_ == X.nrows() and X.ncols() == 1);
659
660 for (unsigned i = 0; i < A.row_; i++) {
661 if (is_true(is_zero(*X.get(i, 0))))
662 throw SymEngineException("Scaling factor can't be zero");
663 for (unsigned jj = A.p_[i]; jj < A.p_[i + 1]; jj++)
664 A.x_[jj] = mul(A.x_[jj], X.get(i, 0));
665 }
666}

Field Documentation

◆ col_

unsigned SymEngine::CSRMatrix::col_
private

Definition at line 480 of file matrix.h.

◆ j_

std::vector<unsigned> SymEngine::CSRMatrix::j_
private

Definition at line 476 of file matrix.h.

◆ p_

std::vector<unsigned> SymEngine::CSRMatrix::p_
private

Definition at line 475 of file matrix.h.

◆ row_

unsigned SymEngine::CSRMatrix::row_
private

Definition at line 479 of file matrix.h.

◆ x_

vec_basic SymEngine::CSRMatrix::x_
private

Definition at line 477 of file matrix.h.


The documentation for this class was generated from the following files: