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