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