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