Skip to content

Commit a0050f9

Browse files
authored
Expose Sparse Op's CSR matrix to Python (#74)
1 parent 73bfa97 commit a0050f9

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

pyci/include/pyci.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ struct SparseOp final {
672672
template<class WfnType>
673673
void py_update(const SQuantOp &, const WfnType &);
674674

675+
Array<double> py_data() const;
676+
677+
Array<long> py_indices() const;
678+
679+
Array<long> py_indptr() const;
680+
675681
private:
676682
void sort_row(const long);
677683

pyci/src/binding.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ n : int
10831083

10841084
sparse_op.def("squeeze", &SparseOp::squeeze, "Free any unused memory allocated to this object.");
10851085

1086+
sparse_op.def("data", &SparseOp::py_data, "Return CSR matrix data vector", py::keep_alive<0, 1>());
1087+
sparse_op.def("indices", &SparseOp::py_indices, "Return CSR matrix indices vector", py::keep_alive<0, 1>());
1088+
sparse_op.def("indptr", &SparseOp::py_indptr, "Return CSR matrix index pointer vector", py::keep_alive<0, 1>());
1089+
10861090
/*
10871091
Section: Free functions
10881092
*/

pyci/src/sparseop.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void SparseOp::solve_ci(const long n, const double *coeffs, const long ncv, cons
115115
const double tol, double *evals, double *evecs) const {
116116
if ((nrow > 1 && n >= nrow) || (nrow == 1 && n > 1)) {
117117
throw std::invalid_argument("cannot find >=n eigenpairs for sparse operator with n rows");
118-
} else if (!symmetric) {
118+
} else if (nrow != ncol) {
119119
throw pybind11::type_error("Can only solve sparse symmetric matrix operators");
120120
} else if (nrow == 1) {
121121
*evals = get_element(0, 0) + ecore;
@@ -501,4 +501,16 @@ void SparseOp::add_row(const SQuantOp &ham, const GenCIWfn &wfn, const long idet
501501
append<long>(indptr, indices.size());
502502
}
503503

504+
Array<double> SparseOp::py_data() const {
505+
return Array<double>(data.size(), &data[0]);
506+
}
507+
508+
Array<long> SparseOp::py_indices() const {
509+
return Array<long>(indices.size(), &indices[0]);
510+
}
511+
512+
Array<long> SparseOp::py_indptr() const {
513+
return Array<long>(indptr.size(), &indptr[0]);
514+
}
515+
504516
} // namespace pyci

0 commit comments

Comments
 (0)