MCPcopy Create free account
hub / github.com/pybind/pybind11 / load

Function load

include/pybind11/eigen/matrix.h:661–698  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

659 static constexpr bool rowMajor = Type::IsRowMajor;
660
661 bool load(handle src, bool) {
662 if (!src) {
663 return false;
664 }
665
666 auto obj = reinterpret_borrow<object>(src);
667 object sparse_module = module_::import("scipy.sparse");
668 object matrix_type = sparse_module.attr(rowMajor ? "csr_matrix" : "csc_matrix");
669
670 if (!type::handle_of(obj).is(matrix_type)) {
671 try {
672 obj = matrix_type(obj);
673 } catch (const error_already_set &) {
674 return false;
675 }
676 }
677
678 auto values = array_t<Scalar>((object) obj.attr("data"));
679 auto innerIndices = array_t<StorageIndex>((object) obj.attr("indices"));
680 auto outerIndices = array_t<StorageIndex>((object) obj.attr("indptr"));
681 auto shape = pybind11::tuple((pybind11::object) obj.attr("shape"));
682 auto nnz = obj.attr("nnz").cast<Index>();
683
684 if (!values || !innerIndices || !outerIndices) {
685 return false;
686 }
687
688 value = EigenMapSparseMatrix<Scalar,
689 Type::Flags &(Eigen::RowMajor | Eigen::ColMajor),
690 StorageIndex>(shape[0].cast<Index>(),
691 shape[1].cast<Index>(),
692 std::move(nnz),
693 outerIndices.mutable_data(),
694 innerIndices.mutable_data(),
695 values.mutable_data());
696
697 return true;
698 }
699
700 static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
701 const_cast<Type &>(src).makeCompressed();

Callers

nothing calls this directly

Calls 5

importFunction · 0.85
tupleClass · 0.85
moveFunction · 0.85
attrMethod · 0.80
mutable_dataMethod · 0.45

Tested by

no test coverage detected