mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
added as_matrix call policy
This commit is contained in:
@@ -52,6 +52,20 @@ public:
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief CallPolicies that causes a function that returns a numpy.ndarray to
|
||||
* return a numpy.matrix instead.
|
||||
*/
|
||||
template <typename Base = python::default_call_policies>
|
||||
struct as_matrix : Base {
|
||||
static PyObject * postcall(PyObject *, PyObject * result) {
|
||||
python::object a = python::object(python::handle<>(result));
|
||||
numpy::matrix m(a, false);
|
||||
Py_INCREF(m.ptr());
|
||||
return m.ptr();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace boost::numpy
|
||||
namespace python
|
||||
{
|
||||
|
||||
@@ -16,6 +16,16 @@ class TestNdarray(unittest.TestCase):
|
||||
self.assertEqual(shape,a1.shape)
|
||||
self.assert_((a1 == a2).all())
|
||||
|
||||
def testNdzeros_matrix(self):
|
||||
for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
|
||||
dt = numpy.dtype(dtp)
|
||||
shape = (6, 10)
|
||||
a1 = ndarray_mod.zeros_matrix(shape, dt)
|
||||
a2 = numpy.matrix(numpy.zeros(shape, dtype=dtp))
|
||||
self.assertEqual(shape,a1.shape)
|
||||
self.assert_((a1 == a2).all())
|
||||
self.assertEqual(type(a1), type(a2))
|
||||
|
||||
def testNdarray(self):
|
||||
a = range(0,60)
|
||||
for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
|
||||
|
||||
@@ -24,10 +24,12 @@ np::ndarray c_empty(p::tuple shape, np::dtype dt)
|
||||
np::ndarray transpose(np::ndarray arr) { return arr.transpose();}
|
||||
np::ndarray squeeze(np::ndarray arr) { return arr.squeeze();}
|
||||
np::ndarray reshape(np::ndarray arr,p::tuple tup) { return arr.reshape(tup);}
|
||||
|
||||
BOOST_PYTHON_MODULE(ndarray_mod)
|
||||
{
|
||||
np::initialize();
|
||||
p::def("zeros", zeros);
|
||||
p::def("zeros_matrix", zeros, np::as_matrix<>());
|
||||
p::def("array", array2);
|
||||
p::def("array", array1);
|
||||
p::def("empty", empty1);
|
||||
|
||||
Reference in New Issue
Block a user