ESScript  Revision_4488
WrappedArray.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by 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 since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15 
16 
19 #ifndef WrappedArray_20081202_H
20 #define WrappedArray_20081202_H
21 #include "system_dep.h"
22 #include "DataTypes.h"
23 #include "boost/python/extract.hpp"
24 
25 namespace escript
26 {
27 
29 {
30 public:
31  WrappedArray(const boost::python::object& obj_in);
32  ~WrappedArray();
33  unsigned int getRank() const;
34  const DataTypes::ShapeType& getShape() const;
35  double getElt() const;
36  double getElt(unsigned int i) const;
37  double getElt(unsigned int i, unsigned int j) const;
38  double getElt(unsigned int i, unsigned int j, unsigned int k) const;
39  double getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
40  void convertArray() const;
41 private:
42  template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
43  const boost::python::object& obj;
44  int rank;
46  double m_scalar;
47  mutable double* dat;
48 };
49 
50 inline unsigned int
52 {
53  return rank;
54 }
55 
56 inline const DataTypes::ShapeType&
58 {
59  return shape;
60 }
61 
62 inline double
64 {
65  return m_scalar;
66 }
67 
68 
69 inline double
70 WrappedArray::getElt(unsigned int i) const
71 { // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
72  return (dat!=0)?dat[i]:(boost::python::extract<double>(obj[i].attr("__float__")()));
73 }
74 
75 inline
76 double
77 WrappedArray::getElt(unsigned int i, unsigned int j) const
78 {
79  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<double>(obj[i][j].attr("__float__")()));
80 }
81 
82 inline
83 double
84 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
85 {
86  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<double>(obj[i][j][k].attr("__float__")()));
87 }
88 
89 inline
90 double
91 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
92 {
93  return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<double>(obj[i][j][k][m].attr("__float__")()));
94 }
95 
96 }
97 
98 #endif
99