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

Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (10 years, 4 months ago) by ksteube
File MIME type: text/plain
File size: 34407 byte(s)
```Copyright updated in all files

```
 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 namespace escript 22 { 23 namespace DataMaths 24 { 25 26 /** 27 \namespace escript::DataMaths 28 \brief Contains maths operations performed on data vectors. 29 30 In order to properly identify the datapoints, in most cases, the vector, shape and offset of the point must all be supplied. 31 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, ...). 32 */ 33 34 // /** 35 // \brief 36 // Perform the unary operation on the data point specified by the view's 37 // default offset. Applies the specified operation to each value in the data 38 // point. 39 // 40 // Called by escript::unaryOp. 41 // 42 // \param operation - Input - 43 // Operation to apply. Operation must be a pointer to a function. 44 // */ 45 // template 46 // void 47 // unaryOp(DataAbstract& data, UnaryFunction operation); 48 49 // I'm going to try not to have the above function. It relies on the value of the offset already 50 // being known. I don't want that, offsets need to be explicit. 51 52 53 54 /** 55 \brief 56 Perform the unary operation on the data point specified by the given 57 offset. Applies the specified operation to each value in the data 58 point. Operation must be a pointer to a function. 59 60 Called by escript::unaryOp. 61 62 \param data - vector containing the datapoint 63 \param shape - shape of the point 64 \param offset - offset of the point within data 65 \param operation - Input - 66 Operation to apply. Must be a pointer to a function. 67 */ 68 template 69 void 70 unaryOp(DataTypes::ValueType& data, const DataTypes::ShapeType& shape, 71 DataTypes::ValueType::size_type offset, 72 UnaryFunction operation); 73 74 // /** 75 // \brief 76 // Perform the binary operation on the data points specified by the default 77 // offsets in this view and in view "right". Applies the specified operation 78 // to corresponding values in both data points. Operation must be a pointer 79 // to a function. 80 // 81 // Called by escript::binaryOp. 82 // 83 // \param right - Input - 84 // View to act as RHS in given binary operation. 85 // \param operation - Input - 86 // Operation to apply. Must be a pointer to a function. 87 // */ 88 // template 89 // void 90 // binaryOp(DataAbstract& left, const DataAbstract& right, 91 // BinaryFunction operation); 92 93 // trying to avoid having this one as well. Again, implicit offset 94 95 /** 96 \brief 97 Perform the binary operation on the data points specified by the given 98 offsets in the "left" and "right" vectors. Applies the specified operation 99 to corresponding values in both data points. Operation must be a pointer 100 to a function. 101 102 Called by escript::binaryOp. 103 \param left,right - vectors containing the datapoints 104 \param leftShape,rightShape - shapes of datapoints in the vectors 105 \param leftOffset,rightOffset - beginnings of datapoints in the vectors 106 \param operation - Input - 107 Operation to apply. Must be a pointer to a function. 108 */ 109 template 110 void 111 binaryOp(DataTypes::ValueType& left, 112 const DataTypes::ShapeType& leftShape, 113 DataTypes::ValueType::size_type leftOffset, 114 const DataTypes::ValueType& right, 115 const DataTypes::ShapeType& rightShape, 116 DataTypes::ValueType::size_type rightOffset, 117 BinaryFunction operation); 118 119 // /** 120 // \brief 121 // Perform the binary operation on the data point specified by the default 122 // offset in this view using the scalar value "right". Applies the specified 123 // operation to values in the data point. Operation must be a pointer to 124 // a function. 125 // 126 // Called by escript::binaryOp. 127 // 128 // \param right - Input - 129 // Value to act as RHS in given binary operation. 130 // \param operation - Input - 131 // Operation to apply. Must be a pointer to a function. 132 // */ 133 // template 134 // void 135 // binaryOp(DataAbstract& left, double right, 136 // BinaryFunction operation); 137 138 // Implicit offset again 139 140 141 /** 142 \brief 143 Perform the binary operation on the data point specified by the given 144 offset in the vector using the scalar value "right". Applies the specified 145 operation to values in the data point. Operation must be a pointer 146 to a function. 147 148 Called by escript::binaryOp. 149 150 \param left - vector containing the datapoints 151 \param shape - shape of datapoint in the vector 152 \param offset - beginning of datapoint in the vector 153 \param right - scalar value for the right hand side of the operation 154 \param operation - Input - 155 Operation to apply. Must be a pointer to a function. 156 */ 157 template 158 void 159 binaryOp(DataTypes::ValueType& left, 160 const DataTypes::ShapeType& shape, 161 DataTypes::ValueType::size_type offset, 162 double right, 163 BinaryFunction operation); 164 165 // /** 166 // \brief 167 // Perform the given data point reduction operation on the data point 168 // specified by the default offset into the view. Reduces all elements of 169 // the data point using the given operation, returning the result as a 170 // scalar. Operation must be a pointer to a function. 171 // 172 // Called by escript::algorithm. 173 // 174 // \param operation - Input - 175 // Operation to apply. Must be a pointer to a function. 176 // */ 177 // template 178 // double 179 // reductionOp(const DataAbstract& left, BinaryFunction operation, 180 // double initial_value); 181 182 // implicit offset 183 184 /** 185 \brief 186 Perform the given data point reduction operation on the data point 187 specified by the given offset into the view. Reduces all elements of 188 the data point using the given operation, returning the result as a 189 scalar. Operation must be a pointer to a function. 190 191 Called by escript::algorithm. 192 193 \param left - vector containing the datapoint 194 \param shape - shape of datapoints in the vector 195 \param offset - beginning of datapoint in the vector 196 \param operation - Input - 197 Operation to apply. Must be a pointer to a function. 198 \param initial_value 199 */ 200 template 201 double 202 reductionOp(const DataTypes::ValueType& left, 203 const DataTypes::ShapeType& shape, 204 DataTypes::ValueType::size_type offset, 205 BinaryFunction operation, 206 double initial_value); 207 208 /** 209 \brief 210 Perform a matrix multiply of the given views. 211 212 NB: Only multiplies together the two given datapoints, 213 would need to call this over all data-points to multiply the entire 214 Data objects involved. 215 216 \param left,right - vectors containing the datapoints 217 \param leftShape,rightShape - shapes of datapoints in the vectors 218 \param leftOffset,rightOffset - beginnings of datapoints in the vectors 219 \param result - Vector to store the resulting datapoint in 220 \param resultShape - expected shape of the resulting datapoint 221 */ 222 ESCRIPT_DLL_API 223 void 224 matMult(const DataTypes::ValueType& left, 225 const DataTypes::ShapeType& leftShape, 226 DataTypes::ValueType::size_type leftOffset, 227 const DataTypes::ValueType& right, 228 const DataTypes::ShapeType& rightShape, 229 DataTypes::ValueType::size_type rightOffset, 230 DataTypes::ValueType& result, 231 const DataTypes::ShapeType& resultShape); 232 // Hmmmm why is there no offset for the result?? 233 234 235 236 237 /** 238 \brief 239 Determine the shape of the result array for a matrix multiplication 240 of the given views. 241 242 \param left,right - shapes of the left and right matricies 243 \return the shape of the matrix which would result from multiplying left and right 244 */ 245 ESCRIPT_DLL_API 246 DataTypes::ShapeType 247 determineResultShape(const DataTypes::ShapeType& left, 248 const DataTypes::ShapeType& right); 249 250 /** 251 \brief 252 computes a symmetric matrix from your square matrix A: (A + transpose(A)) / 2 253 254 \param in - vector containing the matrix A 255 \param inShape - shape of the matrix A 256 \param inOffset - the beginning of A within the vector in 257 \param ev - vector to store the output matrix 258 \param evShape - expected shape of the output matrix 259 \param evOffset - starting location for storing ev in vector ev 260 */ 261 ESCRIPT_DLL_API 262 inline 263 void 264 symmetric(const DataTypes::ValueType& in, 265 const DataTypes::ShapeType& inShape, 266 DataTypes::ValueType::size_type inOffset, 267 DataTypes::ValueType& ev, 268 const DataTypes::ShapeType& evShape, 269 DataTypes::ValueType::size_type evOffset) 270 { 271 if (DataTypes::getRank(inShape) == 2) { 272 int i0, i1; 273 int s0=inShape[0]; 274 int s1=inShape[1]; 275 for (i0=0; i0 880 // inline 881 // void 882 // DataArrayView::unaryOp(UnaryFunction operation) 883 // { 884 // unaryOp(m_offset,operation); 885 // } 886 887 888 inline 889 bool 890 checkOffset(const DataTypes::ValueType& data, 891 const DataTypes::ShapeType& shape, 892 DataTypes::ValueType::size_type offset) 893 { 894 return (data.size() >= (offset+DataTypes::noValues(shape))); 895 } 896 897 template 898 inline 899 void 900 unaryOp(DataTypes::ValueType& data, const DataTypes::ShapeType& shape, 901 DataTypes::ValueType::size_type offset, 902 UnaryFunction operation) 903 { 904 EsysAssert((data.size()>0)&&checkOffset(data,shape,offset), 905 "Error - Couldn't perform unaryOp due to insufficient storage."); 906 DataTypes::ValueType::size_type nVals=DataTypes::noValues(shape); 907 for (DataTypes::ValueType::size_type i=0;i 913 // inline 914 // void 915 // binaryOp(const DataArrayView& right, 916 // BinaryFunction operation) 917 // { 918 // binaryOp(m_offset,right,right.getOffset(),operation); 919 // } 920 921 922 template 923 inline 924 void 925 binaryOp(DataTypes::ValueType& left, 926 const DataTypes::ShapeType& leftShape, 927 DataTypes::ValueType::size_type leftOffset, 928 const DataTypes::ValueType& right, 929 const DataTypes::ShapeType& rightShape, 930 DataTypes::ValueType::size_type rightOffset, 931 BinaryFunction operation) 932 { 933 EsysAssert(leftShape==rightShape, 934 "Error - Couldn't perform binaryOp due to shape mismatch,"); 935 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape, leftOffset)), 936 "Error - Couldn't perform binaryOp due to insufficient storage in left object."); 937 EsysAssert(((right.size()>0)&&checkOffset(right,rightShape,rightOffset)), 938 "Error - Couldn't perform binaryOp due to insufficient storage in right object."); 939 for (DataTypes::ValueType::size_type i=0;i 945 inline 946 void 947 binaryOp(DataTypes::ValueType& left, 948 const DataTypes::ShapeType& leftShape, 949 DataTypes::ValueType::size_type offset, 950 double right, 951 BinaryFunction operation) 952 { 953 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape,offset)), 954 "Error - Couldn't perform binaryOp due to insufficient storage in left object."); 955 for (DataTypes::ValueType::size_type i=0;i 961 inline 962 double 963 reductionOp(const DataTypes::ValueType& left, 964 const DataTypes::ShapeType& leftShape, 965 DataTypes::ValueType::size_type offset, 966 BinaryFunction operation, 967 double initial_value) 968 { 969 EsysAssert(((left.size()>0)&&checkOffset(left,leftShape,offset)), 970 "Error - Couldn't perform reductionOp due to insufficient storage."); 971 double current_value=initial_value; 972 for (DataTypes::ValueType::size_type i=0;i