[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>
|
---|
[2752] | 8 | #include <iomanip>
|
---|
| 9 |
|
---|
[772] | 10 | #include "pexceptions.h"
|
---|
| 11 | #include "tarray.h"
|
---|
| 12 |
|
---|
[3234] | 13 | namespace SOPHYA {
|
---|
| 14 |
|
---|
[926] | 15 | /*!
|
---|
[3234] | 16 | \class TArray
|
---|
[926] | 17 | \ingroup TArray
|
---|
[2267] | 18 | Class for template arrays with numerical data types (int, float, complex).
|
---|
[772] | 19 |
|
---|
[2917] | 20 | This class handles arrays with number of dimensions up to
|
---|
| 21 | \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS" (=5). The class has a performant
|
---|
| 22 | memory management system, including reference sharing for the array data.
|
---|
| 23 | (copy constructor and sub-arrays (or slices)).
|
---|
[2267] | 24 |
|
---|
[2917] | 25 | An important feature of this class is the transparent handling of sub-arrays,
|
---|
| 26 | or slices. Arbitrary sub-arrays or slices can be defined, provided regular
|
---|
| 27 | spacing along each array axe (or dimension).
|
---|
| 28 | The second example below illustrate the use of this possibility.
|
---|
| 29 |
|
---|
| 30 | Standard arithmetic operations, sum or product as well as application of usual
|
---|
| 31 | math functions (Sin, Cos ...) on numerical arrays are implemented.
|
---|
| 32 | These operations usually provide high performance, despite of the complex
|
---|
| 33 | memory management pattern.
|
---|
| 34 |
|
---|
| 35 | ASCII input/output (or read write) and binary I/O (persistence) in different
|
---|
| 36 | formats are also provided through helper (or handler) classes.
|
---|
| 37 |
|
---|
| 38 | This class is mainly intented for arrays with large number of data elements.
|
---|
| 39 | (Size() \> 100 .. 1000). Arrays with few data elements (\< 10) have significant
|
---|
| 40 | memory overhead, due to variables describing array shape and memory organisation.
|
---|
| 41 | However, a single higher dimensional array can be used to represent a large number
|
---|
| 42 | of identical size small arrays. For example, TArray<T> tab(2,2, 1000) can be used
|
---|
| 43 | to hold 1000 2x2 matrices.
|
---|
| 44 |
|
---|
[3101] | 45 | \warning Notice that array elements along the X axis are contiguous in memory,
|
---|
| 46 | independent of the array rank (number of dimensions), for packed arrays.
|
---|
| 47 |
|
---|
[2267] | 48 | \b Array is a typedef for double precision floating point arrays ( TArray<r_8> )
|
---|
[2917] | 49 |
|
---|
[2267] | 50 | \sa SOPHYA::Range
|
---|
| 51 | \sa SOPHYA::Sequence
|
---|
| 52 | \sa SOPHYA::MathArray
|
---|
[2917] | 53 | \sa SOPHYA::NDataBlock
|
---|
[2267] | 54 |
|
---|
| 55 | \code
|
---|
| 56 | #include "array.h"
|
---|
| 57 | // ...
|
---|
| 58 | // Creating and initialising a 1-D array of integers
|
---|
| 59 | TArray<int> ia(5);
|
---|
| 60 | EnumeratedSequence es;
|
---|
| 61 | es = 24, 35, 46, 57, 68;
|
---|
| 62 | ia = es;
|
---|
[2917] | 63 | cout << "Array<int> ia = \n" << ia;
|
---|
[2267] | 64 | // 2-D array of floats
|
---|
| 65 | TArray<r_4> b(6,4), c(6,4);
|
---|
| 66 | // Initializing b with a constant
|
---|
| 67 | b = 2.71828;
|
---|
| 68 | // Filling c with random numbers
|
---|
| 69 | c = RandomSequence();
|
---|
| 70 | // Arithmetic operations
|
---|
| 71 | TArray<r_4> d = b+0.3f*c;
|
---|
[2917] | 72 | cout << "Array<float> d = \n" << d;
|
---|
[2267] | 73 | \endcode
|
---|
| 74 |
|
---|
[2917] | 75 | Example for sub-arrays, or slices
|
---|
| 76 | \code
|
---|
| 77 | // Creating and initialising a 2-D (6 x 4) array of integers
|
---|
| 78 | TArray<int> iaa(6, 4);
|
---|
| 79 | iaa = RegularSequence(1,2);
|
---|
| 80 | cout << "Array<int> iaa = \n" << iaa;
|
---|
| 81 | // We extract a sub-array - data is shared with iaa
|
---|
| 82 | TArray<int> iae = iaa(Range(1, Range::lastIndex(), 3) ,
|
---|
| 83 | Range::all(), Range::first() );
|
---|
| 84 | cout << "Array<int> iae=subarray(iaa) = \n" << iae;
|
---|
| 85 | // Changing iae elements changes corresponding iaa elements
|
---|
| 86 | iae = 0;
|
---|
| 87 | cout << "Array<int> iae=0 --> iaa = \n" << iaa;
|
---|
| 88 | \endcode
|
---|
[926] | 89 | */
|
---|
[772] | 90 |
|
---|
[1389] | 91 | /*! \ingroup TArray
|
---|
| 92 | \typedef sa_size_t
|
---|
| 93 | \brief Array index range and size, defined to be a 4-byte or 8-byte integer
|
---|
| 94 | */
|
---|
| 95 |
|
---|
[772] | 96 | // -------------------------------------------------------
|
---|
| 97 | // Methodes de la classe
|
---|
| 98 | // -------------------------------------------------------
|
---|
| 99 |
|
---|
[894] | 100 | ////////////////////////// Les constructeurs / destructeurs
|
---|
| 101 |
|
---|
| 102 | //! Default constructor
|
---|
[772] | 103 | template <class T>
|
---|
| 104 | TArray<T>::TArray()
|
---|
[804] | 105 | : BaseArray() , mNDBlock()
|
---|
[772] | 106 | {
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[894] | 109 | //! Constructor
|
---|
| 110 | /*!
|
---|
| 111 | \param ndim : number of dimensions (less or equal to
|
---|
| 112 | \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS")
|
---|
| 113 | \param siz[ndim] : size along each dimension
|
---|
| 114 | \param step : step (same for all dimensions)
|
---|
[2564] | 115 | \param fzero : if \b true , set array elements to zero
|
---|
[894] | 116 | */
|
---|
[772] | 117 | template <class T>
|
---|
[2564] | 118 | TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step, bool fzero)
|
---|
| 119 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), fzero)
|
---|
[772] | 120 | {
|
---|
[1156] | 121 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, sa_size_t)";
|
---|
[772] | 122 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[894] | 125 | //! Constructor
|
---|
| 126 | /*!
|
---|
| 127 | \param nx,ny,nz,nt,nu : sizes along first, second, third, fourth and fifth dimension
|
---|
[2564] | 128 | \param fzero : if \b true , set array elements to zero
|
---|
[894] | 129 | */
|
---|
[772] | 130 | template <class T>
|
---|
[2564] | 131 | TArray<T>::TArray(sa_size_t nx, sa_size_t ny, sa_size_t nz, sa_size_t nt, sa_size_t nu, bool fzero)
|
---|
[804] | 132 | : BaseArray() , mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)*((nt>0)?nt:1)*((nu>0)?nu:1))
|
---|
[772] | 133 | {
|
---|
[1156] | 134 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
[772] | 135 | size[0] = nx; size[1] = ny; size[2] = nz;
|
---|
[804] | 136 | size[3] = nt; size[4] = nu;
|
---|
[1156] | 137 | int_4 ndim = 1;
|
---|
[804] | 138 | if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) && (size[4] > 0) ) ndim = 5;
|
---|
| 139 | else if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) ) ndim = 4;
|
---|
| 140 | else if ((size[1] > 0) && (size[2] > 0)) ndim = 3;
|
---|
[772] | 141 | else if (size[1] > 0) ndim = 2;
|
---|
| 142 | else ndim = 1;
|
---|
[1156] | 143 | string exmsg = "TArray<T>::TArray(sa_size_t, sa_size_t, sa_size_t, sa_size_t, sa_size_t)";
|
---|
[772] | 144 | if (!UpdateSizes(ndim, size, 1, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[894] | 147 | //! Constructor
|
---|
| 148 | /*!
|
---|
| 149 | \param ndim : number of dimensions
|
---|
| 150 | \param siz[ndim] : size along each dimension
|
---|
| 151 | \param db : datas are given by this NDataBlock
|
---|
| 152 | \param share : if true, data are shared, if false they are copied
|
---|
| 153 | \param step : step (same for all dimensions) in data block
|
---|
| 154 | \param offset : offset for first element in data block
|
---|
| 155 | */
|
---|
[772] | 156 | template <class T>
|
---|
[1156] | 157 | 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] | 158 | : BaseArray() , mNDBlock(db, share)
|
---|
[772] | 159 | {
|
---|
[1156] | 160 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, NDataBlock<T> & ... )";
|
---|
[772] | 161 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
[3332] | 162 | if (mNDBlock.Size() < (size_t)ComputeTotalSize(ndim, siz, step, offset)) {
|
---|
[1636] | 163 | exmsg += " DataBlock.Size() < ComputeTotalSize(...) " ;
|
---|
| 164 | throw( ParmError(exmsg) );
|
---|
| 165 | }
|
---|
[772] | 166 | }
|
---|
| 167 |
|
---|
[894] | 168 | //! Constructor
|
---|
| 169 | /*!
|
---|
| 170 | \param ndim : number of dimensions
|
---|
| 171 | \param siz[ndim] : size along each dimension
|
---|
| 172 | \param values : datas are given by this pointer
|
---|
| 173 | \param share : if true, data are shared, if false they are copied
|
---|
| 174 | \param step : step (same for all dimensions) in data block
|
---|
| 175 | \param offset : offset for first element in data block
|
---|
| 176 | \param br : if not NULL, dats are bridge with other datas
|
---|
| 177 | \sa NDataBlock
|
---|
| 178 | */
|
---|
[772] | 179 | template <class T>
|
---|
[1156] | 180 | TArray<T>::TArray(int_4 ndim, const sa_size_t * siz, T* values, sa_size_t step, sa_size_t offset, Bridge* br)
|
---|
[804] | 181 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br)
|
---|
[772] | 182 | {
|
---|
[1156] | 183 | string exmsg = "TArray<T>::TArray(int_4, sa_size_t *, T* ... )";
|
---|
[772] | 184 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[894] | 187 | //! Constructor by copy
|
---|
[976] | 188 | /*!
|
---|
| 189 | \warning datas are \b SHARED with \b a.
|
---|
| 190 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 191 | */
|
---|
[772] | 192 | template <class T>
|
---|
| 193 | TArray<T>::TArray(const TArray<T>& a)
|
---|
[804] | 194 | : BaseArray() , mNDBlock(a.mNDBlock)
|
---|
[772] | 195 | {
|
---|
| 196 | string exmsg = "TArray<T>::TArray(const TArray<T>&)";
|
---|
| 197 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 198 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[894] | 201 | //! Constructor by copy
|
---|
| 202 | /*!
|
---|
| 203 | \param share : if true, data are shared, if false they are copied
|
---|
| 204 | */
|
---|
[772] | 205 | template <class T>
|
---|
| 206 | TArray<T>::TArray(const TArray<T>& a, bool share)
|
---|
[804] | 207 | : BaseArray() , mNDBlock(a.mNDBlock, share)
|
---|
[772] | 208 | {
|
---|
[1517] | 209 | if (a.NbDimensions() == 0) return;
|
---|
[772] | 210 | string exmsg = "TArray<T>::TArray(const TArray<T>&, bool)";
|
---|
| 211 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 212 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[1081] | 215 | //! Constructor with size and contents copied (after conversion) from a different type TArray
|
---|
| 216 | template <class T>
|
---|
| 217 | TArray<T>::TArray(const BaseArray& a)
|
---|
| 218 | : BaseArray() , mNDBlock()
|
---|
| 219 | {
|
---|
[1517] | 220 | if (a.NbDimensions() == 0) return;
|
---|
[1081] | 221 | string exmsg = "TArray<T>::TArray(const BaseArray&)";
|
---|
| 222 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 223 | mNDBlock.ReSize(totsize_);
|
---|
| 224 | // if (a.mInfo) mInfo = new DVList(*(a.mInfo)); - pb protected !
|
---|
| 225 | ConvertAndCopyElt(a);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[894] | 228 | //! Destructor
|
---|
[772] | 229 | template <class T>
|
---|
| 230 | TArray<T>::~TArray()
|
---|
| 231 | {
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[894] | 234 | ////////////////////////// Les methodes de copie/share
|
---|
| 235 |
|
---|
| 236 | //! Set array equal to \b a and return *this
|
---|
[976] | 237 | /*!
|
---|
[1364] | 238 | If the array is already allocated, CopyElt() is called
|
---|
| 239 | for checking that the two arrays have the same size and
|
---|
| 240 | for copying the array element values. For non allocated
|
---|
| 241 | arrays, CloneOrShare() is called. The array memory
|
---|
| 242 | organization is also copied from \b a.
|
---|
[976] | 243 | \warning Datas are copied (cloned) from \b a.
|
---|
[1364] | 244 | \sa CopyElt
|
---|
| 245 | \sa CloneOrShare
|
---|
[976] | 246 | \sa NDataBlock::operator=(const NDataBlock<T>&)
|
---|
| 247 | */
|
---|
[772] | 248 | template <class T>
|
---|
[804] | 249 | TArray<T>& TArray<T>::Set(const TArray<T>& a)
|
---|
[772] | 250 | {
|
---|
[970] | 251 | if (this == &a) return(*this);
|
---|
| 252 | if (a.NbDimensions() < 1)
|
---|
| 253 | throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! ");
|
---|
| 254 | if (NbDimensions() < 1) CloneOrShare(a);
|
---|
| 255 | else CopyElt(a);
|
---|
[772] | 256 | return(*this);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[1081] | 259 | //! Set array elements equal to the \b a array elements, after conversion
|
---|
| 260 | template <class T>
|
---|
| 261 | TArray<T>& TArray<T>::SetBA(const BaseArray& a)
|
---|
| 262 | {
|
---|
| 263 | if (this == &a) return(*this);
|
---|
| 264 | if (a.NbDimensions() < 1)
|
---|
| 265 | throw RangeCheckError("TArray<T>::SetBA(a ) - Array a not allocated ! ");
|
---|
| 266 | if (NbDimensions() < 1) {
|
---|
| 267 | string exmsg = "TArray<T>::SetBA(const BaseArray& a)";
|
---|
| 268 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 269 | mNDBlock.ReSize(totsize_);
|
---|
| 270 | }
|
---|
| 271 | ConvertAndCopyElt(a);
|
---|
| 272 | return(*this);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[894] | 275 | //! Clone array \b a
|
---|
[772] | 276 | template <class T>
|
---|
| 277 | void TArray<T>::Clone(const TArray<T>& a)
|
---|
| 278 | {
|
---|
| 279 | string exmsg = "TArray<T>::Clone()";
|
---|
| 280 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 281 | mNDBlock.Clone(a.mNDBlock);
|
---|
[894] | 282 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
[772] | 283 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[970] | 286 | //! Clone if \b a is not temporary, share if temporary
|
---|
[976] | 287 | /*! \sa NDataBlock::CloneOrShare(const NDataBlock<T>&) */
|
---|
[970] | 288 | template <class T>
|
---|
| 289 | void TArray<T>::CloneOrShare(const TArray<T>& a)
|
---|
| 290 | {
|
---|
| 291 | string exmsg = "TArray<T>::CloneOrShare()";
|
---|
[1103] | 292 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
[970] | 293 | mNDBlock.CloneOrShare(a.mNDBlock);
|
---|
[1103] | 294 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
| 295 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
[970] | 296 | }
|
---|
| 297 |
|
---|
| 298 | //! Share data with a
|
---|
| 299 | template <class T>
|
---|
| 300 | void TArray<T>::Share(const TArray<T>& a)
|
---|
| 301 | {
|
---|
| 302 | string exmsg = "TArray<T>::Share()";
|
---|
[1103] | 303 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
[970] | 304 | mNDBlock.Share(a.mNDBlock);
|
---|
[1103] | 305 | if (mInfo) {delete mInfo; mInfo = NULL;}
|
---|
| 306 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
[970] | 307 | }
|
---|
| 308 |
|
---|
| 309 |
|
---|
[1393] | 310 | //! Sets or changes the array size
|
---|
[894] | 311 | /*!
|
---|
| 312 | \param ndim : number of dimensions
|
---|
| 313 | \param siz[ndim] : size along each dimension
|
---|
| 314 | \param step : step (same for all dimensions)
|
---|
[2564] | 315 | \param fzero : if \b true , set array elements to zero
|
---|
[894] | 316 | */
|
---|
[772] | 317 | template <class T>
|
---|
[2564] | 318 | void TArray<T>::ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step, bool fzero)
|
---|
[772] | 319 | {
|
---|
[1099] | 320 | if (arrtype_ != 0) {
|
---|
| 321 | if (ndim != 2)
|
---|
| 322 | throw( ParmError("TArray<T>::ReSize(ndim!=2,...) for Matrix" ) );
|
---|
| 323 | if ((arrtype_ == 2) && (siz[0] > 1) && (siz[1] > 1))
|
---|
| 324 | throw( ParmError("TArray<T>::ReSize(,siz[0]>1 && size[1]>1) for Vector" ) );
|
---|
| 325 | }
|
---|
[1393] | 326 | string exmsg = "TArray<T>::ReSize(int_4 ...)";
|
---|
[772] | 327 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
[2564] | 328 | mNDBlock.ReSize(totsize_, fzero);
|
---|
[772] | 329 | }
|
---|
| 330 |
|
---|
[1393] | 331 | //! Sets or changes the array size.
|
---|
| 332 | /*!
|
---|
| 333 | The array size and memory layout are copied from the array \b a.
|
---|
| 334 | \param a : Array used as template for setting the size and memory layout.
|
---|
[2564] | 335 | \param pack : if \b true , create a packed array, else same memory layout as \b a.
|
---|
| 336 | \param fzero : if \b true , set array elements to zero
|
---|
[1393] | 337 | */
|
---|
| 338 | template <class T>
|
---|
[2564] | 339 | void TArray<T>::ReSize(const BaseArray& a, bool pack, bool fzero)
|
---|
[1393] | 340 | {
|
---|
| 341 | if (arrtype_ != 0) {
|
---|
| 342 | if (a.NbDimensions() != 2)
|
---|
| 343 | throw( ParmError("TArray<T>::ReSize(a.NbDimensions()!=2,...) for Matrix" ) );
|
---|
| 344 | if ((arrtype_ == 2) && (a.Size(0) > 1) && (a.Size(1) > 1))
|
---|
| 345 | throw( ParmError("TArray<T>::ReSize(a.Size(0)>1 && a.Size(1)>1) for Vector" ) );
|
---|
| 346 | }
|
---|
| 347 | string exmsg = "TArray<T>::ReSize(const TArray<T>&)";
|
---|
[2564] | 348 | if (pack) {
|
---|
| 349 | sa_size_t siz[BASEARRAY_MAXNDIMS];
|
---|
[2569] | 350 | int ksz;
|
---|
| 351 | for(ksz=0; ksz<a.NbDimensions(); ksz++) siz[ksz] = a.Size(ksz);
|
---|
| 352 | for(ksz=a.NbDimensions(); ksz<BASEARRAY_MAXNDIMS; ksz++) siz[ksz] = 1;
|
---|
[2564] | 353 | if (!UpdateSizes(a.NbDimensions(), siz, 1, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
[2569] | 354 | SetMemoryMapping(a.GetMemoryMapping());
|
---|
[2564] | 355 | mNDBlock.ReSize(totsize_, fzero);
|
---|
| 356 | }
|
---|
| 357 | else {
|
---|
| 358 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
| 359 | mNDBlock.ReSize(totsize_);
|
---|
| 360 | }
|
---|
[1393] | 361 | }
|
---|
| 362 |
|
---|
[894] | 363 | //! Re-allocate space for array
|
---|
| 364 | /*!
|
---|
| 365 | \param ndim : number of dimensions
|
---|
| 366 | \param siz[ndim] : size along each dimension
|
---|
| 367 | \param step : step (same for all dimensions)
|
---|
| 368 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 369 | only if the required space is greater than the old one.
|
---|
| 370 | */
|
---|
[772] | 371 | template <class T>
|
---|
[1156] | 372 | void TArray<T>::Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step, bool force)
|
---|
[772] | 373 | {
|
---|
[1099] | 374 | if (arrtype_ != 0) {
|
---|
| 375 | if (ndim != 2)
|
---|
| 376 | throw( ParmError("TArray<T>::Realloc(ndim!=2,...) for Matrix" ) );
|
---|
| 377 | if ((arrtype_ == 2) && (siz[0] > 1) && (siz[1] > 1))
|
---|
| 378 | throw( ParmError("TArray<T>::Realloc(,siz[0]>1 && size[1]>1) for Vector" ) );
|
---|
| 379 | }
|
---|
[772] | 380 | string exmsg = "TArray<T>::Realloc()";
|
---|
| 381 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
[1389] | 382 | mNDBlock.Realloc(totsize_, force);
|
---|
[772] | 383 | }
|
---|
| 384 |
|
---|
[3173] | 385 | //! To clear the array sizes - corresponding to an unallocated array.
|
---|
| 386 | template <class T>
|
---|
| 387 | TArray<T>& TArray<T>::ZeroSize()
|
---|
| 388 | {
|
---|
| 389 | if (NbDimensions() == 0) return (*this);
|
---|
| 390 | SetZeroSize();
|
---|
| 391 | mNDBlock.Dealloc();
|
---|
| 392 | return (*this);
|
---|
| 393 | }
|
---|
[787] | 394 |
|
---|
[2917] | 395 | /*!
|
---|
| 396 | \brief Compact arrays - supresses size=1 axes.
|
---|
| 397 | Changes the array rank (number of dimensions), suppressing all axes with size equal 1.
|
---|
| 398 | Example:
|
---|
| 399 | Compacting Rank=NDim=5 Sizes=3x1x6x1x1 =====\> Rank=NDim=2 Sizes=3x6
|
---|
| 400 | */
|
---|
[772] | 401 | template <class T>
|
---|
[787] | 402 | TArray<T>& TArray<T>::CompactAllDimensions()
|
---|
[772] | 403 | {
|
---|
[787] | 404 | CompactAllDim();
|
---|
| 405 | return(*this);
|
---|
[772] | 406 | }
|
---|
| 407 |
|
---|
[2917] | 408 | /*!
|
---|
| 409 | \brief Compact array taling dimensions, for size=1 traling axes.
|
---|
| 410 | Changes the array rank (number of dimensions), suppressing all axes with size equal 1,
|
---|
| 411 | after the last axe with size \> 1
|
---|
| 412 | Example:
|
---|
| 413 | Compacting Rank=NDim=5 Sizes=3x1x6x1x1 =====\> Rank=NDim=3 Sizes=3x1x6
|
---|
| 414 | */
|
---|
[785] | 415 | template <class T>
|
---|
[787] | 416 | TArray<T>& TArray<T>::CompactTrailingDimensions()
|
---|
[785] | 417 | {
|
---|
[787] | 418 | CompactTrailingDim();
|
---|
[785] | 419 | return(*this);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[2888] | 422 | /*!
|
---|
| 423 | \brief Return the value (as a MuTyV) for element at position \b ip in the array.
|
---|
| 424 | This method is used for conversion between arrays of different types.
|
---|
| 425 | \param ip : element position in the array
|
---|
| 426 | */
|
---|
[785] | 427 | template <class T>
|
---|
[1156] | 428 | MuTyV & TArray<T>::ValueAtPosition(sa_size_t ip) const
|
---|
[785] | 429 | {
|
---|
[787] | 430 | #ifdef SO_BOUNDCHECKING
|
---|
[2888] | 431 | if ( (ip >= totsize_) || (ip < 0) )
|
---|
| 432 | throw( ParmError("TArray<T>::ValueAtPosition(sa_size_t ip) Out-of-bound Error") );
|
---|
[787] | 433 | #endif
|
---|
[1081] | 434 | my_mtv = *(mNDBlock.Begin()+Offset(ip));
|
---|
| 435 | return( my_mtv );
|
---|
[785] | 436 | }
|
---|
| 437 |
|
---|
[2888] | 438 | /*!
|
---|
| 439 | \brief Return the value (as a MuTyV) for element at position \b ip in the datablock.
|
---|
| 440 | This method is used for conversion between arrays of different types.
|
---|
| 441 | \param ip : element position in the array DataBlock, regardless of
|
---|
| 442 | the array memory organisation
|
---|
| 443 | */
|
---|
| 444 | template <class T>
|
---|
| 445 | MuTyV & TArray<T>::ValueAtPositionDB(sa_size_t ip) const
|
---|
| 446 | {
|
---|
| 447 | #ifdef SO_BOUNDCHECKING
|
---|
| 448 | if ( (ip >= mNDBlock.Size() ) || (ip < 0) )
|
---|
| 449 | throw( ParmError("TArray<T>::ValueAtPositionDB(sa_size_t ip) Out-of-bound Error") );
|
---|
| 450 | #endif
|
---|
| 451 | my_mtv = *(mNDBlock.Begin()+ip);
|
---|
| 452 | return( my_mtv );
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[894] | 455 | /*!
|
---|
[2917] | 456 | \brief Return a new array with elements packed in memory
|
---|
| 457 |
|
---|
| 458 |
|
---|
[894] | 459 | \param force : if true, pack elements in a new array.
|
---|
| 460 | If false and array is already packed, return
|
---|
| 461 | an array that share data with the current one.
|
---|
| 462 | \return packed array
|
---|
| 463 | */
|
---|
[804] | 464 | template <class T>
|
---|
| 465 | TArray<T> TArray<T>::PackElements(bool force) const
|
---|
| 466 | {
|
---|
| 467 | if (NbDimensions() < 1)
|
---|
| 468 | throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
|
---|
| 469 | if ( !force && (AvgStep() == 1) ) {
|
---|
[970] | 470 | TArray<T> ra;
|
---|
| 471 | ra.Share(*this);
|
---|
[804] | 472 | return(ra);
|
---|
| 473 | }
|
---|
| 474 | else {
|
---|
| 475 | TArray<T> ra(ndim_, size_, 1);
|
---|
| 476 | ra.CopyElt(*this);
|
---|
| 477 | return(ra);
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[785] | 481 | // SubArrays
|
---|
[804] | 482 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 483 | //! Extract a sub-array
|
---|
| 484 | /*!
|
---|
| 485 | \param rx,ry,rz,rt,ru : range of extraction along dimensions
|
---|
[2917] | 486 | \param compact : if \b compact == true, compact trailing dimensions (suppressed if =1)
|
---|
| 487 | (See CompactTrailingDimensions() )
|
---|
[2915] | 488 | \sa SOPHYA::Range
|
---|
[894] | 489 | */
|
---|
[785] | 490 | template <class T>
|
---|
[2917] | 491 | TArray<T> TArray<T>::SubArray(Range rx, Range ry, Range rz, Range rt, Range ru, bool compact) const
|
---|
[785] | 492 | {
|
---|
[804] | 493 | if (NbDimensions() < 1)
|
---|
| 494 | throw RangeCheckError("TArray<T>::operator () (Range, ...) - Not Allocated Array ! ");
|
---|
[1156] | 495 | int_4 ndim = 0;
|
---|
[2915] | 496 |
|
---|
| 497 | // Updating Range objects using actual array size
|
---|
| 498 | rx.Update(SizeX());
|
---|
| 499 | ry.Update(SizeY());
|
---|
| 500 | rz.Update(SizeZ());
|
---|
| 501 | if (NbDimensions() > 3) rt.Update(Size(3));
|
---|
| 502 | else rt.Update(0);
|
---|
| 503 | if (NbDimensions() > 4) ru.Update(Size(4));
|
---|
| 504 | else ru.Update(0);
|
---|
| 505 |
|
---|
[1156] | 506 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
| 507 | sa_size_t step[BASEARRAY_MAXNDIMS];
|
---|
| 508 | sa_size_t pos[BASEARRAY_MAXNDIMS];
|
---|
[785] | 509 | size[0] = rx.Size();
|
---|
| 510 | size[1] = ry.Size();
|
---|
| 511 | size[2] = rz.Size();
|
---|
| 512 | size[3] = rt.Size();
|
---|
| 513 | size[4] = ru.Size();
|
---|
| 514 |
|
---|
| 515 | step[0] = rx.Step();
|
---|
| 516 | step[1] = ry.Step();
|
---|
| 517 | step[2] = rz.Step();
|
---|
| 518 | step[3] = rt.Step();
|
---|
| 519 | step[4] = ru.Step();
|
---|
| 520 |
|
---|
| 521 | pos[0] = rx.Start();
|
---|
| 522 | pos[1] = ry.Start();
|
---|
| 523 | pos[2] = rz.Start();
|
---|
| 524 | pos[3] = rt.Start();
|
---|
| 525 | pos[4] = ru.Start();
|
---|
| 526 |
|
---|
| 527 | ndim = ndim_;
|
---|
| 528 | TArray<T> ra;
|
---|
[804] | 529 | UpdateSubArraySizes(ra, ndim, size, pos, step);
|
---|
[787] | 530 | ra.DataBlock().Share(this->DataBlock());
|
---|
[2917] | 531 | if (compact) ra.CompactTrailingDim();
|
---|
[785] | 532 | return(ra);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
[772] | 535 | // ...... Operation de calcul sur les tableaux ......
|
---|
| 536 | // ------- Attention --------
|
---|
| 537 | // Boucles normales prenant en compte les steps ....
|
---|
[894] | 538 | // Possibilite de // , vectorisation
|
---|
| 539 |
|
---|
| 540 | //! Fill TArray with Sequence \b seq
|
---|
| 541 | /*!
|
---|
| 542 | \param seq : sequence to fill the array
|
---|
| 543 | \sa Sequence
|
---|
| 544 | */
|
---|
[772] | 545 | template <class T>
|
---|
[1103] | 546 | TArray<T>& TArray<T>::SetSeq(Sequence const & seq)
|
---|
[772] | 547 | {
|
---|
[804] | 548 | if (NbDimensions() < 1)
|
---|
[813] | 549 | throw RangeCheckError("TArray<T>::SetSeq(Sequence ) - Not Allocated Array ! ");
|
---|
[1103] | 550 |
|
---|
[785] | 551 | T * pe;
|
---|
[1156] | 552 | sa_size_t j,k;
|
---|
| 553 | int_4 ka;
|
---|
[1103] | 554 | if (arrtype_ == 0) ka = 0;
|
---|
| 555 | else ka = macoli_;
|
---|
[1156] | 556 | sa_size_t step = Step(ka);
|
---|
| 557 | sa_size_t gpas = Size(ka);
|
---|
| 558 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1103] | 559 | for(j=0; j<naxa; j++) {
|
---|
| 560 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[2153] | 561 | /*
|
---|
| 562 | Appel explicite de l'operateur de conversion
|
---|
| 563 | suite a la suggestion de M. Reinecke, Reza 31/7/2002
|
---|
[1103] | 564 | #if !defined(__GNUG__)
|
---|
| 565 | for(k=0; k<gpas; k++) pe[k*step] = (T) seq(j*gpas+k);
|
---|
| 566 | #else
|
---|
| 567 | // g++ (up to 2.95.1) se melange les pinceaux s'il y a le cast (T) pour l'instanciation des complexes
|
---|
| 568 | for(k=0; k<gpas; k++) pe[k*step] = seq(j*gpas+k);
|
---|
| 569 | #endif
|
---|
[2153] | 570 | --- Appel explicite de l'operateur de conversion sur l'objet MuTyV
|
---|
| 571 | */
|
---|
[2884] | 572 | for(k=0; k<gpas; k++) seq(j*gpas+k).Convert(pe[k*step]);
|
---|
| 573 | //REMPLACE suite pb compil gcc4 for(k=0; k<gpas; k++) pe[k*step] = seq(j*gpas+k).operator T();
|
---|
[785] | 574 | }
|
---|
[772] | 575 | return(*this);
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | // >>>> Operations avec 2nd membre de type scalaire
|
---|
| 579 |
|
---|
[894] | 580 | //! Fill an array with a constant value \b x
|
---|
[772] | 581 | template <class T>
|
---|
[2575] | 582 | TArray<T>& TArray<T>::SetCst(T x)
|
---|
[772] | 583 | {
|
---|
[804] | 584 | if (NbDimensions() < 1)
|
---|
[2575] | 585 | throw RangeCheckError("TArray<T>::SetCst(T ) - Not Allocated Array ! ");
|
---|
[785] | 586 | T * pe;
|
---|
[1156] | 587 | sa_size_t j,k;
|
---|
[785] | 588 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 589 | sa_size_t step = AvgStep();
|
---|
| 590 | sa_size_t maxx = totsize_*step;
|
---|
[785] | 591 | pe = Data();
|
---|
| 592 | for(k=0; k<maxx; k+=step ) pe[k] = x;
|
---|
| 593 | }
|
---|
| 594 | else { // Non regular data spacing ...
|
---|
[1156] | 595 | int_4 ka = MaxSizeKA();
|
---|
| 596 | sa_size_t step = Step(ka);
|
---|
| 597 | sa_size_t gpas = Size(ka)*step;
|
---|
| 598 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 599 | for(j=0; j<naxa; j++) {
|
---|
| 600 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[785] | 601 | for(k=0; k<gpas; k+=step) pe[k] = x;
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
[772] | 604 | return(*this);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[2564] | 607 | //! Add a constant value \b x to the source array and store the result in \b res.
|
---|
| 608 | /*!
|
---|
| 609 | Add a constant to the source array \b this and store the result in \b res (res = *this+x).
|
---|
[2938] | 610 |
|
---|
| 611 | If not initially allocated, and if the source array (this) is not flagged as
|
---|
| 612 | temporary, the output array \b res is automatically
|
---|
[2564] | 613 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 614 | If \b res is not allocated and (this) is temporary, data is shared between \b res and this.
|
---|
| 615 |
|
---|
[2564] | 616 | Returns a reference to the output array \b res.
|
---|
[2938] | 617 |
|
---|
[2564] | 618 | \param x : constant to add to the array elements
|
---|
| 619 | \param res : Output array containing the result (res=this+x).
|
---|
| 620 | */
|
---|
[772] | 621 | template <class T>
|
---|
[2564] | 622 | TArray<T>& TArray<T>::AddCst(T x, TArray<T>& res) const
|
---|
[772] | 623 | {
|
---|
[804] | 624 | if (NbDimensions() < 1)
|
---|
[2564] | 625 | throw RangeCheckError("TArray<T>::AddCst(T,res) - Not allocated source array ");
|
---|
[2938] | 626 | if (res.NbDimensions() < 1) {
|
---|
| 627 | if ( IsTemp() ) res.Share(*this);
|
---|
| 628 | else res.SetSize(*this, true, false);
|
---|
| 629 | }
|
---|
[2564] | 630 | bool smo;
|
---|
| 631 | if (!CompareSizes(res, smo))
|
---|
| 632 | throw(SzMismatchError("TArray<T>::AddCst(T, res) SizeMismatch(this,res) ")) ;
|
---|
| 633 |
|
---|
| 634 | const T * pe;
|
---|
| 635 | T * per;
|
---|
[2575] | 636 | sa_size_t j,k;
|
---|
[2564] | 637 | if (smo && (IsPacked() > 0) && (res.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 638 | sa_size_t maxx = totsize_;
|
---|
[785] | 639 | pe = Data();
|
---|
[2564] | 640 | per = res.Data();
|
---|
[2587] | 641 | for(k=0; k<maxx; k++) *per++ = *pe++ + x;
|
---|
[785] | 642 | }
|
---|
| 643 | else { // Non regular data spacing ...
|
---|
[2564] | 644 | int_4 ax,axr;
|
---|
| 645 | sa_size_t step, stepr;
|
---|
[2575] | 646 | sa_size_t gpas, naxa;
|
---|
| 647 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 648 | for(j=0; j<naxa; j++) {
|
---|
[2564] | 649 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 650 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
[2575] | 651 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = *pe+x;
|
---|
[785] | 652 | }
|
---|
| 653 | }
|
---|
[2564] | 654 | return(res);
|
---|
[772] | 655 | }
|
---|
| 656 |
|
---|
[2564] | 657 | //! Subtract a constant value \b x from the source array and store the result in \b res.
|
---|
[970] | 658 | /*!
|
---|
[2564] | 659 | Subtract a constant from the source array \b this and store the result in \b res (res = *this-x).
|
---|
[2938] | 660 |
|
---|
| 661 | If not initially allocated, and if the source array (this) is not flagged as
|
---|
| 662 | temporary, the output array \b res is automatically
|
---|
[2564] | 663 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 664 | If \b res is not allocated and (this) is temporary, data is shared between \b res and this.
|
---|
| 665 |
|
---|
[2564] | 666 | Returns a reference to the output array \b res.
|
---|
[2938] | 667 |
|
---|
[2564] | 668 | \param x : constant to subtract from the array elements
|
---|
| 669 | \param res : Output array containing the result (res=this+x or res=x-this).
|
---|
[2575] | 670 | \param fginv == true : Invert subtraction argument order (res = x-(*this))
|
---|
[970] | 671 | */
|
---|
[772] | 672 | template <class T>
|
---|
[2564] | 673 | TArray<T>& TArray<T>::SubCst(T x, TArray<T>& res, bool fginv) const
|
---|
[772] | 674 | {
|
---|
[804] | 675 | if (NbDimensions() < 1)
|
---|
[2564] | 676 | throw RangeCheckError("TArray<T>::SubCst(T,res) - Not allocated source array ");
|
---|
[2938] | 677 | if (res.NbDimensions() < 1) {
|
---|
| 678 | if ( IsTemp() ) res.Share(*this);
|
---|
| 679 | else res.SetSize(*this, true, false);
|
---|
| 680 | }
|
---|
[2564] | 681 | bool smo;
|
---|
| 682 | if (!CompareSizes(res, smo))
|
---|
| 683 | throw(SzMismatchError("TArray<T>::SubCst(T, res) SizeMismatch(this,res) ")) ;
|
---|
| 684 |
|
---|
| 685 | const T * pe;
|
---|
| 686 | T * per;
|
---|
[2575] | 687 | sa_size_t j,k;
|
---|
[2564] | 688 | if (smo && (IsPacked() > 0) && (res.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 689 | sa_size_t maxx = totsize_;
|
---|
[785] | 690 | pe = Data();
|
---|
[2564] | 691 | per = res.Data();
|
---|
| 692 | if (!fginv)
|
---|
[2587] | 693 | for(k=0; k<maxx; k++) *per++ = *pe++ - x;
|
---|
[2564] | 694 | else
|
---|
[2587] | 695 | for(k=0; k<maxx; k++) *per++ = x - *pe++;
|
---|
[785] | 696 | }
|
---|
| 697 | else { // Non regular data spacing ...
|
---|
[2564] | 698 | int_4 ax,axr;
|
---|
| 699 | sa_size_t step, stepr;
|
---|
[2575] | 700 | sa_size_t gpas, naxa;
|
---|
| 701 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 702 | for(j=0; j<naxa; j++) {
|
---|
[2564] | 703 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 704 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
| 705 | if (!fginv)
|
---|
[2575] | 706 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = *pe-x;
|
---|
| 707 | else
|
---|
| 708 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = x-*pe;
|
---|
[785] | 709 | }
|
---|
| 710 | }
|
---|
[2564] | 711 | return(res);
|
---|
[772] | 712 | }
|
---|
| 713 |
|
---|
[2564] | 714 | //! Multiply the source array by a constant value \b x and store the result in \b res.
|
---|
| 715 | /*!
|
---|
| 716 | Multiply the source array \b this by a constant \b x and store the result in \b res (res = *this*x).
|
---|
[2938] | 717 |
|
---|
| 718 | If not initially allocated, and if the source array (this) is not flagged as
|
---|
| 719 | temporary, the output array \b res is automatically
|
---|
[2564] | 720 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 721 | If \b res is not allocated and (this) is temporary, data is shared between \b res and this.
|
---|
| 722 |
|
---|
[2564] | 723 | Returns a reference to the output array \b res.
|
---|
[2938] | 724 |
|
---|
[2564] | 725 | \param x : Array elements are multiplied by x
|
---|
| 726 | \param res : Output array containing the result (res=this*x).
|
---|
| 727 | */
|
---|
[772] | 728 | template <class T>
|
---|
[2564] | 729 | TArray<T>& TArray<T>::MulCst(T x, TArray<T>& res) const
|
---|
[772] | 730 | {
|
---|
[804] | 731 | if (NbDimensions() < 1)
|
---|
[2564] | 732 | throw RangeCheckError("TArray<T>::MulCst(T,res) - Not allocated source array ");
|
---|
[2938] | 733 | if (res.NbDimensions() < 1) {
|
---|
| 734 | if ( IsTemp() ) res.Share(*this);
|
---|
| 735 | else res.SetSize(*this, true, false);
|
---|
| 736 | }
|
---|
[2564] | 737 | bool smo;
|
---|
| 738 | if (!CompareSizes(res, smo))
|
---|
| 739 | throw(SzMismatchError("TArray<T>::MulCst(T, res) SizeMismatch(this,res) ")) ;
|
---|
| 740 |
|
---|
| 741 | const T * pe;
|
---|
| 742 | T * per;
|
---|
[2575] | 743 | sa_size_t j,k;
|
---|
[2564] | 744 | if (smo && (IsPacked() > 0) && (res.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 745 | sa_size_t maxx = totsize_;
|
---|
[785] | 746 | pe = Data();
|
---|
[2564] | 747 | per = res.Data();
|
---|
[2587] | 748 | for(k=0; k<maxx; k++) *per++ = *pe++ * x;
|
---|
[785] | 749 | }
|
---|
| 750 | else { // Non regular data spacing ...
|
---|
[2564] | 751 | int_4 ax,axr;
|
---|
| 752 | sa_size_t step, stepr;
|
---|
[2575] | 753 | sa_size_t gpas, naxa;
|
---|
| 754 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 755 | for(j=0; j<naxa; j++) {
|
---|
[2564] | 756 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 757 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
[2575] | 758 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = (*pe)*x;
|
---|
[785] | 759 | }
|
---|
| 760 | }
|
---|
[2564] | 761 | return(res);
|
---|
[772] | 762 | }
|
---|
| 763 |
|
---|
[2564] | 764 | //! Divide the source array by a constant value \b x and store the result in \b res.
|
---|
[970] | 765 | /*!
|
---|
[2564] | 766 | Divide the source array \b this by a constant \b x and store the result in \b res (res = *this/x).
|
---|
[2938] | 767 |
|
---|
| 768 | If not initially allocated, and if the source array (this) is not flagged as
|
---|
| 769 | temporary, the output array \b res is automatically
|
---|
[2564] | 770 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 771 | If \b res is not allocated and (this) is temporary, data is shared between \b res and this.
|
---|
| 772 |
|
---|
[2564] | 773 | Returns a reference to the output array \b res.
|
---|
[2938] | 774 |
|
---|
[2564] | 775 | \param x : Array elements are divied by x
|
---|
| 776 | \param res : Output array containing the result (res=(*this)/x or res=x/(*this)).
|
---|
[2575] | 777 | \param fginv == true : Invert the division argument order (res = x/(*this))
|
---|
[970] | 778 | */
|
---|
[772] | 779 | template <class T>
|
---|
[2564] | 780 | TArray<T>& TArray<T>::DivCst(T x, TArray<T>& res, bool fginv) const
|
---|
[772] | 781 | {
|
---|
[804] | 782 | if (NbDimensions() < 1)
|
---|
[2564] | 783 | throw RangeCheckError("TArray<T>::DivCst(T,res) - Not allocated source array ! ");
|
---|
[970] | 784 | if (!fginv && (x == (T) 0) )
|
---|
[2564] | 785 | throw MathExc("TArray<T>::DivCst(T,res) - Divide by zero ! ");
|
---|
[2938] | 786 | if (res.NbDimensions() < 1) {
|
---|
| 787 | if ( IsTemp() ) res.Share(*this);
|
---|
| 788 | else res.SetSize(*this, true, false);
|
---|
| 789 | }
|
---|
[2564] | 790 | bool smo;
|
---|
| 791 | if (!CompareSizes(res, smo))
|
---|
| 792 | throw(SzMismatchError("TArray<T>::DivCst(T, res) SizeMismatch(this,res) ")) ;
|
---|
| 793 |
|
---|
| 794 | const T * pe;
|
---|
| 795 | T * per;
|
---|
[2589] | 796 | sa_size_t j,k;
|
---|
[2564] | 797 | if (smo && (IsPacked() > 0) && (res.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 798 | sa_size_t maxx = totsize_;
|
---|
[785] | 799 | pe = Data();
|
---|
[2564] | 800 | per = res.Data();
|
---|
| 801 | if (!fginv)
|
---|
[2587] | 802 | for(k=0; k<maxx; k++) *per++ = *pe++ / x;
|
---|
[970] | 803 | else
|
---|
[2587] | 804 | for(k=0; k<maxx; k++) *per++ = x / *pe++;
|
---|
[785] | 805 | }
|
---|
| 806 | else { // Non regular data spacing ...
|
---|
[2564] | 807 | int_4 ax,axr;
|
---|
| 808 | sa_size_t step, stepr;
|
---|
[2575] | 809 | sa_size_t gpas, naxa;
|
---|
| 810 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 811 | for(j=0; j<naxa; j++) {
|
---|
[2564] | 812 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 813 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
| 814 | if (!fginv)
|
---|
[2575] | 815 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = (*pe)/x;
|
---|
| 816 | else
|
---|
| 817 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = x/(*pe);
|
---|
[785] | 818 | }
|
---|
| 819 | }
|
---|
[2564] | 820 | return(res);
|
---|
[772] | 821 | }
|
---|
| 822 |
|
---|
| 823 |
|
---|
[2564] | 824 | //! Stores the opposite of the source array in \b res (res=-(*this)).
|
---|
| 825 | /*!
|
---|
[2938] | 826 | If not initially allocated, and if the source array (this) is not flagged as
|
---|
| 827 | temporary, the output array \b res is automatically
|
---|
[2564] | 828 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 829 | If \b res is not allocated and (this) is temporary, data is shared between \b res and this.
|
---|
| 830 |
|
---|
[2564] | 831 | Returns a reference to the output array \b res.
|
---|
| 832 | */
|
---|
[1156] | 833 | template <class T>
|
---|
[2564] | 834 | TArray<T>& TArray<T>::NegateElt(TArray<T>& res) const
|
---|
[1156] | 835 | {
|
---|
| 836 | if (NbDimensions() < 1)
|
---|
[2564] | 837 | throw RangeCheckError("TArray<T>::NegateElt(res) - Not allocated source array ");
|
---|
[2938] | 838 | if (res.NbDimensions() < 1) {
|
---|
| 839 | if ( IsTemp() ) res.Share(*this);
|
---|
| 840 | else res.SetSize(*this, true, false);
|
---|
| 841 | }
|
---|
[2564] | 842 | bool smo;
|
---|
| 843 | if (!CompareSizes(res, smo))
|
---|
| 844 | throw(SzMismatchError("TArray<T>::NegateElt(res) SizeMismatch(this,res) ")) ;
|
---|
| 845 |
|
---|
| 846 | const T * pe;
|
---|
| 847 | T * per;
|
---|
[2575] | 848 | sa_size_t j,k;
|
---|
[2564] | 849 | if (smo && (IsPacked() > 0) && (res.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 850 | sa_size_t maxx = totsize_;
|
---|
[1156] | 851 | pe = Data();
|
---|
[2564] | 852 | per = res.Data();
|
---|
[2587] | 853 | for(k=0; k<maxx; k++) *per++ = -(*pe++);
|
---|
[1156] | 854 | }
|
---|
| 855 | else { // Non regular data spacing ...
|
---|
[2564] | 856 | int_4 ax,axr;
|
---|
| 857 | sa_size_t step, stepr;
|
---|
[2575] | 858 | sa_size_t gpas, naxa;
|
---|
| 859 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 860 | for(j=0; j<naxa; j++) {
|
---|
[2564] | 861 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 862 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
[2575] | 863 | for(k=0; k<gpas; k+=step, pe+=step, per+=stepr) *per = -(*pe);
|
---|
[1156] | 864 | }
|
---|
| 865 | }
|
---|
[2564] | 866 | return(res);
|
---|
[1156] | 867 | }
|
---|
[804] | 868 |
|
---|
[772] | 869 | // >>>> Operations avec 2nd membre de type tableau
|
---|
[2575] | 870 |
|
---|
| 871 | //! Two TArrays element by element addition
|
---|
| 872 | /*!
|
---|
| 873 | Perform element by element addition of the source array (this) and the \b a array
|
---|
| 874 | and store the result in \b res (res = *this+a). The source and argument arrays (this, a)
|
---|
| 875 | should have the same sizes.
|
---|
[2938] | 876 |
|
---|
| 877 | If not initially allocated, and if none of the source arrays is flagged as
|
---|
| 878 | temporary, the output array \b res is automatically
|
---|
[2575] | 879 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 880 | If \b res is not allocated and one of the source array is temporary,
|
---|
| 881 | data is shared between \b res and the temporary source array.
|
---|
| 882 |
|
---|
[2575] | 883 | Returns a reference to the output array \b res.
|
---|
[2938] | 884 |
|
---|
[2575] | 885 | \param a : Array to be added to the source array.
|
---|
| 886 | \param res : Output array containing the result (res=this+a).
|
---|
| 887 | */
|
---|
[772] | 888 | template <class T>
|
---|
[2575] | 889 | TArray<T>& TArray<T>::AddElt(const TArray<T>& a, TArray<T>& res) const
|
---|
[772] | 890 | {
|
---|
[804] | 891 | if (NbDimensions() < 1)
|
---|
[2575] | 892 | throw RangeCheckError("TArray<T>::AddElt(...) - Not allocated source array ! ");
|
---|
| 893 | bool smoa;
|
---|
| 894 | if (!CompareSizes(a, smoa))
|
---|
| 895 | throw(SzMismatchError("TArray<T>::AddElt(...) SizeMismatch(this,a)")) ;
|
---|
[2938] | 896 | if (res.NbDimensions() < 1) {
|
---|
| 897 | if ( IsTemp() ) res.Share(*this);
|
---|
| 898 | else if ( a.IsTemp() ) res.Share(a);
|
---|
| 899 | else res.SetSize(*this, true, false);
|
---|
| 900 | }
|
---|
[2575] | 901 | bool smor;
|
---|
| 902 | if (!CompareSizes(res, smor))
|
---|
| 903 | throw(SzMismatchError("TArray<T>::AddElt(...) SizeMismatch(this,res) ")) ;
|
---|
[785] | 904 |
|
---|
[2575] | 905 | bool smora;
|
---|
| 906 | a.CompareSizes(res, smora);
|
---|
| 907 |
|
---|
| 908 | bool smo = smoa && smor; // The three arrays have same memory organisation
|
---|
| 909 |
|
---|
| 910 | const T * pe;
|
---|
[785] | 911 | const T * pea;
|
---|
[2575] | 912 | T * per;
|
---|
| 913 | sa_size_t j,k;
|
---|
| 914 | if (smo && IsPacked() && a.IsPacked() && res.IsPacked() ) { // All packed arrays
|
---|
| 915 | sa_size_t maxx = totsize_;
|
---|
[785] | 916 | pe = Data();
|
---|
| 917 | pea = a.Data();
|
---|
[2575] | 918 | per = res.Data();
|
---|
[2587] | 919 | // for(k=0; k<maxx; k++, pe++, pea++, per++) *per = *pe + *pea ;
|
---|
| 920 | for(k=0; k<maxx; k++) *per++ = *pe++ + *pea++ ;
|
---|
[772] | 921 | }
|
---|
[785] | 922 | else { // Non regular data spacing ...
|
---|
[2575] | 923 | int_4 ax,axa,axr;
|
---|
[1156] | 924 | sa_size_t step, stepa;
|
---|
| 925 | sa_size_t gpas, naxa;
|
---|
[2575] | 926 | sa_size_t stepr, stgpas;
|
---|
| 927 | if ( !smo && smora ) { // same mem-org for a,res , different from this
|
---|
| 928 | a.GetOpeParams(*this, smo, axa, ax, stepa, step, gpas, naxa);
|
---|
| 929 | a.GetOpeParams(res, smo, axa, axr, stepa, stepr, gpas, naxa);
|
---|
| 930 | stgpas = stepa;
|
---|
| 931 | }
|
---|
| 932 | else { // same mem-org for all, or same (this,a) OR same(this,res)
|
---|
| 933 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
| 934 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 935 | stgpas = step;
|
---|
| 936 | }
|
---|
[813] | 937 | for(j=0; j<naxa; j++) {
|
---|
| 938 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 939 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[2575] | 940 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
| 941 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr) *per = *pe + *pea ;
|
---|
[785] | 942 | }
|
---|
| 943 | }
|
---|
[2575] | 944 | return(res);
|
---|
[772] | 945 | }
|
---|
| 946 |
|
---|
[2575] | 947 | //! Two TArrays element by element subtraction
|
---|
[970] | 948 | /*!
|
---|
[2575] | 949 | Perform element by element subtraction of the source array (this) and the \b a array
|
---|
| 950 | and the store result in \b res (res = *this-a or res=a-(*this)).
|
---|
| 951 | The source and argument arrays (this, a) should have the same sizes.
|
---|
[2938] | 952 |
|
---|
| 953 | If not initially allocated, and if none of the source arrays is flagged as
|
---|
| 954 | temporary, the output array \b res is automatically
|
---|
[2575] | 955 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 956 | If \b res is not allocated and one of the source array is temporary,
|
---|
| 957 | data is shared between \b res and the temporary source array.
|
---|
| 958 |
|
---|
[2575] | 959 | Returns a reference to the output array \b res.
|
---|
[2938] | 960 |
|
---|
[2575] | 961 | \param a : Array to be added to the source array.
|
---|
| 962 | \param res : Output array containing the result (res=*this+x).
|
---|
| 963 | \param fginv == true : Invert subtraction argument order (res = a-(*this))
|
---|
[970] | 964 | */
|
---|
[2575] | 965 |
|
---|
[772] | 966 | template <class T>
|
---|
[2575] | 967 | TArray<T>& TArray<T>::SubElt(const TArray<T>& a, TArray<T>& res, bool fginv) const
|
---|
[772] | 968 | {
|
---|
[804] | 969 | if (NbDimensions() < 1)
|
---|
[2575] | 970 | throw RangeCheckError("TArray<T>::SubElt(...) - Not allocated source array ! ");
|
---|
| 971 | bool smoa;
|
---|
| 972 | if (!CompareSizes(a, smoa))
|
---|
| 973 | throw(SzMismatchError("TArray<T>::SubElt(...) SizeMismatch(this,a)")) ;
|
---|
[2938] | 974 | if (res.NbDimensions() < 1) {
|
---|
| 975 | if ( IsTemp() ) res.Share(*this);
|
---|
| 976 | else if ( a.IsTemp() ) res.Share(a);
|
---|
| 977 | else res.SetSize(*this, true, false);
|
---|
| 978 | }
|
---|
[2575] | 979 | bool smor;
|
---|
| 980 | if (!CompareSizes(res, smor))
|
---|
| 981 | throw(SzMismatchError("TArray<T>::SubElt(...) SizeMismatch(this,res) ")) ;
|
---|
[785] | 982 |
|
---|
[2575] | 983 | bool smora;
|
---|
| 984 | a.CompareSizes(res, smora);
|
---|
| 985 |
|
---|
| 986 | bool smo = smoa && smor; // The three arrays have same memory organisation
|
---|
| 987 |
|
---|
| 988 | const T * pe;
|
---|
[785] | 989 | const T * pea;
|
---|
[2575] | 990 | T * per;
|
---|
| 991 | sa_size_t j,k;
|
---|
| 992 | if (smo && IsPacked() && a.IsPacked() && res.IsPacked() ) { // All packed arrays
|
---|
| 993 | sa_size_t maxx = totsize_;
|
---|
[785] | 994 | pe = Data();
|
---|
| 995 | pea = a.Data();
|
---|
[2575] | 996 | per = res.Data();
|
---|
| 997 | if (!fginv)
|
---|
[2587] | 998 | for(k=0; k<maxx; k++) *per++ = *pe++ - *pea++ ;
|
---|
[970] | 999 | else
|
---|
[2587] | 1000 | for(k=0; k<maxx; k++) *per++ = *pea++ - *pe++ ;
|
---|
[772] | 1001 | }
|
---|
[785] | 1002 | else { // Non regular data spacing ...
|
---|
[2575] | 1003 | int_4 ax,axa,axr;
|
---|
[1156] | 1004 | sa_size_t step, stepa;
|
---|
| 1005 | sa_size_t gpas, naxa;
|
---|
[2575] | 1006 | sa_size_t stepr, stgpas;
|
---|
| 1007 | if ( !smo && smora ) { // same mem-org for a,res , different from this
|
---|
| 1008 | a.GetOpeParams(*this, smo, axa, ax, stepa, step, gpas, naxa);
|
---|
| 1009 | a.GetOpeParams(res, smo, axa, axr, stepa, stepr, gpas, naxa);
|
---|
| 1010 | stgpas = stepa;
|
---|
| 1011 | }
|
---|
| 1012 | else { // same mem-org for all, or same (this,a) OR same(this,res)
|
---|
| 1013 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
| 1014 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 1015 | stgpas = step;
|
---|
| 1016 | }
|
---|
[813] | 1017 | for(j=0; j<naxa; j++) {
|
---|
| 1018 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 1019 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[2575] | 1020 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
| 1021 | if (!fginv)
|
---|
| 1022 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr) *per = *pe - *pea ;
|
---|
| 1023 | else
|
---|
| 1024 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr) *per = *pea - *pea ;
|
---|
[785] | 1025 | }
|
---|
| 1026 | }
|
---|
[2575] | 1027 | return(res);
|
---|
[772] | 1028 | }
|
---|
| 1029 |
|
---|
[970] | 1030 |
|
---|
[2575] | 1031 | //! Two TArrays element by element multiplication
|
---|
| 1032 | /*!
|
---|
| 1033 | Perform element by element multiplication of the source array (this) and the \b a array
|
---|
| 1034 | and store the result in \b res (res = *this*a). The source and argument arrays (this, a)
|
---|
| 1035 | should have the same sizes.
|
---|
[2938] | 1036 |
|
---|
| 1037 | If not initially allocated, and if none of the source arrays is flagged as
|
---|
| 1038 | temporary, the output array \b res is automatically
|
---|
[2575] | 1039 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 1040 | If \b res is not allocated and one of the source array is temporary,
|
---|
| 1041 | data is shared between \b res and the temporary source array.
|
---|
| 1042 |
|
---|
[2575] | 1043 | Returns a reference to the output array \b res.
|
---|
[2938] | 1044 |
|
---|
[2575] | 1045 | \param a : Array to be added to the source array.
|
---|
| 1046 | \param res : Output array containing the result (res=(*this)*a).
|
---|
| 1047 | */
|
---|
[772] | 1048 | template <class T>
|
---|
[2575] | 1049 | TArray<T>& TArray<T>::MulElt(const TArray<T>& a, TArray<T>& res) const
|
---|
[772] | 1050 | {
|
---|
[804] | 1051 | if (NbDimensions() < 1)
|
---|
[2575] | 1052 | throw RangeCheckError("TArray<T>::MulElt(...) - Not allocated source array ! ");
|
---|
| 1053 | bool smoa;
|
---|
| 1054 | if (!CompareSizes(a, smoa))
|
---|
| 1055 | throw(SzMismatchError("TArray<T>::MulElt(...) SizeMismatch(this,a)")) ;
|
---|
[2938] | 1056 | if (res.NbDimensions() < 1) {
|
---|
| 1057 | if ( IsTemp() ) res.Share(*this);
|
---|
| 1058 | else if ( a.IsTemp() ) res.Share(a);
|
---|
| 1059 | else res.SetSize(*this, true, false);
|
---|
| 1060 | }
|
---|
[2575] | 1061 | bool smor;
|
---|
| 1062 | if (!CompareSizes(res, smor))
|
---|
| 1063 | throw(SzMismatchError("TArray<T>::MulElt(...) SizeMismatch(this,res) ")) ;
|
---|
[785] | 1064 |
|
---|
[2575] | 1065 | bool smora;
|
---|
| 1066 | a.CompareSizes(res, smora);
|
---|
| 1067 |
|
---|
| 1068 | bool smo = smoa && smor; // The three arrays have same memory organisation
|
---|
| 1069 |
|
---|
| 1070 | const T * pe;
|
---|
[785] | 1071 | const T * pea;
|
---|
[2575] | 1072 | T * per;
|
---|
| 1073 | sa_size_t j,k;
|
---|
| 1074 | if (smo && IsPacked() && a.IsPacked() && res.IsPacked() ) { // All packed arrays
|
---|
| 1075 | sa_size_t maxx = totsize_;
|
---|
[785] | 1076 | pe = Data();
|
---|
| 1077 | pea = a.Data();
|
---|
[2575] | 1078 | per = res.Data();
|
---|
[2587] | 1079 | for(k=0; k<maxx; k++) *per++ = *pe++ * *pea++ ;
|
---|
[772] | 1080 | }
|
---|
[785] | 1081 | else { // Non regular data spacing ...
|
---|
[2575] | 1082 | int_4 ax,axa,axr;
|
---|
[1156] | 1083 | sa_size_t step, stepa;
|
---|
| 1084 | sa_size_t gpas, naxa;
|
---|
[2575] | 1085 | sa_size_t stepr, stgpas;
|
---|
| 1086 | if ( !smo && smora ) { // same mem-org for a,res , different from this
|
---|
| 1087 | a.GetOpeParams(*this, smo, axa, ax, stepa, step, gpas, naxa);
|
---|
| 1088 | a.GetOpeParams(res, smo, axa, axr, stepa, stepr, gpas, naxa);
|
---|
| 1089 | stgpas = stepa;
|
---|
| 1090 | }
|
---|
| 1091 | else { // same mem-org for all, or same (this,a) OR same(this,res)
|
---|
| 1092 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
| 1093 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 1094 | stgpas = step;
|
---|
| 1095 | }
|
---|
[813] | 1096 | for(j=0; j<naxa; j++) {
|
---|
[2575] | 1097 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 1098 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[2575] | 1099 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
| 1100 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr) *per = (*pe) * (*pea);
|
---|
[785] | 1101 | }
|
---|
| 1102 | }
|
---|
[2575] | 1103 | return(res);
|
---|
[772] | 1104 | }
|
---|
| 1105 |
|
---|
[804] | 1106 |
|
---|
[2575] | 1107 | //! Two TArrays element by element division
|
---|
[970] | 1108 | /*!
|
---|
[2575] | 1109 | Perform element by element division of the source array (this) and the \b a array
|
---|
| 1110 | and store the result in \b res (res = *this/a). The source and argument arrays (this, a)
|
---|
| 1111 | should have the same sizes.
|
---|
[2938] | 1112 |
|
---|
| 1113 | If not initially allocated, and if none of the source arrays is flagged as
|
---|
| 1114 | temporary, the output array \b res is automatically
|
---|
[2575] | 1115 | resized as a packed array with the same sizes as the source (this) array.
|
---|
[2938] | 1116 | If \b res is not allocated and one of the source array is temporary,
|
---|
| 1117 | data is shared between \b res and the temporary source array.
|
---|
| 1118 |
|
---|
[2575] | 1119 | Returns a reference to the output array \b res.
|
---|
[2938] | 1120 |
|
---|
[2575] | 1121 | \param a : Array to be added to the source array.
|
---|
| 1122 | \param res : Output array containing the result (res=*this/a).
|
---|
| 1123 | \param fginv == true : Inverts the division argument order (res = a/(*this))
|
---|
| 1124 | \param divzero == true : Result is set to zero (res(i)=0) if the operation's
|
---|
| 1125 | second argument is equal to zero (a(i)/(*this)(i)==0)
|
---|
[970] | 1126 | */
|
---|
[772] | 1127 | template <class T>
|
---|
[2575] | 1128 | TArray<T>& TArray<T>::DivElt(const TArray<T>& a, TArray<T>& res, bool fginv, bool divzero) const
|
---|
[772] | 1129 | {
|
---|
[804] | 1130 | if (NbDimensions() < 1)
|
---|
[2575] | 1131 | throw RangeCheckError("TArray<T>::DivElt(...) - Not allocated source array ! ");
|
---|
| 1132 | bool smoa;
|
---|
| 1133 | if (!CompareSizes(a, smoa))
|
---|
| 1134 | throw(SzMismatchError("TArray<T>::DivElt(...) SizeMismatch(this,a)")) ;
|
---|
[2938] | 1135 | if (res.NbDimensions() < 1) {
|
---|
| 1136 | if ( IsTemp() ) res.Share(*this);
|
---|
| 1137 | else if ( a.IsTemp() ) res.Share(a);
|
---|
| 1138 | else res.SetSize(*this, true, false);
|
---|
| 1139 | }
|
---|
[2575] | 1140 | bool smor;
|
---|
| 1141 | if (!CompareSizes(res, smor))
|
---|
| 1142 | throw(SzMismatchError("TArray<T>::DivElt(...) SizeMismatch(this,res) ")) ;
|
---|
[785] | 1143 |
|
---|
[2575] | 1144 | bool smora;
|
---|
| 1145 | a.CompareSizes(res, smora);
|
---|
| 1146 |
|
---|
| 1147 | bool smo = smoa && smor; // The three arrays have same memory organisation
|
---|
| 1148 |
|
---|
| 1149 | const T * pe;
|
---|
[785] | 1150 | const T * pea;
|
---|
[2575] | 1151 | T * per;
|
---|
| 1152 | sa_size_t j,k;
|
---|
| 1153 | if (smo && IsPacked() && a.IsPacked() && res.IsPacked() ) { // All packed arrays
|
---|
| 1154 | sa_size_t maxx = totsize_;
|
---|
[785] | 1155 | pe = Data();
|
---|
| 1156 | pea = a.Data();
|
---|
[2575] | 1157 | per = res.Data();
|
---|
[1072] | 1158 | if(divzero) {
|
---|
[2575] | 1159 | if (!fginv)
|
---|
[2587] | 1160 | for(k=0; k<maxx; k++)
|
---|
| 1161 | if (*pea==(T)0) *per = (T)0; else *per++ = *pe++ / *pea++ ;
|
---|
[1072] | 1162 | else
|
---|
[2587] | 1163 | for(k=0; k<maxx; k++)
|
---|
| 1164 | if (*pe==(T)0) *per = (T)0; else *per++ = *pea++ / *pe++ ;
|
---|
[1072] | 1165 | }
|
---|
[2575] | 1166 | else {
|
---|
| 1167 | if (!fginv)
|
---|
[2587] | 1168 | for(k=0; k<maxx; k++) *per++ = *pe++ / *pea++ ;
|
---|
[2575] | 1169 | else
|
---|
[2587] | 1170 | for(k=0; k<maxx; k++) *per = *pea++ / *pe++ ;
|
---|
[2575] | 1171 | }
|
---|
[772] | 1172 | }
|
---|
[785] | 1173 | else { // Non regular data spacing ...
|
---|
[2575] | 1174 | int_4 ax,axa,axr;
|
---|
[1156] | 1175 | sa_size_t step, stepa;
|
---|
| 1176 | sa_size_t gpas, naxa;
|
---|
[2575] | 1177 | sa_size_t stepr, stgpas;
|
---|
| 1178 | if ( !smo && smora ) { // same mem-org for a,res , different from this
|
---|
| 1179 | a.GetOpeParams(*this, smo, axa, ax, stepa, step, gpas, naxa);
|
---|
| 1180 | a.GetOpeParams(res, smo, axa, axr, stepa, stepr, gpas, naxa);
|
---|
| 1181 | stgpas = stepa;
|
---|
| 1182 | }
|
---|
| 1183 | else { // same mem-org for all, or same (this,a) OR same(this,res)
|
---|
| 1184 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
| 1185 | GetOpeParams(res, smo, ax, axr, step, stepr, gpas, naxa);
|
---|
| 1186 | stgpas = step;
|
---|
| 1187 | }
|
---|
| 1188 | // DBG cout << "DBG-A-DIVELT naxa=" << naxa << " gpas= " << gpas
|
---|
| 1189 | // << " step=" << step << " stepa=" << stepa << " stepr=" << stepr
|
---|
| 1190 | // << " ax= " << ax << " axa= " << axa << " axr= " << axr << endl;
|
---|
[813] | 1191 | for(j=0; j<naxa; j++) {
|
---|
| 1192 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 1193 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[2575] | 1194 | per = res.DataBlock().Begin()+res.Offset(axr,j);
|
---|
[1072] | 1195 | if(divzero) {
|
---|
[2575] | 1196 | if (!fginv)
|
---|
| 1197 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr)
|
---|
| 1198 | if (*pea==(T)0) *per = (T)0; else *per = *pe / *pea ;
|
---|
| 1199 | else
|
---|
| 1200 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr)
|
---|
| 1201 | if (*pe==(T)0) *per = (T)0; else *per = *pea / *pe ;
|
---|
[1072] | 1202 | }
|
---|
[2575] | 1203 | else {
|
---|
| 1204 | if (!fginv)
|
---|
| 1205 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr)
|
---|
| 1206 | *per = *pe / *pea ;
|
---|
| 1207 | else
|
---|
| 1208 | for(k=0; k<gpas; k+=stgpas, pe+=step, pea+=stepa, per+=stepr)
|
---|
| 1209 | *per = *pea / *pe ;
|
---|
| 1210 | }
|
---|
[785] | 1211 | }
|
---|
| 1212 | }
|
---|
[2575] | 1213 | return(res);
|
---|
[772] | 1214 | }
|
---|
| 1215 |
|
---|
[2575] | 1216 |
|
---|
[894] | 1217 | //! Copy elements of \b a
|
---|
[804] | 1218 | template <class T>
|
---|
| 1219 | TArray<T>& TArray<T>::CopyElt(const TArray<T>& a)
|
---|
| 1220 | {
|
---|
| 1221 | if (NbDimensions() < 1)
|
---|
| 1222 | throw RangeCheckError("TArray<T>::CopyElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 1223 | bool smo;
|
---|
| 1224 | if (!CompareSizes(a, smo))
|
---|
[1050] | 1225 | throw(SzMismatchError("TArray<T>::CopyElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[772] | 1226 |
|
---|
[804] | 1227 | T * pe;
|
---|
| 1228 | const T * pea;
|
---|
[2575] | 1229 | sa_size_t j,k;
|
---|
[1099] | 1230 | if (smo && (AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
[2587] | 1231 | if (IsPacked() && a.IsPacked()) memcpy(Data(), a.Data(), totsize_*sizeof(T)); // Packed arrays
|
---|
| 1232 | else {
|
---|
| 1233 | sa_size_t step = AvgStep();
|
---|
| 1234 | sa_size_t stepa = a.AvgStep();
|
---|
| 1235 | sa_size_t maxx = totsize_*step;
|
---|
| 1236 | pe = Data();
|
---|
| 1237 | pea = a.Data();
|
---|
| 1238 | for(k=0; k<maxx; k+=step, pe+=step, pea+=stepa ) *pe = *pea ;
|
---|
| 1239 | }
|
---|
[804] | 1240 | }
|
---|
| 1241 | else { // Non regular data spacing ...
|
---|
[1156] | 1242 | int_4 ax,axa;
|
---|
| 1243 | sa_size_t step, stepa;
|
---|
| 1244 | sa_size_t gpas, naxa;
|
---|
[1099] | 1245 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[813] | 1246 | for(j=0; j<naxa; j++) {
|
---|
| 1247 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 1248 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
[2575] | 1249 | for(k=0; k<gpas; k+=step, pe+=step, pea+=stepa) *pe = *pea;
|
---|
[804] | 1250 | }
|
---|
| 1251 | }
|
---|
| 1252 | return(*this);
|
---|
| 1253 | }
|
---|
| 1254 |
|
---|
[1081] | 1255 | //! Converts and Copy elements of \b a
|
---|
| 1256 | template <class T>
|
---|
| 1257 | TArray<T>& TArray<T>::ConvertAndCopyElt(const BaseArray& a)
|
---|
| 1258 | {
|
---|
| 1259 | if (NbDimensions() < 1)
|
---|
| 1260 | throw RangeCheckError("TArray<T>::ConvertAndCopyElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
[1099] | 1261 | bool smo;
|
---|
| 1262 | if (!CompareSizes(a, smo))
|
---|
[1081] | 1263 | throw(SzMismatchError("TArray<T>::ConvertAndCopyElt(const TArray<T>&) SizeMismatch")) ;
|
---|
[804] | 1264 |
|
---|
[1081] | 1265 | T * pe;
|
---|
[1156] | 1266 | sa_size_t j,k,ka;
|
---|
| 1267 | sa_size_t offa;
|
---|
[1081] | 1268 | // Non regular data spacing ...
|
---|
[1156] | 1269 | int_4 ax,axa;
|
---|
| 1270 | sa_size_t step, stepa;
|
---|
| 1271 | sa_size_t gpas, naxa;
|
---|
[1099] | 1272 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
[1081] | 1273 | for(j=0; j<naxa; j++) {
|
---|
| 1274 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
[1099] | 1275 | offa = a.Offset(axa,j);
|
---|
[2147] | 1276 | /*
|
---|
| 1277 | Appel explicite de l'operateur de conversion
|
---|
| 1278 | suite a la suggestion de M. Reinecke, Reza 31/7/2002
|
---|
[1085] | 1279 | #if !defined(__GNUG__)
|
---|
| 1280 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = (T)a.ValueAtPosition(offa+ka);
|
---|
| 1281 | #else
|
---|
| 1282 | // g++ (up to 2.95.1) se melange les pinceaux s'il y a le cast (T) pour l'instanciation des complexes
|
---|
[1081] | 1283 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = a.ValueAtPosition(offa+ka);
|
---|
[1085] | 1284 | #endif
|
---|
[2147] | 1285 | --- Appel explicite de l'operateur de conversion sur l'objet MuTyV
|
---|
| 1286 | */
|
---|
[2888] | 1287 | /* ----- Janvier 2006 ------
|
---|
| 1288 | Un bug important etait semble-t-il present depuis longtemps
|
---|
| 1289 | On appelait a.ValueAtPosition(ip) qui renvoie l'element ip en tenant compte
|
---|
| 1290 | de la structure du tableau , alors qu'on veut acceder l'element ip du datablock
|
---|
| 1291 | Methode ValueAtPositionDB(ip) ajoute et utilisee a la place de ValueAtPosition(ip)
|
---|
| 1292 | */
|
---|
| 1293 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa)
|
---|
| 1294 | a.ValueAtPositionDB(offa+ka).Convert(pe[k]);
|
---|
[2884] | 1295 | //REMPLACE Suite pb compil gcc4 pe[k] = a.ValueAtPosition(offa+ka).operator T();
|
---|
[1081] | 1296 | }
|
---|
| 1297 | return(*this);
|
---|
| 1298 | }
|
---|
| 1299 |
|
---|
[2575] | 1300 | //! Return the the scalar product of the two arrays (Sum_k[(*this)(k)*a(k)])
|
---|
| 1301 | template <class T>
|
---|
| 1302 | T TArray<T>::ScalarProduct(const TArray<T>& a) const
|
---|
| 1303 | {
|
---|
| 1304 | if (NbDimensions() < 1)
|
---|
| 1305 | throw RangeCheckError("TArray<T>::ScalarProduct(...) - Not allocated source array ");
|
---|
| 1306 | bool smo;
|
---|
| 1307 | if (!CompareSizes(a, smo))
|
---|
| 1308 | throw(SzMismatchError("TArray<T>::ScalarProduct(...) SizeMismatch(this,a) ")) ;
|
---|
[1081] | 1309 |
|
---|
[2575] | 1310 | T res = (T)(0);
|
---|
| 1311 | const T * pe;
|
---|
| 1312 | const T * pea;
|
---|
| 1313 | sa_size_t j,k;
|
---|
| 1314 | if (smo && (IsPacked() > 0) && (a.IsPacked() > 0)) { // regularly spaced elements
|
---|
| 1315 | sa_size_t maxx = totsize_;
|
---|
| 1316 | pe = Data();
|
---|
| 1317 | pea = a.Data();
|
---|
[2587] | 1318 | for(k=0; k<maxx; k++) res += *pe++ * *pea++;
|
---|
[2575] | 1319 | }
|
---|
| 1320 | else { // Non regular data spacing ...
|
---|
| 1321 | int_4 ax,axa;
|
---|
| 1322 | sa_size_t step, stepa;
|
---|
| 1323 | sa_size_t gpas, naxa;
|
---|
| 1324 | GetOpeParams(a, smo, ax, axa, step, stepa, gpas, naxa);
|
---|
| 1325 | for(j=0; j<naxa; j++) {
|
---|
| 1326 | pe = mNDBlock.Begin()+Offset(ax,j);
|
---|
| 1327 | pea = a.DataBlock().Begin()+a.Offset(axa,j);
|
---|
| 1328 | for(k=0; k<gpas; k+=step, pe+=step, pea+=stepa) res += (*pe)*(*pea);
|
---|
| 1329 | }
|
---|
| 1330 | }
|
---|
| 1331 | return(res);
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 |
|
---|
[804] | 1335 | // Somme et produit des elements
|
---|
[2575] | 1336 | //! Returns the sum of all array elements
|
---|
[804] | 1337 | template <class T>
|
---|
| 1338 | T TArray<T>::Sum() const
|
---|
| 1339 | {
|
---|
| 1340 | if (NbDimensions() < 1)
|
---|
| 1341 | throw RangeCheckError("TArray<T>::Sum() - Not Allocated Array ! ");
|
---|
| 1342 | T ret=0;
|
---|
| 1343 | const T * pe;
|
---|
[1156] | 1344 | sa_size_t j,k;
|
---|
[804] | 1345 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 1346 | sa_size_t step = AvgStep();
|
---|
| 1347 | sa_size_t maxx = totsize_*step;
|
---|
[804] | 1348 | pe = Data();
|
---|
| 1349 | for(k=0; k<maxx; k+=step ) ret += pe[k];
|
---|
| 1350 | }
|
---|
| 1351 | else { // Non regular data spacing ...
|
---|
[1156] | 1352 | int_4 ka = MaxSizeKA();
|
---|
| 1353 | sa_size_t step = Step(ka);
|
---|
| 1354 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1355 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 1356 | for(j=0; j<naxa; j++) {
|
---|
| 1357 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[804] | 1358 | for(k=0; k<gpas; k+=step) ret += pe[k] ;
|
---|
| 1359 | }
|
---|
| 1360 | }
|
---|
| 1361 | return ret;
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
[2575] | 1364 | //! Return the product of all elements
|
---|
[804] | 1365 | template <class T>
|
---|
| 1366 | T TArray<T>::Product() const
|
---|
| 1367 | {
|
---|
| 1368 | if (NbDimensions() < 1)
|
---|
| 1369 | throw RangeCheckError("TArray<T>::Product() - Not Allocated Array ! ");
|
---|
[1113] | 1370 | T ret=(T)1;
|
---|
[804] | 1371 | const T * pe;
|
---|
[1156] | 1372 | sa_size_t j,k;
|
---|
[804] | 1373 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 1374 | sa_size_t step = AvgStep();
|
---|
| 1375 | sa_size_t maxx = totsize_*step;
|
---|
[804] | 1376 | pe = Data();
|
---|
| 1377 | for(k=0; k<maxx; k+=step ) ret *= pe[k];
|
---|
| 1378 | }
|
---|
| 1379 | else { // Non regular data spacing ...
|
---|
[1156] | 1380 | int_4 ka = MaxSizeKA();
|
---|
| 1381 | sa_size_t step = Step(ka);
|
---|
| 1382 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1383 | sa_size_t naxa = Size()/Size(ka);
|
---|
[813] | 1384 | for(j=0; j<naxa; j++) {
|
---|
| 1385 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
[804] | 1386 | for(k=0; k<gpas; k+=step) ret *= pe[k] ;
|
---|
| 1387 | }
|
---|
| 1388 | }
|
---|
| 1389 | return ret;
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
[2575] | 1392 | //! Returns the sum of all array elements squared (Sum_k((*this)(k)*(*this)(k)).
|
---|
[1113] | 1393 | template <class T>
|
---|
[3332] | 1394 | T TArray<T>::SumSq() const
|
---|
[1113] | 1395 | {
|
---|
| 1396 | if (NbDimensions() < 1)
|
---|
[3332] | 1397 | throw RangeCheckError("TArray<T>::SumSq() - Not Allocated Array ! ");
|
---|
[1113] | 1398 | T ret=0;
|
---|
| 1399 | const T * pe;
|
---|
[1156] | 1400 | sa_size_t j,k;
|
---|
[1113] | 1401 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
[1156] | 1402 | sa_size_t step = AvgStep();
|
---|
| 1403 | sa_size_t maxx = totsize_*step;
|
---|
[1113] | 1404 | pe = Data();
|
---|
| 1405 | for(k=0; k<maxx; k+=step ) ret += pe[k]*pe[k];
|
---|
| 1406 | }
|
---|
| 1407 | else { // Non regular data spacing ...
|
---|
[1156] | 1408 | int_4 ka = MaxSizeKA();
|
---|
| 1409 | sa_size_t step = Step(ka);
|
---|
| 1410 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1411 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1113] | 1412 | for(j=0; j<naxa; j++) {
|
---|
| 1413 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 1414 | for(k=0; k<gpas; k+=step) ret += pe[k]*pe[k] ;
|
---|
| 1415 | }
|
---|
| 1416 | }
|
---|
| 1417 | return ret;
|
---|
| 1418 | }
|
---|
[804] | 1419 |
|
---|
[3332] | 1420 | /*!
|
---|
| 1421 | \brief Returns the array norm squared, defined as Sum_k [ el(k)* el(k) ]
|
---|
| 1422 | For arrays with integer or real data, this method calls SumSq(), which computes
|
---|
| 1423 | the sum of array elements squared. For complex arrays, it computes and returns
|
---|
| 1424 | the sum of array elements module squared (= Sum_k [el(k)*conj(el(k))]
|
---|
| 1425 | */
|
---|
| 1426 | template <class T>
|
---|
| 1427 | T TArray<T>::Norm2() const
|
---|
| 1428 | {
|
---|
| 1429 | return SumSq();
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
| 1432 |
|
---|
| 1433 | // Fonction auxiliaire pour specialisation de la methode Norm2() pour tableaux complexes
|
---|
| 1434 | template <class T>
|
---|
| 1435 | complex<T> _ComputeComplexNorm_Private_(TArray< complex<T> > const & ca)
|
---|
| 1436 | {
|
---|
| 1437 | if (ca.NbDimensions() < 1)
|
---|
| 1438 | throw RangeCheckError("TArray< complex<T> >::Norm2() - Not Allocated Array ! ");
|
---|
| 1439 | complex<T> ret= complex<T>(0., 0.);
|
---|
| 1440 | const complex<T> * pe;
|
---|
| 1441 | sa_size_t j,k;
|
---|
| 1442 | if (ca.AvgStep() > 0) { // regularly spaced elements
|
---|
| 1443 | sa_size_t step = ca.AvgStep();
|
---|
| 1444 | sa_size_t maxx = ca.Size()*step;
|
---|
| 1445 | pe = ca.Data();
|
---|
| 1446 | for(k=0; k<maxx; k+=step ) ret += pe[k]*conj(pe[k]);
|
---|
| 1447 | }
|
---|
| 1448 | else { // Non regular data spacing ...
|
---|
| 1449 | int_4 ka = ca.MaxSizeKA();
|
---|
| 1450 | sa_size_t step = ca.Step(ka);
|
---|
| 1451 | sa_size_t gpas = ca.Size(ka)*step;
|
---|
| 1452 | sa_size_t naxa = ca.Size()/ca.Size(ka);
|
---|
| 1453 | for(j=0; j<naxa; j++) {
|
---|
| 1454 | pe = ca.DataBlock().Begin()+ca.Offset(ka,j);
|
---|
| 1455 | for(k=0; k<gpas; k+=step) ret += pe[k]*conj(pe[k]) ;
|
---|
| 1456 | }
|
---|
| 1457 | }
|
---|
| 1458 | return ret;
|
---|
| 1459 |
|
---|
| 1460 | }
|
---|
| 1461 |
|
---|
| 1462 | // --- Specialisation de la methode Norm2() pour tableaux complexes ---
|
---|
| 1463 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 1464 | complex<r_4> TArray< complex<r_4> >::Norm2() const
|
---|
| 1465 | {
|
---|
| 1466 | return _ComputeComplexNorm_Private_(*this);
|
---|
| 1467 | }
|
---|
| 1468 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 1469 | complex<r_8> TArray< complex<r_8> >::Norm2() const
|
---|
| 1470 | {
|
---|
| 1471 | return _ComputeComplexNorm_Private_(*this);
|
---|
| 1472 | }
|
---|
| 1473 | //-------------------
|
---|
| 1474 |
|
---|
[1113] | 1475 | //! Return the minimum and the maximum values of the array elements
|
---|
| 1476 | /*!
|
---|
| 1477 | This method generates an exception (\c MathExc) if called for complex arrays
|
---|
| 1478 | */
|
---|
[2338] | 1479 |
|
---|
[1113] | 1480 | template <class T>
|
---|
| 1481 | void TArray<T>::MinMax(T& min, T& max) const
|
---|
| 1482 | {
|
---|
| 1483 | const T * pe;
|
---|
[1156] | 1484 | sa_size_t j,k;
|
---|
| 1485 | int_4 ka = MaxSizeKA();
|
---|
| 1486 | sa_size_t step = Step(ka);
|
---|
| 1487 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1488 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1113] | 1489 | min = (*this)[0];
|
---|
| 1490 | max = (*this)[0];
|
---|
| 1491 | for(j=0; j<naxa; j++) {
|
---|
| 1492 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 1493 | for(k=0; k<gpas; k+=step) {
|
---|
| 1494 | if (pe[k]<min) min = pe[k];
|
---|
| 1495 | else if (pe[k]>max) max = pe[k];
|
---|
| 1496 | }
|
---|
| 1497 | }
|
---|
| 1498 | return;
|
---|
| 1499 | }
|
---|
[804] | 1500 |
|
---|
[2338] | 1501 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[1113] | 1502 | void TArray< complex<r_4> >::MinMax(complex<r_4>& min, complex<r_4>& max) const
|
---|
| 1503 | {
|
---|
| 1504 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1505 | }
|
---|
[2338] | 1506 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[1113] | 1507 | void TArray< complex<r_8> >::MinMax(complex<r_8>& min, complex<r_8>& max) const
|
---|
| 1508 | {
|
---|
| 1509 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
[2338] | 1512 |
|
---|
[772] | 1513 | // ----------------------------------------------------
|
---|
| 1514 | // Impression, etc ...
|
---|
| 1515 | // ----------------------------------------------------
|
---|
| 1516 |
|
---|
[894] | 1517 | //! Return a string that contain the type \b T of the array
|
---|
[772] | 1518 | template <class T>
|
---|
[813] | 1519 | string TArray<T>::InfoString() const
|
---|
[772] | 1520 | {
|
---|
[813] | 1521 | string rs = "TArray<" ;
|
---|
| 1522 | rs += typeid(T).name();
|
---|
| 1523 | rs += "> ";
|
---|
[787] | 1524 | return(rs);
|
---|
[772] | 1525 | }
|
---|
| 1526 |
|
---|
[894] | 1527 | //! Print array
|
---|
| 1528 | /*!
|
---|
| 1529 | \param os : output stream
|
---|
| 1530 | \param maxprt : maximum numer of print
|
---|
| 1531 | \param si : if true, display attached DvList
|
---|
[1550] | 1532 | \param ascd : if true, suppresses the display of line numbers,
|
---|
[2788] | 1533 | suitable for ascii dump format.
|
---|
[894] | 1534 | \sa SetMaxPrint
|
---|
[1550] | 1535 | \sa WriteASCII
|
---|
[894] | 1536 | */
|
---|
[772] | 1537 | template <class T>
|
---|
[1550] | 1538 | void TArray<T>::Print(ostream& os, sa_size_t maxprt, bool si, bool ascd) const
|
---|
[772] | 1539 | {
|
---|
| 1540 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
[1156] | 1541 | sa_size_t npr = 0;
|
---|
[2752] | 1542 | // keep stream's io flags
|
---|
[2756] | 1543 | // ios_base::fmtflags ioflg = os.flags(); compile pas sur OSF-cxx
|
---|
| 1544 | // os << right ; compile pas sur OSF-cxx
|
---|
[2752] | 1545 |
|
---|
[772] | 1546 | Show(os, si);
|
---|
[850] | 1547 | if (ndim_ < 1) return;
|
---|
[2752] | 1548 |
|
---|
| 1549 | // Calcul de la largeur d'impression pour chaque element
|
---|
| 1550 | int fprtw = os.precision()+7;
|
---|
| 1551 | int prtw = 5;
|
---|
| 1552 |
|
---|
| 1553 | if ( (typeid(T) == typeid( int_4 )) || (typeid(T) == typeid( uint_4 )) ) prtw = 8;
|
---|
| 1554 | else if ( (typeid(T) == typeid( int_8 )) || (typeid(T) == typeid( uint_8 )) ) prtw = 11;
|
---|
| 1555 | else if ( typeid(T) == typeid( r_4 ) ) prtw = fprtw;
|
---|
| 1556 | else if ( typeid(T) == typeid( r_8 ) ) prtw = fprtw;
|
---|
| 1557 | else if ( typeid(T) == typeid(complex<r_4>) ) prtw = fprtw;
|
---|
| 1558 | else if ( typeid(T) == typeid(complex<r_8>) ) prtw = fprtw;
|
---|
| 1559 |
|
---|
| 1560 |
|
---|
[1156] | 1561 | sa_size_t k0,k1,k2,k3,k4;
|
---|
[772] | 1562 | for(k4=0; k4<size_[4]; k4++) {
|
---|
[2788] | 1563 | if ((size_[4] > 1) && !ascd)
|
---|
| 1564 | os << "\n ----- Dimension 5 (U) K4= " << k4 << endl;
|
---|
[772] | 1565 | for(k3=0; k3<size_[3]; k3++) {
|
---|
[2788] | 1566 | if ((size_[3] > 1) && !ascd)
|
---|
| 1567 | os << "\n ----- Dimension 4 (T) K3= " << k3 << endl;
|
---|
[772] | 1568 | for(k2=0; k2<size_[2]; k2++) {
|
---|
[2788] | 1569 | if ((size_[2] > 1) && !ascd)
|
---|
| 1570 | os << "\n ----- Dimension 3 (Z) K2= " << k2 << endl;
|
---|
[772] | 1571 | for(k1=0; k1<size_[1]; k1++) {
|
---|
[2788] | 1572 | if ( (size_[1] > 1) && (size_[0] > 10) && !ascd)
|
---|
| 1573 | os << "----- Dimension 2 (Y) K1= " << k1 << endl;
|
---|
[772] | 1574 | for(k0=0; k0<size_[0]; k0++) {
|
---|
[1550] | 1575 | if(k0 > 0) os << " ";
|
---|
[2752] | 1576 | os << setw(prtw) << Elem(k0, k1, k2, k3, k4); npr++;
|
---|
[2788] | 1577 | if (npr >= (sa_size_t) maxprt) {
|
---|
[772] | 1578 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 1579 | }
|
---|
| 1580 | }
|
---|
| 1581 | os << endl;
|
---|
| 1582 | }
|
---|
| 1583 | }
|
---|
| 1584 | }
|
---|
| 1585 | }
|
---|
[813] | 1586 | os << endl;
|
---|
[2756] | 1587 | //compile pas sur OSF-cxx os.flags(ioflg); // reset stream io flags
|
---|
[772] | 1588 | }
|
---|
| 1589 |
|
---|
[1517] | 1590 | //! Fill the array, decoding the ASCII input stream
|
---|
| 1591 | /*!
|
---|
| 1592 | \param is : input stream (ASCII)
|
---|
[1558] | 1593 | \param nr : Number of non empty (or comment) lines in stream (return value)
|
---|
| 1594 | \param nc : Number of columns (= ntot/nlines) (return value)
|
---|
[2286] | 1595 | \param clm : Lines starting with clm character are treated as comment lines
|
---|
| 1596 | \param sep : word separator in lines
|
---|
[1558] | 1597 | \return Number of decoded elements
|
---|
[2286] | 1598 | */
|
---|
[1517] | 1599 | template <class T>
|
---|
[2286] | 1600 | sa_size_t TArray<T>::ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
|
---|
| 1601 | char clm, const char* sep)
|
---|
[1517] | 1602 | {
|
---|
[1550] | 1603 | EnumeratedSequence es;
|
---|
[2286] | 1604 | sa_size_t n = es.FillFromFile(is, nr, nc, clm, sep);
|
---|
[1558] | 1605 | if ( (n < 1) || (nr < 1) || (nc < 1) ) return(n);
|
---|
| 1606 | if (!IsAllocated()) {
|
---|
| 1607 | sa_size_t sz[2];
|
---|
| 1608 | if (arrtype_ == 2) { // C'est un vecteur
|
---|
| 1609 | sz[0] = sz[1] = 1;
|
---|
| 1610 | sz[veceli_] = n;
|
---|
| 1611 | }
|
---|
| 1612 | else {
|
---|
| 1613 | sz[RowsKA()] = nr;
|
---|
| 1614 | sz[ColsKA()] = nc;
|
---|
| 1615 | }
|
---|
| 1616 | ReSize(2, sz);
|
---|
| 1617 | }
|
---|
| 1618 | SetSeq(es);
|
---|
| 1619 | cout << "TArray<T>::ReadASCII()/Info: " << n << " elements read from stream "
|
---|
| 1620 | << " (Row,Col= " << nr << "," << nc << ")" << endl;
|
---|
| 1621 | return(n);
|
---|
[1517] | 1622 | }
|
---|
[772] | 1623 |
|
---|
[1517] | 1624 | //! Writes the array content to the output stream, (in ASCII)
|
---|
| 1625 | /*!
|
---|
| 1626 | \param os : output stream (ASCII)
|
---|
[1558] | 1627 | \sa Print
|
---|
[1517] | 1628 | */
|
---|
| 1629 | template <class T>
|
---|
| 1630 | void TArray<T>::WriteASCII(ostream& os) const
|
---|
| 1631 | {
|
---|
[1550] | 1632 | Print(os, Size(), false, true);
|
---|
[1517] | 1633 | }
|
---|
[772] | 1634 |
|
---|
[1517] | 1635 |
|
---|
| 1636 |
|
---|
[772] | 1637 | ///////////////////////////////////////////////////////////////
|
---|
| 1638 | ///////////////////////////////////////////////////////////////
|
---|
| 1639 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
[804] | 1640 | /*
|
---|
[772] | 1641 | #pragma define_template TArray<uint_1>
|
---|
[804] | 1642 | */
|
---|
[772] | 1643 | #pragma define_template TArray<uint_2>
|
---|
[2927] | 1644 | #pragma define_template TArray<uint_4>
|
---|
[1543] | 1645 | #pragma define_template TArray<uint_8>
|
---|
[2927] | 1646 | #pragma define_template TArray<int_2>
|
---|
[772] | 1647 | #pragma define_template TArray<int_4>
|
---|
| 1648 | #pragma define_template TArray<int_8>
|
---|
| 1649 | #pragma define_template TArray<r_4>
|
---|
| 1650 | #pragma define_template TArray<r_8>
|
---|
| 1651 | #pragma define_template TArray< complex<r_4> >
|
---|
| 1652 | #pragma define_template TArray< complex<r_8> >
|
---|
| 1653 | #endif
|
---|
| 1654 |
|
---|
| 1655 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[804] | 1656 | /*
|
---|
| 1657 | template class TArray<uint_1>;
|
---|
| 1658 | */
|
---|
[772] | 1659 | template class TArray<uint_2>;
|
---|
[2927] | 1660 | template class TArray<uint_4>;
|
---|
[1543] | 1661 | template class TArray<uint_8>;
|
---|
[2927] | 1662 | template class TArray<int_2>;
|
---|
[772] | 1663 | template class TArray<int_4>;
|
---|
| 1664 | template class TArray<int_8>;
|
---|
| 1665 | template class TArray<r_4>;
|
---|
| 1666 | template class TArray<r_8>;
|
---|
| 1667 | template class TArray< complex<r_4> >;
|
---|
| 1668 | template class TArray< complex<r_8> >;
|
---|
| 1669 | #endif
|
---|
| 1670 |
|
---|
[3234] | 1671 | } // FIN namespace SOPHYA
|
---|