/[escript]/branches/complex/escriptcore/src/WrappedArray.h
ViewVC logotype

Contents of /branches/complex/escriptcore/src/WrappedArray.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5870 - (show annotations)
Thu Jan 14 06:50:47 2016 UTC (3 years, 6 months ago) by jfenwick
File MIME type: text/plain
File size: 5030 byte(s)
Made space in wrapped array for complex values as well.
Note: This code passes existing unit tests (for double values) but the complex functionality is 
_completely_ untested.


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 Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
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
18 /** \file WrappedArray.h */
19
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 <complex>
26
27 namespace escript
28 {
29
30 class WrappedArray
31 {
32 public:
33 typedef std::complex<double> complextype;
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 double getElt() const;
40 double getElt(unsigned int i) const;
41 double getElt(unsigned int i, unsigned int j) const;
42 double getElt(unsigned int i, unsigned int j, unsigned int k) const;
43 double getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
44
45 complextype getEltC() const;
46 complextype getEltC(unsigned int i) const;
47 complextype getEltC(unsigned int i, unsigned int j) const;
48 complextype getEltC(unsigned int i, unsigned int j, unsigned int k) const;
49 complextype 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?
62 escript::DataTypes::ShapeType shape;
63 double scalar_r;
64 complextype scalar_c;
65 mutable double* dat_r; // real data
66 mutable complextype* 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
76 WrappedArray::getRank() const
77 {
78 return rank;
79 }
80
81 inline const DataTypes::ShapeType&
82 WrappedArray::getShape() const
83 {
84 return shape;
85 }
86
87 inline double
88 WrappedArray::getElt() const
89 {
90 return scalar_r;
91 }
92
93
94 inline double
95 WrappedArray::getElt(unsigned int i) const
96 { // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
97 if (iscomplex)
98 {
99 return nan("");
100 }
101 return (dat_r!=0)?dat_r[i]:(boost::python::extract<double>(obj[i].attr("__float__")()));
102 }
103
104 inline
105 double
106 WrappedArray::getElt(unsigned int i, unsigned int j) const
107 {
108 if (iscomplex)
109 {
110 return nan("");
111 }
112 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<double>(obj[i][j].attr("__float__")()));
113 }
114
115 inline
116 double
117 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
118 {
119 if (iscomplex)
120 {
121 return nan("");
122 }
123 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<double>(obj[i][j][k].attr("__float__")()));
124 }
125
126 inline
127 double
128 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
129 {
130 if (iscomplex)
131 {
132 return nan("");
133 }
134 return (dat_r!=0)?dat_r[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<double>(obj[i][j][k][m].attr("__float__")()));
135 }
136
137
138
139
140
141 inline WrappedArray::complextype
142 WrappedArray::getEltC() const
143 {
144 return scalar_c;
145 }
146
147
148 inline WrappedArray::complextype
149 WrappedArray::getEltC(unsigned int i) const
150 {
151 if (!iscomplex)
152 {
153 return nan("");
154 }
155 return (dat_c!=0)?dat_c[i]:(boost::python::extract<complextype>(obj[i])); // don't know if this will work with numpy
156 }
157
158 inline
159 WrappedArray::complextype
160 WrappedArray::getEltC(unsigned int i, unsigned int j) const
161 {
162 if (!iscomplex)
163 {
164 return nan("");
165 }
166 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<complextype>(obj[i][j]));
167 }
168
169 inline
170 WrappedArray::complextype
171 WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k) const
172 {
173 if (!iscomplex)
174 {
175 return nan("");
176 }
177 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<complextype>(obj[i][j][k]));
178 }
179
180 inline
181 WrappedArray::complextype
182 WrappedArray::getEltC(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
183 {
184 if (!iscomplex)
185 {
186 return nan("");
187 }
188 return (dat_c!=0)?dat_c[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<complextype>(obj[i][j][k][m]));
189 }
190
191
192
193
194
195 }
196
197 #endif
198

  ViewVC Help
Powered by ViewVC 1.1.26