21 #include <boost/python/object.hpp>
24 namespace py = boost::python;
25 namespace np = boost::python::numpy;
26 namespace nm = boost::python::numeric;
30 void print(
const T &x, std::ostream &os)
36 void print<char>(
const char &x, std::ostream &os)
42 void print<PyObject*>(PyObject*
const &x, std::ostream &os)
46 py::object p(py::borrowed(x));
47 std::string s = py::extract<std::string>(py::str(p));
54 void printElements(
const np::arrayt<T> &a,
int dim,
int *indices)
60 while (i<a.rank()-1) cout << indices[i++] <<
',';
61 cout << indices[i] <<
") = ";
62 print(a(indices), cout);
67 const int n = a.shape()[dim];
68 for (indices[dim]=0; indices[dim]<n; ++indices[dim])
70 printElements(a, dim+1, indices);
78 if (np::getItemtype<T>() == arr.itemtype())
80 cout <<
"getItemtype<" <<
typeid(T).name() <<
"> matches type of array" << endl;
82 if (np::isCompatibleType<T>(arr.itemtype()))
86 cout <<
"Printing array of type " <<
typeid(T).name() << endl;
87 int indices[np::MAX_DIM];
92 cout <<
"mapped c++ type = " <<
typeid(T).name() << endl;
100 cout <<
"rank() = " << arr.rank() << endl;
101 cout <<
"itemtype() = " << arr.itemtype() << endl;
102 cout <<
"itemsize() = " << arr.itemsize() << endl;
103 cout <<
"isWriteable() = " << arr.isWriteable() << endl;
104 cout <<
"isCContiguous() = " << arr.isCContiguous() << endl;
105 cout <<
"isFContiguous() = " << arr.isFContiguous() << endl;
106 cout <<
"shape() = ";
107 for (i=0; i<arr.rank()-1; ++i)
108 cout << arr.shape()[i] <<
',';
109 cout << arr.shape()[i] << endl;
110 cout <<
"strides() = ";
111 for (i=0; i<arr.rank()-1; ++i)
112 cout << arr.
strides()[i] <<
',';
113 cout << arr.
strides()[i] << endl;
117 void printArray(nm::array pyarr,
bool printContents)
122 handleType<PyObject*>(arr, printContents);
123 handleType<float>(arr, printContents);
124 handleType<double>(arr, printContents);
126 handleType<bool>(arr, printContents);
127 handleType<int>(arr, printContents);
128 handleType<short>(arr, printContents);
129 handleType<char>(arr, printContents);
130 handleType<long>(arr, printContents);
131 handleType<long long>(arr, printContents);
133 handleType<unsigned int>(arr, printContents);
134 handleType<unsigned short>(arr, printContents);
135 handleType<unsigned char>(arr, printContents);
136 handleType<unsigned long>(arr, printContents);
137 handleType<unsigned long long>(arr, printContents);
145 handleType<T>(arr,
true);
149 std::string scalar_to_str(
const T &x)
151 std::ostringstream os;
157 std::string scalar_to_str(
const char &x)
159 std::ostringstream os;
169 for (
int i=0; i<dims; ++i)
180 for (
int i=0; i<dims[0]; ++i)
182 ret(i) = arr1(i) + arr2(i);
184 return ret.getObject();
187 py::object SumNumericArray(nm::array arr1, nm::array arr2)
189 const np::ssize_t len = py::extract<int>(py::getattr(arr1,
"shape")[0]);
191 for (
int i=0; i<len; ++i)
193 float x1 = py::extract<float>(arr1[i]);
194 float x2 = py::extract<float>(arr2[i]);
195 ret[i] = py::object(x1 + x2);
202 BOOST_PYTHON_MODULE(libdemo)
205 py::def(
"printArray", printArray);
206 py::def(
"printConvertedArray_int", printConvertedArray<int>);
207 py::def(
"char_to_str", scalar_to_str<char>);
208 py::def(
"int_to_str", scalar_to_str<int>);
209 py::def(
"float_to_str", scalar_to_str<float>);
210 py::def(
"double_to_str", scalar_to_str<double>);
211 py::def(
"SumArrayT", SumArrayT);
212 py::def(
"SumNumericArray", SumNumericArray);
213 py::def(
"ReturnedFromCPP", ReturnedFromCPP);
int getItemtype(const object &a)
Obtain numpy's data type number from an boost-python object which should hold a ndarray.
This is the base class from which type specific variants are derived.
Py_ssize_t ssize_t
Synonymous for Py_ssize_t.
object empty(int rank, const Py_ssize_t *dims, int type)
Create a new ndarray with uninitialized memory.
void importNumpyAndRegisterTypes()
Initializes things.
This class defines operators () and [] to allow for direct memory access to array elements of type T...
const ssize_t * strides() const
Returned in number of bytes.