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 
26 namespace escript
27 {
28 
30 {
31 public:
32  WrappedArray(const boost::python::object& obj_in);
33  ~WrappedArray();
34  unsigned int getRank() const;
35  const DataTypes::ShapeType& getShape() const;
36  double getElt() const;
37  double getElt(unsigned int i) const;
38  double getElt(unsigned int i, unsigned int j) const;
39  double getElt(unsigned int i, unsigned int j, unsigned int k) const;
40  double getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
41  void convertArray() const;
42 private:
43  template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
44  const boost::python::object& obj;
45  int rank;
47  double m_scalar;
48  mutable double* dat;
49 };
50 
51 inline unsigned int
53 {
54  return rank;
55 }
56 
57 inline const DataTypes::ShapeType&
59 {
60  return shape;
61 }
62 
63 inline double
65 {
66  return m_scalar;
67 }
68 
69 
70 inline double
71 WrappedArray::getElt(unsigned int i) const
72 { // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
73  return (dat!=0)?dat[i]:(boost::python::extract<double>(obj[i].attr("__float__")()));
74 }
75 
76 inline
77 double
78 WrappedArray::getElt(unsigned int i, unsigned int j) const
79 {
80  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<double>(obj[i][j].attr("__float__")()));
81 }
82 
83 inline
84 double
85 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
86 {
87  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<double>(obj[i][j][k].attr("__float__")()));
88 }
89 
90 inline
91 double
92 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
93 {
94  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<double>(obj[i][j][k][m].attr("__float__")()));
95 }
96 
97 }
98 
99 #endif
100 
escript::DataTypes::ShapeType shape
Definition: WrappedArray.h:46
Definition: AbstractContinuousDomain.cpp:24
void convertNumpyArray(const T *array, const std::vector< int > &strides) const
Definition: WrappedArray.cpp:188
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:38
int rank
Definition: WrappedArray.h:45
WrappedArray(const boost::python::object &obj_in)
Definition: WrappedArray.cpp:84
unsigned int getRank() const
Definition: WrappedArray.h:52
double * dat
Definition: WrappedArray.h:48
DataTypes::ValueType::size_type getRelIndex(const DataTypes::ShapeType &shape, DataTypes::ValueType::size_type i)
Compute the offset (in 1D vector) of a given subscript with a shape.
Definition: DataTypes.h:183
double getElt() const
Definition: WrappedArray.h:64
const boost::python::object & obj
Definition: WrappedArray.h:44
~WrappedArray()
Definition: WrappedArray.cpp:300
double m_scalar
Definition: WrappedArray.h:47
void convertArray() const
Definition: WrappedArray.cpp:245
const DataTypes::ShapeType & getShape() const
Definition: WrappedArray.h:58
Definition: WrappedArray.h:29