[772] | 1 | // template array class for numerical types
|
---|
| 2 | // R. Ansari, C.Magneville 03/2000
|
---|
| 3 |
|
---|
| 4 | #include "machdefs.h"
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
[922] | 7 | #include <math.h>
|
---|
[772] | 8 | #include "pexceptions.h"
|
---|
| 9 | #include "tarray.h"
|
---|
| 10 |
|
---|
[926] | 11 | /*!
|
---|
| 12 | \class SOPHYA::TArray
|
---|
| 13 | \ingroup TArray
|
---|
[2267] | 14 | Class for template arrays with numerical data types (int, float, complex).
|
---|
[772] | 15 |
|
---|
[2267] | 16 | This class implements arrays with number of dimensions up to
|
---|
[926] | 17 | \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS"
|
---|
[2267] | 18 |
|
---|
| 19 | Standard arithmetic operations on numerical arrays are implemented,
|
---|
| 20 | as well as sub-array manipulation services.
|
---|
| 21 | \b Array is a typedef for double precision floating point arrays ( TArray<r_8> )
|
---|
| 22 | \sa SOPHYA::Range
|
---|
| 23 | \sa SOPHYA::Sequence
|
---|
| 24 | \sa SOPHYA::MathArray
|
---|
| 25 |
|
---|
| 26 | \code
|
---|
| 27 | #include "array.h"
|
---|
| 28 | // ...
|
---|
| 29 | // Creating and initialising a 1-D array of integers
|
---|
| 30 | TArray<int> ia(5);
|
---|
| 31 | EnumeratedSequence es;
|
---|
| 32 | es = 24, 35, 46, 57, 68;
|
---|
| 33 | ia = es;
|
---|
| 34 | cout << "Array<int> ia = " << ia;
|
---|
| 35 | // 2-D array of floats
|
---|
| 36 | TArray<r_4> b(6,4), c(6,4);
|
---|
| 37 | // Initializing b with a constant
|
---|
| 38 | b = 2.71828;
|
---|
| 39 | // Filling c with random numbers
|
---|
| 40 | c = RandomSequence();
|
---|
| 41 | // Arithmetic operations
|
---|
| 42 | TArray<r_4> d = b+0.3f*c;
|
---|
| 43 | cout << "Array<float> d = " << d;
|
---|
| 44 | \endcode
|
---|
| 45 |
|
---|
[926] | 46 | */
|
---|
[772] | 47 |
|
---|
[1389] | 48 | /*! \ingroup TArray
|
---|
| 49 | \typedef sa_size_t
|
---|
| 50 | \brief Array index range and size, defined to be a 4-byte or 8-byte integer
|
---|
| 51 | */
|
---|
| 52 |
|
---|
[772] | 53 | // -------------------------------------------------------
|
---|
| 54 | // Methodes de la classe
|
---|
| 55 | // -------------------------------------------------------
|
---|
| 56 |
|
---|
[894] | 57 | ////////////////////////// Les constructeurs / destructeurs
|
---|
| 58 |
|
---|
| 59 | //! Default constructor
|
---|
[772] | 60 | template <class T>
|
---|
| 61 | TArray<T>::TArray()
|
---|
[804] | 62 | : BaseArray() , mNDBlock()
|
---|
[772] | 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[894] | 66 | //! Constructor
|
---|
| 67 | /*!
|
---|
| 68 | \param ndim : number of dimensions (less or equal to
|
---|
| 69 | \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS")
|
---|
| 70 | \param siz[ndim] : size along each dimension
|
---|
| 71 | \param step : step (same for all dimensions)
|
---|
| 72 | */
|
---|
[772] | 73 | template <class T>
|
---|
[1156] | 74 | TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step)
|
---|
[804] | 75 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1))
|
---|
[772] | 76 | {
|
---|
[1156] | 77 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, sa_size_t)";
|
---|
[772] | 78 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[894] | 81 | //! Constructor
|
---|
| 82 | /*!
|
---|
| 83 | \param nx,ny,nz,nt,nu : sizes along first, second, third, fourth and fifth dimension
|
---|
| 84 | */
|
---|
[772] | 85 | template <class T>
|
---|
[1156] | 86 | TArray<T>::TArray(sa_size_t nx, sa_size_t ny, sa_size_t nz, sa_size_t nt, sa_size_t nu)
|
---|
[804] | 87 | : BaseArray() , mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)*((nt>0)?nt:1)*((nu>0)?nu:1))
|
---|
[772] | 88 | {
|
---|
[1156] | 89 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
[772] | 90 | size[0] = nx; size[1] = ny; size[2] = nz;
|
---|
[804] | 91 | size[3] = nt; size[4] = nu;
|
---|
[1156] | 92 | int_4 ndim = 1;
|
---|
[804] | 93 | if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) && (size[4] > 0) ) ndim = 5;
|
---|
| 94 | else if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) ) ndim = 4;
|
---|
| 95 | else if ((size[1] > 0) && (size[2] > 0)) ndim = 3;
|
---|
[772] | 96 | else if (size[1] > 0) ndim = 2;
|
---|
| 97 | else ndim = 1;
|
---|
[1156] | 98 | string exmsg = "TArray<T>::TArray(sa_size_t, sa_size_t, sa_size_t, sa_size_t, sa_size_t)";
|
---|
[772] | 99 | if (!UpdateSizes(ndim, size, 1, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[894] | 102 | //! Constructor
|
---|
| 103 | /*!
|
---|
| 104 | \param ndim : number of dimensions
|
---|
| 105 | \param siz[ndim] : size along each dimension
|
---|
| 106 | \param db : datas are given by this NDataBlock
|
---|
| 107 | \param share : if true, data are shared, if false they are copied
|
---|
| 108 | \param step : step (same for all dimensions) in data block
|
---|
| 109 | \param offset : offset for first element in data block
|
---|
| 110 | */
|
---|
[772] | 111 | template <class T>
|
---|
[1156] | 112 | TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, NDataBlock<T> & db, bool share, sa_size_t step, sa_size_t offset)
|
---|
[804] | 113 | : BaseArray() , mNDBlock(db, share)
|
---|
[772] | 114 | {
|
---|
[1156] | 115 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, NDataBlock<T> & ... )";
|
---|
[772] | 116 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
[1636] | 117 | if (mNDBlock.Size() < ComputeTotalSize(ndim, siz, step, offset)) {
|
---|
| 118 | exmsg += " DataBlock.Size() < ComputeTotalSize(...) " ;
|
---|
| 119 | throw( ParmError(exmsg) );
|
---|
| 120 | }
|
---|
[772] | 121 | }
|
---|
| 122 |
|
---|
[894] | 123 | //! Constructor
|
---|
| 124 | /*!
|
---|
| 125 | \param ndim : number of dimensions
|
---|
| 126 | \param siz[ndim] : size along each dimension
|
---|
| 127 | \param values : datas are given by this pointer
|
---|
| 128 | \param share : if true, data are shared, if false they are copied
|
---|
| 129 | \param step : step (same for all dimensions) in data block
|
---|
| 130 | \param offset : offset for first element in data block
|
---|
| 131 | \param br : if not NULL, dats are bridge with other datas
|
---|
| 132 | \sa NDataBlock
|
---|
| 133 | */
|
---|
[772] | 134 | template <class T>
|
---|
[1156] | 135 | TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, T* values, sa_size_t step, sa_size_t offset, Bridge* br)
|
---|
[804] | 136 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br)
|
---|
[772] | 137 | {
|
---|
[1156] | 138 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, T* ... )";
|
---|
[772] | 139 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[894] | 142 | //! Constructor by copy
|
---|
[976] | 143 | /*!
|
---|
| 144 | \warning datas are \b SHARED with \b a.
|
---|
| 145 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 146 | */
|
---|
[772] | 147 | template <class T>
|
---|
| 148 | TArray<T>::TArray(const TArray<T>& a)
|
---|
[804] | 149 | : BaseArray() , mNDBlock(a.mNDBlock)
|
---|
[772] | 150 | {
|
---|
| 151 | string exmsg = "TArray<T>::TArray(const TArray<T>&)";
|
---|
| 152 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 153 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[894] | 156 | //! Constructor by copy
|
---|
| 157 | /*!
|
---|
| 158 | \param share : if true, data are shared, if false they are copied
|
---|
| 159 | */
|
---|
[772] | 160 | template <class T>
|
---|
| 161 | TArray<T>::TArray(const TArray<T>& a, bool share)
|
---|
[804] | 162 | : BaseArray() , mNDBlock(a.mNDBlock, share)
|
---|
[772] | 163 | {
|
---|
[1517] | 164 | if (a.NbDimensions() == 0) return;
|
---|
[772] | 165 | string exmsg = "TArray<T>::TArray(const TArray<T>&, bool)";
|
---|
| 166 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 167 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[1081] | 170 | //! Constructor with size and contents copied (after conversion) from a different type TArray
|
---|
| 171 | template <class T>
|
---|
| 172 | TArray<T>::TArray(const BaseArray& a)
|
---|
| 173 | : BaseArray() , mNDBlock()
|
---|
| 174 | {
|
---|
[1517] | 175 | if (a.NbDimensions() == 0) return;
|
---|
[1081] | 176 | string exmsg = "TArray<T>::TArray(const BaseArray&)";
|
---|
| 177 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 178 | mNDBlock.ReSize(totsize_);
|
---|
| 179 | // if (a.mInfo) mInfo = new DVList(*(a.mInfo)); - pb protected !
|
---|
| 180 | ConvertAndCopyElt(a);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[894] | 183 | //! Destructor
|
---|
[772] | 184 | template <class T>
|
---|
| 185 | TArray<T>::~TArray()
|
---|
| 186 | {
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[894] | 189 | ////////////////////////// Les methodes de copie/share
|
---|
| 190 |
|
---|
| 191 | //! Set array equal to \b a and return *this
|
---|
[976] | 192 | /*!
|
---|
[1364] | 193 | If the array is already allocated, CopyElt() is called
|
---|
| 194 | for checking that the two arrays have the same size and
|
---|
| 195 | for copying the array element values. For non allocated
|
---|
| 196 | arrays, CloneOrShare() is called. The array memory
|
---|
| 197 | organization is also copied from \b a.
|
---|
[976] | 198 | \warning Datas are copied (cloned) from \b a.
|
---|
[1364] | 199 | \sa CopyElt
|
---|
| 200 | \sa CloneOrShare
|
---|
[976] | 201 | \sa NDataBlock::operator=(const NDataBlock<T>&)
|
---|
| 202 | */
|
---|
[772] | 203 | template <class T>
|
---|
[804] | 204 | TArray<T>& TArray<T>::Set(const TArray<T>& a)
|
---|
[772] | 205 | {
|
---|
[970] | 206 | if (this == &a) return(*this);
|
---|
| 207 | if (a.NbDimensions() < 1)
|
---|
| 208 | throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! ");
|
---|
| 209 | if (NbDimensions() < 1) CloneOrShare(a);
|
---|
| 210 | else CopyElt(a);
|
---|
[772] | 211 | return(*this);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[1081] | 214 | //! Set array elements equal to the \b a array elements, after conversion
|
---|
| 215 | template <class T>
|
---|
| 216 | TArray<T>& TArray<T>::SetBA(const BaseArray& a)
|
---|
| 217 | {
|
---|
| 218 | if (this == &a) return(*this);
|
---|
| 219 | if (a.NbDimensions() < 1)
|
---|
| 220 | throw RangeCheckError("TArray<T>::SetBA(a ) - Array a not allocated ! ");
|
---|
| 221 | if (NbDimensions() < 1) {
|
---|
| 222 | string exmsg = "TArray<T>::SetBA(const BaseArray& a)";
|
---|
| 223 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 224 | mNDBlock.ReSize(totsize_);
|
---|
| 225 | }
|
---|
| 226 | ConvertAndCopyElt(a);
|
---|
| 227 | return(*this);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[894] | 230 | //! Clone array \b a
|
---|
[772] | 231 | template <class T>
|
---|
| 232 | void TArray<T>::Clone(const TArray<T>& a)
|
---|
| 233 | {
|
---|
| 234 | string exmsg = "TArray<T>::Clone()";
|
---|
| 235 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 236 | mNDBlock.Clone(a.mNDBlock);
|
---|
[894] | 237 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
[772] | 238 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[970] | 241 | //! Clone if \b a is not temporary, share if temporary
|
---|
[976] | 242 | /*! \sa NDataBlock::CloneOrShare(const NDataBlock<T>&) */
|
---|
[970] | 243 | template <class T>
|
---|
| 244 | void TArray<T>::CloneOrShare(const TArray<T>& a)
|
---|
| 245 | {
|
---|
| 246 | string exmsg = "TArray<T>::CloneOrShare()";
|
---|
[1103] | 247 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
[970] | 248 | mNDBlock.CloneOrShare(a.mNDBlock);
|
---|
[1103] | 249 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
| 250 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
[970] | 251 | }
|
---|
| 252 |
|
---|
| 253 | //! Share data with a
|
---|
| 254 | template <class T>
|
---|
| 255 | void TArray<T>::Share(const TArray<T>& a)
|
---|
| 256 | {
|
---|
| 257 | string exmsg = "TArray<T>::Share()";
|
---|
[1103] | 258 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
[970] | 259 | mNDBlock.Share(a.mNDBlock);
|
---|
[1103] | 260 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
| 261 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
[970] | 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[1393] | 265 | //! Sets or changes the array size
|
---|
[894] | 266 | /*!
|
---|
| 267 | \param ndim : number of dimensions
|
---|
| 268 | \param siz[ndim] : size along each dimension
|
---|
| 269 | \param step : step (same for all dimensions)
|
---|
| 270 | */
|
---|
[772] | 271 | template <class T>
|
---|
[1156] | 272 | void TArray<T>::ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step)
|
---|
[772] | 273 | {
|
---|
[1099] | 274 | if (arrtype_ != 0) {
|
---|
| 275 | if (ndim != 2)
|
---|
| 276 | throw( ParmError("TArray<T>::ReSize(ndim!=2,...) for Matrix" ) );
|
---|
| 277 | if ((arrtype_ == 2) && (siz[0] > 1) && (siz[1] > 1))
|
---|
| 278 | throw( ParmError("TArray<T>::ReSize(,siz[0]>1 && size[1]>1) for Vector" ) );
|
---|
| 279 | }
|
---|
[1393] | 280 | string exmsg = "TArray<T>::ReSize(int_4 ...)";
|
---|
[772] | 281 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
| 282 | mNDBlock.ReSize(totsize_);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[1393] | 285 | //! Sets or changes the array size.
|
---|
| 286 | /*!
|
---|
| 287 | The array size and memory layout are copied from the array \b a.
|
---|
| 288 | \param a : Array used as template for setting the size and memory layout.
|
---|
| 289 | */
|
---|
| 290 | template <class T>
|
---|
[1517] | 291 | void TArray<T>::ReSize(const BaseArray& a)
|
---|
[1393] | 292 | {
|
---|
| 293 | if (arrtype_ != 0) {
|
---|
| 294 | if (a.NbDimensions() != 2)
|
---|
| 295 | throw( ParmError("TArray<T>::ReSize(a.NbDimensions()!=2,...) for Matrix" ) );
|
---|
| 296 | if ((arrtype_ == 2) && (a.Size(0) > 1) && (a.Size(1) > 1))
|
---|
| 297 | throw( ParmError("TArray<T>::ReSize(a.Size(0)>1 && a.Size(1)>1) for Vector" ) );
|
---|
| 298 | }
|
---|
| 299 | string exmsg = "TArray<T>::ReSize(const TArray<T>&)";
|
---|
| 300 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 301 | mNDBlock.ReSize(totsize_);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[894] | 304 | //! Re-allocate space for array
|
---|
| 305 | /*!
|
---|
| 306 | \param ndim : number of dimensions
|
---|
| 307 | \param siz[ndim] : size along each dimension
|
---|
| 308 | \param step : step (same for all dimensions)
|
---|
| 309 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 310 | only if the required space is greater than the old one.
|
---|
| 311 | */
|
---|
[772] | 312 | template <class T>
|
---|
[1156] | 313 | void TArray<T>::Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step, bool force)
|
---|
[772] | 314 | {
|
---|
[1099] | 315 | if (arrtype_ != 0) {
|
---|
| 316 | if (ndim != 2)
|
---|
| 317 | throw( ParmError("TArray<T>::Realloc(ndim!=2,...) for Matrix" ) );
|
---|
| 318 | if ((arrtype_ == 2) && (siz[0] > 1) && (siz[1] > 1))
|
---|
| 319 | throw( ParmError("TArray<T>::Realloc(,siz[0]>1 && size[1]>1) for Vector" ) );
|
---|
| 320 | }
|
---|
[772] | 321 | string exmsg = "TArray<T>::Realloc()";
|
---|
| 322 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
[1389] | 323 | mNDBlock.Realloc(totsize_, force);
|
---|
[772] | 324 | }
|
---|
| 325 |
|
---|
[787] | 326 |
|
---|
[894] | 327 | //! Compact dimensions in one or more is equal to 1.
|
---|
[772] | 328 | template <class T>
|
---|
[787] | 329 | TArray<T>& TArray<T>::CompactAllDimensions()
|
---|
[772] | 330 | {
|
---|
[787] | 331 | CompactAllDim();
|
---|
| 332 | return(*this);
|
---|
[772] | 333 | }
|
---|
| 334 |
|
---|
[894] | 335 | //! Compact dimensions if the last one is equal to 1.
|
---|
[785] | 336 | template <class T>
|
---|
[787] | 337 | TArray<T>& TArray<T>::CompactTrailingDimensions()
|
---|
[785] | 338 | {
|
---|
[787] | 339 | CompactTrailingDim();
|
---|
[785] | 340 | return(*this);
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[894] | 343 | //! Give value (in \b double) for element at position \b ip..
|
---|
[785] | 344 | template <class T>
|
---|
[1156] | 345 | MuTyV & TArray<T>::ValueAtPosition(sa_size_t ip) const
|
---|
[785] | 346 | {
|
---|
[787] | 347 | #ifdef SO_BOUNDCHECKING
|
---|
[1156] | 348 | if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(sa_size_t ip) Out-of-bound Error") );
|
---|
[787] | 349 | #endif
|
---|
[1081] | 350 | my_mtv = *(mNDBlock.Begin()+Offset(ip));
|
---|
| 351 | return( my_mtv );
|
---|
[785] | 352 | }
|
---|
| 353 |
|
---|
[894] | 354 | //! Return array with elements packed
|
---|
| 355 | /*!
|
---|
| 356 | \param force : if true, pack elements in a new array.
|
---|
| 357 | If false and array is already packed, return
|
---|
| 358 | an array that share data with the current one.
|
---|
| 359 | \return packed array
|
---|
| 360 | */
|
---|
[804] | 361 | template <class T>
|
---|
| 362 | TArray<T> TArray<T>::PackElements(bool force) const
|
---|
| 363 | {
|
---|
| 364 | if (NbDimensions() < 1)
|
---|
| 365 | throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
|
---|
| 366 | if ( !force && (AvgStep() == 1) ) {
|
---|
[970] | 367 | TArray<T> ra;
|
---|
| 368 | ra.Share(*this);
|
---|
[804] | 369 | ra.SetTemp(true);
|
---|
| 370 | return(ra);
|
---|
| 371 | }
|
---|
| 372 | else {
|
---|
| 373 | TArray<T> ra(ndim_, size_, 1);
|
---|
| 374 | ra.CopyElt(*this);
|
---|
| 375 | ra.SetTemp(true);
|
---|
| 376 | return(ra);
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[785] | 380 | // SubArrays
|
---|
[804] | 381 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 382 | //! Extract a sub-array
|
---|
| 383 | /*!
|
---|
| 384 | \param rx,ry,rz,rt,ru : range of extraction along dimensions
|
---|
| 385 | \sa Range
|
---|
| 386 | */
|
---|
[785] | 387 | template <class T>
|
---|
[804] | 388 | TArray<T> TArray<T>::SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const
|
---|
[785] | 389 | {
|
---|
[804] | 390 | if (NbDimensions() < 1)
|
---|
| 391 | throw RangeCheckError("TArray<T>::operator () (Range, ...) - Not Allocated Array ! ");
|
---|
[1156] | 392 | int_4 ndim = 0;
|
---|
| 393 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
| 394 | sa_size_t step[BASEARRAY_MAXNDIMS];
|
---|
| 395 | sa_size_t pos[BASEARRAY_MAXNDIMS];
|
---|
[785] | 396 | size[0] = rx.Size();
|
---|
| 397 | size[1] = ry.Size();
|
---|
| 398 | size[2] = rz.Size();
|
---|
| 399 | size[3] = rt.Size();
|
---|
| 400 | size[4] = ru.Size();
|
---|
| 401 |
|
---|
| 402 | step[0] = rx.Step();
|
---|
| 403 | step[1] = ry.Step();
|
---|
| 404 | step[2] = rz.Step();
|
---|
| 405 | step[3] = rt.Step();
|
---|
| 406 | step[4] = ru.Step();
|
---|
| 407 |
|
---|
| 408 | pos[0] = rx.Start();
|
---|
| 409 | pos[1] = ry.Start();
|
---|
| 410 | pos[2] = rz.Start();
|
---|
| 411 | pos[3] = rt.Start();
|
---|
| 412 | pos[4] = ru.Start();
|
---|
| 413 |
|
---|
| 414 | ndim = ndim_;
|
---|
| 415 | TArray<T> ra;
|
---|
[804] | 416 | UpdateSubArraySizes(ra, ndim, size, pos, step);
|
---|
[787] | 417 | ra.DataBlock().Share(this->DataBlock());
|
---|
[785] | 418 | ra.SetTemp(true);
|
---|
| 419 | return(ra);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[772] | 422 | // ...... Operation de calcul sur les tableaux ......
|
---|
| 423 | // ------- Attention --------
|
---|
| 424 | // Boucles normales prenant en compte les steps ....
|
---|
[894] | 425 | // Possibilite de // , vectorisation
|
---|
| 426 |
|
---|
| 427 | //! Fill TArray with Sequence \b seq
|
---|
| 428 | /*!
|
---|
| 429 | \param seq : sequence to fill the array
|
---|
| 430 | \sa Sequence
|
---|
| 431 | */
|
---|
[772] | 432 | template <class T>
|
---|
[1103] | 433 | TArray<T>& TArray<T>::SetSeq(Sequence const & seq)
|
---|
[772] | 434 | {
|
---|
[804] | 435 | if (NbDimensions() < 1)
|
---|
[813] | 436 | throw RangeCheckError("TArray<T>::SetSeq(Sequence ) - Not Allocated Array ! ");
|
---|
[1103] | 437 |
|
---|
[785] | 438 | T * pe;
|
---|
[1156] | 439 | sa_size_t j,k;
|
---|
| 440 | int_4 ka;
|
---|
[1103] | 441 | if (arrtype_ == 0) ka = 0;
|
---|
| 442 | else ka = macoli_;
|
---|
[1156] | 443 | sa_size_t step = Step(ka);
|
---|
| 444 | sa_size_t gpas = Size(ka);
|
---|
| 445 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1103] | 446 | for(j=0; j<naxa; j++) {
|
---|
| 447 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[2153] | 448 | /*
|
---|
| 449 | Appel explicite de l'operateur de conversion
|
---|
| 450 | suite a la suggestion de M. Reinecke, Reza 31/7/2002
|
---|
[1103] | 451 | #if !defined(__GNUG__)
|
---|
| 452 | for(k=0; k<gpas; k++) pe[k*step] = (T) seq(j*gpas+k);
|
---|
| 453 | #else
|
---|
| 454 | // g++ (up to 2.95.1) se melange les pinceaux s'il y a le cast (T) pour l'instanciation des complexes
|
---|
| 455 | for(k=0; k<gpas; k++) pe[k*step] = seq(j*gpas+k);
|
---|
| 456 | #endif
|
---|
[2153] | 457 | --- Appel explicite de l'operateur de conversion sur l'objet MuTyV
|
---|
| 458 | */
|
---|
| 459 | for(k=0; k<gpas; k++) pe[k*step] = seq(j*gpas+k).operator T();
|
---|
[785] | 460 | }
|
---|
[772] | 461 | return(*this);
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | // >>>> Operations avec 2nd membre de type scalaire
|
---|
| 465 |
|
---|
[894] | 466 | //! Fill an array with a constant value \b x
|
---|
[772] | 467 | template <class T>
|
---|
[813] | 468 | TArray<T>& TArray<T>::SetT(T x)
|
---|
[772] | 469 | {
|
---|
[804] | 470 | if (NbDimensions() < 1)
|
---|
[813] | 471 | throw RangeCheckError("TArray<T>::SetT(T ) - Not Allocated Array ! ");
|
---|
[785] | 472 | T * pe;
|
---|
[1156] | 473 | sa_size_t j,k;
|
---|
[785] | 474 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 475 | sa_size_t step = AvgStep();
|
---|
| 476 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 477 | pe = Data();
|
---|
| 478 | for(k=0; k<maxx; k+=step ) pe[k] = x;
|
---|
| 479 | }
|
---|
| 480 | else { // Non regular data spacing ...
|
---|
[1156] | 481 | int_4 ka = MaxSizeKA();
|
---|
| 482 | sa_size_t step = Step(ka);
|
---|
| 483 | sa_size_t gpas = Size(ka)*step;
|
---|
| 484 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 485 | for(j=0; j<naxa; j++) {
|
---|
| 486 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[785] | 487 | for(k=0; k<gpas; k+=step) pe[k] = x;
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
[772] | 490 | return(*this);
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[894] | 493 | //! Add a constant value \b x to an array
|
---|
[772] | 494 | template <class T>
|
---|
[804] | 495 | TArray<T>& TArray<T>::Add(T x)
|
---|
[772] | 496 | {
|
---|
[804] | 497 | if (NbDimensions() < 1)
|
---|
| 498 | throw RangeCheckError("TArray<T>::Add(T ) - Not Allocated Array ! ");
|
---|
[785] | 499 | T * pe;
|
---|
[1156] | 500 | sa_size_t j,k;
|
---|
[785] | 501 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 502 | sa_size_t step = AvgStep();
|
---|
| 503 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 504 | pe = Data();
|
---|
| 505 | for(k=0; k<maxx; k+=step ) pe[k] += x;
|
---|
| 506 | }
|
---|
| 507 | else { // Non regular data spacing ...
|
---|
[1156] | 508 | int_4 ka = MaxSizeKA();
|
---|
| 509 | sa_size_t step = Step(ka);
|
---|
| 510 | sa_size_t gpas = Size(ka)*step;
|
---|
| 511 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 512 | for(j=0; j<naxa; j++) {
|
---|
| 513 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[785] | 514 | for(k=0; k<gpas; k+=step) pe[k] += x;
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
[772] | 517 | return(*this);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[894] | 520 | //! Substract a constant value \b x to an array
|
---|
[970] | 521 | /*!
|
---|
| 522 | Substract a constant from the *this = *this-x
|
---|
| 523 | \param fginv == true : Perfoms the inverse subtraction (*this = x-(*this))
|
---|
| 524 | */
|
---|
[772] | 525 | template <class T>
|
---|
[970] | 526 | TArray<T>& TArray<T>::Sub(T x, bool fginv)
|
---|
[772] | 527 | {
|
---|
[804] | 528 | if (NbDimensions() < 1)
|
---|
| 529 | throw RangeCheckError("TArray<T>::Sub(T ) - Not Allocated Array ! ");
|
---|
[785] | 530 | T * pe;
|
---|
[1156] | 531 | sa_size_t j,k;
|
---|
[785] | 532 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 533 | sa_size_t step = AvgStep();
|
---|
| 534 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 535 | pe = Data();
|
---|
[970] | 536 | if (fginv)
|
---|
| 537 | for(k=0; k<maxx; k+=step ) pe[k] = x-pe[k];
|
---|
| 538 | else
|
---|
| 539 | for(k=0; k<maxx; k+=step ) pe[k] -= x;
|
---|
[785] | 540 | }
|
---|
| 541 | else { // Non regular data spacing ...
|
---|
[1156] | 542 | int_4 ka = MaxSizeKA();
|
---|
| 543 | sa_size_t step = Step(ka);
|
---|
| 544 | sa_size_t gpas = Size(ka)*step;
|
---|
| 545 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 546 | for(j=0; j<naxa; j++) {
|
---|
| 547 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[970] | 548 | if (fginv)
|
---|
| 549 | for(k=0; k<gpas; k+=step) pe[k] = x-pe[k];
|
---|
| 550 | else
|
---|
| 551 | for(k=0; k<gpas; k+=step) pe[k] -= x;
|
---|
[785] | 552 | }
|
---|
| 553 | }
|
---|
[772] | 554 | return(*this);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[894] | 557 | //! Multiply an array by a constant value \b x
|
---|
[772] | 558 | template <class T>
|
---|
[804] | 559 | TArray<T>& TArray<T>::Mul(T x)
|
---|
[772] | 560 | {
|
---|
[804] | 561 | if (NbDimensions() < 1)
|
---|
| 562 | throw RangeCheckError("TArray<T>::Mul(T ) - Not Allocated Array ! ");
|
---|
[785] | 563 | T * pe;
|
---|
[1156] | 564 | sa_size_t j,k;
|
---|
[785] | 565 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 566 | sa_size_t step = AvgStep();
|
---|
| 567 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 568 | pe = Data();
|
---|
| 569 | for(k=0; k<maxx; k+=step ) pe[k] *= x;
|
---|
| 570 | }
|
---|
| 571 | else { // Non regular data spacing ...
|
---|
[1156] | 572 | int_4 ka = MaxSizeKA();
|
---|
| 573 | sa_size_t step = Step(ka);
|
---|
| 574 | sa_size_t gpas = Size(ka)*step;
|
---|
| 575 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 576 | for(j=0; j<naxa; j++) {
|
---|
| 577 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[785] | 578 | for(k=0; k<gpas; k+=step) pe[k] *= x;
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
[772] | 581 | return(*this);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[894] | 584 | //! Divide an array by a constant value \b x
|
---|
[970] | 585 | /*!
|
---|
| 586 | Divide the array by a constant *this = *this/x
|
---|
| 587 | \param fginv == true : Perfoms the inverse division (*this = x/(*this))
|
---|
| 588 | */
|
---|
[772] | 589 | template <class T>
|
---|
[970] | 590 | TArray<T>& TArray<T>::Div(T x, bool fginv)
|
---|
[772] | 591 | {
|
---|
[804] | 592 | if (NbDimensions() < 1)
|
---|
| 593 | throw RangeCheckError("TArray<T>::Div(T ) - Not Allocated Array ! ");
|
---|
[970] | 594 | if (!fginv && (x == (T) 0) )
|
---|
[894] | 595 | throw MathExc("TArray<T>::Div(T ) - Divide by zero ! ");
|
---|
[785] | 596 | T * pe;
|
---|
[1156] | 597 | sa_size_t j,k;
|
---|
[785] | 598 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 599 | sa_size_t step = AvgStep();
|
---|
| 600 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 601 | pe = Data();
|
---|
[970] | 602 | if (fginv)
|
---|
| 603 | for(k=0; k<maxx; k+=step ) pe[k] = x/pe[k];
|
---|
| 604 | else
|
---|
| 605 | for(k=0; k<maxx; k+=step ) pe[k] /= x;
|
---|
[785] | 606 | }
|
---|
| 607 | else { // Non regular data spacing ...
|
---|
[1156] | 608 | int_4 ka = MaxSizeKA();
|
---|
| 609 | sa_size_t step = Step(ka);
|
---|
| 610 | sa_size_t gpas = Size(ka)*step;
|
---|
| 611 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 612 | for(j=0; j<naxa; j++) {
|
---|
| 613 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[970] | 614 | if (fginv)
|
---|
| 615 | for(k=0; k<gpas; k+=step) pe[k] = x/pe[k];
|
---|
| 616 | else
|
---|
| 617 | for(k=0; k<gpas; k+=step) pe[k] /= x;
|
---|
[785] | 618 | }
|
---|
| 619 | }
|
---|
[772] | 620 | return(*this);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 |
|
---|
[1156] | 624 | //! Replace array elements values by their opposite ( a(i) -> -a(i) )
|
---|
| 625 | template <class T>
|
---|
| 626 | TArray<T>& TArray<T>::NegateElt()
|
---|
| 627 | {
|
---|
| 628 | if (NbDimensions() < 1)
|
---|
| 629 | throw RangeCheckError("TArray<T>::NegateElt() - Not Allocated Array ! ");
|
---|
| 630 | T * pe;
|
---|
| 631 | sa_size_t j,k;
|
---|
| 632 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
| 633 | sa_size_t step = AvgStep();
|
---|
| 634 | sa_size_t maxx = totsize_*step;
|
---|
| 635 | pe = Data();
|
---|
| 636 | for(k=0; k<maxx; k+=step ) pe[k] = -pe[k];
|
---|
| 637 | }
|
---|
| 638 | else { // Non regular data spacing ...
|
---|
| 639 | int_4 ka = MaxSizeKA();
|
---|
| 640 | sa_size_t step = Step(ka);
|
---|
| 641 | sa_size_t gpas = Size(ka)*step;
|
---|
| 642 | sa_size_t naxa = Size()/Size(ka);
|
---|
| 643 | for(j=0; j<naxa; j++) {
|
---|
| 644 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 645 | for(k=0; k<gpas; k+=step) pe[k] = -pe[k];
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 | return(*this);
|
---|
| 649 | }
|
---|
[804] | 650 |
|
---|
[772] | 651 | // >>>> Operations avec 2nd membre de type tableau
|
---|
[894] | 652 | //! Add two TArrays
|
---|
[772] | 653 | template <class T>
|
---|
[804] | 654 | TArray<T>& TArray<T>::AddElt(const TArray<T>& a)
|
---|
[772] | 655 | {
|
---|
[804] | 656 | if (NbDimensions() < 1)
|
---|
| 657 | throw RangeCheckError("TArray<T>::AddElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 658 | bool smo;
|
---|
| 659 | if (!CompareSizes(a, smo))
|
---|
[813] | 660 | throw(SzMismatchError("TArray<T>::AddElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[785] | 661 |
|
---|
| 662 | T * pe;
|
---|
| 663 | const T * pea;
|
---|
[1156] | 664 | sa_size_t j,k,ka;
|
---|
[1099] | 665 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0)) { // regularly spaced elements
|
---|
[1156] | 666 | sa_size_t step = AvgStep();
|
---|
| 667 | sa_size_t stepa = a.AvgStep();
|
---|
| 668 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 669 | pe = Data();
|
---|
| 670 | pea = a.Data();
|
---|
| 671 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] += pea[ka] ;
|
---|
[772] | 672 | }
|
---|
[785] | 673 | else { // Non regular data spacing ...
|
---|
[1156] | 674 | int_4 ax,axa;
|
---|
| 675 | sa_size_t step, stepa;
|
---|
| 676 | sa_size_t gpas, naxa;
|
---|
[1099] | 677 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 678 | for(j=0; j<naxa; j++) {
|
---|
| 679 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 680 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[785] | 681 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] += pea[ka];
|
---|
| 682 | }
|
---|
| 683 | }
|
---|
[772] | 684 | return(*this);
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[894] | 687 | //! Substract two TArrays
|
---|
[970] | 688 | /*!
|
---|
| 689 | Substract two TArrays *this = *this-a
|
---|
| 690 | \param fginv == true : Perfoms the inverse subtraction (*this = a-(*this))
|
---|
| 691 | */
|
---|
[772] | 692 | template <class T>
|
---|
[970] | 693 | TArray<T>& TArray<T>::SubElt(const TArray<T>& a, bool fginv)
|
---|
[772] | 694 | {
|
---|
[804] | 695 | if (NbDimensions() < 1)
|
---|
| 696 | throw RangeCheckError("TArray<T>::SubElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 697 | bool smo;
|
---|
| 698 | if (!CompareSizes(a, smo))
|
---|
[813] | 699 | throw(SzMismatchError("TArray<T>::SubElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[785] | 700 |
|
---|
| 701 | T * pe;
|
---|
| 702 | const T * pea;
|
---|
[1156] | 703 | sa_size_t j,k,ka;
|
---|
[1099] | 704 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
[1156] | 705 | sa_size_t step = AvgStep();
|
---|
| 706 | sa_size_t stepa = a.AvgStep();
|
---|
| 707 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 708 | pe = Data();
|
---|
| 709 | pea = a.Data();
|
---|
[970] | 710 | if (fginv)
|
---|
| 711 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]-pe[k] ;
|
---|
| 712 | else
|
---|
| 713 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] -= pea[ka] ;
|
---|
[772] | 714 | }
|
---|
[785] | 715 | else { // Non regular data spacing ...
|
---|
[1156] | 716 | int_4 ax,axa;
|
---|
| 717 | sa_size_t step, stepa;
|
---|
| 718 | sa_size_t gpas, naxa;
|
---|
[1099] | 719 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 720 | for(j=0; j<naxa; j++) {
|
---|
| 721 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 722 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[970] | 723 | if (fginv)
|
---|
| 724 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]-pe[k] ;
|
---|
| 725 | else
|
---|
| 726 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] -= pea[ka];
|
---|
[785] | 727 | }
|
---|
| 728 | }
|
---|
[772] | 729 | return(*this);
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[970] | 732 |
|
---|
[894] | 733 | //! Multiply two TArrays (elements by elements)
|
---|
[772] | 734 | template <class T>
|
---|
[804] | 735 | TArray<T>& TArray<T>::MulElt(const TArray<T>& a)
|
---|
[772] | 736 | {
|
---|
[804] | 737 | if (NbDimensions() < 1)
|
---|
| 738 | throw RangeCheckError("TArray<T>::MulElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 739 | bool smo;
|
---|
| 740 | if (!CompareSizes(a, smo))
|
---|
[813] | 741 | throw(SzMismatchError("TArray<T>::MulElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[785] | 742 |
|
---|
| 743 | T * pe;
|
---|
| 744 | const T * pea;
|
---|
[1156] | 745 | sa_size_t j,k,ka;
|
---|
[1099] | 746 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
[1156] | 747 | sa_size_t step = AvgStep();
|
---|
| 748 | sa_size_t stepa = a.AvgStep();
|
---|
| 749 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 750 | pe = Data();
|
---|
| 751 | pea = a.Data();
|
---|
| 752 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] *= pea[ka] ;
|
---|
[772] | 753 | }
|
---|
[785] | 754 | else { // Non regular data spacing ...
|
---|
[1156] | 755 | int_4 ax,axa;
|
---|
| 756 | sa_size_t step, stepa;
|
---|
| 757 | sa_size_t gpas, naxa;
|
---|
[1099] | 758 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 759 | for(j=0; j<naxa; j++) {
|
---|
[1099] | 760 | pe = mNDBlock.Begin()+Offset(axa,j);
|
---|
| 761 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[785] | 762 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] *= pea[ka];
|
---|
| 763 | }
|
---|
| 764 | }
|
---|
[772] | 765 | return(*this);
|
---|
| 766 | }
|
---|
| 767 |
|
---|
[804] | 768 |
|
---|
[894] | 769 | //! Divide two TArrays (elements by elements)
|
---|
[970] | 770 | /*!
|
---|
| 771 | Divide two TArrays *this = (*this)/a
|
---|
| 772 | \param fginv == true : Perfoms the inverse division (*this = a/(*this))
|
---|
[1072] | 773 | \param divzero == true : if a(i)==0. result is set to zero: (*this)(i)==0.
|
---|
[970] | 774 | */
|
---|
[772] | 775 | template <class T>
|
---|
[1072] | 776 | TArray<T>& TArray<T>::DivElt(const TArray<T>& a, bool fginv, bool divzero)
|
---|
[772] | 777 | {
|
---|
[804] | 778 | if (NbDimensions() < 1)
|
---|
| 779 | throw RangeCheckError("TArray<T>::DivElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 780 | bool smo;
|
---|
| 781 | if (!CompareSizes(a, smo))
|
---|
[813] | 782 | throw(SzMismatchError("TArray<T>::DivElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[785] | 783 |
|
---|
| 784 | T * pe;
|
---|
| 785 | const T * pea;
|
---|
[1156] | 786 | sa_size_t j,k,ka;
|
---|
[1099] | 787 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
[1156] | 788 | sa_size_t step = AvgStep();
|
---|
| 789 | sa_size_t stepa = a.AvgStep();
|
---|
| 790 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 791 | pe = Data();
|
---|
| 792 | pea = a.Data();
|
---|
[1072] | 793 | if(divzero) {
|
---|
| 794 | if (fginv)
|
---|
| 795 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa )
|
---|
| 796 | {if(pe[k]==(T)0) pe[k] = (T)0; else pe[k] = pea[ka]/pe[k];}
|
---|
| 797 | else
|
---|
| 798 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa )
|
---|
| 799 | {if(pea[k]==(T)0) pe[k] = (T)0; else pe[k] /= pea[ka] ;}
|
---|
| 800 | } else {
|
---|
| 801 | if (fginv)
|
---|
| 802 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]/pe[k];
|
---|
| 803 | else
|
---|
| 804 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] /= pea[ka] ;
|
---|
| 805 | }
|
---|
[772] | 806 | }
|
---|
[785] | 807 | else { // Non regular data spacing ...
|
---|
[1156] | 808 | int_4 ax,axa;
|
---|
| 809 | sa_size_t step, stepa;
|
---|
| 810 | sa_size_t gpas, naxa;
|
---|
[1099] | 811 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 812 | for(j=0; j<naxa; j++) {
|
---|
| 813 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 814 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[1072] | 815 | if(divzero) {
|
---|
| 816 | if (fginv)
|
---|
| 817 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa)
|
---|
| 818 | {if(pe[k]==(T)0) pe[k] = (T)0; else pe[k] = pea[ka]/pe[k];}
|
---|
| 819 | else
|
---|
| 820 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa)
|
---|
| 821 | {if(pea[k]==(T)0) pe[k] = (T)0; else pe[k] /= pea[ka];}
|
---|
| 822 | } else {
|
---|
| 823 | if (fginv)
|
---|
| 824 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]/pe[k];
|
---|
| 825 | else
|
---|
| 826 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] /= pea[ka];
|
---|
| 827 | }
|
---|
[785] | 828 | }
|
---|
| 829 | }
|
---|
[772] | 830 | return(*this);
|
---|
| 831 | }
|
---|
| 832 |
|
---|
[894] | 833 | //! Copy elements of \b a
|
---|
[804] | 834 | template <class T>
|
---|
| 835 | TArray<T>& TArray<T>::CopyElt(const TArray<T>& a)
|
---|
| 836 | {
|
---|
| 837 | if (NbDimensions() < 1)
|
---|
| 838 | throw RangeCheckError("TArray<T>::CopyElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 839 | bool smo;
|
---|
| 840 | if (!CompareSizes(a, smo))
|
---|
[1050] | 841 | throw(SzMismatchError("TArray<T>::CopyElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[772] | 842 |
|
---|
[804] | 843 | T * pe;
|
---|
| 844 | const T * pea;
|
---|
[1156] | 845 | sa_size_t j,k,ka;
|
---|
[1099] | 846 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
[1156] | 847 | sa_size_t step = AvgStep();
|
---|
| 848 | sa_size_t stepa = a.AvgStep();
|
---|
| 849 | sa_size_t maxx = totsize_*step;
|
---|
[804] | 850 | pe = Data();
|
---|
| 851 | pea = a.Data();
|
---|
| 852 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka] ;
|
---|
| 853 | }
|
---|
| 854 | else { // Non regular data spacing ...
|
---|
[1156] | 855 | int_4 ax,axa;
|
---|
| 856 | sa_size_t step, stepa;
|
---|
| 857 | sa_size_t gpas, naxa;
|
---|
[1099] | 858 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 859 | for(j=0; j<naxa; j++) {
|
---|
| 860 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 861 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[804] | 862 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka];
|
---|
| 863 | }
|
---|
| 864 | }
|
---|
| 865 | return(*this);
|
---|
| 866 | }
|
---|
| 867 |
|
---|
[1081] | 868 | //! Converts and Copy elements of \b a
|
---|
| 869 | template <class T>
|
---|
| 870 | TArray<T>& TArray<T>::ConvertAndCopyElt(const BaseArray& a)
|
---|
| 871 | {
|
---|
| 872 | if (NbDimensions() < 1)
|
---|
| 873 | throw RangeCheckError("TArray<T>::ConvertAndCopyElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 874 | bool smo;
|
---|
| 875 | if (!CompareSizes(a, smo))
|
---|
[1081] | 876 | throw(SzMismatchError("TArray<T>::ConvertAndCopyElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[804] | 877 |
|
---|
[1081] | 878 | T * pe;
|
---|
[1156] | 879 | sa_size_t j,k,ka;
|
---|
| 880 | sa_size_t offa;
|
---|
[1081] | 881 | // Non regular data spacing ...
|
---|
[1156] | 882 | int_4 ax,axa;
|
---|
| 883 | sa_size_t step, stepa;
|
---|
| 884 | sa_size_t gpas, naxa;
|
---|
[1099] | 885 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[1081] | 886 | for(j=0; j<naxa; j++) {
|
---|
| 887 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 888 | offa = a.Offset(axa,j);
|
---|
[2147] | 889 | /*
|
---|
| 890 | Appel explicite de l'operateur de conversion
|
---|
| 891 | suite a la suggestion de M. Reinecke, Reza 31/7/2002
|
---|
[1085] | 892 | #if !defined(__GNUG__)
|
---|
| 893 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = (T)a.ValueAtPosition(offa+ka);
|
---|
| 894 | #else
|
---|
| 895 | // g++ (up to 2.95.1) se melange les pinceaux s'il y a le cast (T) pour l'instanciation des complexes
|
---|
[1081] | 896 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = a.ValueAtPosition(offa+ka);
|
---|
[1085] | 897 | #endif
|
---|
[2147] | 898 | --- Appel explicite de l'operateur de conversion sur l'objet MuTyV
|
---|
| 899 | */
|
---|
| 900 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa)
|
---|
| 901 | pe[k] = a.ValueAtPosition(offa+ka).operator T();
|
---|
[1081] | 902 | }
|
---|
| 903 | return(*this);
|
---|
| 904 | }
|
---|
| 905 |
|
---|
| 906 |
|
---|
[804] | 907 | // Somme et produit des elements
|
---|
[894] | 908 | //! Sum all elements
|
---|
[804] | 909 | template <class T>
|
---|
| 910 | T TArray<T>::Sum() const
|
---|
| 911 | {
|
---|
| 912 | if (NbDimensions() < 1)
|
---|
| 913 | throw RangeCheckError("TArray<T>::Sum() - Not Allocated Array ! ");
|
---|
| 914 | T ret=0;
|
---|
| 915 | const T * pe;
|
---|
[1156] | 916 | sa_size_t j,k;
|
---|
[804] | 917 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 918 | sa_size_t step = AvgStep();
|
---|
| 919 | sa_size_t maxx = totsize_*step;
|
---|
[804] | 920 | pe = Data();
|
---|
| 921 | for(k=0; k<maxx; k+=step ) ret += pe[k];
|
---|
| 922 | }
|
---|
| 923 | else { // Non regular data spacing ...
|
---|
[1156] | 924 | int_4 ka = MaxSizeKA();
|
---|
| 925 | sa_size_t step = Step(ka);
|
---|
| 926 | sa_size_t gpas = Size(ka)*step;
|
---|
| 927 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 928 | for(j=0; j<naxa; j++) {
|
---|
| 929 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[804] | 930 | for(k=0; k<gpas; k+=step) ret += pe[k] ;
|
---|
| 931 | }
|
---|
| 932 | }
|
---|
| 933 | return ret;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
[894] | 936 | //! Multiply all elements
|
---|
[804] | 937 | template <class T>
|
---|
| 938 | T TArray<T>::Product() const
|
---|
| 939 | {
|
---|
| 940 | if (NbDimensions() < 1)
|
---|
| 941 | throw RangeCheckError("TArray<T>::Product() - Not Allocated Array ! ");
|
---|
[1113] | 942 | T ret=(T)1;
|
---|
[804] | 943 | const T * pe;
|
---|
[1156] | 944 | sa_size_t j,k;
|
---|
[804] | 945 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 946 | sa_size_t step = AvgStep();
|
---|
| 947 | sa_size_t maxx = totsize_*step;
|
---|
[804] | 948 | pe = Data();
|
---|
| 949 | for(k=0; k<maxx; k+=step ) ret *= pe[k];
|
---|
| 950 | }
|
---|
| 951 | else { // Non regular data spacing ...
|
---|
[1156] | 952 | int_4 ka = MaxSizeKA();
|
---|
| 953 | sa_size_t step = Step(ka);
|
---|
| 954 | sa_size_t gpas = Size(ka)*step;
|
---|
| 955 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 956 | for(j=0; j<naxa; j++) {
|
---|
| 957 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[804] | 958 | for(k=0; k<gpas; k+=step) ret *= pe[k] ;
|
---|
| 959 | }
|
---|
| 960 | }
|
---|
| 961 | return ret;
|
---|
| 962 | }
|
---|
| 963 |
|
---|
[1113] | 964 | //! Returns the sum of all elements squared (Sum
|
---|
| 965 | template <class T>
|
---|
| 966 | T TArray<T>::SumX2() const
|
---|
| 967 | {
|
---|
| 968 | if (NbDimensions() < 1)
|
---|
| 969 | throw RangeCheckError("TArray<T>::SumX2() - Not Allocated Array ! ");
|
---|
| 970 | T ret=0;
|
---|
| 971 | const T * pe;
|
---|
[1156] | 972 | sa_size_t j,k;
|
---|
[1113] | 973 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 974 | sa_size_t step = AvgStep();
|
---|
| 975 | sa_size_t maxx = totsize_*step;
|
---|
[1113] | 976 | pe = Data();
|
---|
| 977 | for(k=0; k<maxx; k+=step ) ret += pe[k]*pe[k];
|
---|
| 978 | }
|
---|
| 979 | else { // Non regular data spacing ...
|
---|
[1156] | 980 | int_4 ka = MaxSizeKA();
|
---|
| 981 | sa_size_t step = Step(ka);
|
---|
| 982 | sa_size_t gpas = Size(ka)*step;
|
---|
| 983 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1113] | 984 | for(j=0; j<naxa; j++) {
|
---|
| 985 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 986 | for(k=0; k<gpas; k+=step) ret += pe[k]*pe[k] ;
|
---|
| 987 | }
|
---|
| 988 | }
|
---|
| 989 | return ret;
|
---|
| 990 | }
|
---|
[804] | 991 |
|
---|
[1113] | 992 | //! Return the minimum and the maximum values of the array elements
|
---|
| 993 | /*!
|
---|
| 994 | This method generates an exception (\c MathExc) if called for complex arrays
|
---|
| 995 | */
|
---|
| 996 | template <class T>
|
---|
| 997 | void TArray<T>::MinMax(T& min, T& max) const
|
---|
| 998 | {
|
---|
| 999 | const T * pe;
|
---|
[1156] | 1000 | sa_size_t j,k;
|
---|
| 1001 | int_4 ka = MaxSizeKA();
|
---|
| 1002 | sa_size_t step = Step(ka);
|
---|
| 1003 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1004 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1113] | 1005 | min = (*this)[0];
|
---|
| 1006 | max = (*this)[0];
|
---|
| 1007 | for(j=0; j<naxa; j++) {
|
---|
| 1008 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 1009 | for(k=0; k<gpas; k+=step) {
|
---|
| 1010 | if (pe[k]<min) min = pe[k];
|
---|
| 1011 | else if (pe[k]>max) max = pe[k];
|
---|
| 1012 | }
|
---|
| 1013 | }
|
---|
| 1014 | return;
|
---|
| 1015 | }
|
---|
[804] | 1016 |
|
---|
[1113] | 1017 | void TArray< complex<r_4> >::MinMax(complex<r_4>& min, complex<r_4>& max) const
|
---|
| 1018 | {
|
---|
| 1019 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
| 1022 | void TArray< complex<r_8> >::MinMax(complex<r_8>& min, complex<r_8>& max) const
|
---|
| 1023 | {
|
---|
| 1024 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
[772] | 1027 | // ----------------------------------------------------
|
---|
| 1028 | // Impression, etc ...
|
---|
| 1029 | // ----------------------------------------------------
|
---|
| 1030 |
|
---|
[894] | 1031 | //! Return a string that contain the type \b T of the array
|
---|
[772] | 1032 | template <class T>
|
---|
[813] | 1033 | string TArray<T>::InfoString() const
|
---|
[772] | 1034 | {
|
---|
[813] | 1035 | string rs = "TArray<" ;
|
---|
| 1036 | rs += typeid(T).name();
|
---|
| 1037 | rs += "> ";
|
---|
[787] | 1038 | return(rs);
|
---|
[772] | 1039 | }
|
---|
| 1040 |
|
---|
[894] | 1041 | //! Print array
|
---|
| 1042 | /*!
|
---|
| 1043 | \param os : output stream
|
---|
| 1044 | \param maxprt : maximum numer of print
|
---|
| 1045 | \param si : if true, display attached DvList
|
---|
[1550] | 1046 | \param ascd : if true, suppresses the display of line numbers,
|
---|
| 1047 | suitable for ascii dump format.
|
---|
[894] | 1048 | \sa SetMaxPrint
|
---|
[1550] | 1049 | \sa WriteASCII
|
---|
[894] | 1050 | */
|
---|
[772] | 1051 | template <class T>
|
---|
[1550] | 1052 | void TArray<T>::Print(ostream& os, sa_size_t maxprt, bool si, bool ascd) const
|
---|
[772] | 1053 | {
|
---|
| 1054 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
[1156] | 1055 | sa_size_t npr = 0;
|
---|
[772] | 1056 | Show(os, si);
|
---|
[850] | 1057 | if (ndim_ < 1) return;
|
---|
[1156] | 1058 | sa_size_t k0,k1,k2,k3,k4;
|
---|
[772] | 1059 | for(k4=0; k4<size_[4]; k4++) {
|
---|
[1550] | 1060 | if ((size_[4] > 1) && ascd)
|
---|
| 1061 | cout << "\n ----- Dimension 5 (U) K4= " << k4 << endl;
|
---|
[772] | 1062 | for(k3=0; k3<size_[3]; k3++) {
|
---|
[1550] | 1063 | if ((size_[3] > 1) && ascd)
|
---|
| 1064 | cout << "\n ----- Dimension 4 (T) K3= " << k3 << endl;
|
---|
[772] | 1065 | for(k2=0; k2<size_[2]; k2++) {
|
---|
[1550] | 1066 | if ((size_[2] > 1) & ascd)
|
---|
| 1067 | cout << "\n ----- Dimension 3 (Z) K2= " << k2 << endl;
|
---|
[772] | 1068 | for(k1=0; k1<size_[1]; k1++) {
|
---|
[1550] | 1069 | if ( (size_[1] > 1) && (size_[0] > 10) && ascd)
|
---|
| 1070 | cout << "----- Dimension 2 (Y) K1= " << k1 << endl;
|
---|
[772] | 1071 | for(k0=0; k0<size_[0]; k0++) {
|
---|
[1550] | 1072 | if(k0 > 0) os << " ";
|
---|
[785] | 1073 | os << Elem(k0, k1, k2, k3, k4); npr++;
|
---|
[1156] | 1074 | if (npr >= (sa_size_t) maxprt) {
|
---|
[772] | 1075 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 1076 | }
|
---|
| 1077 | }
|
---|
| 1078 | os << endl;
|
---|
| 1079 | }
|
---|
| 1080 | }
|
---|
| 1081 | }
|
---|
| 1082 | }
|
---|
[813] | 1083 | os << endl;
|
---|
[772] | 1084 | }
|
---|
| 1085 |
|
---|
[1517] | 1086 | //! Fill the array, decoding the ASCII input stream
|
---|
| 1087 | /*!
|
---|
| 1088 | \param is : input stream (ASCII)
|
---|
[1558] | 1089 | \param nr : Number of non empty (or comment) lines in stream (return value)
|
---|
| 1090 | \param nc : Number of columns (= ntot/nlines) (return value)
|
---|
| 1091 | \return Number of decoded elements
|
---|
[1517] | 1092 | */
|
---|
| 1093 | template <class T>
|
---|
[1558] | 1094 | sa_size_t TArray<T>::ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc)
|
---|
[1517] | 1095 | {
|
---|
[1550] | 1096 | EnumeratedSequence es;
|
---|
[1558] | 1097 | sa_size_t n = es.FillFromFile(is, nr, nc);
|
---|
| 1098 | if ( (n < 1) || (nr < 1) || (nc < 1) ) return(n);
|
---|
| 1099 | if (!IsAllocated()) {
|
---|
| 1100 | sa_size_t sz[2];
|
---|
| 1101 | if (arrtype_ == 2) { // C'est un vecteur
|
---|
| 1102 | sz[0] = sz[1] = 1;
|
---|
| 1103 | sz[veceli_] = n;
|
---|
| 1104 | }
|
---|
| 1105 | else {
|
---|
| 1106 | sz[RowsKA()] = nr;
|
---|
| 1107 | sz[ColsKA()] = nc;
|
---|
| 1108 | }
|
---|
| 1109 | ReSize(2, sz);
|
---|
| 1110 | }
|
---|
| 1111 | SetSeq(es);
|
---|
| 1112 | cout << "TArray<T>::ReadASCII()/Info: " << n << " elements read from stream "
|
---|
| 1113 | << " (Row,Col= " << nr << "," << nc << ")" << endl;
|
---|
| 1114 | return(n);
|
---|
[1517] | 1115 | }
|
---|
[772] | 1116 |
|
---|
[1517] | 1117 | //! Writes the array content to the output stream, (in ASCII)
|
---|
| 1118 | /*!
|
---|
| 1119 | \param os : output stream (ASCII)
|
---|
[1558] | 1120 | \sa Print
|
---|
[1517] | 1121 | */
|
---|
| 1122 | template <class T>
|
---|
| 1123 | void TArray<T>::WriteASCII(ostream& os) const
|
---|
| 1124 | {
|
---|
[1550] | 1125 | Print(os, Size(), false, true);
|
---|
[1517] | 1126 | }
|
---|
[772] | 1127 |
|
---|
[1517] | 1128 |
|
---|
| 1129 |
|
---|
[772] | 1130 | ///////////////////////////////////////////////////////////////
|
---|
| 1131 | ///////////////////////////////////////////////////////////////
|
---|
| 1132 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
[804] | 1133 | /*
|
---|
[772] | 1134 | #pragma define_template TArray<uint_1>
|
---|
[804] | 1135 | #pragma define_template TArray<int_2>
|
---|
| 1136 | #pragma define_template TArray<uint_4>
|
---|
| 1137 | */
|
---|
[772] | 1138 | #pragma define_template TArray<uint_2>
|
---|
[1543] | 1139 | #pragma define_template TArray<uint_8>
|
---|
[772] | 1140 | #pragma define_template TArray<int_4>
|
---|
| 1141 | #pragma define_template TArray<int_8>
|
---|
| 1142 | #pragma define_template TArray<r_4>
|
---|
| 1143 | #pragma define_template TArray<r_8>
|
---|
| 1144 | #pragma define_template TArray< complex<r_4> >
|
---|
| 1145 | #pragma define_template TArray< complex<r_8> >
|
---|
| 1146 | #endif
|
---|
| 1147 |
|
---|
| 1148 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[804] | 1149 | /*
|
---|
| 1150 | template class TArray<uint_1>;
|
---|
| 1151 | template class TArray<int_2>;
|
---|
| 1152 | template class TArray<uint_4>;
|
---|
| 1153 | */
|
---|
[772] | 1154 | template class TArray<uint_2>;
|
---|
[1543] | 1155 | template class TArray<uint_8>;
|
---|
[772] | 1156 | template class TArray<int_4>;
|
---|
| 1157 | template class TArray<int_8>;
|
---|
| 1158 | template class TArray<r_4>;
|
---|
| 1159 | template class TArray<r_8>;
|
---|
| 1160 | template class TArray< complex<r_4> >;
|
---|
| 1161 | template class TArray< complex<r_8> >;
|
---|
| 1162 | #endif
|
---|
| 1163 |
|
---|
| 1164 |
|
---|