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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 576 - (hide annotations)
Fri Mar 3 08:28:42 2006 UTC (13 years, 3 months ago) by gross
File MIME type: text/plain
File size: 34958 byte(s)
some steps towards eigenvalue and eigenvector calculation
1 jgs 102 // $Id$
2 jgs 108
3 jgs 82 /*
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 jgs 474
17 jgs 82 #if !defined escript_DataArrayView_20040323_H
18     #define escript_DataArrayView_20040323_H
19    
20 jgs 468 #include "EsysAssert.h"
21 jgs 82
22 jgs 474 #include "DataVector.h"
23 gross 576 #include "LocalOps.h"
24 jgs 474
25 jgs 82 #include <boost/python/numeric.hpp>
26     #include <boost/python/object.hpp>
27    
28 jgs 108 #include <vector>
29 jgs 102
30 jgs 82 namespace escript {
31    
32     /**
33     \brief
34 jgs 108 DataArrayView provides a view of external data, configured according
35     to the given shape information and offset.
36 jgs 82
37     Description:
38     DataArrayView provides a view of data allocated externally. The
39     external data should provide sufficient values so that the dimensions
40 jgs 108 specified for the view shape will be satisfied. The viewer can update
41 jgs 82 values within the external data but cannot resize the external data.
42 jgs 108
43     The view provided represents a single n-dimensional data-point
44     comprised of values taken from the given data array, starting at the
45     specified offset and extending for as many values as are necessary to
46 jgs 117 satisfy the given shape. The default offset can be changed, or different
47     offsets specified, in order to provide views of other data-points in
48     the underlying data array.
49 jgs 82 */
50    
51     class DataArrayView {
52    
53     friend bool operator==(const DataArrayView& left, const DataArrayView& right);
54     friend bool operator!=(const DataArrayView& left, const DataArrayView& right);
55    
56     public:
57    
58 jgs 117 //
59     // Some basic types which define the data values and view shapes.
60     typedef DataVector ValueType;
61     //typedef std::vector<double> ValueType;
62 jgs 108 typedef std::vector<int> ShapeType;
63 jgs 82 typedef std::vector<std::pair<int, int> > RegionType;
64 jgs 108 typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
65 jgs 82
66     /**
67     \brief
68     Default constructor for DataArrayView.
69    
70     Description:
71 jgs 108 Default constructor for DataArrayView.
72     Creates an empty view with no associated data array.
73    
74     This is essentially useless but here for completeness.
75 jgs 82 */
76     DataArrayView();
77    
78     /**
79     \brief
80     Constructor for DataArrayView.
81    
82     Description:
83     Constructor for DataArrayView.
84    
85 jgs 108 \param data - Input -
86     Array holding data to be viewed. This must be large enough
87     to supply sufficient values for the specified shape starting at
88     the given offset.
89     \param viewShape - Input -
90     The shape of the view to create.
91     \param offset - Input -
92     Offset into the data at which view should start.
93 jgs 82 */
94     DataArrayView(ValueType& data,
95     const ShapeType& viewShape,
96     int offset=0);
97    
98     /**
99     \brief
100     Copy constructor for DataArrayView.
101    
102     Description:
103     Copy constructor for DataArrayView.
104    
105 jgs 108 \param other - Input -
106     DataArrayView to copy.
107 jgs 82
108 jgs 108 NOTE: The copy references the same data array.
109 jgs 82 */
110     DataArrayView(const DataArrayView& other);
111    
112     /**
113     \brief
114 jgs 108 Copy from a numarray into the data array viewed by this.
115     This must have same shape as the given value - will throw if not.
116 jgs 82 */
117     void
118     copy(const boost::python::numeric::array& value);
119    
120     /**
121     \brief
122 jgs 108 Copy data from another DataArrayView into the data array viewed by this
123     starting at the default offset in both views.
124     The shapes of both views must be the same - will throw if not.
125     NB: views may or may not reference same underlying data array!
126 jgs 82 */
127     void
128     copy(const DataArrayView& other);
129    
130     /**
131     \brief
132 jgs 108 Copy data from another DataArrayView into this view starting at the
133     given offset in this view and the default offset in the other view.
134     The shapes of both views must be the same - will throw if not.
135     NB: views may or may not reference same underlying data array!
136 jgs 82 */
137     void
138     copy(ValueType::size_type offset,
139     const DataArrayView& other);
140    
141     /**
142     \brief
143 jgs 108 Copy data from another DataArrayView into this view starting at the
144     given offsets in each view.
145     The shapes of both views must be compatible - will throw if not.
146     NB: views may or may not reference same underlying data array!
147 jgs 82
148 jgs 108 \param thisOffset - Input -
149     Offset into this view's data array to copy to.
150     \param other - Input -
151     View to copy data from.
152     \param otherOffset - Input -
153     Offset into the other view's data array to copy from.
154 jgs 82 */
155     void
156     copy(ValueType::size_type thisOffset,
157     const DataArrayView& other,
158     ValueType::size_type otherOffset);
159    
160     /**
161     \brief
162 jgs 108 Copy the given single value over the entire view starting at the given
163     offset in the view's data array.
164 jgs 82
165 jgs 108 \param offset - Input -
166     Offset into this view's data array to copy to.
167     \param value - Input -
168     Value to copy.
169 jgs 82 */
170     void
171     copy(ValueType::size_type offset,
172     ValueType::value_type value);
173    
174     /**
175 jgs 108 \brief
176     Check if view is empty. ie: does not point to any actual data.
177     */
178     bool
179     isEmpty() const;
180    
181     /**
182 jgs 82 \brief
183 jgs 108 Return this view's offset into the viewed data array.
184 jgs 82 */
185     ValueType::size_type
186     getOffset() const;
187    
188     /**
189 jgs 108 \brief
190     Set the offset into the data array at which this view is to start.
191     This could be used to step through the underlying data array by incrementing
192     the offset by noValues successively. Thus this view would provide a moving
193     window on the underlying data with the given shape.
194 jgs 82 */
195 jgs 108 void
196     setOffset(ValueType::size_type offset);
197 jgs 82
198     /**
199     \brief
200 jgs 108 Increment the offset by the number of values in the shape, if possible. Thus
201     moving the view onto the next data point of the given shape in the underlying
202     data array.
203 jgs 82 */
204 jgs 108 void
205     incrOffset();
206 jgs 82
207     /**
208     \brief
209 jgs 108 Check the (given) offset will not result in two few values being available in
210     the underlying data array for this view's shape.
211 jgs 82 */
212 jgs 108 bool
213     checkOffset() const;
214 jgs 117
215 jgs 108 bool
216     checkOffset(ValueType::size_type offset) const;
217 jgs 82
218     /**
219 jgs 108 \brief
220     Return the rank of the shape of this view.
221     */
222     int
223     getRank() const;
224    
225     /**
226 jgs 82 \brief
227 jgs 108 Return the number of values for the shape of this view.
228 jgs 82 */
229     int
230     noValues() const;
231    
232     /**
233     \brief
234 jgs 108 Calculate the number of values for the given shape or region.
235     This is purely a utility method and has no bearing on this view.
236 jgs 82 */
237 jgs 117 static
238     int
239 jgs 108 noValues(const ShapeType& shape);
240 jgs 117
241     static
242     int
243 jgs 108 noValues(const RegionLoopRangeType& region);
244 jgs 82
245     /**
246     \brief
247 jgs 108 Return a reference to the underlying data array.
248 jgs 82 */
249     ValueType&
250     getData() const;
251    
252     /**
253     \brief
254 jgs 108 Return a reference to the data value with the given
255     index in this view. This takes into account the offset.
256 jgs 82 */
257     ValueType::reference
258     getData(ValueType::size_type i) const;
259    
260     /**
261     \brief
262 jgs 108 Return the shape of this view.
263 jgs 82 */
264 jgs 117 const
265     ShapeType&
266 jgs 108 getShape() const;
267 jgs 82
268     /**
269     \brief
270 jgs 108 Return true if the given shape is the same as this view's shape.
271 jgs 82 */
272 jgs 108 bool
273 jgs 117 checkShape(const ShapeType& other) const;
274 jgs 82
275     /**
276     \brief
277 jgs 108 Create a shape error message. Normally used when there is a shape
278 jgs 117 mismatch between this shape and the other shape.
279 jgs 82
280 jgs 108 \param messagePrefix - Input -
281     First part of the error message.
282     \param other - Input -
283     The other shape.
284 jgs 82 */
285 jgs 108 std::string
286     createShapeErrorMessage(const std::string& messagePrefix,
287 jgs 117 const ShapeType& other) const;
288 jgs 82
289     /**
290     \brief
291 jgs 108 Return the viewed data as a formatted string.
292     Not recommended for very large objects!
293 jgs 82
294 jgs 108 \param suffix - Input -
295     Suffix appended to index display.
296 jgs 82 */
297 jgs 108 std::string
298     toString(const std::string& suffix=std::string("")) const;
299 jgs 82
300     /**
301     \brief
302 jgs 108 Return the given shape as a string.
303     This is purely a utility method and has no bearing on this view.
304 jgs 82
305 jgs 108 \param shape - Input.
306 jgs 82 */
307 jgs 117 static
308     std::string
309     shapeToString(const ShapeType& shape);
310 jgs 82
311 jgs 108 /**
312     \brief
313     Return the 1 dimensional index into the data array of the only element
314     in the view, *ignoring the offset*.
315     Assumes a rank 0 view.
316 jgs 82 */
317 jgs 108 ValueType::size_type
318     relIndex() const;
319 jgs 82
320     /**
321 jgs 108 \brief
322     Return the 1 dimensional index into the data array of the element at
323     position i in the view, *ignoring the offset*.
324     Assumes a rank 1 view.
325 jgs 82 */
326 jgs 108 ValueType::size_type
327     relIndex(ValueType::size_type i) const;
328 jgs 82
329     /**
330 jgs 108 \brief
331     Return the 1 dimensional index into the data array of the element at
332     position i,j in the view, *ignoring the offset*.
333     Assumes a rank 2 view.
334 jgs 82 */
335 jgs 108 ValueType::size_type
336     relIndex(ValueType::size_type i,
337     ValueType::size_type j) const;
338 jgs 82
339     /**
340 jgs 108 \brief
341     Return the 1 dimensional index into the data array of the element at
342     position i,j,k in the view, *ignoring the offset*.
343     Assumes a rank 3 view.
344 jgs 82 */
345 jgs 108 ValueType::size_type
346     relIndex(ValueType::size_type i,
347     ValueType::size_type j,
348     ValueType::size_type k) const;
349 jgs 82
350     /**
351     \brief
352 jgs 108 Return the 1 dimensional index into the data array of the element at
353     position i,j,k,m in the view, *ignoring the offset*.
354     Assumes a rank 4 view.
355 jgs 82 */
356     ValueType::size_type
357     relIndex(ValueType::size_type i,
358     ValueType::size_type j,
359     ValueType::size_type k,
360     ValueType::size_type m) const;
361    
362     /**
363     \brief
364 jgs 108 Return the 1 dimensional index into the data array of the only element
365     in the view.
366     Assumes a rank 0 view.
367 jgs 82 */
368     ValueType::size_type
369 jgs 108 index() const;
370 jgs 82
371     /**
372     \brief
373 jgs 108 Return the 1 dimensional index into the data array of the element at
374     position i in the view.
375     Assumes a rank 1 view.
376 jgs 82 */
377     ValueType::size_type
378 jgs 108 index(ValueType::size_type i) const;
379 jgs 82
380     /**
381     \brief
382 jgs 108 Return the 1 dimensional index into the data array of the element at
383     position i,j in the view.
384     Assumes a rank 2 view.
385 jgs 82 */
386     ValueType::size_type
387     index(ValueType::size_type i,
388 jgs 108 ValueType::size_type j) const;
389 jgs 82
390     /**
391     \brief
392 jgs 108 Return the 1 dimensional index into the data array of the element at
393     position i,j,k in the view.
394     Assumes a rank 3 view.
395 jgs 82 */
396     ValueType::size_type
397     index(ValueType::size_type i,
398     ValueType::size_type j,
399     ValueType::size_type k) const;
400    
401     /**
402     \brief
403 jgs 108 Return the 1 dimensional index into the data array of the element at
404     position i,j,k,m in the view.
405     Assumes a rank 4 view.
406 jgs 82 */
407     ValueType::size_type
408     index(ValueType::size_type i,
409 jgs 108 ValueType::size_type j,
410     ValueType::size_type k,
411     ValueType::size_type m) const;
412 jgs 82
413     /**
414     \brief
415 jgs 108 Return a reference for the only element in the view.
416     Assumes a rank 0 view.
417 jgs 82 */
418 jgs 108 ValueType::reference
419     operator()();
420 jgs 82
421 jgs 108 ValueType::const_reference
422     operator()() const;
423    
424 jgs 82 /**
425     \brief
426 jgs 108 Return a reference to the element at position i in the view.
427     Assumes a rank 1 view.
428 jgs 82 */
429     ValueType::reference
430     operator()(ValueType::size_type i);
431    
432     ValueType::const_reference
433     operator()(ValueType::size_type i) const;
434    
435     /**
436     \brief
437 jgs 108 Return a reference to the element at position i,j in the view.
438     Assumes a rank 2 view.
439 jgs 82 */
440     ValueType::reference
441     operator()(ValueType::size_type i,
442     ValueType::size_type j);
443    
444     ValueType::const_reference
445     operator()(ValueType::size_type i,
446     ValueType::size_type j) const;
447    
448     /**
449     \brief
450 jgs 108 Return a reference to the element at position i,j,k in the view.
451     Assumes a rank 3 view.
452 jgs 82 */
453     ValueType::reference
454     operator()(ValueType::size_type i,
455     ValueType::size_type j,
456     ValueType::size_type k);
457    
458     ValueType::const_reference
459     operator()(ValueType::size_type i,
460     ValueType::size_type j,
461     ValueType::size_type k) const;
462    
463     /**
464     \brief
465 jgs 108 Return a reference to the element at position i,j,k,m in the view.
466     Assumes a rank 4 view.
467 jgs 82 */
468     ValueType::reference
469     operator()(ValueType::size_type i,
470     ValueType::size_type j,
471     ValueType::size_type k,
472     ValueType::size_type m);
473    
474     ValueType::const_reference
475     operator()(ValueType::size_type i,
476     ValueType::size_type j,
477     ValueType::size_type k,
478     ValueType::size_type m) const;
479    
480     /**
481 jgs 108 \brief
482     Determine the shape of the specified slice region.
483     This is purely a utility method and has no bearing on this view.
484    
485     \param region - Input -
486     Slice region.
487 jgs 82 */
488 jgs 117 static
489     ShapeType
490 jgs 108 getResultSliceShape(const RegionType& region);
491 jgs 82
492 jgs 108 /**
493     \brief
494     Determine the region specified by the given python slice object.
495 jgs 82
496 jgs 108 \param key - Input -
497     python slice object specifying region to be returned.
498    
499     The slice object is a tuple of n python slice specifiers, where
500     n <= the rank of this Data object. Each slice specifier specifies the
501     range of indexes to be sliced from the corresponding dimension. The
502     first specifier corresponds to the first dimension, the second to the
503     second and so on. Where n < the rank, the remaining dimensions are
504     sliced across the full range of their indicies.
505    
506     Each slice specifier is of the form "a:b", which specifies a slice
507     from index a, up to but not including index b. Where index a is ommitted
508     a is assumed to be 0. Where index b is ommitted, b is assumed to be the
509     length of this dimension. Where both are ommitted (eg: ":") the slice is
510     assumed to encompass that entire dimension.
511    
512     Where one of the slice specifiers is a single integer, eg: [1], we
513     want to generate a rank-1 dimension object, as opposed to eg: [1,2]
514     which implies we want to take a rank dimensional object with one
515     dimension of size 1.
516    
517     The return value is a vector of pairs with length equal to the rank of
518     this object. Each pair corresponds to the range of indicies from the
519     corresponding dimension to be sliced from, as specified in the input
520     slice object.
521    
522     Examples:
523    
524     For a rank 1 object of shape(5):
525    
526     getSliceRegion(:) => < <0,5> >
527     getSliceRegion(2:3) => < <2,3> >
528     getSliceRegion(:3) => < <0,3> >
529     getSliceRegion(2:) => < <2,5> >
530    
531     For a rank 2 object of shape(4,5):
532    
533     getSliceRegion(2:3) => < <2,3> <0,5> >
534     getSliceRegion(2) => < <2,3> <0,5> >
535     NB: but return object requested will have rank 1, shape(5), with
536     values taken from index 2 of this object's first dimension.
537    
538     For a rank 3 object of shape (2,4,6):
539    
540     getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
541     getSliceRegion(:,:,:) => < <0,2> <0,4> <0,6> >
542     getSliceRegion(0:1) => < <0,1> <0,4> <0,6> >
543     getSliceRegion(:1,0:2) => < <0,1> <0,2> <0,6> >
544    
545     */
546 jgs 117 RegionType
547 jgs 108 getSliceRegion(const boost::python::object& key) const;
548    
549 jgs 82 /**
550     \brief
551 jgs 108 Copy a data slice specified by the given region from the given view
552     into this view, using the default offsets in both views.
553    
554     \param other - Input -
555     View to copy data from.
556     \param region - Input -
557     Region in other view to copy data from.
558 jgs 82 */
559     void
560 jgs 108 copySlice(const DataArrayView& other,
561     const RegionLoopRangeType& region);
562 jgs 82
563     /**
564     \brief
565 jgs 108 Copy a data slice specified by the given region and offset from the
566     given view into this view at the given offset.
567    
568     \param thisOffset - Input -
569     Copy the slice to this offset in this view.
570     \param other - Input -
571     View to copy data from.
572     \param otherOffset - Input -
573     Copy the slice from this offset in the given view.
574     \param region - Input -
575     Region in other view to copy data from.
576 jgs 82 */
577     void
578 jgs 108 copySlice(ValueType::size_type thisOffset,
579     const DataArrayView& other,
580     ValueType::size_type otherOffset,
581     const RegionLoopRangeType& region);
582 jgs 82
583     /**
584     \brief
585 jgs 108 Copy data into a slice specified by the given region in this view from
586     the given view, using the default offsets in both views.
587 jgs 102
588 jgs 108 \param other - Input -
589     View to copy data from.
590     \param region - Input -
591     Region in this view to copy data to.
592 jgs 102 */
593 jgs 108 void
594     copySliceFrom(const DataArrayView& other,
595     const RegionLoopRangeType& region);
596 jgs 102
597     /**
598     \brief
599 jgs 108 Copy data into a slice specified by the given region and offset in
600     this view from the given view at the given offset.
601 jgs 102
602 jgs 108 \param thisOffset - Input -
603     Copy the slice to this offset in this view.
604     \param other - Input -
605     View to copy data from.
606     \param otherOffset - Input -
607     Copy the slice from this offset in the given view.
608     \param region - Input -
609     Region in this view to copy data to.
610 jgs 102 */
611 jgs 108 void
612     copySliceFrom(ValueType::size_type thisOffset,
613     const DataArrayView& other,
614     ValueType::size_type otherOffset,
615     const RegionLoopRangeType& region);
616 jgs 102
617     /**
618     \brief
619 jgs 108 Perform the unary operation on the data point specified by the view's
620     default offset. Applies the specified operation to each value in the data
621     point.
622    
623     Called by escript::unaryOp.
624    
625     \param operation - Input -
626     Operation to apply. Operation must be a pointer to a function.
627 jgs 82 */
628     template <class UnaryFunction>
629 jgs 108 void
630     unaryOp(UnaryFunction operation);
631 jgs 82
632     /**
633     \brief
634 jgs 108 Perform the unary operation on the data point specified by the given
635     offset. Applies the specified operation to each value in the data
636     point. Operation must be a pointer to a function.
637    
638     Called by escript::unaryOp.
639    
640     \param offset - Input -
641     Apply the operation to data starting at this offset in this view.
642     \param operation - Input -
643     Operation to apply. Must be a pointer to a function.
644 jgs 82 */
645     template <class UnaryFunction>
646 jgs 108 void
647     unaryOp(ValueType::size_type offset,
648     UnaryFunction operation);
649 jgs 82
650     /**
651     \brief
652 jgs 108 Perform the binary operation on the data points specified by the default
653     offsets in this view and in view "right". Applies the specified operation
654     to corresponding values in both data points. Operation must be a pointer
655     to a function.
656    
657     Called by escript::binaryOp.
658    
659     \param right - Input -
660     View to act as RHS in given binary operation.
661     \param operation - Input -
662     Operation to apply. Must be a pointer to a function.
663 jgs 82 */
664     template <class BinaryFunction>
665     void
666 jgs 108 binaryOp(const DataArrayView& right,
667 jgs 82 BinaryFunction operation);
668    
669     /**
670     \brief
671 jgs 108 Perform the binary operation on the data points specified by the given
672     offsets in this view and in view "right". Applies the specified operation
673     to corresponding values in both data points. Operation must be a pointer
674     to a function.
675    
676     Called by escript::binaryOp.
677    
678     \param leftOffset - Input -
679     Apply the operation to data starting at this offset in this view.
680     \param right - Input -
681     View to act as RHS in given binary operation.
682     \param rightOffset - Input -
683     Apply the operation to data starting at this offset in right.
684     \param operation - Input -
685     Operation to apply. Must be a pointer to a function.
686 jgs 82 */
687     template <class BinaryFunction>
688     void
689 jgs 108 binaryOp(ValueType::size_type leftOffset,
690     const DataArrayView& right,
691     ValueType::size_type rightOffset,
692 jgs 82 BinaryFunction operation);
693    
694     /**
695     \brief
696 jgs 108 Perform the binary operation on the data point specified by the default
697     offset in this view using the scalar value "right". Applies the specified
698     operation to values in the data point. Operation must be a pointer to
699     a function.
700    
701     Called by escript::binaryOp.
702    
703     \param right - Input -
704     Value to act as RHS in given binary operation.
705     \param operation - Input -
706     Operation to apply. Must be a pointer to a function.
707 jgs 82 */
708     template <class BinaryFunction>
709     void
710 jgs 108 binaryOp(double right,
711 jgs 82 BinaryFunction operation);
712    
713     /**
714     \brief
715 jgs 108 Perform the binary operation on the data point specified by the given
716     offset in this view using the scalar value "right". Applies the specified
717     operation to values in the data point. Operation must be a pointer
718     to a function.
719    
720     Called by escript::binaryOp.
721    
722     \param offset - Input -
723     Apply the operation to data starting at this offset in this view.
724     \param right - Input -
725     Value to act as RHS in given binary operation.
726     \param operation - Input -
727     Operation to apply. Must be a pointer to a function.
728 jgs 82 */
729     template <class BinaryFunction>
730     void
731 jgs 108 binaryOp(ValueType::size_type offset,
732     double right,
733 jgs 82 BinaryFunction operation);
734    
735     /**
736     \brief
737 jgs 108 Perform the given data point reduction operation on the data point
738     specified by the default offset into the view. Reduces all elements of
739     the data point using the given operation, returning the result as a
740     scalar. Operation must be a pointer to a function.
741    
742     Called by escript::algorithm.
743    
744     \param operation - Input -
745     Operation to apply. Must be a pointer to a function.
746     */
747 jgs 147 template <class BinaryFunction>
748 jgs 108 double
749 jgs 147 reductionOp(BinaryFunction operation,
750     double initial_value) const;
751 jgs 108
752     /**
753     \brief
754 jgs 113 Perform the given data point reduction operation on the data point
755 jgs 108 specified by the given offset into the view. Reduces all elements of
756     the data point using the given operation, returning the result as a
757     scalar. Operation must be a pointer to a function.
758    
759     Called by escript::algorithm.
760    
761     \param offset - Input -
762     Apply the operation to data starting at this offset in this view.
763     \param operation - Input -
764     Operation to apply. Must be a pointer to a function.
765     */
766 jgs 147 template <class BinaryFunction>
767 jgs 108 double
768     reductionOp(ValueType::size_type offset,
769 jgs 147 BinaryFunction operation,
770     double initial_value) const;
771 jgs 108
772     /**
773     \brief
774 gross 576 solves a local eigenvalue problem
775    
776     \param in - Input - The left hand side.
777     \param ev - Output - The eigenvalues
778     */
779     inline
780     void
781     DataArrayView::eigenvalues(const DataArrayView& in,
782     DataArrayView& ev);
783    
784    
785     /**
786     \brief
787 jgs 113 Perform a matrix multiply of the given views.
788     This is purely a utility method and has no bearing on this view.
789 jgs 82
790 jgs 126 NB: Only multiplies together the two given views, ie: two data-points,
791     would need to call this over all data-points to multiply the entire
792     Data objects involved.
793    
794 jgs 82 \param left - Input - The left hand side.
795     \param right - Input - The right hand side.
796     \param result - Output - The result of the operation.
797     */
798 jgs 117 static
799     void
800 jgs 82 matMult(const DataArrayView& left,
801     const DataArrayView& right,
802     DataArrayView& result);
803    
804 jgs 108 /**
805     \brief
806     Determine the shape of the result array for a matrix multiplication
807     of the given views.
808     This is purely a utility method and has no bearing on this view.
809     */
810 jgs 117 static
811     ShapeType
812 jgs 108 determineResultShape(const DataArrayView& left,
813     const DataArrayView& right);
814    
815 jgs 82 protected:
816    
817     private:
818    
819     //
820 jgs 108 // The maximum rank allowed for the shape of any view.
821 jgs 82 static const int m_maxRank=4;
822    
823     //
824 jgs 108 // The data values for the view.
825     // NOTE: This points to data external to the view.
826 jgs 117 // This is just a pointer to an array of ValueType.
827 jgs 82 ValueType* m_data;
828    
829     //
830     // The offset into the data array used by different views.
831 jgs 108 // This is simply an integer specifying a position in the data array
832     // pointed to by m_data.
833 jgs 82 ValueType::size_type m_offset;
834    
835     //
836     // The shape of the data.
837 jgs 108 // This is simply an STL vector specifying the lengths of each dimension
838     // of the shape as ints.
839 jgs 82 ShapeType m_shape;
840    
841     //
842     // The number of values needed for the array.
843 jgs 108 // This can be derived from m_shape by multiplying the size of each dimension, but
844     // is stored here for convenience.
845 jgs 82 int m_noValues;
846    
847     };
848    
849 jgs 108 bool operator==(const DataArrayView& left, const DataArrayView& right);
850     bool operator!=(const DataArrayView& left, const DataArrayView& right);
851    
852 jgs 102 /**
853     \brief
854 jgs 108 Modify region to copy from in order to
855     deal with the case where one range in the region contains identical indexes,
856     eg: <<1,1><0,3><0,3>>
857     This situation implies we want to copy from an object with rank greater than that of this
858     object. eg: we want to copy the values from a two dimensional slice out of a three
859     dimensional object into a two dimensional object.
860     We do this by taking a slice from the other object where one dimension of
861     the slice region is of size 1. So in the above example, we modify the above
862     region like so: <<1,2><0,3><0,3>> and take this slice.
863     */
864     DataArrayView::RegionLoopRangeType
865     getSliceRegionLoopRange(const DataArrayView::RegionType& region);
866    
867     /**
868     \brief
869 jgs 102 Calculate the slice range from the given python key object
870     Used by DataArrayView::getSliceRegion.
871     Returns the python slice object key as a pair of ints where the first
872     member is the start and the second member is the end. the presence of a possible
873     step attribute with value different from one will throw an exception
874    
875     /param key - Input - key object specifying slice range.
876     */
877     std::pair<int,int>
878     getSliceRange(const boost::python::object& key,
879     const int shape);
880    
881 jgs 108 /**
882     Inline function definitions.
883     */
884    
885     template <class UnaryFunction>
886 jgs 82 inline
887 jgs 108 void
888     DataArrayView::unaryOp(UnaryFunction operation)
889 jgs 82 {
890 jgs 108 unaryOp(m_offset,operation);
891 jgs 82 }
892    
893 jgs 108 template <class UnaryFunction>
894 jgs 82 inline
895 jgs 108 void
896     DataArrayView::unaryOp(ValueType::size_type offset,
897     UnaryFunction operation)
898 jgs 82 {
899 jgs 108 EsysAssert((!isEmpty()&&checkOffset(offset)),
900     "Error - Couldn't perform unaryOp due to insufficient storage.");
901     for (ValueType::size_type i=0;i<noValues();i++) {
902     (*m_data)[offset+i]=operation((*m_data)[offset+i]);
903     }
904 jgs 82 }
905    
906 jgs 108 template <class BinaryFunction>
907 jgs 82 inline
908 jgs 108 void
909     DataArrayView::binaryOp(const DataArrayView& right,
910     BinaryFunction operation)
911 jgs 82 {
912 jgs 108 binaryOp(m_offset,right,right.getOffset(),operation);
913 jgs 82 }
914    
915 jgs 108 template <class BinaryFunction>
916 jgs 82 inline
917     void
918 jgs 108 DataArrayView::binaryOp(ValueType::size_type leftOffset,
919     const DataArrayView& right,
920     ValueType::size_type rightOffset,
921     BinaryFunction operation)
922 jgs 82 {
923 jgs 108 EsysAssert(getShape()==right.getShape(),
924     "Error - Couldn't perform binaryOp due to shape mismatch,");
925     EsysAssert((!isEmpty()&&checkOffset(leftOffset)),
926     "Error - Couldn't perform binaryOp due to insufficient storage in left object.");
927     EsysAssert((!right.isEmpty()&&right.checkOffset(rightOffset)),
928     "Error - Couldn't perform binaryOp due to insufficient storage in right object.");
929     for (ValueType::size_type i=0;i<noValues();i++) {
930     (*m_data)[leftOffset+i]=operation((*m_data)[leftOffset+i],(*right.m_data)[rightOffset+i]);
931 jgs 82 }
932     }
933    
934 jgs 108 template <class BinaryFunction>
935 jgs 82 inline
936     void
937 jgs 108 DataArrayView::binaryOp(double right,
938     BinaryFunction operation)
939 jgs 82 {
940 jgs 108 binaryOp(m_offset,right,operation);
941 jgs 82 }
942    
943 jgs 108 template <class BinaryFunction>
944 jgs 82 inline
945 jgs 108 void
946     DataArrayView::binaryOp(ValueType::size_type offset,
947     double right,
948     BinaryFunction operation)
949 jgs 102 {
950 jgs 108 EsysAssert((!isEmpty()&&checkOffset(offset)),
951 jgs 113 "Error - Couldn't perform binaryOp due to insufficient storage in left object.");
952 jgs 108 for (ValueType::size_type i=0;i<noValues();i++) {
953     (*m_data)[offset+i]=operation((*m_data)[offset+i],right);
954 jgs 102 }
955     }
956    
957 jgs 147 template <class BinaryFunction>
958 jgs 102 inline
959     double
960 jgs 147 DataArrayView::reductionOp(BinaryFunction operation,
961     double initial_value) const
962 jgs 82 {
963 jgs 147 return reductionOp(m_offset,operation,initial_value);
964 jgs 82 }
965    
966 jgs 147 template <class BinaryFunction>
967 jgs 82 inline
968 jgs 108 double
969     DataArrayView::reductionOp(ValueType::size_type offset,
970 jgs 147 BinaryFunction operation,
971     double initial_value) const
972 jgs 82 {
973 jgs 108 EsysAssert((!isEmpty()&&checkOffset(offset)),
974     "Error - Couldn't perform reductionOp due to insufficient storage.");
975 jgs 147 double current_value=initial_value;
976 jgs 108 for (ValueType::size_type i=0;i<noValues();i++) {
977 jgs 147 current_value=operation(current_value,(*m_data)[offset+i]);
978 jgs 82 }
979 jgs 147 return current_value;
980 jgs 82 }
981    
982     inline
983 jgs 108 DataArrayView::ValueType::size_type
984     DataArrayView::relIndex() const
985 jgs 82 {
986 jgs 108 EsysAssert((getRank()==0),"Incorrect number of indices for the rank of this object.");
987     return 0;
988 jgs 82 }
989    
990     inline
991 jgs 108 DataArrayView::ValueType::size_type
992     DataArrayView::index() const
993 jgs 82 {
994 jgs 108 EsysAssert((getRank()==0),"Incorrect number of indices for the rank of this object.");
995     return (m_offset);
996 jgs 82 }
997    
998     inline
999 jgs 108 DataArrayView::ValueType::size_type
1000     DataArrayView::relIndex(ValueType::size_type i) const
1001 jgs 82 {
1002 jgs 108 EsysAssert((getRank()==1),"Incorrect number of indices for the rank of this object.");
1003     EsysAssert((i < noValues(m_shape)), "Error - Invalid index.");
1004     return i;
1005 jgs 82 }
1006    
1007     inline
1008     DataArrayView::ValueType::size_type
1009     DataArrayView::index(ValueType::size_type i) const
1010     {
1011 jgs 108 EsysAssert((getRank()==1),"Incorrect number of indices for the rank of this object.");
1012     EsysAssert((i < noValues(m_shape)), "Error - Invalid index.");
1013     return (m_offset+i);
1014 jgs 82 }
1015    
1016     inline
1017     DataArrayView::ValueType::size_type
1018     DataArrayView::relIndex(ValueType::size_type i,
1019     ValueType::size_type j) const
1020     {
1021 jgs 108 EsysAssert((getRank()==2),"Incorrect number of indices for the rank of this object.");
1022 jgs 82 ValueType::size_type temp=i+j*m_shape[0];
1023 jgs 108 EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1024 jgs 82 return temp;
1025     }
1026    
1027     inline
1028     DataArrayView::ValueType::size_type
1029     DataArrayView::index(ValueType::size_type i,
1030     ValueType::size_type j) const
1031     {
1032 jgs 108 EsysAssert((getRank()==2),"Incorrect number of indices for the rank of this object.");
1033 jgs 82 ValueType::size_type temp=i+j*m_shape[0];
1034 jgs 108 EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1035     return (m_offset+temp);
1036 jgs 82 }
1037    
1038     inline
1039     DataArrayView::ValueType::size_type
1040     DataArrayView::relIndex(ValueType::size_type i,
1041     ValueType::size_type j,
1042     ValueType::size_type k) const
1043     {
1044 jgs 108 EsysAssert((getRank()==3),"Incorrect number of indices for the rank of this object.");
1045     ValueType::size_type temp=i+j*m_shape[0]+k*m_shape[1]*m_shape[0];
1046     EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1047     return temp;
1048 jgs 82 }
1049    
1050     inline
1051     DataArrayView::ValueType::size_type
1052     DataArrayView::index(ValueType::size_type i,
1053     ValueType::size_type j,
1054     ValueType::size_type k) const
1055     {
1056 jgs 108 EsysAssert((getRank()==3),"Incorrect number of indices for the rank of this object.");
1057     ValueType::size_type temp=i+j*m_shape[0]+k*m_shape[1]*m_shape[0];
1058     EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1059     return (m_offset+temp);
1060 jgs 82 }
1061    
1062     inline
1063     DataArrayView::ValueType::size_type
1064     DataArrayView::relIndex(ValueType::size_type i,
1065     ValueType::size_type j,
1066     ValueType::size_type k,
1067     ValueType::size_type m) const
1068     {
1069 jgs 108 EsysAssert((getRank()==4),"Incorrect number of indices for the rank of this object.");
1070 jgs 82 ValueType::size_type temp=i+j*m_shape[0]+k*m_shape[1]*m_shape[0]+m*m_shape[2]*m_shape[1]*m_shape[0];
1071 jgs 108 EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1072 jgs 82 return temp;
1073     }
1074    
1075     inline
1076     DataArrayView::ValueType::size_type
1077     DataArrayView::index(ValueType::size_type i,
1078     ValueType::size_type j,
1079     ValueType::size_type k,
1080     ValueType::size_type m) const
1081     {
1082 jgs 108 EsysAssert((getRank()==4),"Incorrect number of indices for the rank of this object.");
1083 jgs 82 ValueType::size_type temp=i+j*m_shape[0]+k*m_shape[1]*m_shape[0]+m*m_shape[2]*m_shape[1]*m_shape[0];
1084 jgs 108 EsysAssert((temp < noValues(m_shape)), "Error - Invalid index.");
1085     return (m_offset+temp);
1086 jgs 82 }
1087    
1088     inline
1089     DataArrayView::ValueType::reference
1090 jgs 108 DataArrayView::operator()()
1091 jgs 82 {
1092 jgs 108 return (*m_data)[index()];
1093 jgs 82 }
1094    
1095     inline
1096     DataArrayView::ValueType::const_reference
1097 jgs 108 DataArrayView::operator()() const
1098 jgs 82 {
1099 jgs 108 return (*m_data)[index()];
1100 jgs 82 }
1101    
1102     inline
1103     DataArrayView::ValueType::reference
1104 jgs 108 DataArrayView::operator()(ValueType::size_type i)
1105 jgs 82 {
1106 jgs 108 return (*m_data)[index(i)];
1107 jgs 82 }
1108    
1109     inline
1110     DataArrayView::ValueType::const_reference
1111 jgs 108 DataArrayView::operator()(ValueType::size_type i) const
1112 jgs 82 {
1113 jgs 108 return (*m_data)[index(i)];
1114 jgs 82 }
1115    
1116     inline
1117     DataArrayView::ValueType::reference
1118     DataArrayView::operator()(ValueType::size_type i,
1119     ValueType::size_type j)
1120     {
1121 jgs 108 return (*m_data)[index(i,j)];
1122 jgs 82 }
1123    
1124     inline
1125     DataArrayView::ValueType::const_reference
1126     DataArrayView::operator()(ValueType::size_type i,
1127     ValueType::size_type j) const
1128     {
1129 jgs 108 return (*m_data)[index(i,j)];
1130 jgs 82 }
1131    
1132     inline
1133     DataArrayView::ValueType::reference
1134 jgs 108 DataArrayView::operator()(ValueType::size_type i,
1135     ValueType::size_type j,
1136     ValueType::size_type k)
1137 jgs 82 {
1138 jgs 108 return (*m_data)[index(i,j,k)];
1139 jgs 82 }
1140    
1141     inline
1142     DataArrayView::ValueType::const_reference
1143 jgs 108 DataArrayView::operator()(ValueType::size_type i,
1144     ValueType::size_type j,
1145     ValueType::size_type k) const
1146 jgs 82 {
1147 jgs 108 return (*m_data)[index(i,j,k)];
1148 jgs 82 }
1149    
1150     inline
1151     DataArrayView::ValueType::reference
1152 jgs 108 DataArrayView::operator()(ValueType::size_type i,
1153     ValueType::size_type j,
1154     ValueType::size_type k,
1155     ValueType::size_type m)
1156 jgs 82 {
1157 jgs 108 return (*m_data)[index(i,j,k,m)];
1158 jgs 82 }
1159    
1160     inline
1161     DataArrayView::ValueType::const_reference
1162 jgs 108 DataArrayView::operator()(ValueType::size_type i,
1163     ValueType::size_type j,
1164     ValueType::size_type k,
1165     ValueType::size_type m) const
1166 jgs 82 {
1167 jgs 108 return (*m_data)[index(i,j,k,m)];
1168 jgs 82 }
1169    
1170     } // end of namespace
1171    
1172     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26