# Contents of /trunk/escript/src/DataMaths.h

Revision 2098 - (show annotations)
Tue Nov 25 12:04:36 2008 UTC (10 years, 10 months ago) by phornby
File MIME type: text/plain
File size: 31563 byte(s)
```Fix unsigned/signed comparison.
```
 1 2 /******************************************************* 3 * 4 * Copyright (c) 2003-2008 by University of Queensland 5 * Earth Systems Science Computational Center (ESSCC) 6 * http://www.uq.edu.au/esscc 7 * 8 * Primary Business: Queensland, Australia 9 * Licensed under the Open Software License version 3.0 10 * http://www.opensource.org/licenses/osl-3.0.php 11 * 12 *******************************************************/ 13 14 15 #ifndef escript_DataMaths_20080822_H 16 #define escript_DataMaths_20080822_H 17 #include "DataAbstract.h" 18 #include "DataException.h" 19 #include "LocalOps.h" 20 21 /** 22 \file DataMaths.h 23 \brief Describes binary operations performed on DataVector. 24 25 26 For operations on DataAbstract see BinaryOp.h. 27 For operations on double* see LocalOps.h. 28 */ 29 30 31 namespace escript 32 { 33 namespace DataMaths 34 { 35 36 /** 37 \namespace escript::DataMaths 38 \brief Contains maths operations performed on data vectors. 39 40 In order to properly identify the datapoints, in most cases, the vector, shape and offset of the point must all be supplied. 41 Note that vector in this context refers to a data vector storing datapoints not a mathematical vector. (However, datapoints within the data vector could represent scalars, vectors, matricies, ...). 42 */ 43 44 45 /** 46 \brief 47 Perform the unary operation on the data point specified by the given 48 offset. Applies the specified operation to each value in the data 49 point. Operation must be a pointer to a function. 50 51 Called by escript::unaryOp. 52 53 \param data - vector containing the datapoint 54 \param shape - shape of the point 55 \param offset - offset of the point within data 56 \param operation - Input - 57 Operation to apply. Must be a pointer to a function. 58 */ 59 template 60 void 61 unaryOp(DataTypes::ValueType& data, const DataTypes::ShapeType& shape, 62 DataTypes::ValueType::size_type offset, 63 UnaryFunction operation); 64 65 /** 66 \brief 67 Perform the binary operation on the data points specified by the given 68 offsets in the "left" and "right" vectors. Applies the specified operation 69 to corresponding values in both data points. Operation must be a pointer 70 to a function. 71 72 Called by escript::binaryOp. 73 \param left,right - vectors containing the datapoints 74 \param leftShape,rightShape - shapes of datapoints in the vectors 75 \param leftOffset,rightOffset - beginnings of datapoints in the vectors 76 \param operation - Input - 77 Operation to apply. Must be a pointer to a function. 78 */ 79 template 80 void 81 binaryOp(DataTypes::ValueType& left, 82 const DataTypes::ShapeType& leftShape, 83 DataTypes::ValueType::size_type leftOffset, 84 const DataTypes::ValueType& right, 85 const DataTypes::ShapeType& rightShape, 86 DataTypes::ValueType::size_type rightOffset, 87 BinaryFunction operation); 88 89 /** 90 \brief 91 Perform the binary operation on the data point specified by the given 92 offset in the vector using the scalar value "right". Applies the specified 93 operation to values in the data point. Operation must be a pointer 94 to a function. 95 96 Called by escript::binaryOp. 97 98 \param left - vector containing the datapoints 99 \param shape - shape of datapoint in the vector 100 \param offset - beginning of datapoint in the vector 101 \param right - scalar value for the right hand side of the operation 102 \param operation - Input - 103 Operation to apply. Must be a pointer to a function. 104 */ 105 template 106 void 107 binaryOp(DataTypes::ValueType& left, 108 const DataTypes::ShapeType& shape, 109 DataTypes::ValueType::size_type offset, 110 double right, 111 BinaryFunction operation); 112 113 /** 114 \brief 115 Perform the given data point reduction operation on the data point 116 specified by the given offset into the view. Reduces all elements of 117 the data point using the given operation, returning the result as a 118 scalar. Operation must be a pointer to a function. 119 120 Called by escript::algorithm. 121 122 \param left - vector containing the datapoint 123 \param shape - shape of datapoints in the vector 124 \param offset - beginning of datapoint in the vector 125 \param operation - Input - 126 Operation to apply. Must be a pointer to a function. 127 \param initial_value 128 */ 129 template 130 double 131 reductionOp(const DataTypes::ValueType& left, 132 const DataTypes::ShapeType& shape, 133 DataTypes::ValueType::size_type offset, 134 BinaryFunction operation, 135 double initial_value); 136 137 /** 138 \brief 139 Perform a matrix multiply of the given views. 140 141 NB: Only multiplies together the two given datapoints, 142 would need to call this over all data-points to multiply the entire 143 Data objects involved. 144 145 \param left,right - vectors containing the datapoints 146 \param leftShape,rightShape - shapes of datapoints in the vectors 147 \param leftOffset,rightOffset - beginnings of datapoints in the vectors 148 \param result - Vector to store the resulting datapoint in 149 \param resultShape - expected shape of the resulting datapoint 150 */ 151 ESCRIPT_DLL_API 152 void 153 matMult(const DataTypes::ValueType& left, 154 const DataTypes::ShapeType& leftShape, 155 DataTypes::ValueType::size_type leftOffset, 156 const DataTypes::ValueType& right, 157 const DataTypes::ShapeType& rightShape, 158 DataTypes::ValueType::size_type rightOffset, 159 DataTypes::ValueType& result, 160 const DataTypes::ShapeType& resultShape); 161 // Hmmmm why is there no offset for the result?? 162 163 164 165 166 /** 167 \brief 168 Determine the shape of the result array for a matrix multiplication 169 of the given views. 170 171 \param left,right - shapes of the left and right matricies 172 \return the shape of the matrix which would result from multiplying left and right 173 */ 174 ESCRIPT_DLL_API 175 DataTypes::ShapeType 176 determineResultShape(const DataTypes::ShapeType& left, 177 const DataTypes::ShapeType& right); 178 179 /** 180 \brief 181 computes a symmetric matrix from your square matrix A: (A + transpose(A)) / 2 182 183 \param in - vector containing the matrix A 184 \param inShape - shape of the matrix A 185 \param inOffset - the beginning of A within the vector in 186 \param ev - vector to store the output matrix 187 \param evShape - expected shape of the output matrix 188 \param evOffset - starting location for storing ev in vector ev 189 */ 190 ESCRIPT_DLL_API 191 inline 192 void 193 symmetric(const DataTypes::ValueType& in, 194 const DataTypes::ShapeType& inShape, 195 DataTypes::ValueType::size_type inOffset, 196 DataTypes::ValueType& ev, 197 const DataTypes::ShapeType& evShape, 198 DataTypes::ValueType::size_type evOffset) 199 { 200 if (DataTypes::getRank(inShape) == 2) { 201 int i0, i1; 202 int s0=inShape[0]; 203 int s1=inShape[1]; 204 for (i0=0; i0= (offset+DataTypes::noValues(shape))); 817 } 818 819 template 820 inline 821 void 822 unaryOp(DataTypes::ValueType& data, const DataTypes::ShapeType& shape, 823 DataTypes::ValueType::size_type offset, 824 UnaryFunction operation) 825 { 826 EsysAssert((data.size()>0)&&checkOffset(data,shape,offset), 827 "Error - Couldn't perform unaryOp due to insufficient storage."); 828 DataTypes::ValueType::size_type nVals=DataTypes::noValues(shape); 829 for (DataTypes::ValueType::size_type i=0;i 836 inline 837 void 838 binaryOp(DataTypes::ValueType& left, 839 const DataTypes::ShapeType& leftShape, 840 DataTypes::ValueType::size_type leftOffset, 841 const DataTypes::ValueType& right, 842 const DataTypes::ShapeType& rightShape, 843 DataTypes::ValueType::size_type rightOffset, 844 BinaryFunction operation) 845 { 846 EsysAssert(leftShape==rightShape, 847 "Error - Couldn't perform binaryOp due to shape mismatch,"); 848 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape, leftOffset)), 849 "Error - Couldn't perform binaryOp due to insufficient storage in left object."); 850 EsysAssert(((right.size()>0)&&checkOffset(right,rightShape,rightOffset)), 851 "Error - Couldn't perform binaryOp due to insufficient storage in right object."); 852 for (DataTypes::ValueType::size_type i=0;i 858 inline 859 void 860 binaryOp(DataTypes::ValueType& left, 861 const DataTypes::ShapeType& leftShape, 862 DataTypes::ValueType::size_type offset, 863 double right, 864 BinaryFunction operation) 865 { 866 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape,offset)), 867 "Error - Couldn't perform binaryOp due to insufficient storage in left object."); 868 for (DataTypes::ValueType::size_type i=0;i 874 inline 875 double 876 reductionOp(const DataTypes::ValueType& left, 877 const DataTypes::ShapeType& leftShape, 878 DataTypes::ValueType::size_type offset, 879 BinaryFunction operation, 880 double initial_value) 881 { 882 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape,offset)), 883 "Error - Couldn't perform reductionOp due to insufficient storage."); 884 double current_value=initial_value; 885 for (DataTypes::ValueType::size_type i=0;i