mirror of
https://github.com/RYDE-WORK/pybind11.git
synced 2026-02-04 06:23:30 +08:00
Simplify code in eigen.h using new array ctors
This commit is contained in:
parent
67b3daeea4
commit
6956b655f0
@ -85,7 +85,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
|
|||||||
if (!buffer.check())
|
if (!buffer.check())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
buffer_info info = buffer.request();
|
auto info = buffer.request();
|
||||||
if (info.ndim == 1) {
|
if (info.ndim == 1) {
|
||||||
typedef Eigen::InnerStride<> Strides;
|
typedef Eigen::InnerStride<> Strides;
|
||||||
if (!isVector &&
|
if (!isVector &&
|
||||||
@ -127,37 +127,19 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
|
|||||||
|
|
||||||
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
|
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
|
||||||
if (isVector) {
|
if (isVector) {
|
||||||
return array(buffer_info(
|
return array(
|
||||||
/* Pointer to buffer */
|
{ (size_t) src.size() }, // shape
|
||||||
const_cast<Scalar *>(src.data()),
|
{ sizeof(Scalar) * static_cast<size_t>(src.innerStride()) }, // strides
|
||||||
/* Size of one scalar */
|
src.data() // data
|
||||||
sizeof(Scalar),
|
).release();
|
||||||
/* Python struct-style format descriptor */
|
|
||||||
format_descriptor<Scalar>::format(),
|
|
||||||
/* Number of dimensions */
|
|
||||||
1,
|
|
||||||
/* Buffer dimensions */
|
|
||||||
{ (size_t) src.size() },
|
|
||||||
/* Strides (in bytes) for each index */
|
|
||||||
{ sizeof(Scalar) * static_cast<size_t>(src.innerStride()) }
|
|
||||||
)).release();
|
|
||||||
} else {
|
} else {
|
||||||
return array(buffer_info(
|
return array(
|
||||||
/* Pointer to buffer */
|
{ (size_t) src.rows(), // shape
|
||||||
const_cast<Scalar *>(src.data()),
|
|
||||||
/* Size of one scalar */
|
|
||||||
sizeof(Scalar),
|
|
||||||
/* Python struct-style format descriptor */
|
|
||||||
format_descriptor<Scalar>::format(),
|
|
||||||
/* Number of dimensions */
|
|
||||||
isVector ? 1 : 2,
|
|
||||||
/* Buffer dimensions */
|
|
||||||
{ (size_t) src.rows(),
|
|
||||||
(size_t) src.cols() },
|
(size_t) src.cols() },
|
||||||
/* Strides (in bytes) for each index */
|
{ sizeof(Scalar) * static_cast<size_t>(src.rowStride()), // strides
|
||||||
{ sizeof(Scalar) * static_cast<size_t>(src.rowStride()),
|
sizeof(Scalar) * static_cast<size_t>(src.colStride()) },
|
||||||
sizeof(Scalar) * static_cast<size_t>(src.colStride()) }
|
src.data() // data
|
||||||
)).release();
|
).release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,9 +230,9 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
|
|||||||
!outerIndicesArray.check())
|
!outerIndicesArray.check())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
buffer_info outerIndices = outerIndicesArray.request();
|
auto outerIndices = outerIndicesArray.request();
|
||||||
buffer_info innerIndices = innerIndicesArray.request();
|
auto innerIndices = innerIndicesArray.request();
|
||||||
buffer_info values = valuesArray.request();
|
auto values = valuesArray.request();
|
||||||
|
|
||||||
value = Eigen::MappedSparseMatrix<Scalar, Type::Flags, StorageIndex>(
|
value = Eigen::MappedSparseMatrix<Scalar, Type::Flags, StorageIndex>(
|
||||||
shape[0].cast<Index>(),
|
shape[0].cast<Index>(),
|
||||||
@ -270,50 +252,9 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
|
|||||||
object matrix_type = module::import("scipy.sparse").attr(
|
object matrix_type = module::import("scipy.sparse").attr(
|
||||||
rowMajor ? "csr_matrix" : "csc_matrix");
|
rowMajor ? "csr_matrix" : "csc_matrix");
|
||||||
|
|
||||||
array data(buffer_info(
|
array data((size_t) src.nonZeros(), src.valuePtr());
|
||||||
// Pointer to buffer
|
array outerIndices((size_t) (rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
|
||||||
const_cast<Scalar *>(src.valuePtr()),
|
array innerIndices((size_t) src.nonZeros(), src.innerIndexPtr());
|
||||||
// Size of one scalar
|
|
||||||
sizeof(Scalar),
|
|
||||||
// Python struct-style format descriptor
|
|
||||||
format_descriptor<Scalar>::format(),
|
|
||||||
// Number of dimensions
|
|
||||||
1,
|
|
||||||
// Buffer dimensions
|
|
||||||
{ (size_t) src.nonZeros() },
|
|
||||||
// Strides
|
|
||||||
{ sizeof(Scalar) }
|
|
||||||
));
|
|
||||||
|
|
||||||
array outerIndices(buffer_info(
|
|
||||||
// Pointer to buffer
|
|
||||||
const_cast<StorageIndex *>(src.outerIndexPtr()),
|
|
||||||
// Size of one scalar
|
|
||||||
sizeof(StorageIndex),
|
|
||||||
// Python struct-style format descriptor
|
|
||||||
format_descriptor<StorageIndex>::format(),
|
|
||||||
// Number of dimensions
|
|
||||||
1,
|
|
||||||
// Buffer dimensions
|
|
||||||
{ (size_t) (rowMajor ? src.rows() : src.cols()) + 1 },
|
|
||||||
// Strides
|
|
||||||
{ sizeof(StorageIndex) }
|
|
||||||
));
|
|
||||||
|
|
||||||
array innerIndices(buffer_info(
|
|
||||||
// Pointer to buffer
|
|
||||||
const_cast<StorageIndex *>(src.innerIndexPtr()),
|
|
||||||
// Size of one scalar
|
|
||||||
sizeof(StorageIndex),
|
|
||||||
// Python struct-style format descriptor
|
|
||||||
format_descriptor<StorageIndex>::format(),
|
|
||||||
// Number of dimensions
|
|
||||||
1,
|
|
||||||
// Buffer dimensions
|
|
||||||
{ (size_t) src.nonZeros() },
|
|
||||||
// Strides
|
|
||||||
{ sizeof(StorageIndex) }
|
|
||||||
));
|
|
||||||
|
|
||||||
return matrix_type(
|
return matrix_type(
|
||||||
std::make_tuple(data, innerIndices, outerIndices),
|
std::make_tuple(data, innerIndices, outerIndices),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user