escript Revision_
WrappedArray.h
Go to the documentation of this file.
1
2/*****************************************************************************
3*
4* Copyright (c) 2003-2020 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-2017 by Centre for Geoscience Computing (GeoComp)
14* Development from 2019 by School of Earth and Environmental Sciences
15**
16*****************************************************************************/
17
18
21#ifndef WrappedArray_20081202_H
22#define WrappedArray_20081202_H
23#include "system_dep.h"
24#include "DataTypes.h"
25#include "boost/python/extract.hpp"
26#include "boost/python/object.hpp"
27#include <complex>
28
29namespace escript
30{
31
33{
34public:
35 WrappedArray(const boost::python::object& obj_in);
37 unsigned int getRank() const;
38 const DataTypes::ShapeType& getShape() const;
39 bool isComplex() const;
40 DataTypes::real_t getElt() const;
41 DataTypes::real_t getElt(unsigned int i) const;
42 DataTypes::real_t getElt(unsigned int i, unsigned int j) const;
43 DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k) const;
44 DataTypes::real_t getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
45
46 DataTypes::cplx_t getEltC() const;
47 DataTypes::cplx_t getEltC(unsigned int i) const;
48 DataTypes::cplx_t getEltC(unsigned int i, unsigned int j) const;
49 DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k) const;
50 DataTypes::cplx_t getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
51
52
53 void convertArray() const;
54private:
55 void convertArrayR() const;
56 void convertArrayC() const;
57 template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
58 template<typename T> void convertNumpyArrayC(const T* array, const std::vector<int>& strides) const;
59 const boost::python::object& obj;
60 int rank;
61 mutable bool converted; // has the array been converted to a C array
62 bool iscomplex; // is the wrapped array storing complex values?
66 mutable DataTypes::real_t* dat_r; // real data
67 mutable DataTypes::cplx_t* dat_c; // complex data - only one of these members should be used
68};
69
70
71inline bool WrappedArray::isComplex() const
72{
73 return iscomplex;
74}
75
76inline unsigned int
78{
79 return rank;
80}
81
82inline const DataTypes::ShapeType&
84{
85 return shape;
86}
87
90{
91 if (iscomplex)
92 {
93 return nan("");
94 }
95 return scalar_r;
96}
97
98
100WrappedArray::getElt(unsigned int i) const
101{ // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
102 if (iscomplex)
103 {
104 return nan("");
105 }
106 return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i].attr("__float__")()));
107}
108
109inline
111WrappedArray::getElt(unsigned int i, unsigned int j) const
112{
113 if (iscomplex)
114 {
115 return nan("");
116 }
117 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j].attr("__float__")()));
118}
119
120inline
122WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
123{
124 if (iscomplex)
125 {
126 return nan("");
127 }
128 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k].attr("__float__")()));
129}
130
131inline
133WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
134{
135 if (iscomplex)
136 {
137 return nan("");
138 }
139 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__")()));
140}
141
142
143
144
145
148{
149 if (!iscomplex)
150 {
151 return scalar_r;
152 }
153 return scalar_c;
154}
155
156
158WrappedArray::getEltC(unsigned int i) const
159{
160 if (!iscomplex) // let's try to get a real value out instead
161 {
162 return (dat_r!=0)?dat_r[i]:(boost::python::extract<DataTypes::real_t>(obj[i]));
163 }
164 return (dat_c!=0)?dat_c[i]:(boost::python::extract<DataTypes::cplx_t>(obj[i])); // don't know if this will work with numpy
165}
166
167inline
169WrappedArray::getEltC(unsigned int i, unsigned int j) const
170{
171 if (!iscomplex)
172 {
173 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::real_t>(obj[i][j]));
174 }
175 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j]));
176}
177
178inline
180WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k) const
181{
182 if (!iscomplex)
183 {
184 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k]));
185 }
186 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k]));
187}
188
189inline
191WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
192{
193 if (!iscomplex)
194 {
195 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::real_t>(obj[i][j][k][m]));
196 }
197 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<DataTypes::cplx_t>(obj[i][j][k][m]));
198}
199
200
201
202
203
204}
205
206#endif
207
Definition WrappedArray.h:33
DataTypes::real_t * dat_r
Definition WrappedArray.h:66
bool converted
Definition WrappedArray.h:61
bool isComplex() const
Definition WrappedArray.h:71
DataTypes::cplx_t scalar_c
Definition WrappedArray.h:65
DataTypes::real_t scalar_r
Definition WrappedArray.h:64
const boost::python::object & obj
Definition WrappedArray.h:59
bool iscomplex
Definition WrappedArray.h:62
DataTypes::real_t getElt() const
Definition WrappedArray.h:89
unsigned int getRank() const
Definition WrappedArray.h:77
DataTypes::cplx_t * dat_c
Definition WrappedArray.h:67
const DataTypes::ShapeType & getShape() const
Definition WrappedArray.h:83
escript::DataTypes::ShapeType shape
Definition WrappedArray.h:63
int rank
Definition WrappedArray.h:60
DataTypes::cplx_t getEltC() const
Definition WrappedArray.h:147
#define ESCRIPT_DLL_API
Definition escriptcore/src/system_dep.h:30
std::complex< real_t > cplx_t
complex data type
Definition DataTypes.h:55
double real_t
type of all real-valued scalars in escript
Definition DataTypes.h:52
std::vector< int > ShapeType
The shape of a single datapoint.
Definition DataTypes.h:44
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:240
Definition AbstractContinuousDomain.cpp:23