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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5504 - (show annotations)
Wed Mar 4 22:58:13 2015 UTC (4 years, 1 month ago) by jfenwick
File MIME type: text/plain
File size: 2666 byte(s)
Again with a more up to date copy


1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2015 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 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
26 namespace escript
27 {
28
29 class WrappedArray
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;
46 escript::DataTypes::ShapeType shape;
47 double m_scalar;
48 mutable double* dat;
49 };
50
51 inline unsigned int
52 WrappedArray::getRank() const
53 {
54 return rank;
55 }
56
57 inline const DataTypes::ShapeType&
58 WrappedArray::getShape() const
59 {
60 return shape;
61 }
62
63 inline double
64 WrappedArray::getElt() const
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

  ViewVC Help
Powered by ViewVC 1.1.26