/[escript]/trunk/escript/src/DataArrayView.cpp
ViewVC logotype

Annotation of /trunk/escript/src/DataArrayView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (hide annotations)
Fri Apr 1 05:48:57 2005 UTC (14 years, 6 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataArrayView.cpp
File size: 26526 byte(s)
*** empty log message ***

1 jgs 82 // $Id$
2    
3     /*
4     ******************************************************************************
5     * *
6     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
7     * *
8     * This software is the property of ACcESS. No part of this code *
9     * may be copied in any form or by any means without the expressed written *
10     * consent of ACcESS. Copying, use or modification of this software *
11     * by any unauthorised person is illegal unless that person has a software *
12     * license agreement with ACcESS. *
13     * *
14     ******************************************************************************
15     */
16    
17     #include "escript/Data/DataArrayView.h"
18     #include "escript/Data/DataException.h"
19    
20     #include <sstream>
21     #include <vector>
22     #include <iostream>
23     #include <boost/python/extract.hpp>
24     #include <boost/python/object.hpp>
25    
26     using namespace std;
27     using namespace boost::python;
28    
29     namespace escript {
30    
31     DataArrayView::DataArrayView():
32     m_shape(),
33 jgs 108 m_offset(0),
34     m_noValues(0)
35 jgs 82 {
36     m_data=0;
37     }
38    
39     DataArrayView::DataArrayView(ValueType& data,
40     const ShapeType& viewShape,
41     int offset):
42     m_data(&data),
43     m_shape(viewShape),
44     m_offset(offset),
45     m_noValues(noValues(viewShape))
46     {
47     //
48 jgs 108 // check the shape rank and size and throw an exception if a
49     // shape incompatible with the given data array is specified
50 jgs 82 if (viewShape.size() > m_maxRank) {
51     stringstream temp;
52 jgs 108 temp << "Error- Couldn't construct DataArrayView, invalid rank."
53     << "Input data rank: " << viewShape.size() << " "
54     << "Maximum rank allowed for DataArrayView: " << m_maxRank;
55 jgs 82 throw DataException(temp.str());
56     }
57 jgs 108 if (m_data->size() < (m_offset+noValues())) {
58 jgs 82 stringstream temp;
59 jgs 108 temp << "Error- Couldn't construct DataArrayView, insufficient data values. "
60     << "Shape requires: " << noValues()+m_offset << " values."
61     << "Data values available: " << m_data->size();
62 jgs 82 throw DataException(temp.str());
63     }
64     }
65    
66     DataArrayView::DataArrayView(const DataArrayView& other):
67     m_data(other.m_data),
68     m_offset(other.m_offset),
69     m_shape(other.m_shape),
70     m_noValues(other.m_noValues)
71     {
72     }
73    
74     bool
75     DataArrayView::isEmpty() const
76     {
77 jgs 108 return (m_data==0);
78 jgs 82 }
79    
80     void
81     DataArrayView::copy(const boost::python::numeric::array& value)
82     {
83 jgs 117 ShapeType tempShape;
84 jgs 108 for (int i=0; i<value.getrank(); i++) {
85 jgs 82 tempShape.push_back(extract<int>(value.getshape()[i]));
86     }
87 jgs 108
88 jgs 82 EsysAssert((!isEmpty()&&checkShape(tempShape)),
89     createShapeErrorMessage("Error - Couldn't copy due to shape mismatch.",tempShape));
90    
91     if (value.getrank()==0) {
92     (*this)()=extract<double>(value[value.getshape()]);
93     } else if (value.getrank()==1) {
94     for (ValueType::size_type i=0;i<tempShape[0];++i) {
95     (*this)(i)=extract<double>(value[i]);
96     }
97     } else if (value.getrank()==2) {
98     for (ValueType::size_type i=0;i<tempShape[0];++i) {
99     for (ValueType::size_type j=0;j<tempShape[1];++j) {
100     (*this)(i,j)=extract<double>(value[i][j]);
101     }
102     }
103     } else if (value.getrank()==3) {
104     for (ValueType::size_type i=0;i<tempShape[0];++i) {
105     for (ValueType::size_type j=0;j<tempShape[1];++j) {
106     for (ValueType::size_type k=0;k<tempShape[2];++k) {
107     (*this)(i,j,k)=extract<double>(value[i][j][k]);
108     }
109     }
110     }
111     } else if (value.getrank()==4) {
112     for (ValueType::size_type i=0;i<tempShape[0];++i) {
113     for (ValueType::size_type j=0;j<tempShape[1];++j) {
114     for (ValueType::size_type k=0;k<tempShape[2];++k) {
115     for (ValueType::size_type m=0;m<tempShape[3];++m) {
116     (*this)(i,j,k,m)=extract<double>(value[i][j][k][m]);
117     }
118     }
119     }
120     }
121     }
122     }
123 jgs 102
124 jgs 82 void
125     DataArrayView::copy(const DataArrayView& other)
126     {
127     copy(m_offset,other);
128     }
129    
130     void
131     DataArrayView::copy(ValueType::size_type offset,
132     const DataArrayView& other)
133     {
134 jgs 108 EsysAssert((!isEmpty()&&!other.isEmpty()&&checkOffset(offset)),
135     "Error - Couldn't copy due to insufficient storage.");
136     EsysAssert((checkShape(other.getShape())),
137 jgs 82 createShapeErrorMessage("Error - Couldn't copy due to shape mismatch.",other.getShape()));
138 jgs 108 if (checkOffset(offset)) {
139     memcpy(&(*m_data)[offset],&(*other.m_data)[other.m_offset],sizeof(double)*noValues());
140     } else {
141     throw DataException("Error - invalid offset specified.");
142     }
143 jgs 82 }
144    
145     void
146     DataArrayView::copy(ValueType::size_type thisOffset,
147     const DataArrayView& other,
148     ValueType::size_type otherOffset)
149     {
150 jgs 108 EsysAssert((!isEmpty()&&!other.isEmpty()&&checkOffset(thisOffset)&&other.checkOffset(otherOffset)),
151     "Error - Couldn't copy due to insufficient storage.");
152     EsysAssert((checkShape(other.getShape())),
153 jgs 82 createShapeErrorMessage("Error - Couldn't copy due to shape mismatch.",other.getShape()));
154 jgs 108 if (checkOffset(thisOffset)&&other.checkOffset(otherOffset)) {
155     memcpy(&(*m_data)[thisOffset],&(*other.m_data)[otherOffset],sizeof(double)*noValues());
156     } else {
157     throw DataException("Error - invalid offset specified.");
158     }
159 jgs 82 }
160    
161     void
162     DataArrayView::copy(ValueType::size_type offset,
163     ValueType::value_type value)
164     {
165 jgs 108 EsysAssert((!isEmpty()&&checkOffset(offset)),
166     "Error - Couldn't copy due to insufficient storage.");
167     if (checkOffset(offset)) {
168     ValueType temp(noValues(),value);
169     memcpy(&(*m_data)[offset],&temp[0],sizeof(double)*noValues());
170     } else {
171     throw DataException("Error - invalid offset specified.");
172     }
173 jgs 82 }
174    
175     int
176     DataArrayView::getRank() const
177     {
178     return m_shape.size();
179     }
180    
181 jgs 117 const
182     DataArrayView::ShapeType&
183 jgs 82 DataArrayView::getShape() const
184     {
185     return m_shape;
186     }
187    
188     int
189     DataArrayView::noValues(const ShapeType& shape)
190     {
191     ShapeType::const_iterator i;
192     //
193     // An empty shape vector means rank 0 which contains 1 value
194     int noValues=1;
195 jgs 108 for (i=shape.begin();i!=shape.end();i++) {
196 jgs 82 noValues*=(*i);
197     }
198     return noValues;
199     }
200 jgs 108
201     int
202     DataArrayView::noValues(const RegionLoopRangeType& region)
203     {
204     //
205     // An empty region vector means rank 0 which contains 1 value
206     int noValues=1;
207     for (int i=0; i<region.size(); i++) {
208     noValues*=region[i].second-region[i].first;
209     }
210     return noValues;
211     }
212 jgs 102
213 jgs 82 int
214     DataArrayView::noValues() const
215     {
216     return m_noValues;
217     }
218    
219     bool
220     DataArrayView::checkShape(const DataArrayView::ShapeType& other) const
221     {
222     return (m_shape==other);
223     }
224    
225     string
226     DataArrayView::createShapeErrorMessage(const string& messagePrefix,
227     const DataArrayView::ShapeType& other) const
228     {
229     stringstream temp;
230     temp << messagePrefix
231     << " This shape: " << shapeToString(m_shape)
232     << " Other shape: " << shapeToString(other);
233     return temp.str();
234     }
235 jgs 108
236     DataArrayView::ValueType::size_type
237     DataArrayView::getOffset() const
238     {
239     return m_offset;
240     }
241    
242 jgs 82 void
243     DataArrayView::setOffset(ValueType::size_type offset)
244     {
245 jgs 108 EsysAssert((checkOffset(offset)), "Error - Invalid offset.");
246     if (checkOffset(offset)) {
247     m_offset=offset;
248     } else {
249     throw DataException("Error - invalid offset specified.");
250     }
251 jgs 82 }
252    
253 jgs 108 void
254     DataArrayView::incrOffset()
255     {
256     EsysAssert((checkOffset(m_offset+noValues())), "Error - Cannot increment offset.");
257     if (checkOffset(m_offset+noValues())) {
258     m_offset=m_offset+noValues();
259     } else {
260     throw DataException("Error - Cannot increment offset.");
261     }
262     }
263    
264     bool
265     DataArrayView::checkOffset() const
266     {
267     return checkOffset(m_offset);
268     }
269    
270     bool
271     DataArrayView::checkOffset(ValueType::size_type offset) const
272     {
273     return (m_data->size() >= (offset+noValues()));
274     }
275    
276     DataArrayView::ValueType&
277     DataArrayView::getData() const
278     {
279     EsysAssert(!isEmpty(),"Error - View is empty");
280     return *m_data;
281     }
282    
283     DataArrayView::ValueType::reference
284     DataArrayView::getData(ValueType::size_type i) const
285     {
286     //
287     // don't do any index checking so allow one past the end of the
288     // vector providing the equivalent of end()
289     return (*m_data)[m_offset+i];
290     }
291    
292 jgs 82 DataArrayView::ShapeType
293     DataArrayView::getResultSliceShape(const RegionType& region)
294     {
295 jgs 102 int dimSize;
296 jgs 82 RegionType::const_iterator i;
297 jgs 117 ShapeType result;
298 jgs 102 for (i=region.begin();i!=region.end();i++) {
299 jgs 108 dimSize=((i->second)-(i->first));
300 jgs 82 if (dimSize!=0) {
301     result.push_back(dimSize);
302     }
303     }
304     return result;
305     }
306    
307 jgs 102 DataArrayView::RegionType
308     DataArrayView::getSliceRegion(const boost::python::object& key) const
309     {
310 jgs 108 int slice_rank, i;
311 jgs 102 int this_rank=getRank();
312 jgs 117 RegionType out(this_rank);
313 jgs 102 /* allow for case where key is singular eg: [1], this implies we
314     want to generate a rank-1 dimension object, as opposed to eg: [1,2]
315     which implies we want to take a rank dimensional object with one
316     dimension of size 1 */
317     extract<tuple> key_tuple(key);
318     if (key_tuple.check()) {
319     slice_rank=extract<int> (key.attr("__len__")());
320 jgs 108 /* ensure slice is correctly dimensioned */
321     if (slice_rank>this_rank) {
322     throw DataException("Error - rank of slices does not match rank of slicee");
323     } else {
324     /* calculate values for slice range */
325     for (i=0;i<slice_rank;i++) {
326     out[i]=getSliceRange(key[i],getShape()[i]);
327     }
328     }
329 jgs 102 } else {
330     slice_rank=1;
331 jgs 108 if (slice_rank>this_rank) {
332     throw DataException("Error - rank of slices does not match rank of slicee");
333     } else {
334     out[0]=getSliceRange(key,getShape()[0]);
335     }
336 jgs 102 }
337     for (i=slice_rank;i<this_rank;i++) {
338     out[i]=std::pair<int,int>(0,getShape()[i]);
339     }
340     return out;
341     }
342    
343     std::pair<int,int>
344     getSliceRange(const boost::python::object& key,
345     const int shape)
346     {
347     /* default slice range is range of entire shape dimension */
348     int s0=0, s1=shape;;
349     extract<int> slice_int(key);
350     if (slice_int.check()) {
351     /* if the key is a single int set start=key and end=key */
352     /* in this case, we want to return a rank-1 dimension object from
353     this object, taken from a single index value for one of this
354     object's dimensions */
355     s0=slice_int();
356     s1=s0;
357     } else {
358     /* if key is a pair extract begin and end values */
359     extract<int> step(key.attr("step"));
360     if (step.check() && step()!=1) {
361     throw DataException("Error - Data does not support increments in slicing ");
362     } else {
363     extract<int> start(key.attr("start"));
364     if (start.check()) {
365     s0=start();
366     }
367     extract<int> stop(key.attr("stop"));
368     if (stop.check()) {
369     s1=stop();
370     }
371     }
372     }
373 jgs 108 if (s0 < 0)
374     throw DataException("Error - slice index out of range.");
375     if (s0 == s1 and s1 >= shape)
376     throw DataException("Error - slice index out of range.");
377     if (s0 != s1 and s1>shape)
378     throw DataException("Error - slice index out of range.");
379     if (s0 > s1)
380     throw DataException("Error - lower index must less or equal upper index.");
381 jgs 102 return std::pair<int,int>(s0,s1);
382     }
383    
384 jgs 108 DataArrayView::RegionLoopRangeType
385     getSliceRegionLoopRange(const DataArrayView::RegionType& region)
386     {
387     DataArrayView::RegionLoopRangeType region_loop_range(region.size());
388     for (int i=0;i<region.size();i++) {
389     if (region[i].first==region[i].second) {
390     region_loop_range[i].first=region[i].first;
391     region_loop_range[i].second=region[i].second+1;
392     } else {
393     region_loop_range[i].first=region[i].first;
394     region_loop_range[i].second=region[i].second;
395     }
396     }
397     return region_loop_range;
398     }
399    
400 jgs 82 void
401     DataArrayView::copySlice(const DataArrayView& other,
402 jgs 108 const RegionLoopRangeType& region)
403 jgs 82 {
404     //
405 jgs 102 // version of copySlice that uses the default offsets
406 jgs 82 copySlice(m_offset,other,other.m_offset,region);
407     }
408    
409     void
410     DataArrayView::copySlice(ValueType::size_type thisOffset,
411     const DataArrayView& other,
412     ValueType::size_type otherOffset,
413 jgs 108 const RegionLoopRangeType& region)
414 jgs 82 {
415 jgs 102
416 jgs 82 //
417 jgs 108 // Make sure views are not empty
418 jgs 102
419 jgs 108 EsysAssert(!isEmpty(),
420     "Error - this view is empty.");
421     EsysAssert(!other.isEmpty(),
422     "Error - other view is empty.");
423    
424 jgs 82 //
425 jgs 108 // Check the view to be sliced from is compatible with the region to be sliced,
426     // and that the region to be sliced is compatible with this view:
427 jgs 102
428 jgs 108 EsysAssert(checkOffset(thisOffset),
429     "Error - offset incompatible with this view.");
430     EsysAssert(otherOffset+noValues()<=other.getData().size(),
431     "Error - offset incompatible with other view.");
432 jgs 102
433 jgs 108 EsysAssert(other.getRank()==region.size(),
434     "Error - slice not same rank as view to be sliced from.");
435 jgs 102
436 jgs 108 EsysAssert(noValues()==noValues(getResultSliceShape(region)),
437     "Error - slice shape not compatible shape for this view.");
438 jgs 102
439     //
440 jgs 108 // copy the values in the specified region of the other view into this view
441 jgs 102
442 jgs 82 int numCopy=0;
443 jgs 108
444     switch (region.size()) {
445 jgs 82 case 0:
446 jgs 108 /* this case should never be encountered,
447     as python will never pass us an empty region.
448     here for completeness only, allows slicing of a scalar */
449     (*m_data)[thisOffset+numCopy]=(*other.m_data)[otherOffset+other.relIndex()];
450     numCopy++;
451 jgs 82 break;
452     case 1:
453 jgs 108 for (int i=region[0].first;i<region[0].second;i++) {
454     (*m_data)[thisOffset+numCopy]=(*other.m_data)[otherOffset+other.relIndex(i)];
455     numCopy++;
456 jgs 82 }
457     break;
458     case 2:
459 jgs 108 for (int j=region[1].first;j<region[1].second;j++) {
460     for (int i=region[0].first;i<region[0].second;i++) {
461     (*m_data)[thisOffset+numCopy]=(*other.m_data)[otherOffset+other.relIndex(i,j)];
462 jgs 102 numCopy++;
463 jgs 82 }
464     }
465     break;
466     case 3:
467 jgs 108 for (int k=region[2].first;k<region[2].second;k++) {
468     for (int j=region[1].first;j<region[1].second;j++) {
469     for (int i=region[0].first;i<region[0].second;i++) {
470     (*m_data)[thisOffset+numCopy]=(*other.m_data)[otherOffset+other.relIndex(i,j,k)];
471 jgs 102 numCopy++;
472 jgs 82 }
473     }
474     }
475     break;
476     case 4:
477 jgs 108 for (int l=region[3].first;l<region[3].second;l++) {
478     for (int k=region[2].first;k<region[2].second;k++) {
479     for (int j=region[1].first;j<region[1].second;j++) {
480     for (int i=region[0].first;i<region[0].second;i++) {
481     (*m_data)[thisOffset+numCopy]=(*other.m_data)[otherOffset+other.relIndex(i,j,k,l)];
482 jgs 102 numCopy++;
483 jgs 82 }
484     }
485     }
486     }
487     break;
488     default:
489     stringstream mess;
490 jgs 108 mess << "Error - (copySlice) Invalid slice region rank: " << region.size();
491 jgs 82 throw DataException(mess.str());
492     }
493     }
494 jgs 102
495 jgs 82 void
496     DataArrayView::copySliceFrom(const DataArrayView& other,
497 jgs 108 const RegionLoopRangeType& region_loop_range)
498 jgs 82 {
499     //
500 jgs 108 // version of copySliceFrom that uses the default offsets
501     copySliceFrom(m_offset,other,other.m_offset,region_loop_range);
502 jgs 82 }
503    
504     void
505     DataArrayView::copySliceFrom(ValueType::size_type thisOffset,
506     const DataArrayView& other,
507     ValueType::size_type otherOffset,
508 jgs 108 const RegionLoopRangeType& region)
509 jgs 82 {
510 jgs 102
511 jgs 82 //
512 jgs 108 // Make sure views are not empty
513 jgs 102
514 jgs 108 EsysAssert(!isEmpty(),
515     "Error - this view is empty.");
516     EsysAssert(!other.isEmpty(),
517     "Error - other view is empty.");
518    
519 jgs 82 //
520 jgs 108 // Check this view is compatible with the region to be sliced,
521     // and that the region to be sliced is compatible with the other view:
522 jgs 102
523 jgs 108 EsysAssert(other.checkOffset(otherOffset),
524     "Error - offset incompatible with other view.");
525     EsysAssert(thisOffset+other.noValues()<=getData().size(),
526     "Error - offset incompatible with this view.");
527 jgs 102
528 jgs 108 EsysAssert(getRank()==region.size(),
529     "Error - slice not same rank as this view.");
530 jgs 102
531 jgs 108 EsysAssert(other.getRank()==0 || other.noValues()==noValues(getResultSliceShape(region)),
532     "Error - slice shape not compatible shape for other view.");
533 jgs 102
534     //
535 jgs 108 // copy the values in the other view into the specified region of this view
536 jgs 102
537 jgs 108 // allow for case where other view is a scalar
538     if (other.getRank()==0) {
539    
540     int numCopy=0;
541    
542     switch (region.size()) {
543     case 0:
544     /* this case should never be encountered,
545     as python will never pass us an empty region.
546     here for completeness only, allows slicing of a scalar */
547     (*m_data)[thisOffset+relIndex()]=(*other.m_data)[otherOffset];
548 jgs 102 numCopy++;
549 jgs 108 break;
550     case 1:
551     for (int i=region[0].first;i<region[0].second;i++) {
552     (*m_data)[thisOffset+relIndex(i)]=(*other.m_data)[otherOffset];
553 jgs 102 numCopy++;
554 jgs 108 }
555     break;
556     case 2:
557     for (int j=region[1].first;j<region[1].second;j++) {
558     for (int i=region[0].first;i<region[0].second;i++) {
559     (*m_data)[thisOffset+relIndex(i,j)]=(*other.m_data)[otherOffset];
560 jgs 102 numCopy++;
561 jgs 82 }
562 jgs 108 }
563     break;
564     case 3:
565     for (int k=region[2].first;k<region[2].second;k++) {
566     for (int j=region[1].first;j<region[1].second;j++) {
567     for (int i=region[0].first;i<region[0].second;i++) {
568     (*m_data)[thisOffset+relIndex(i,j,k)]=(*other.m_data)[otherOffset];
569     numCopy++;
570     }
571     }
572     }
573     break;
574     case 4:
575     for (int l=region[3].first;l<region[3].second;l++) {
576     for (int k=region[2].first;k<region[2].second;k++) {
577     for (int j=region[1].first;j<region[1].second;j++) {
578     for (int i=region[0].first;i<region[0].second;i++) {
579     (*m_data)[thisOffset+relIndex(i,j,k,l)]=(*other.m_data)[otherOffset];
580     numCopy++;
581     }
582     }
583     }
584     }
585     break;
586     default:
587     stringstream mess;
588     mess << "Error - (copySliceFrom) Invalid slice region rank: " << region.size();
589     throw DataException(mess.str());
590     }
591    
592     } else {
593    
594     int numCopy=0;
595    
596     switch (region.size()) {
597     case 0:
598     /* this case should never be encountered,
599     as python will never pass us an empty region.
600     here for completeness only, allows slicing of a scalar */
601     (*m_data)[thisOffset+relIndex()]=(*other.m_data)[otherOffset+numCopy];
602     numCopy++;
603     break;
604     case 1:
605     for (int i=region[0].first;i<region[0].second;i++) {
606     (*m_data)[thisOffset+relIndex(i)]=(*other.m_data)[otherOffset+numCopy];
607     numCopy++;
608     }
609     break;
610     case 2:
611     for (int j=region[1].first;j<region[1].second;j++) {
612     for (int i=region[0].first;i<region[0].second;i++) {
613     (*m_data)[thisOffset+relIndex(i,j)]=(*other.m_data)[otherOffset+numCopy];
614     numCopy++;
615     }
616     }
617     break;
618     case 3:
619     for (int k=region[2].first;k<region[2].second;k++) {
620     for (int j=region[1].first;j<region[1].second;j++) {
621     for (int i=region[0].first;i<region[0].second;i++) {
622     (*m_data)[thisOffset+relIndex(i,j,k)]=(*other.m_data)[otherOffset+numCopy];
623     numCopy++;
624     }
625     }
626     }
627     break;
628     case 4:
629     for (int l=region[3].first;l<region[3].second;l++) {
630     for (int k=region[2].first;k<region[2].second;k++) {
631     for (int j=region[1].first;j<region[1].second;j++) {
632     for (int i=region[0].first;i<region[0].second;i++) {
633     (*m_data)[thisOffset+relIndex(i,j,k,l)]=(*other.m_data)[otherOffset+numCopy];
634     numCopy++;
635     }
636     }
637     }
638     }
639     break;
640     default:
641     stringstream mess;
642     mess << "Error - (copySliceFrom) Invalid slice region rank: " << region.size();
643     throw DataException(mess.str());
644     }
645    
646 jgs 82 }
647 jgs 108
648 jgs 82 }
649 jgs 102
650 jgs 82 string
651     DataArrayView::toString(const string& suffix) const
652     {
653 jgs 108 EsysAssert(!isEmpty(),"Error - View is empty.");
654 jgs 82 stringstream temp;
655     string finalSuffix=suffix;
656     if (suffix.length() > 0) {
657     finalSuffix+=" ";
658     }
659     switch (getRank()) {
660     case 0:
661     temp << finalSuffix << (*this)();
662     break;
663     case 1:
664 jgs 102 for (int i=0;i<getShape()[0];i++) {
665 jgs 82 temp << "(" << i << ") " << finalSuffix << (*this)(i);
666     if (i!=(getShape()[0]-1)) {
667     temp << endl;
668     }
669     }
670     break;
671     case 2:
672 jgs 102 for (int i=0;i<getShape()[0];i++) {
673     for (int j=0;j<getShape()[1];j++) {
674 jgs 82 temp << "(" << i << "," << j << ") " << finalSuffix << (*this)(i,j);
675     if (!(i==(getShape()[0]-1) && j==(getShape()[1]-1))) {
676     temp << endl;
677     }
678     }
679     }
680     break;
681     case 3:
682 jgs 102 for (int i=0;i<getShape()[0];i++) {
683     for (int j=0;j<getShape()[1];j++) {
684     for (int k=0;k<getShape()[2];k++) {
685     temp << "(" << i << "," << j << "," << k << ") " << finalSuffix << (*this)(i,j,k);
686     if (!(i==(getShape()[0]-1) && j==(getShape()[1]-1) && k==(getShape()[2]-1))) {
687 jgs 82 temp << endl;
688     }
689     }
690     }
691     }
692     break;
693     case 4:
694 jgs 102 for (int i=0;i<getShape()[0];i++) {
695     for (int j=0;j<getShape()[1];j++) {
696     for (int k=0;k<getShape()[2];k++) {
697     for (int l=0;l<getShape()[3];l++) {
698     temp << "(" << i << "," << j << "," << k << "," << l << ") " << finalSuffix << (*this)(i,j,k,l);
699     if (!(i==(getShape()[0]-1) && j==(getShape()[1]-1) && k==(getShape()[2]-1) && l==(getShape()[3]-1))) {
700     temp << endl;
701     }
702     }
703     }
704     }
705     }
706     break;
707 jgs 82 default:
708     stringstream mess;
709     mess << "Error - (toString) Invalid rank: " << getRank();
710     throw DataException(mess.str());
711     }
712     return temp.str();
713     }
714    
715     string
716     DataArrayView::shapeToString(const DataArrayView::ShapeType& shape)
717     {
718     stringstream temp;
719     temp << "(";
720 jgs 102 for (int i=0;i<shape.size();i++) {
721 jgs 82 temp << shape[i];
722 jgs 102 if (i < shape.size()-1) {
723 jgs 82 temp << ",";
724     }
725     }
726     temp << ")";
727     return temp.str();
728     }
729    
730     void
731     DataArrayView::matMult(const DataArrayView& left,
732     const DataArrayView& right,
733     DataArrayView& result)
734     {
735 jgs 113
736 jgs 82 if (left.getRank()==0 || right.getRank()==0) {
737     stringstream temp;
738     temp << "Error - (matMult) Invalid for rank 0 objects.";
739     throw DataException(temp.str());
740     }
741    
742     if (left.getShape()[left.getRank()-1]!=right.getShape()[0]) {
743     stringstream temp;
744     temp << "Error - (matMult) Dimension: " << left.getRank()
745     << ", size: " << left.getShape()[left.getRank()-1]
746     << " of LHS and dimension: 1, size: " << right.getShape()[0]
747     << " of RHS don't match.";
748     throw DataException(temp.str());
749     }
750 jgs 113
751 jgs 82 int outputRank=left.getRank()+right.getRank()-2;
752    
753     if (outputRank < 0) {
754     stringstream temp;
755     temp << "Error - (matMult) Left and right operands cannot be multiplied "
756     << "as they have incompatable rank.";
757     throw DataException(temp.str());
758     }
759    
760     if (outputRank != result.getRank()) {
761     stringstream temp;
762     temp << "Error - (matMult) Rank of result array is: "
763     << result.getRank()
764     << " it must be: " << outputRank;
765     throw DataException(temp.str());
766     }
767    
768     for (int i=0; i<(left.getRank()-1); ++i) {
769     if (left.getShape()[i]!=result.getShape()[i]) {
770     stringstream temp;
771     temp << "Error - (matMult) Dimension: " << i
772     << " of LHS and output array don't match.";
773     throw DataException(temp.str());
774     }
775     }
776    
777     for (int i=1; i<right.getRank(); ++i) {
778     if (right.getShape()[i] != result.getShape()[i+left.getRank()-2]) {
779     stringstream temp;
780     temp << "Error - (matMult) Dimension: " << i
781     << ", size: " << right.getShape()[i]
782     << " of RHS and dimension: " << i+left.getRank()-1
783     << ", size: " << result.getShape()[i+left.getRank()-1]
784     << " of output array don't match.";
785     throw DataException(temp.str());
786     }
787     }
788    
789     switch (left.getRank()) {
790 jgs 113
791 jgs 82 case 1:
792 jgs 113 switch (right.getRank()) {
793     case 1:
794     result()=0;
795     for (int i=0;i<left.getShape()[0];i++) {
796     result()+=left(i)*right(i);
797     }
798     break;
799     case 2:
800     for (int i=0;i<result.getShape()[0];i++) {
801     result(i)=0;
802     for (int j=0;j<right.getShape()[0];j++) {
803     result(i)+=left(j)*right(j,i);
804     }
805     }
806     break;
807     default:
808     stringstream temp; temp << "Error - (matMult) Invalid rank. Programming error.";
809     throw DataException(temp.str());
810     break;
811     }
812 jgs 82 break;
813 jgs 113
814 jgs 82 case 2:
815 jgs 113 switch (right.getRank()) {
816     case 1:
817     result()=0;
818     for (int i=0;i<left.getShape()[0];i++) {
819     result(i)=0;
820     for (int j=0;j<left.getShape()[1];j++) {
821     result(i)+=left(i,j)*right(i);
822     }
823     }
824     break;
825     case 2:
826     for (int i=0;i<result.getShape()[0];i++) {
827     for (int j=0;j<result.getShape()[1];j++) {
828     result(i,j)=0;
829     for (int jR=0;jR<right.getShape()[0];jR++) {
830     result(i,j)+=left(i,jR)*right(jR,j);
831     }
832     }
833     }
834     break;
835     default:
836     stringstream temp; temp << "Error - (matMult) Invalid rank. Programming error.";
837     throw DataException(temp.str());
838     break;
839     }
840 jgs 82 break;
841 jgs 113
842 jgs 82 default:
843 jgs 113 stringstream temp; temp << "Error - (matMult) Not supported for rank: " << left.getRank();
844 jgs 82 throw DataException(temp.str());
845     break;
846     }
847 jgs 113
848 jgs 82 }
849    
850 jgs 113 DataArrayView::ShapeType
851     DataArrayView::determineResultShape(const DataArrayView& left,
852     const DataArrayView& right)
853     {
854 jgs 117 ShapeType temp;
855 jgs 113 for (int i=0; i<(left.getRank()-1); i++) {
856     temp.push_back(left.getShape()[i]);
857     }
858     for (int i=1; i<right.getRank(); i++) {
859     temp.push_back(right.getShape()[i]);
860     }
861     return temp;
862     }
863    
864 jgs 82 bool operator==(const DataArrayView& left, const DataArrayView& right)
865     {
866     //
867     // The views are considered equal if they have the same shape and each value
868     // of the view is the same.
869     bool result=(left.m_shape==right.m_shape);
870     if (result) {
871 jgs 108 for (int i=0;i<left.noValues();i++) {
872 jgs 82 result=result && (left.getData(i)==right.getData(i));
873     }
874     }
875     return result;
876     }
877    
878     bool operator!=(const DataArrayView& left, const DataArrayView& right)
879     {
880     return (!operator==(left,right));
881     }
882    
883     } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26