/[escript]/branches/schroedinger/escript/src/Data.h
ViewVC logotype

Annotation of /branches/schroedinger/escript/src/Data.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 813 - (hide annotations)
Mon Aug 21 02:08:47 2006 UTC (13 years, 2 months ago) by ksteube
Original Path: trunk/escript/src/Data.h
File MIME type: text/plain
File size: 43635 byte(s)
Tensor products for Data objects are now computed by a C++ method
C_GeneralTensorProduct, which calls C function matrix_matrix_product
to do the actual calculation.

Can perform product with either input transposed in place, meaning
without first computing the transpose in a separate step.

1 jgs 94 // $Id$
2 jgs 121 /*
3 elspeth 615 ************************************************************
4     * Copyright 2006 by ACcESS MNRF *
5     * *
6     * http://www.access.edu.au *
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     ************************************************************
12 jgs 121 */
13 jgs 94
14 jgs 121 /** \file Data.h */
15 jgs 94
16     #ifndef DATA_H
17     #define DATA_H
18 woo409 757 #include "system_dep.h"
19 jgs 94
20 jgs 474 #include "DataAbstract.h"
21     #include "DataAlgorithm.h"
22     #include "FunctionSpace.h"
23     #include "BinaryOp.h"
24     #include "UnaryOp.h"
25     #include "DataException.h"
26 jgs 94
27     extern "C" {
28 jgs 474 #include "DataC.h"
29 woo409 757 #include "paso/Paso.h"
30 jgs 94 }
31    
32 bcumming 790 #ifndef PASO_MPI
33     #define MPI_Comm long
34     #endif
35    
36 jgs 94 #include <string>
37     #include <algorithm>
38    
39     #include <boost/shared_ptr.hpp>
40     #include <boost/python/object.hpp>
41     #include <boost/python/tuple.hpp>
42     #include <boost/python/numeric.hpp>
43    
44 jgs 121 namespace escript {
45    
46     //
47 jgs 149 // Forward declaration for various implementations of Data.
48 jgs 121 class DataConstant;
49     class DataTagged;
50     class DataExpanded;
51    
52 jgs 94 /**
53     \brief
54 jgs 121 Data creates the appropriate Data object for the given construction
55     arguments.
56 jgs 94
57     Description:
58     Data is essentially a factory class which creates the appropriate Data
59     object for the given construction arguments. It retains control over
60     the object created for the lifetime of the object.
61     The type of Data object referred to may change during the lifetime of
62     the Data object.
63     */
64     class Data {
65    
66     public:
67    
68 jgs 110 // These typedefs allow function names to be cast to pointers
69     // to functions of the appropriate type when calling unaryOp etc.
70 jgs 94 typedef double (*UnaryDFunPtr)(double);
71     typedef double (*BinaryDFunPtr)(double,double);
72    
73     /**
74 jgs 102 Constructors.
75     */
76    
77     /**
78 jgs 94 \brief
79     Default constructor.
80     Creates a DataEmpty object.
81     */
82 woo409 757 ESCRIPT_DLL_API
83 jgs 94 Data();
84    
85     /**
86     \brief
87     Copy constructor.
88     WARNING: Only performs a shallow copy.
89     */
90 woo409 757 ESCRIPT_DLL_API
91 jgs 94 Data(const Data& inData);
92    
93     /**
94     \brief
95     Constructor from another Data object. If "what" is different from the
96 jgs 102 function space of inData the inData are tried to be interpolated to what,
97 jgs 94 otherwise a shallow copy of inData is returned.
98     */
99 woo409 757 ESCRIPT_DLL_API
100 jgs 94 Data(const Data& inData,
101     const FunctionSpace& what);
102    
103     /**
104     \brief
105     Constructor which copies data from a DataArrayView.
106    
107     \param value - Input - Data value for a single point.
108     \param what - Input - A description of what this data represents.
109     \param expanded - Input - Flag, if true fill the entire container with
110     the value. Otherwise a more efficient storage
111     mechanism will be used.
112     */
113 woo409 757 ESCRIPT_DLL_API
114 jgs 94 Data(const DataArrayView& value,
115     const FunctionSpace& what=FunctionSpace(),
116     bool expanded=false);
117    
118     /**
119     \brief
120     Constructor which creates a Data from a DataArrayView shape.
121    
122     \param value - Input - Single value applied to all Data.
123     \param dataPointShape - Input - The shape of each data point.
124     \param what - Input - A description of what this data represents.
125     \param expanded - Input - Flag, if true fill the entire container with
126     the given value. Otherwise a more efficient storage
127     mechanism will be used.
128     */
129 woo409 757 ESCRIPT_DLL_API
130 jgs 94 Data(double value,
131     const DataArrayView::ShapeType& dataPointShape=DataArrayView::ShapeType(),
132     const FunctionSpace& what=FunctionSpace(),
133     bool expanded=false);
134    
135     /**
136     \brief
137     Constructor which performs a deep copy of a region from another Data object.
138    
139     \param inData - Input - Input Data object.
140     \param region - Input - Region to copy.
141     */
142 woo409 757 ESCRIPT_DLL_API
143 jgs 94 Data(const Data& inData,
144     const DataArrayView::RegionType& region);
145    
146     /**
147     \brief
148     Constructor which will create Tagged data if expanded is false.
149     No attempt is made to ensure the tag keys match the tag keys
150     within the function space.
151    
152     \param tagKeys - Input - List of tag values.
153     \param values - Input - List of values, one for each tag.
154     \param defaultValue - Input - A default value, used if tag doesn't exist.
155     \param what - Input - A description of what this data represents.
156     \param expanded - Input - Flag, if true fill the entire container with
157     the appropriate values.
158 jgs 544 ==>*
159 jgs 94 */
160 woo409 757 ESCRIPT_DLL_API
161 jgs 94 Data(const DataTagged::TagListType& tagKeys,
162     const DataTagged::ValueListType& values,
163     const DataArrayView& defaultValue,
164     const FunctionSpace& what=FunctionSpace(),
165     bool expanded=false);
166    
167     /**
168     \brief
169     Constructor which copies data from a python numarray.
170    
171     \param value - Input - Data value for a single point.
172     \param what - Input - A description of what this data represents.
173     \param expanded - Input - Flag, if true fill the entire container with
174     the value. Otherwise a more efficient storage
175     mechanism will be used.
176     */
177 woo409 757 ESCRIPT_DLL_API
178 jgs 94 Data(const boost::python::numeric::array& value,
179     const FunctionSpace& what=FunctionSpace(),
180     bool expanded=false);
181    
182     /**
183     \brief
184     Constructor which copies data from any object that can be converted into
185     a python numarray.
186    
187     \param value - Input - Input data.
188     \param what - Input - A description of what this data represents.
189     \param expanded - Input - Flag, if true fill the entire container with
190     the value. Otherwise a more efficient storage
191     mechanism will be used.
192     */
193 woo409 757 ESCRIPT_DLL_API
194 jgs 94 Data(const boost::python::object& value,
195     const FunctionSpace& what=FunctionSpace(),
196     bool expanded=false);
197    
198     /**
199     \brief
200     Constructor which creates a DataConstant.
201     Copies data from any object that can be converted
202     into a numarray. All other parameters are copied from other.
203    
204     \param value - Input - Input data.
205     \param other - Input - contains all other parameters.
206     */
207 woo409 757 ESCRIPT_DLL_API
208 jgs 94 Data(const boost::python::object& value,
209     const Data& other);
210    
211     /**
212     \brief
213     Constructor which creates a DataConstant of "shape" with constant value.
214     */
215 woo409 757 ESCRIPT_DLL_API
216 jgs 94 Data(double value,
217     const boost::python::tuple& shape=boost::python::make_tuple(),
218     const FunctionSpace& what=FunctionSpace(),
219     bool expanded=false);
220 jgs 151 /**
221     \brief
222     Destructor
223     */
224 woo409 757 ESCRIPT_DLL_API
225 jgs 151 ~Data();
226 jgs 94
227     /**
228     \brief
229 jgs 102 Perform a deep copy.
230 jgs 94 */
231 woo409 757 ESCRIPT_DLL_API
232 jgs 94 void
233 jgs 102 copy(const Data& other);
234 jgs 94
235     /**
236 jgs 102 Member access methods.
237 jgs 94 */
238    
239     /**
240     \brief
241 gross 783 switches on update protection
242    
243     */
244     ESCRIPT_DLL_API
245     void
246     setProtection();
247    
248     /**
249     \brief
250     Returns trueif the data object is protected against update
251    
252     */
253     ESCRIPT_DLL_API
254     bool
255     isProtected() const;
256     /**
257     \brief
258 jgs 121 Return the values of all data-points as a single python numarray object.
259 jgs 117 */
260 woo409 757 ESCRIPT_DLL_API
261 jgs 121 const boost::python::numeric::array
262 jgs 117 convertToNumArray();
263    
264     /**
265     \brief
266 jgs 121 Return the values of all data-points for the given sample as a single python numarray object.
267     */
268 woo409 757 ESCRIPT_DLL_API
269 jgs 121 const boost::python::numeric::array
270     convertToNumArrayFromSampleNo(int sampleNo);
271    
272     /**
273     \brief
274     Return the value of the specified data-point as a single python numarray object.
275     */
276 dhawcroft 789 #ifndef PASO_MPI
277 woo409 757 ESCRIPT_DLL_API
278 jgs 121 const boost::python::numeric::array
279 bcumming 790 convertToNumArrayFromDPNo(int ProcNo,
280     int sampleNo,
281 jgs 121 int dataPointNo);
282 dhawcroft 789 #else
283     ESCRIPT_DLL_API
284     const boost::python::numeric::array
285     convertToNumArrayFromDPNo(int procNo,
286     int sampleNo,
287     int dataPointNo);
288     #endif
289 jgs 121
290 dhawcroft 789
291 jgs 121 /**
292     \brief
293 jgs 149 Fills the expanded Data object from values of a python numarray object.
294     */
295 woo409 757 ESCRIPT_DLL_API
296 jgs 149 void
297     fillFromNumArray(const boost::python::numeric::array);
298    
299     /**
300     \brief
301     Return the tag number associated with the given data-point.
302    
303     The data-point number here corresponds to the data-point number in the
304     numarray returned by convertToNumArray.
305     */
306 woo409 757 ESCRIPT_DLL_API
307 jgs 149 int
308     getTagNumber(int dpno);
309    
310     /**
311     \brief
312 jgs 94 Return the C wrapper for the Data object.
313     */
314 woo409 757 ESCRIPT_DLL_API
315 jgs 102 escriptDataC
316     getDataC();
317 jgs 94
318     /**
319     \brief
320     Return the C wrapper for the Data object - const version.
321     */
322 woo409 757 ESCRIPT_DLL_API
323 jgs 102 escriptDataC
324     getDataC() const;
325 jgs 94
326     /**
327     \brief
328     Write the data as a string.
329     */
330 woo409 757 ESCRIPT_DLL_API
331 jgs 102 inline
332     std::string
333     toString() const
334     {
335     return m_data->toString();
336     }
337 jgs 94
338     /**
339     \brief
340     Return the DataArrayView of the point data. This essentially contains
341     the shape information for each data point although it also may be used
342     to manipulate the point data.
343     */
344 woo409 757 ESCRIPT_DLL_API
345 jgs 94 inline
346     const DataArrayView&
347     getPointDataView() const
348     {
349     return m_data->getPointDataView();
350     }
351    
352     /**
353     \brief
354 jgs 102 Whatever the current Data type make this into a DataExpanded.
355 jgs 94 */
356 woo409 757 ESCRIPT_DLL_API
357 jgs 94 void
358     expand();
359    
360     /**
361     \brief
362 jgs 102 If possible convert this Data to DataTagged. This will only allow
363 jgs 94 Constant data to be converted to tagged. An attempt to convert
364     Expanded data to tagged will throw an exception.
365 jgs 544 ==>*
366 jgs 94 */
367 woo409 757 ESCRIPT_DLL_API
368 jgs 94 void
369     tag();
370    
371     /**
372     \brief
373     Return true if this Data is expanded.
374     */
375 woo409 757 ESCRIPT_DLL_API
376 jgs 94 bool
377     isExpanded() const;
378    
379     /**
380     \brief
381     Return true if this Data is tagged.
382     */
383 woo409 757 ESCRIPT_DLL_API
384 jgs 94 bool
385     isTagged() const;
386    
387     /**
388     \brief
389 jgs 102 Return true if this Data is constant.
390 jgs 94 */
391 woo409 757 ESCRIPT_DLL_API
392 jgs 94 bool
393 jgs 102 isConstant() const;
394 jgs 94
395     /**
396     \brief
397 jgs 102 Return true if this Data is empty.
398 jgs 94 */
399 woo409 757 ESCRIPT_DLL_API
400 jgs 94 bool
401 jgs 102 isEmpty() const;
402 jgs 94
403     /**
404     \brief
405     Return the function space.
406     */
407 woo409 757 ESCRIPT_DLL_API
408 jgs 94 inline
409     const FunctionSpace&
410     getFunctionSpace() const
411     {
412     return m_data->getFunctionSpace();
413     }
414    
415     /**
416     \brief
417     Return a copy of the function space.
418     */
419 woo409 757 ESCRIPT_DLL_API
420 jgs 94 const FunctionSpace
421     getCopyOfFunctionSpace() const;
422    
423     /**
424     \brief
425     Return the domain.
426     */
427 woo409 757 ESCRIPT_DLL_API
428 jgs 94 inline
429     const AbstractDomain&
430     getDomain() const
431     {
432     return getFunctionSpace().getDomain();
433     }
434    
435     /**
436     \brief
437     Return a copy of the domain.
438     */
439 woo409 757 ESCRIPT_DLL_API
440 jgs 94 const AbstractDomain
441     getCopyOfDomain() const;
442    
443     /**
444     \brief
445     Return the rank of the point data.
446     */
447 woo409 757 ESCRIPT_DLL_API
448 jgs 94 inline
449     int
450     getDataPointRank() const
451     {
452     return m_data->getPointDataView().getRank();
453     }
454    
455     /**
456     \brief
457 jgs 102 Return the number of samples.
458 jgs 94 */
459 woo409 757 ESCRIPT_DLL_API
460 jgs 94 inline
461     int
462 jgs 102 getNumSamples() const
463 jgs 94 {
464 jgs 102 return m_data->getNumSamples();
465 jgs 94 }
466    
467     /**
468     \brief
469 jgs 102 Return the number of data points per sample.
470 jgs 94 */
471 woo409 757 ESCRIPT_DLL_API
472 jgs 102 inline
473 jgs 94 int
474 jgs 102 getNumDataPointsPerSample() const
475     {
476     return m_data->getNumDPPSample();
477     }
478 jgs 94
479     /**
480     \brief
481     Return the sample data for the given sample no. This is not the
482     preferred interface but is provided for use by C code.
483     \param sampleNo - Input - the given sample no.
484     */
485 woo409 757 ESCRIPT_DLL_API
486 jgs 102 inline
487 jgs 94 DataAbstract::ValueType::value_type*
488 jgs 102 getSampleData(DataAbstract::ValueType::size_type sampleNo)
489     {
490     return m_data->getSampleData(sampleNo);
491     }
492 jgs 94
493     /**
494     \brief
495     Return the sample data for the given tag. If an attempt is made to
496     access data that isn't tagged an exception will be thrown.
497     \param tag - Input - the tag key.
498     */
499 woo409 757 ESCRIPT_DLL_API
500 jgs 102 inline
501 jgs 94 DataAbstract::ValueType::value_type*
502 jgs 102 getSampleDataByTag(int tag)
503     {
504     return m_data->getSampleDataByTag(tag);
505     }
506 jgs 94
507     /**
508     \brief
509 jgs 110 Assign the given value to the data-points referenced by the given
510     reference number.
511    
512     The value supplied is a python numarray object. The data from this numarray
513     is unpacked into a DataArray, and this is used to set the corresponding
514     data-points in the underlying Data object.
515    
516     If the underlying Data object cannot be accessed via reference numbers, an
517     exception will be thrown.
518    
519     \param ref - Input - reference number.
520     \param value - Input - value to assign to data-points associated with
521     the given reference number.
522     */
523 woo409 757 ESCRIPT_DLL_API
524 jgs 110 void
525     setRefValue(int ref,
526     const boost::python::numeric::array& value);
527    
528     /**
529     \brief
530     Return the values associated with the data-points referenced by the given
531     reference number.
532    
533     The value supplied is a python numarray object. The data from the corresponding
534     data-points in this Data object are packed into the given numarray object.
535    
536     If the underlying Data object cannot be accessed via reference numbers, an
537     exception will be thrown.
538    
539     \param ref - Input - reference number.
540     \param value - Output - object to receive values from data-points
541     associated with the given reference number.
542     */
543 woo409 757 ESCRIPT_DLL_API
544 jgs 110 void
545     getRefValue(int ref,
546     boost::python::numeric::array& value);
547    
548     /**
549     \brief
550 jgs 94 Return a view into the data for the data point specified.
551     NOTE: Construction of the DataArrayView is a relatively expensive
552     operation.
553     \param sampleNo - Input -
554     \param dataPointNo - Input -
555     */
556 woo409 757 ESCRIPT_DLL_API
557 jgs 94 inline
558     DataArrayView
559     getDataPoint(int sampleNo,
560     int dataPointNo)
561     {
562 bcumming 790 return m_data->getDataPoint(sampleNo,dataPointNo);
563 jgs 94 }
564    
565     /**
566     \brief
567     Return a reference to the data point shape.
568     */
569 woo409 757 ESCRIPT_DLL_API
570 jgs 94 const DataArrayView::ShapeType&
571     getDataPointShape() const;
572    
573     /**
574     \brief
575 jgs 102 Return the data point shape as a tuple of integers.
576 jgs 94 */
577 woo409 757 ESCRIPT_DLL_API
578 jgs 121 const boost::python::tuple
579 jgs 94 getShapeTuple() const;
580    
581     /**
582     \brief
583     Return the size of the data point. It is the product of the
584     data point shape dimensions.
585     */
586 woo409 757 ESCRIPT_DLL_API
587 jgs 94 int
588     getDataPointSize() const;
589    
590     /**
591     \brief
592 jgs 102 Return the number of doubles stored for this Data.
593 jgs 94 */
594 woo409 757 ESCRIPT_DLL_API
595 jgs 94 DataArrayView::ValueType::size_type
596     getLength() const;
597    
598     /**
599     \brief
600 jgs 102 Assign the given value to the tag. Implicitly converts this
601     object to type DataTagged. Throws an exception if this object
602     cannot be converted to a DataTagged object.
603     \param tagKey - Input - Integer key.
604     \param value - Input - Value to associate with given key.
605 jgs 544 ==>*
606 jgs 94 */
607 woo409 757 ESCRIPT_DLL_API
608 jgs 102 void
609     setTaggedValue(int tagKey,
610     const boost::python::object& value);
611    
612     /**
613     \brief
614     Assign the given value to the tag. Implicitly converts this
615     object to type DataTagged. Throws an exception if this object
616     cannot be converted to a DataTagged object.
617     \param tagKey - Input - Integer key.
618     \param value - Input - Value to associate with given key.
619 jgs 544 ==>*
620 jgs 102 */
621 woo409 757 ESCRIPT_DLL_API
622 jgs 102 void
623 jgs 121 setTaggedValueFromCPP(int tagKey,
624     const DataArrayView& value);
625 jgs 102
626     /**
627     \brief
628     Copy other Data object into this Data object where mask is positive.
629     */
630 woo409 757 ESCRIPT_DLL_API
631 jgs 102 void
632     copyWithMask(const Data& other,
633     const Data& mask);
634    
635     /**
636     Data object operation methods and operators.
637     */
638    
639     /**
640     \brief
641     Interpolates this onto the given functionspace and returns
642     the result as a Data object.
643 jgs 123 *
644 jgs 102 */
645 woo409 757 ESCRIPT_DLL_API
646 jgs 94 Data
647     interpolate(const FunctionSpace& functionspace) const;
648    
649     /**
650     \brief
651     Calculates the gradient of the data at the data points of functionspace.
652     If functionspace is not present the function space of Function(getDomain()) is used.
653 jgs 123 *
654 jgs 94 */
655 woo409 757 ESCRIPT_DLL_API
656 jgs 94 Data
657     gradOn(const FunctionSpace& functionspace) const;
658    
659 woo409 757 ESCRIPT_DLL_API
660 jgs 94 Data
661     grad() const;
662    
663     /**
664     \brief
665     Calculate the integral over the function space domain.
666 jgs 123 *
667 jgs 94 */
668 woo409 757 ESCRIPT_DLL_API
669 jgs 94 boost::python::numeric::array
670     integrate() const;
671    
672     /**
673     \brief
674     Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.
675 jgs 123 *
676 jgs 94 */
677 woo409 757 ESCRIPT_DLL_API
678 jgs 94 Data
679 gross 698 wherePositive() const;
680 jgs 94
681     /**
682     \brief
683 jgs 102 Return a Data with a 1 for -ive values and a 0 for +ive or 0 values.
684 jgs 123 *
685 jgs 94 */
686 woo409 757 ESCRIPT_DLL_API
687 jgs 94 Data
688 gross 698 whereNegative() const;
689 jgs 102
690     /**
691     \brief
692     Return a Data with a 1 for +ive or 0 values and a 0 for -ive values.
693 jgs 123 *
694 jgs 102 */
695 woo409 757 ESCRIPT_DLL_API
696 jgs 102 Data
697 gross 698 whereNonNegative() const;
698 jgs 94
699     /**
700     \brief
701 jgs 102 Return a Data with a 1 for -ive or 0 values and a 0 for +ive values.
702 jgs 123 *
703 jgs 94 */
704 woo409 757 ESCRIPT_DLL_API
705 jgs 94 Data
706 gross 698 whereNonPositive() const;
707 jgs 94
708     /**
709     \brief
710 jgs 102 Return a Data with a 1 for 0 values and a 0 for +ive or -ive values.
711 jgs 123 *
712 jgs 94 */
713 woo409 757 ESCRIPT_DLL_API
714 jgs 94 Data
715 jgs 571 whereZero(double tol=0.0) const;
716 jgs 94
717     /**
718     \brief
719 jgs 102 Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.
720 jgs 123 *
721 jgs 94 */
722 woo409 757 ESCRIPT_DLL_API
723 jgs 94 Data
724 jgs 571 whereNonZero(double tol=0.0) const;
725 jgs 102
726     /**
727     \brief
728     Return the maximum absolute value of this Data object.
729 jgs 123 *
730 jgs 94 */
731 woo409 757 ESCRIPT_DLL_API
732 jgs 102 double
733     Lsup() const;
734 jgs 94
735     /**
736     \brief
737 jgs 117 Return the minimum absolute value of this Data object.
738 jgs 123 *
739 jgs 117 */
740 woo409 757 ESCRIPT_DLL_API
741 jgs 117 double
742     Linf() const;
743    
744     /**
745     \brief
746 jgs 102 Return the maximum value of this Data object.
747 jgs 123 *
748 jgs 94 */
749 woo409 757 ESCRIPT_DLL_API
750 jgs 102 double
751     sup() const;
752 jgs 94
753     /**
754     \brief
755 jgs 102 Return the minimum value of this Data object.
756 jgs 123 *
757 jgs 94 */
758 woo409 757 ESCRIPT_DLL_API
759 jgs 94 double
760 jgs 102 inf() const;
761 jgs 94
762     /**
763     \brief
764 jgs 102 Return the absolute value of each data point of this Data object.
765 jgs 123 *
766 jgs 94 */
767 woo409 757 ESCRIPT_DLL_API
768 jgs 102 Data
769     abs() const;
770 jgs 94
771     /**
772     \brief
773 jgs 102 Return the maximum value of each data point of this Data object.
774 jgs 123 *
775 jgs 94 */
776 woo409 757 ESCRIPT_DLL_API
777 jgs 102 Data
778     maxval() const;
779 jgs 94
780     /**
781     \brief
782 jgs 102 Return the minimum value of each data point of this Data object.
783 jgs 123 *
784 jgs 94 */
785 woo409 757 ESCRIPT_DLL_API
786 jgs 94 Data
787 jgs 102 minval() const;
788 jgs 94
789     /**
790     \brief
791 jgs 121 Return the (sample number, data-point number) of the data point with
792     the minimum value in this Data object.
793     */
794 woo409 757 ESCRIPT_DLL_API
795 jgs 121 const boost::python::tuple
796     mindp() const;
797    
798 woo409 757 ESCRIPT_DLL_API
799 jgs 148 void
800 bcumming 782 calc_mindp(int& ProcNo,
801     int& SampleNo,
802     int& DataPointNo) const;
803 jgs 121 /**
804     \brief
805 jgs 102 Return the sign of each data point of this Data object.
806     -1 for negative values, zero for zero values, 1 for positive values.
807 jgs 123 *
808 jgs 94 */
809 woo409 757 ESCRIPT_DLL_API
810 jgs 102 Data
811     sign() const;
812 jgs 94
813     /**
814 jgs 123 \brief
815 ksteube 775 Return the symmetric part of a matrix which is half the matrix plus its transpose.
816     *
817     */
818     ESCRIPT_DLL_API
819     Data
820     symmetric() const;
821    
822     /**
823     \brief
824     Return the nonsymmetric part of a matrix which is half the matrix minus its transpose.
825     *
826     */
827     ESCRIPT_DLL_API
828     Data
829     nonsymmetric() const;
830    
831     /**
832     \brief
833     Return the trace of a matrix
834     *
835     */
836     ESCRIPT_DLL_API
837     Data
838 gross 800 trace(int axis_offset) const;
839 ksteube 775
840     /**
841     \brief
842     Transpose each data point of this Data object around the given axis.
843     *
844     */
845     ESCRIPT_DLL_API
846     Data
847     transpose(int axis_offset) const;
848    
849     /**
850     \brief
851 gross 576 Return the eigenvalues of the symmetric part at each data point of this Data object in increasing values.
852     Currently this function is restricted to rank 2, square shape, and dimension 3.
853     *
854     */
855 woo409 757 ESCRIPT_DLL_API
856 gross 576 Data
857     eigenvalues() const;
858    
859     /**
860     \brief
861     Return the eigenvalues and corresponding eigenvcetors of the symmetric part at each data point of this Data object.
862     the eigenvalues are ordered in increasing size where eigenvalues with relative difference less than
863     tol are treated as equal. The eigenvectors are orthogonal, normalized and the sclaed such that the
864     first non-zero entry is positive.
865     Currently this function is restricted to rank 2, square shape, and dimension 3
866     *
867     */
868 woo409 757 ESCRIPT_DLL_API
869 gross 576 const boost::python::tuple
870     eigenvalues_and_eigenvectors(const double tol=1.e-12) const;
871    
872     /**
873     \brief
874 gross 804 swaps the components axis0 and axis1
875 jgs 123 *
876 jgs 102 */
877 woo409 757 ESCRIPT_DLL_API
878 jgs 102 Data
879 gross 804 swapaxes(const int axis0, const int axis1) const;
880 jgs 102
881     /**
882 jgs 123 \brief
883     Return the sin of each data point of this Data object.
884     *
885 jgs 102 */
886 woo409 757 ESCRIPT_DLL_API
887 jgs 102 Data
888 jgs 123 sin() const;
889    
890     /**
891     \brief
892     Return the cos of each data point of this Data object.
893     *
894     */
895 woo409 757 ESCRIPT_DLL_API
896 jgs 123 Data
897     cos() const;
898    
899     /**
900     \brief
901     Return the tan of each data point of this Data object.
902     *
903     */
904 woo409 757 ESCRIPT_DLL_API
905 jgs 123 Data
906     tan() const;
907    
908     /**
909     \brief
910 jgs 150 Return the asin of each data point of this Data object.
911     *
912     */
913 woo409 757 ESCRIPT_DLL_API
914 jgs 150 Data
915     asin() const;
916    
917     /**
918     \brief
919     Return the acos of each data point of this Data object.
920     *
921     */
922 woo409 757 ESCRIPT_DLL_API
923 jgs 150 Data
924     acos() const;
925    
926     /**
927     \brief
928     Return the atan of each data point of this Data object.
929     *
930     */
931 woo409 757 ESCRIPT_DLL_API
932 jgs 150 Data
933     atan() const;
934    
935     /**
936     \brief
937     Return the sinh of each data point of this Data object.
938     *
939     */
940 woo409 757 ESCRIPT_DLL_API
941 jgs 150 Data
942     sinh() const;
943    
944     /**
945     \brief
946     Return the cosh of each data point of this Data object.
947     *
948     */
949 woo409 757 ESCRIPT_DLL_API
950 jgs 150 Data
951     cosh() const;
952    
953     /**
954     \brief
955     Return the tanh of each data point of this Data object.
956     *
957     */
958 woo409 757 ESCRIPT_DLL_API
959 jgs 150 Data
960     tanh() const;
961    
962     /**
963     \brief
964     Return the asinh of each data point of this Data object.
965     *
966     */
967 woo409 757 ESCRIPT_DLL_API
968 jgs 150 Data
969     asinh() const;
970    
971     /**
972     \brief
973     Return the acosh of each data point of this Data object.
974     *
975     */
976 woo409 757 ESCRIPT_DLL_API
977 jgs 150 Data
978     acosh() const;
979    
980     /**
981     \brief
982     Return the atanh of each data point of this Data object.
983     *
984     */
985 woo409 757 ESCRIPT_DLL_API
986 jgs 150 Data
987     atanh() const;
988    
989     /**
990     \brief
991 jgs 123 Return the log to base 10 of each data point of this Data object.
992     *
993     */
994 woo409 757 ESCRIPT_DLL_API
995 jgs 123 Data
996 gross 286 log10() const;
997 jgs 123
998     /**
999     \brief
1000     Return the natural log of each data point of this Data object.
1001     *
1002     */
1003 woo409 757 ESCRIPT_DLL_API
1004 jgs 123 Data
1005 gross 286 log() const;
1006 jgs 123
1007     /**
1008     \brief
1009     Return the exponential function of each data point of this Data object.
1010     *
1011     */
1012 woo409 757 ESCRIPT_DLL_API
1013 jgs 123 Data
1014 jgs 102 exp() const;
1015    
1016     /**
1017 jgs 123 \brief
1018     Return the square root of each data point of this Data object.
1019     *
1020 jgs 102 */
1021 woo409 757 ESCRIPT_DLL_API
1022 jgs 102 Data
1023     sqrt() const;
1024    
1025     /**
1026 jgs 123 \brief
1027     Return the negation of each data point of this Data object.
1028     *
1029 jgs 121 */
1030 woo409 757 ESCRIPT_DLL_API
1031 jgs 121 Data
1032     neg() const;
1033    
1034     /**
1035 jgs 123 \brief
1036     Return the identity of each data point of this Data object.
1037     Simply returns this object unmodified.
1038     *
1039 jgs 121 */
1040 woo409 757 ESCRIPT_DLL_API
1041 jgs 121 Data
1042     pos() const;
1043    
1044     /**
1045 jgs 94 \brief
1046 jgs 102 Return the given power of each data point of this Data object.
1047 jgs 121
1048     \param right Input - the power to raise the object to.
1049 jgs 123 *
1050 jgs 102 */
1051 woo409 757 ESCRIPT_DLL_API
1052 jgs 102 Data
1053     powD(const Data& right) const;
1054    
1055 jgs 121 /**
1056 jgs 123 \brief
1057     Return the given power of each data point of this boost python object.
1058    
1059     \param right Input - the power to raise the object to.
1060     *
1061 jgs 121 */
1062 woo409 757 ESCRIPT_DLL_API
1063 jgs 102 Data
1064     powO(const boost::python::object& right) const;
1065    
1066     /**
1067 jgs 123 \brief
1068 gross 699 Return the given power of each data point of this boost python object.
1069    
1070     \param left Input - the bases
1071     *
1072     */
1073    
1074 woo409 757 ESCRIPT_DLL_API
1075 gross 699 Data
1076     rpowO(const boost::python::object& left) const;
1077    
1078     /**
1079     \brief
1080 jgs 123 writes the object to a file in the DX file format
1081 jgs 104 */
1082 woo409 757 ESCRIPT_DLL_API
1083 jgs 104 void
1084     saveDX(std::string fileName) const;
1085    
1086     /**
1087 jgs 123 \brief
1088     writes the object to a file in the VTK file format
1089 jgs 110 */
1090 woo409 757 ESCRIPT_DLL_API
1091 jgs 110 void
1092     saveVTK(std::string fileName) const;
1093    
1094     /**
1095 jgs 102 \brief
1096     Overloaded operator +=
1097     \param right - Input - The right hand side.
1098 jgs 123 *
1099 jgs 102 */
1100 woo409 757 ESCRIPT_DLL_API
1101 jgs 102 Data& operator+=(const Data& right);
1102 woo409 757 ESCRIPT_DLL_API
1103 jgs 102 Data& operator+=(const boost::python::object& right);
1104    
1105     /**
1106     \brief
1107     Overloaded operator -=
1108     \param right - Input - The right hand side.
1109 jgs 123 *
1110 jgs 102 */
1111 woo409 757 ESCRIPT_DLL_API
1112 jgs 102 Data& operator-=(const Data& right);
1113 woo409 757 ESCRIPT_DLL_API
1114 jgs 102 Data& operator-=(const boost::python::object& right);
1115    
1116     /**
1117     \brief
1118     Overloaded operator *=
1119     \param right - Input - The right hand side.
1120 jgs 123 *
1121 jgs 102 */
1122 woo409 757 ESCRIPT_DLL_API
1123 jgs 102 Data& operator*=(const Data& right);
1124 woo409 757 ESCRIPT_DLL_API
1125 jgs 102 Data& operator*=(const boost::python::object& right);
1126    
1127     /**
1128     \brief
1129     Overloaded operator /=
1130     \param right - Input - The right hand side.
1131 jgs 123 *
1132 jgs 102 */
1133 woo409 757 ESCRIPT_DLL_API
1134 jgs 102 Data& operator/=(const Data& right);
1135 woo409 757 ESCRIPT_DLL_API
1136 jgs 102 Data& operator/=(const boost::python::object& right);
1137    
1138     /**
1139     \brief
1140 jgs 94 Returns true if this can be interpolated to functionspace.
1141     */
1142 woo409 757 ESCRIPT_DLL_API
1143 jgs 94 bool
1144     probeInterpolation(const FunctionSpace& functionspace) const;
1145    
1146     /**
1147 jgs 102 Data object slicing methods.
1148     */
1149    
1150     /**
1151 jgs 94 \brief
1152 jgs 102 Returns a slice from this Data object.
1153    
1154     /description
1155     Implements the [] get operator in python.
1156     Calls getSlice.
1157    
1158     \param key - Input - python slice tuple specifying
1159     slice to return.
1160 jgs 94 */
1161 woo409 757 ESCRIPT_DLL_API
1162 jgs 102 Data
1163     getItem(const boost::python::object& key) const;
1164    
1165     /**
1166     \brief
1167     Copies slice from value into this Data object.
1168    
1169     Implements the [] set operator in python.
1170     Calls setSlice.
1171    
1172     \param key - Input - python slice tuple specifying
1173     slice to copy from value.
1174     \param value - Input - Data object to copy from.
1175     */
1176 woo409 757 ESCRIPT_DLL_API
1177 jgs 94 void
1178 jgs 102 setItemD(const boost::python::object& key,
1179     const Data& value);
1180 jgs 94
1181 woo409 757 ESCRIPT_DLL_API
1182 jgs 102 void
1183     setItemO(const boost::python::object& key,
1184     const boost::python::object& value);
1185    
1186     // These following public methods should be treated as private.
1187    
1188 jgs 94 /**
1189     \brief
1190 jgs 102 Perform the given unary operation on every element of every data point in
1191     this Data object.
1192 jgs 94 */
1193 jgs 102 template <class UnaryFunction>
1194 woo409 757 ESCRIPT_DLL_API
1195 jgs 102 inline
1196 jgs 94 void
1197 jgs 102 unaryOp(UnaryFunction operation);
1198    
1199     /**
1200     \brief
1201     Return a Data object containing the specified slice of
1202     this Data object.
1203     \param region - Input - Region to copy.
1204 jgs 123 *
1205 jgs 94 */
1206 woo409 757 ESCRIPT_DLL_API
1207 jgs 102 Data
1208     getSlice(const DataArrayView::RegionType& region) const;
1209 jgs 94
1210 jgs 102 /**
1211     \brief
1212     Copy the specified slice from the given value into this
1213     Data object.
1214     \param value - Input - Data to copy from.
1215     \param region - Input - Region to copy.
1216 jgs 123 *
1217 jgs 102 */
1218 woo409 757 ESCRIPT_DLL_API
1219 jgs 102 void
1220     setSlice(const Data& value,
1221     const DataArrayView::RegionType& region);
1222    
1223 jgs 119 /**
1224     \brief
1225     Archive the current Data object to the given file.
1226     \param fileName - Input - file to archive to.
1227     */
1228 woo409 757 ESCRIPT_DLL_API
1229 jgs 119 void
1230     archiveData(const std::string fileName);
1231    
1232     /**
1233     \brief
1234     Extract the Data object archived in the given file, overwriting
1235     the current Data object.
1236     Note - the current object must be of type DataEmpty.
1237     \param fileName - Input - file to extract from.
1238 jgs 121 \param fspace - Input - a suitable FunctionSpace descibing the data.
1239 jgs 119 */
1240 woo409 757 ESCRIPT_DLL_API
1241 jgs 119 void
1242     extractData(const std::string fileName,
1243     const FunctionSpace& fspace);
1244    
1245 bcumming 751
1246     /**
1247     \brief
1248     print the data values to stdout. Used for debugging
1249     */
1250 bcumming 782 ESCRIPT_DLL_API
1251     void
1252     print(void);
1253 bcumming 751
1254 bcumming 782 /**
1255     \brief
1256     return the MPI rank number of the local data
1257     MPI_COMM_WORLD is assumed and the result of MPI_Comm_size()
1258     is returned
1259     */
1260     ESCRIPT_DLL_API
1261     int
1262     get_MPIRank(void) const;
1263    
1264     /**
1265     \brief
1266     return the MPI rank number of the local data
1267     MPI_COMM_WORLD is assumed and the result of MPI_Comm_rank()
1268     is returned
1269     */
1270     ESCRIPT_DLL_API
1271     int
1272     get_MPISize(void) const;
1273    
1274     /**
1275     \brief
1276     return the MPI rank number of the local data
1277     MPI_COMM_WORLD is assumed and returned.
1278     */
1279     ESCRIPT_DLL_API
1280     MPI_Comm
1281     get_MPIComm(void) const;
1282 ksteube 813
1283     /**
1284     \brief
1285     return the object produced by the factory, which is a DataConstant or DataExpanded
1286     */
1287     ESCRIPT_DLL_API
1288     DataAbstract*
1289     borrowData(void) const;
1290    
1291 jgs 102 protected:
1292    
1293 jgs 94 private:
1294    
1295     /**
1296     \brief
1297 jgs 102 Check *this and the right operand are compatible. Throws
1298     an exception if they aren't.
1299     \param right - Input - The right hand side.
1300     */
1301     inline
1302     void
1303     operandCheck(const Data& right) const
1304     {
1305     return m_data->operandCheck(*(right.m_data.get()));
1306     }
1307    
1308     /**
1309     \brief
1310     Perform the specified reduction algorithm on every element of every data point in
1311 jgs 113 this Data object according to the given function and return the single value result.
1312 jgs 102 */
1313 jgs 147 template <class BinaryFunction>
1314 jgs 102 inline
1315     double
1316 jgs 147 algorithm(BinaryFunction operation,
1317     double initial_value) const;
1318 jgs 102
1319 jgs 113 /**
1320     \brief
1321     Reduce each data-point in this Data object using the given operation. Return a Data
1322     object with the same number of data-points, but with each data-point containing only
1323     one value - the result of the reduction operation on the corresponding data-point in
1324     this Data object
1325     */
1326 jgs 147 template <class BinaryFunction>
1327 jgs 106 inline
1328     Data
1329 jgs 147 dp_algorithm(BinaryFunction operation,
1330     double initial_value) const;
1331 jgs 106
1332 jgs 102 /**
1333     \brief
1334     Perform the given binary operation on all of the data's elements.
1335     The underlying type of the right hand side (right) determines the final
1336     type of *this after the operation. For example if the right hand side
1337     is expanded *this will be expanded if necessary.
1338     RHS is a Data object.
1339     */
1340     template <class BinaryFunction>
1341     inline
1342     void
1343     binaryOp(const Data& right,
1344     BinaryFunction operation);
1345    
1346     /**
1347     \brief
1348     Perform the given binary operation on all of the data's elements.
1349     RHS is a boost::python object.
1350     */
1351     template <class BinaryFunction>
1352     inline
1353     void
1354     binaryOp(const boost::python::object& right,
1355     BinaryFunction operation);
1356    
1357     /**
1358     \brief
1359     Convert the data type of the RHS to match this.
1360     \param right - Input - data type to match.
1361     */
1362     void
1363     typeMatchLeft(Data& right) const;
1364    
1365     /**
1366     \brief
1367     Convert the data type of this to match the RHS.
1368     \param right - Input - data type to match.
1369     */
1370     void
1371     typeMatchRight(const Data& right);
1372    
1373     /**
1374     \brief
1375 jgs 94 Construct a Data object of the appropriate type.
1376     */
1377     template <class IValueType>
1378     void
1379     initialise(const IValueType& value,
1380     const FunctionSpace& what,
1381     bool expanded);
1382    
1383     /**
1384     \brief
1385     Reshape the data point if the data point is currently rank 0.
1386     Will throw an exception if the data points are not rank 0.
1387     The original data point value is used for all values of the new
1388     data point.
1389     */
1390     void
1391     reshapeDataPoint(const DataArrayView::ShapeType& shape);
1392    
1393     //
1394 gross 783 // flag to protect the data object against any update
1395     bool m_protected;
1396    
1397     //
1398 jgs 102 // pointer to the actual data object
1399 jgs 94 boost::shared_ptr<DataAbstract> m_data;
1400    
1401 jgs 123 //
1402     // pointer to the internal profiling data
1403     struct profDataEntry *profData;
1404    
1405 jgs 94 };
1406    
1407     template <class IValueType>
1408     void
1409     Data::initialise(const IValueType& value,
1410     const FunctionSpace& what,
1411     bool expanded)
1412     {
1413     //
1414     // Construct a Data object of the appropriate type.
1415     // Construct the object first as there seems to be a bug which causes
1416     // undefined behaviour if an exception is thrown during construction
1417     // within the shared_ptr constructor.
1418     if (expanded) {
1419     DataAbstract* temp=new DataExpanded(value,what);
1420 jgs 102 boost::shared_ptr<DataAbstract> temp_data(temp);
1421     m_data=temp_data;
1422 jgs 94 } else {
1423     DataAbstract* temp=new DataConstant(value,what);
1424 jgs 102 boost::shared_ptr<DataAbstract> temp_data(temp);
1425     m_data=temp_data;
1426 jgs 94 }
1427     }
1428    
1429 jgs 102 /**
1430     Binary Data object operators.
1431     */
1432 jgs 94
1433     /**
1434     \brief
1435     Operator+
1436     Takes two Data objects.
1437     */
1438 woo409 757 ESCRIPT_DLL_API Data operator+(const Data& left, const Data& right);
1439 jgs 94
1440     /**
1441     \brief
1442     Operator-
1443     Takes two Data objects.
1444     */
1445 woo409 757 ESCRIPT_DLL_API Data operator-(const Data& left, const Data& right);
1446 jgs 94
1447     /**
1448     \brief
1449     Operator*
1450     Takes two Data objects.
1451     */
1452 woo409 757 ESCRIPT_DLL_API Data operator*(const Data& left, const Data& right);
1453 jgs 94
1454     /**
1455     \brief
1456     Operator/
1457     Takes two Data objects.
1458     */
1459 woo409 757 ESCRIPT_DLL_API Data operator/(const Data& left, const Data& right);
1460 jgs 94
1461     /**
1462     \brief
1463     Operator+
1464     Takes LHS Data object and RHS python::object.
1465     python::object must be convertable to Data type.
1466     */
1467 woo409 757 ESCRIPT_DLL_API Data operator+(const Data& left, const boost::python::object& right);
1468 jgs 94
1469     /**
1470     \brief
1471     Operator-
1472     Takes LHS Data object and RHS python::object.
1473     python::object must be convertable to Data type.
1474     */
1475 woo409 757 ESCRIPT_DLL_API Data operator-(const Data& left, const boost::python::object& right);
1476 jgs 94
1477     /**
1478     \brief
1479     Operator*
1480     Takes LHS Data object and RHS python::object.
1481     python::object must be convertable to Data type.
1482     */
1483 woo409 757 ESCRIPT_DLL_API Data operator*(const Data& left, const boost::python::object& right);
1484 jgs 94
1485     /**
1486     \brief
1487     Operator/
1488     Takes LHS Data object and RHS python::object.
1489     python::object must be convertable to Data type.
1490     */
1491 woo409 757 ESCRIPT_DLL_API Data operator/(const Data& left, const boost::python::object& right);
1492 jgs 94
1493     /**
1494     \brief
1495     Operator+
1496     Takes LHS python::object and RHS Data object.
1497     python::object must be convertable to Data type.
1498     */
1499 woo409 757 ESCRIPT_DLL_API Data operator+(const boost::python::object& left, const Data& right);
1500 jgs 94
1501     /**
1502     \brief
1503     Operator-
1504     Takes LHS python::object and RHS Data object.
1505     python::object must be convertable to Data type.
1506     */
1507 woo409 757 ESCRIPT_DLL_API Data operator-(const boost::python::object& left, const Data& right);
1508 jgs 94
1509     /**
1510     \brief
1511     Operator*
1512     Takes LHS python::object and RHS Data object.
1513     python::object must be convertable to Data type.
1514     */
1515 woo409 757 ESCRIPT_DLL_API Data operator*(const boost::python::object& left, const Data& right);
1516 jgs 94
1517     /**
1518     \brief
1519     Operator/
1520     Takes LHS python::object and RHS Data object.
1521     python::object must be convertable to Data type.
1522     */
1523 woo409 757 ESCRIPT_DLL_API Data operator/(const boost::python::object& left, const Data& right);
1524 jgs 94
1525     /**
1526     \brief
1527     Output operator
1528     */
1529 woo409 757 ESCRIPT_DLL_API std::ostream& operator<<(std::ostream& o, const Data& data);
1530 jgs 94
1531     /**
1532     \brief
1533 ksteube 813 Compute a tensor product of two Data objects
1534     \param arg0 - Input - Data object
1535     \param arg1 - Input - Data object
1536     \param axis_offset - Input - axis offset
1537     \param transpose - Input - 0: transpose neither, 1: transpose arg0, 2: transpose arg1
1538     */
1539     ESCRIPT_DLL_API
1540     Data
1541     C_GeneralTensorProduct(Data& arg0,
1542     Data& arg1,
1543     int axis_offset=0,
1544     int transpose=0);
1545    
1546     /**
1547     \brief
1548 jgs 94 Return true if operands are equivalent, else return false.
1549     NB: this operator does very little at this point, and isn't to
1550     be relied on. Requires further implementation.
1551     */
1552 woo409 757 //ESCRIPT_DLL_API bool operator==(const Data& left, const Data& right);
1553 jgs 94
1554 jgs 102 /**
1555     \brief
1556     Perform the given binary operation with this and right as operands.
1557     Right is a Data object.
1558     */
1559 jgs 94 template <class BinaryFunction>
1560     inline
1561     void
1562     Data::binaryOp(const Data& right,
1563     BinaryFunction operation)
1564     {
1565     //
1566     // if this has a rank of zero promote it to the rank of the RHS
1567     if (getPointDataView().getRank()==0 && right.getPointDataView().getRank()!=0) {
1568     reshapeDataPoint(right.getPointDataView().getShape());
1569     }
1570     //
1571     // initially make the temporary a shallow copy
1572     Data tempRight(right);
1573     if (getFunctionSpace()!=right.getFunctionSpace()) {
1574     if (right.probeInterpolation(getFunctionSpace())) {
1575     //
1576     // an interpolation is required so create a new Data
1577     tempRight=Data(right,this->getFunctionSpace());
1578     } else if (probeInterpolation(right.getFunctionSpace())) {
1579     //
1580     // interpolate onto the RHS function space
1581     Data tempLeft(*this,right.getFunctionSpace());
1582     m_data=tempLeft.m_data;
1583     }
1584     }
1585     operandCheck(tempRight);
1586     //
1587     // ensure this has the right type for the RHS
1588 jgs 102 typeMatchRight(tempRight);
1589 jgs 94 //
1590     // Need to cast to the concrete types so that the correct binaryOp
1591     // is called.
1592     if (isExpanded()) {
1593     //
1594     // Expanded data will be done in parallel, the right hand side can be
1595     // of any data type
1596     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1597     EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1598     escript::binaryOp(*leftC,*(tempRight.m_data.get()),operation);
1599     } else if (isTagged()) {
1600     //
1601     // Tagged data is operated on serially, the right hand side can be
1602     // either DataConstant or DataTagged
1603     DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1604     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1605     if (right.isTagged()) {
1606     DataTagged* rightC=dynamic_cast<DataTagged*>(tempRight.m_data.get());
1607     EsysAssert((rightC!=0), "Programming error - casting to DataTagged.");
1608     escript::binaryOp(*leftC,*rightC,operation);
1609     } else {
1610     DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1611     EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");
1612     escript::binaryOp(*leftC,*rightC,operation);
1613     }
1614 jgs 102 } else if (isConstant()) {
1615 jgs 94 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1616     DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1617     EsysAssert((leftC!=0 && rightC!=0), "Programming error - casting to DataConstant.");
1618     escript::binaryOp(*leftC,*rightC,operation);
1619     }
1620     }
1621    
1622 jgs 102 /**
1623     \brief
1624     Perform the given binary operation with this and right as operands.
1625     Right is a boost::python object.
1626     */
1627 jgs 94 template <class BinaryFunction>
1628     inline
1629     void
1630     Data::binaryOp(const boost::python::object& right,
1631     BinaryFunction operation)
1632     {
1633     DataArray temp(right);
1634     //
1635     // if this has a rank of zero promote it to the rank of the RHS.
1636     if (getPointDataView().getRank()==0 && temp.getView().getRank()!=0) {
1637     reshapeDataPoint(temp.getView().getShape());
1638     }
1639     //
1640     // Always allow scalar values for the RHS but check other shapes
1641     if (temp.getView().getRank()!=0) {
1642     if (!getPointDataView().checkShape(temp.getView().getShape())) {
1643     throw DataException(getPointDataView().createShapeErrorMessage(
1644     "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));
1645     }
1646     }
1647     if (isExpanded()) {
1648     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1649     EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");
1650     escript::binaryOp(*leftC,temp.getView(),operation);
1651     } else if (isTagged()) {
1652     DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1653     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1654     escript::binaryOp(*leftC,temp.getView(),operation);
1655 jgs 102 } else if (isConstant()) {
1656 jgs 94 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1657     EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");
1658     escript::binaryOp(*leftC,temp.getView(),operation);
1659     }
1660     }
1661    
1662 jgs 102 /**
1663     \brief
1664     Perform the given unary operation on other and return the result.
1665     Given operation is performed on each element of each data point, thus
1666     argument object is a rank n Data object, and returned object is a rank n
1667     Data object.
1668     Calls Data::unaryOp.
1669     */
1670 jgs 94 template <class UnaryFunction>
1671     inline
1672 jgs 102 Data
1673     unaryOp(const Data& other,
1674     UnaryFunction operation)
1675     {
1676     Data result;
1677     result.copy(other);
1678     result.unaryOp(operation);
1679     return result;
1680     }
1681    
1682     /**
1683     \brief
1684     Perform the given unary operation on this.
1685     Given operation is performed on each element of each data point.
1686     Calls escript::unaryOp.
1687     */
1688     template <class UnaryFunction>
1689     inline
1690 jgs 94 void
1691     Data::unaryOp(UnaryFunction operation)
1692     {
1693     if (isExpanded()) {
1694     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1695     EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1696     escript::unaryOp(*leftC,operation);
1697     } else if (isTagged()) {
1698     DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1699     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1700     escript::unaryOp(*leftC,operation);
1701 jgs 102 } else if (isConstant()) {
1702 jgs 94 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1703     EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1704     escript::unaryOp(*leftC,operation);
1705     }
1706     }
1707    
1708 jgs 102 /**
1709     \brief
1710     Perform the given Data object reduction algorithm on this and return the result.
1711     Given operation combines each element of each data point, thus argument
1712     object (*this) is a rank n Data object, and returned object is a scalar.
1713     Calls escript::algorithm.
1714     */
1715 jgs 147 template <class BinaryFunction>
1716 jgs 94 inline
1717     double
1718 jgs 147 Data::algorithm(BinaryFunction operation, double initial_value) const
1719 jgs 94 {
1720     if (isExpanded()) {
1721     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1722     EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1723 jgs 147 return escript::algorithm(*leftC,operation,initial_value);
1724 jgs 102 } else if (isTagged()) {
1725 jgs 94 DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1726     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1727 jgs 147 return escript::algorithm(*leftC,operation,initial_value);
1728 jgs 102 } else if (isConstant()) {
1729 jgs 94 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1730     EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1731 jgs 147 return escript::algorithm(*leftC,operation,initial_value);
1732 jgs 94 }
1733 jgs 102 return 0;
1734 jgs 94 }
1735    
1736 jgs 102 /**
1737     \brief
1738     Perform the given data point reduction algorithm on data and return the result.
1739     Given operation combines each element within each data point into a scalar,
1740     thus argument object is a rank n Data object, and returned object is a
1741     rank 0 Data object.
1742     Calls escript::dp_algorithm.
1743     */
1744 jgs 147 template <class BinaryFunction>
1745 jgs 94 inline
1746     Data
1747 jgs 147 Data::dp_algorithm(BinaryFunction operation, double initial_value) const
1748 jgs 94 {
1749 jgs 106 if (isExpanded()) {
1750 jgs 559 Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1751 jgs 106 DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
1752 jgs 102 DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
1753     EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
1754     EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
1755 jgs 147 escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
1756 jgs 559 return result;
1757 jgs 106 } else if (isTagged()) {
1758     DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
1759 jgs 562 DataArrayView::ShapeType viewShape;
1760     DataArrayView::ValueType viewData(1);
1761     viewData[0]=0;
1762     DataArrayView defaultValue(viewData,viewShape);
1763 jgs 559 DataTagged::TagListType keys;
1764 jgs 562 DataTagged::ValueListType values;
1765 jgs 559 DataTagged::DataMapType::const_iterator i;
1766     for (i=dataT->getTagLookup().begin();i!=dataT->getTagLookup().end();i++) {
1767     keys.push_back(i->first);
1768 jgs 562 values.push_back(defaultValue);
1769 jgs 559 }
1770     Data result(keys,values,defaultValue,getFunctionSpace());
1771 jgs 102 DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());
1772     EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
1773     EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");
1774 jgs 147 escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
1775 jgs 559 return result;
1776 jgs 106 } else if (isConstant()) {
1777 jgs 559 Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1778 jgs 106 DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
1779 jgs 102 DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
1780     EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
1781     EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
1782 jgs 147 escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
1783 jgs 559 return result;
1784 jgs 102 }
1785 jgs 559 Data falseRetVal; // to keep compiler quiet
1786     return falseRetVal;
1787 jgs 94 }
1788    
1789     }
1790     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26