1 |
/* |
2 |
****************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
****************************************************************************** |
13 |
*/ |
14 |
|
15 |
#include "DataArray.h" |
16 |
|
17 |
using namespace boost::python; |
18 |
using namespace std; |
19 |
|
20 |
namespace escript { |
21 |
|
22 |
DataArray::DataArray(double value) |
23 |
{ |
24 |
m_data.resize(1,value,1); |
25 |
// create a view with an empty shape, a scalar. |
26 |
m_dataView.reset(new DataArrayView(m_data,DataArrayView::ShapeType())); |
27 |
} |
28 |
|
29 |
DataArray::DataArray(const DataArrayView::ShapeType& shape, |
30 |
double value) |
31 |
{ |
32 |
int len = DataArrayView::noValues(shape); |
33 |
m_data.resize(len,value,len); |
34 |
m_dataView.reset(new DataArrayView(m_data,shape)); |
35 |
} |
36 |
|
37 |
DataArray::DataArray(const DataArray& value): |
38 |
m_data(value.getData()) |
39 |
{ |
40 |
m_dataView.reset(new DataArrayView(m_data,value.getView().getShape())); |
41 |
} |
42 |
|
43 |
DataArray::DataArray(const DataArrayView& value): |
44 |
m_data(value.getData()) |
45 |
{ |
46 |
m_dataView.reset(new DataArrayView(m_data,value.getShape())); |
47 |
} |
48 |
|
49 |
DataArray::DataArray(const object& value) |
50 |
{ |
51 |
// this will throw if the value cannot be represented |
52 |
numeric::array asNumArray(value); |
53 |
initialise(asNumArray); |
54 |
} |
55 |
|
56 |
DataArray::DataArray(const boost::python::numeric::array& value) |
57 |
{ |
58 |
initialise(value); |
59 |
} |
60 |
|
61 |
void |
62 |
DataArray::initialise(const boost::python::numeric::array& value) |
63 |
{ |
64 |
// extract the shape of the numarray |
65 |
DataArrayView::ShapeType tempShape; |
66 |
for (int i=0; i<value.getrank(); i++) { |
67 |
tempShape.push_back(extract<int>(value.getshape()[i])); |
68 |
} |
69 |
// allocate the space for the data vector |
70 |
int len = DataArrayView::noValues(tempShape); |
71 |
m_data.resize(len,0.,len); |
72 |
// create a view with the same shape |
73 |
m_dataView.reset(new DataArrayView(m_data,tempShape)); |
74 |
// fill the data vector with the values from the numarray |
75 |
m_dataView->copy(value); |
76 |
} |
77 |
|
78 |
const DataArrayView& |
79 |
DataArray::getView() const |
80 |
{ |
81 |
return *m_dataView; |
82 |
} |
83 |
|
84 |
DataArrayView& |
85 |
DataArray::getView() |
86 |
{ |
87 |
return *m_dataView; |
88 |
} |
89 |
|
90 |
const DataArrayView::ValueType& |
91 |
DataArray::getData() const |
92 |
{ |
93 |
return m_data; |
94 |
} |
95 |
|
96 |
DataArrayView::ValueType& |
97 |
DataArray::getData() |
98 |
{ |
99 |
return m_data; |
100 |
} |
101 |
|
102 |
} // end of namespace |