escript  Revision_
WrappedArray.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16 
17 
20 #ifndef WrappedArray_20081202_H
21 #define WrappedArray_20081202_H
22 #include "system_dep.h"
23 #include "DataTypes.h"
24 #include "boost/python/extract.hpp"
25 #include "boost/python/object.hpp"
26 #include <complex>
27 
28 namespace escript
29 {
30 
32 {
33 public:
34  WrappedArray(const boost::python::object& obj_in);
35  ~WrappedArray();
36  unsigned int getRank() const;
37  const DataTypes::ShapeType& getShape() const;
38  bool isComplex() const;
39  DataTypes::real_t getElt() const;
40  DataTypes::real_t getElt(unsigned int i) const;
41  DataTypes::real_t getElt(unsigned int i, unsigned int j) const;
42  DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k) const;
43  DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
44 
45  DataTypes::cplx_t getEltC() const;
46  DataTypes::cplx_t getEltC(unsigned int i) const;
47  DataTypes::cplx_t getEltC(unsigned int i, unsigned int j) const;
48  DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k) const;
49  DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
50 
51 
52  void convertArray() const;
53 private:
54  void convertArrayR() const;
55  void convertArrayC() const;
56  template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
57  template<typename T> void convertNumpyArrayC(const T* array, const std::vector<int>& strides) const;
58  const boost::python::object& obj;
59  int rank;
60  mutable bool converted; // has the array been converted to a C array
61  bool iscomplex; // is the wrapped array storing complex values?
65  mutable DataTypes::real_t* dat_r; // real data
66  mutable DataTypes::cplx_t* dat_c; // complex data - only one of these members should be used
67 };
68 
69 
70 inline bool WrappedArray::isComplex() const
71 {
72  return iscomplex;
73 }
74 
75 inline unsigned int
77 {
78  return rank;
79 }
80 
81 inline const DataTypes::ShapeType&
83 {
84  return shape;
85 }
86 
87 inline DataTypes::real_t
89 {
90  if (iscomplex)
91  {
92  return nan("");
93  }
94  return scalar_r;
95 }
96 
97 
98 inline DataTypes::real_t
99 WrappedArray::getElt(unsigned int i) const
100 { // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
101  if (iscomplex)
102  {
103  return nan("");
104  }
105  return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i].attr("__float__")()));
106 }
107 
108 inline
110 WrappedArray::getElt(unsigned int i, unsigned int j) const
111 {
112  if (iscomplex)
113  {
114  return nan("");
115  }
116  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j].attr("__float__")()));
117 }
118 
119 inline
121 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
122 {
123  if (iscomplex)
124  {
125  return nan("");
126  }
127  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k].attr("__float__")()));
128 }
129 
130 inline
132 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
133 {
134  if (iscomplex)
135  {
136  return nan("");
137  }
138  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k][m].attr("__float__")()));
139 }
140 
141 
142 
143 
144 
145 inline DataTypes::cplx_t
147 {
148  if (!iscomplex)
149  {
150  return scalar_r;
151  }
152  return scalar_c;
153 }
154 
155 
156 inline DataTypes::cplx_t
157 WrappedArray::getEltC(unsigned int i) const
158 {
159  if (!iscomplex) // let's try to get a real value out instead
160  {
161  return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i]));
162  }
163  return (dat_c!=0)?dat_c[i]:(boost::python::extract<DataTypes::cplx_t>(obj[i])); // don't know if this will work with numpy
164 }
165 
166 inline
168 WrappedArray::getEltC(unsigned int i, unsigned int j) const
169 {
170  if (!iscomplex)
171  {
172  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j]));
173  }
174  return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j]));
175 }
176 
177 inline
179 WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k) const
180 {
181  if (!iscomplex)
182  {
183  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k]));
184  }
185  return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k]));
186 }
187 
188 inline
190 WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
191 {
192  if (!iscomplex)
193  {
194  return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k][m]));
195  }
196  return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k][m]));
197 }
198 
199 
200 
201 
202 
203 }
204 
205 #endif
206 
DataTypes::cplx_t getEltC() const
Definition: WrappedArray.h:146
escript::DataTypes::ShapeType shape
Definition: WrappedArray.h:62
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:82
bool isComplex() const
Definition: WrappedArray.h:70
Definition: AbstractContinuousDomain.cpp:22
void convertArrayR() const
Definition: WrappedArray.cpp:416
DataTypes::real_t getElt() const
Definition: WrappedArray.h:88
DataTypes::cplx_t * dat_c
Definition: WrappedArray.h:66
vec_size_type getRelIndex(const DataTypes::ShapeType &shape, vec_size_type i)
Compute the offset (in 1D vector) of a given subscript with a shape.
Definition: DataTypes.h:233
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:42
int rank
Definition: WrappedArray.h:59
unsigned int getRank() const
Definition: WrappedArray.h:76
void convertArray() const
Definition: WrappedArray.cpp:530
WrappedArray(const boost::python::object &obj_in)
Definition: WrappedArray.cpp:152
void convertArrayC() const
Definition: WrappedArray.cpp:473
bool converted
Definition: WrappedArray.h:60
const boost::python::object & obj
Definition: WrappedArray.h:58
~WrappedArray()
Definition: WrappedArray.cpp:542
DataTypes::real_t scalar_r
Definition: WrappedArray.h:63
void convertNumpyArray(const T *array, const std::vector< int > &strides) const
Definition: WrappedArray.cpp:358
bool iscomplex
Definition: WrappedArray.h:61
DataTypes::cplx_t scalar_c
Definition: WrappedArray.h:64
std::complex< real_t > cplx_t
complex data type
Definition: DataTypes.h:53
DataTypes::real_t * dat_r
Definition: WrappedArray.h:65
void convertNumpyArrayC(const T *array, const std::vector< int > &strides) const
Definition: WrappedArray.cpp:299
Definition: WrappedArray.h:31
double real_t
type of all real-valued scalars in escript
Definition: DataTypes.h:50