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

Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (10 years, 7 months ago) by jfenwick
File MIME type: text/plain
File size: 34492 byte(s)
```Merged noarrayview branch onto trunk.

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

 ViewVC Help Powered by ViewVC 1.1.26