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