[cig-commits] r13164 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Oct 29 15:11:27 PDT 2008


Author: luis
Date: 2008-10-29 15:11:26 -0700 (Wed, 29 Oct 2008)
New Revision: 13164

Modified:
   cs/cigma/trunk/src/numpy_util.cpp
   cs/cigma/trunk/src/numpy_util.h
Log:
Easier creation of rank 2 arrays with numpy_util::makeArray

Modified: cs/cigma/trunk/src/numpy_util.cpp
===================================================================
--- cs/cigma/trunk/src/numpy_util.cpp	2008-10-29 22:11:25 UTC (rev 13163)
+++ cs/cigma/trunk/src/numpy_util.cpp	2008-10-29 22:11:26 UTC (rev 13164)
@@ -108,6 +108,13 @@
     return extract<numeric::array>(obj);
 }
 
+// Create a two-dimensional numeric array of shape (m,n) and type t
+numeric::array makeArray(intp m, intp n, PyArray_TYPES t=PyArray_DOUBLE)
+{
+    intp dims[2] = {m,n};
+    object obj(handle<>(PyArray_FromDims(2, dims, t)));
+    return extract<numeric::array>(obj);
+}
 
 // Create a numeric array with dimensions dimens and type t
 numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t=PyArray_DOUBLE)

Modified: cs/cigma/trunk/src/numpy_util.h
===================================================================
--- cs/cigma/trunk/src/numpy_util.h	2008-10-29 22:11:25 UTC (rev 13163)
+++ cs/cigma/trunk/src/numpy_util.h	2008-10-29 22:11:26 UTC (rev 13164)
@@ -20,6 +20,7 @@
  */
 boost::python::numeric::array makeArray(boost::python::object x);
 
+
 /**
  * Creates a one-dimensional numpy array of length n and numpy type t.
  * The elements of the array are initialized to zero.
@@ -31,6 +32,17 @@
 
 
 /**
+ * Creates a two-dimensional numpy array of shape (m,n) and numpy type t.
+ * The elements of the array are initialized to zero.
+ * @param m an integer representing the length of the first dimension.
+ * @param n an integer representing the length of the second dimension.
+ * @param t elements' numpy type. Default is double.
+ * @return a numeric array of shape (m,n) with elements initialized to zero.
+ */
+boost::python::numeric::array makeArray(intp m, intp n, PyArray_TYPES t);
+
+
+/**
  * Creates an n-dimensional numpy array with dimensions dimens and numpy
  * type t. The elements of the array are initialized to zero.
  * @param dimens a vector of integers specifying the dimensions of the array.
@@ -75,6 +87,27 @@
 
 
 /**
+ * Function template creates a two-dimensional numpy array of shape (m,n)
+ * containing a copy of data at data*. See numpy_util.cpp::getEnum<T>() for a
+ * list of specializations.
+ * @param T   C type of data
+ * @param T*  data pointer to start of data
+ * @param m   an integer indicating the size of the first dimension in the array.
+ * @param n   an integer indicating the size of the second dimension in the array.
+ * @return a numpy array of shape (m,n) with elements initialized to data.
+ */
+template <typename T>
+boost::python::numeric::array makeArray(T* data, intp m, intp n)
+{
+    intp dims[2] = {m,n};
+    boost::python::object obj(boost::python::handle<>(PyArray_FromDims(2, dims, getEnum<T>())));
+    void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
+    memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n);
+    return boost::python::extract<boost::python::numeric::array>(obj);
+}
+
+
+/**
  * Function template creates an n-dimensional numpy array with
  * dimensions dims containing a copy of values starting at data.
  * See numpy_util.cpp::getEnum<T>() for a list of specializations.



More information about the CIG-COMMITS mailing list