escript
Revision_
|
00001 00002 /***************************************************************************** 00003 * 00004 * Copyright (c) 2003-2014 by University of Queensland 00005 * http://www.uq.edu.au 00006 * 00007 * Primary Business: Queensland, Australia 00008 * Licensed under the Open Software License version 3.0 00009 * http://www.opensource.org/licenses/osl-3.0.php 00010 * 00011 * Development until 2012 by Earth Systems Science Computational Center (ESSCC) 00012 * Development 2012-2013 by School of Earth Sciences 00013 * Development from 2014 by Centre for Geoscience Computing (GeoComp) 00014 * 00015 *****************************************************************************/ 00016 00017 00020 #ifndef WrappedArray_20081202_H 00021 #define WrappedArray_20081202_H 00022 #include "system_dep.h" 00023 #include "DataTypes.h" 00024 #include "boost/python/extract.hpp" 00025 00026 namespace escript 00027 { 00028 00029 class WrappedArray 00030 { 00031 public: 00032 WrappedArray(const boost::python::object& obj_in); 00033 ~WrappedArray(); 00034 unsigned int getRank() const; 00035 const DataTypes::ShapeType& getShape() const; 00036 double getElt() const; 00037 double getElt(unsigned int i) const; 00038 double getElt(unsigned int i, unsigned int j) const; 00039 double getElt(unsigned int i, unsigned int j, unsigned int k) const; 00040 double getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const; 00041 void convertArray() const; 00042 private: 00043 template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const; 00044 const boost::python::object& obj; 00045 int rank; 00046 escript::DataTypes::ShapeType shape; 00047 double m_scalar; 00048 mutable double* dat; 00049 }; 00050 00051 inline unsigned int 00052 WrappedArray::getRank() const 00053 { 00054 return rank; 00055 } 00056 00057 inline const DataTypes::ShapeType& 00058 WrappedArray::getShape() const 00059 { 00060 return shape; 00061 } 00062 00063 inline double 00064 WrappedArray::getElt() const 00065 { 00066 return m_scalar; 00067 } 00068 00069 00070 inline double 00071 WrappedArray::getElt(unsigned int i) const 00072 { // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter 00073 return (dat!=0)?dat[i]:(boost::python::extract<double>(obj[i].attr("__float__")())); 00074 } 00075 00076 inline 00077 double 00078 WrappedArray::getElt(unsigned int i, unsigned int j) const 00079 { 00080 return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<double>(obj[i][j].attr("__float__")())); 00081 } 00082 00083 inline 00084 double 00085 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const 00086 { 00087 return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<double>(obj[i][j][k].attr("__float__")())); 00088 } 00089 00090 inline 00091 double 00092 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const 00093 { 00094 return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<double>(obj[i][j][k][m].attr("__float__")())); 00095 } 00096 00097 } 00098 00099 #endif 00100