CXX/Array.h's Py::Array samples or documentation?
Phil Austin
phil at geog.ubc.ca
Mon Jan 8 19:57:55 EST 2001
Phlip <phlip_cpp at my-deja.com> writes:
> Pythonesqueise:
[snip]
>
> So I think I can allocate a new array of the correct dimensions; pack it,
> and assign it into the target array. But the example code for Py::Array
> only shows how to create it with one dimension.
Although I'm sure there are better ways to do this,
here's a hacked version of CXX_Array.h that initializes a
multi-dimensional Py::Array using an stl vector of dimensions
(search on the string "fromVector" to see the changes)
#ifndef CXX_ARRAY_H__
#define CXX_ARRAY_H__
#include "CXX_Objects.h"
#include "Numeric/arrayobject.h"
#include <vector>
namespace Py {
class Array: public Sequence
{
public:
virtual bool accepts (PyObject *pyob) const {
return pyob && PyArray_Check (pyob);
}
explicit Array (PyObject *pyob, bool owned = false): Sequence(pyob, owned) {
validate();
}
Array(const Object& other): Sequence(*other) {
validate();
}
Array& operator= (const Object& rhs) {
return (*this = *rhs);
}
Array& operator= (PyObject* rhsp) {
if(ptr() == rhsp) return *this;
set(rhsp);
return *this;
}
explicit Array (int n=0, PyArray_TYPES t = PyArray_DOUBLE)
: Sequence(PyArray_FromDims(1, &n, t), true) {
validate();
}
explicit Array (std::vector<int> dimens, PyArray_TYPES t = PyArray_DOUBLE)
: Sequence(fromVector(dimens,t), true) {
validate();
}
Array clone() const {
PyObject *p = PyArray_CopyFromObject(ptr(), species(), rank(), rank());
return Array(p, true);
}
int species() const {
return PyArray_ObjectType(ptr(), 0);
}
int rank() const {
return ((PyArrayObject*) ptr())->nd;
}
int dimension(int i) const {
if (1 <= i && i <= rank()) {
return ((PyArrayObject*) ptr())->dimensions[i-1];
} else {
return 1;
}
}
int is_contiguous() const {
return PyArray_ISCONTIGUOUS ((PyArrayObject*) ptr());
}
char* to_C() const {
return ((PyArrayObject*) ptr())->data;
}
Array as_contiguous() {
if (is_contiguous()) return Array(ptr());
return Array((PyObject*)PyArray_ContiguousFromObject(ptr(), species(), 1, 0), true);
}
private:
PyObject* fromVector(const std::vector<int>& dimens, const PyArray_TYPES& t){
int ndims=dimens.size();
return (PyObject*) PyArray_FromDims(ndims, (int*) dimens.begin(), t);
}
};
// the recommended species if converting to array
int species(const Object& ob) {
return PyArray_ObjectType(*ob, 0);
}
int species(PyObject *pyob) {
return PyArray_ObjectType(pyob, 0);
}
Array toArray(PyObject* p) {
int t = species(p);
if (t < 0) {
throw RuntimeError("Unsuitable object for toArray");
}
return Array(PyArray_CopyFromObject(p, t, 1, 0), true);
}
Array toArray(const Object&ob) {
return toArray(*ob);
}
} // end namespace Py
#endif
Regards, Phil
More information about the Python-list
mailing list