[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) );
|
---|
[1636] | 162 | if (mNDBlock.Size() < ComputeTotalSize(ndim, siz, step, offset)) {
|
---|
| 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 | ra.SetTemp(true);
|
---|
| 473 | return(ra);
|
---|
| 474 | }
|
---|
| 475 | else {
|
---|
| 476 | TArray<T> ra(ndim_, size_, 1);
|
---|
| 477 | ra.CopyElt(*this);
|
---|
| 478 | ra.SetTemp(true);
|
---|
| 479 | return(ra);
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[785] | 483 | // SubArrays
|
---|
[804] | 484 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 485 | //! Extract a sub-array
|
---|
| 486 | /*!
|
---|
| 487 | \param rx,ry,rz,rt,ru : range of extraction along dimensions
|
---|
[2917] | 488 | \param compact : if \b compact == true, compact trailing dimensions (suppressed if =1)
|
---|
| 489 | (See CompactTrailingDimensions() )
|
---|
[2915] | 490 | \sa SOPHYA::Range
|
---|
[894] | 491 | */
|
---|
[785] | 492 | template <class T>
|
---|
[2917] | 493 | TArray<T> TArray<T>::SubArray(Range rx, Range ry, Range rz, Range rt, Range ru, bool compact) const
|
---|
[785] | 494 | {
|
---|
[804] | 495 | if (NbDimensions() < 1)
|
---|
| 496 | throw RangeCheckError("TArray<T>::operator () (Range, ...) - Not Allocated Array ! ");
|
---|
[1156] | 497 | int_4 ndim = 0;
|
---|
[2915] | 498 |
|
---|
| 499 | // Updating Range objects using actual array size
|
---|
| 500 | rx.Update(SizeX());
|
---|
| 501 | ry.Update(SizeY());
|
---|
| 502 | rz.Update(SizeZ());
|
---|
| 503 | if (NbDimensions() > 3) rt.Update(Size(3));
|
---|
| 504 | else rt.Update(0);
|
---|
| 505 | if (NbDimensions() > 4) ru.Update(Size(4));
|
---|
| 506 | else ru.Update(0);
|
---|
| 507 |
|
---|
[1156] | 508 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
| 509 | sa_size_t step[BASEARRAY_MAXNDIMS];
|
---|
| 510 | sa_size_t pos[BASEARRAY_MAXNDIMS];
|
---|
[785] | 511 | size[0] = rx.Size();
|
---|
| 512 | size[1] = ry.Size();
|
---|
| 513 | size[2] = rz.Size();
|
---|
| 514 | size[3] = rt.Size();
|
---|
| 515 | size[4] = ru.Size();
|
---|
| 516 |
|
---|
| 517 | step[0] = rx.Step();
|
---|
| 518 | step[1] = ry.Step();
|
---|
| 519 | step[2] = rz.Step();
|
---|
| 520 | step[3] = rt.Step();
|
---|
| 521 | step[4] = ru.Step();
|
---|
| 522 |
|
---|
| 523 | pos[0] = rx.Start();
|
---|
| 524 | pos[1] = ry.Start();
|
---|
| 525 | pos[2] = rz.Start();
|
---|
| 526 | pos[3] = rt.Start();
|
---|
| 527 | pos[4] = ru.Start();
|
---|
| 528 |
|
---|
| 529 | ndim = ndim_;
|
---|
| 530 | TArray<T> ra;
|
---|
[804] | 531 | UpdateSubArraySizes(ra, ndim, size, pos, step);
|
---|
[787] | 532 | ra.DataBlock().Share(this->DataBlock());
|
---|
[2917] | 533 | if (compact) ra.CompactTrailingDim();
|
---|
[785] | 534 | ra.SetTemp(true);
|
---|
| 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>
|
---|
| 1397 | T TArray<T>::SumX2() const
|
---|
| 1398 | {
|
---|
| 1399 | if (NbDimensions() < 1)
|
---|
| 1400 | throw RangeCheckError("TArray<T>::SumX2() - Not Allocated Array ! ");
|
---|
| 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 |
|
---|
[1113] | 1423 | //! Return the minimum and the maximum values of the array elements
|
---|
| 1424 | /*!
|
---|
| 1425 | This method generates an exception (\c MathExc) if called for complex arrays
|
---|
| 1426 | */
|
---|
[2338] | 1427 |
|
---|
[1113] | 1428 | template <class T>
|
---|
| 1429 | void TArray<T>::MinMax(T& min, T& max) const
|
---|
| 1430 | {
|
---|
| 1431 | const T * pe;
|
---|
[1156] | 1432 | sa_size_t j,k;
|
---|
| 1433 | int_4 ka = MaxSizeKA();
|
---|
| 1434 | sa_size_t step = Step(ka);
|
---|
| 1435 | sa_size_t gpas = Size(ka)*step;
|
---|
| 1436 | sa_size_t naxa = Size()/Size(ka);
|
---|
[1113] | 1437 | min = (*this)[0];
|
---|
| 1438 | max = (*this)[0];
|
---|
| 1439 | for(j=0; j<naxa; j++) {
|
---|
| 1440 | pe = mNDBlock.Begin()+Offset(ka,j);
|
---|
| 1441 | for(k=0; k<gpas; k+=step) {
|
---|
| 1442 | if (pe[k]<min) min = pe[k];
|
---|
| 1443 | else if (pe[k]>max) max = pe[k];
|
---|
| 1444 | }
|
---|
| 1445 | }
|
---|
| 1446 | return;
|
---|
| 1447 | }
|
---|
[804] | 1448 |
|
---|
[2338] | 1449 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[1113] | 1450 | void TArray< complex<r_4> >::MinMax(complex<r_4>& min, complex<r_4>& max) const
|
---|
| 1451 | {
|
---|
| 1452 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1453 | }
|
---|
[2338] | 1454 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[1113] | 1455 | void TArray< complex<r_8> >::MinMax(complex<r_8>& min, complex<r_8>& max) const
|
---|
| 1456 | {
|
---|
| 1457 | throw MathExc("TArray< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 1458 | }
|
---|
| 1459 |
|
---|
[2338] | 1460 |
|
---|
[772] | 1461 | // ----------------------------------------------------
|
---|
| 1462 | // Impression, etc ...
|
---|
| 1463 | // ----------------------------------------------------
|
---|
| 1464 |
|
---|
[894] | 1465 | //! Return a string that contain the type \b T of the array
|
---|
[772] | 1466 | template <class T>
|
---|
[813] | 1467 | string TArray<T>::InfoString() const
|
---|
[772] | 1468 | {
|
---|
[813] | 1469 | string rs = "TArray<" ;
|
---|
| 1470 | rs += typeid(T).name();
|
---|
| 1471 | rs += "> ";
|
---|
[787] | 1472 | return(rs);
|
---|
[772] | 1473 | }
|
---|
| 1474 |
|
---|
[894] | 1475 | //! Print array
|
---|
| 1476 | /*!
|
---|
| 1477 | \param os : output stream
|
---|
| 1478 | \param maxprt : maximum numer of print
|
---|
| 1479 | \param si : if true, display attached DvList
|
---|
[1550] | 1480 | \param ascd : if true, suppresses the display of line numbers,
|
---|
[2788] | 1481 | suitable for ascii dump format.
|
---|
[894] | 1482 | \sa SetMaxPrint
|
---|
[1550] | 1483 | \sa WriteASCII
|
---|
[894] | 1484 | */
|
---|
[772] | 1485 | template <class T>
|
---|
[1550] | 1486 | void TArray<T>::Print(ostream& os, sa_size_t maxprt, bool si, bool ascd) const
|
---|
[772] | 1487 | {
|
---|
| 1488 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
[1156] | 1489 | sa_size_t npr = 0;
|
---|
[2752] | 1490 | // keep stream's io flags
|
---|
[2756] | 1491 | // ios_base::fmtflags ioflg = os.flags(); compile pas sur OSF-cxx
|
---|
| 1492 | // os << right ; compile pas sur OSF-cxx
|
---|
[2752] | 1493 |
|
---|
[772] | 1494 | Show(os, si);
|
---|
[850] | 1495 | if (ndim_ < 1) return;
|
---|
[2752] | 1496 |
|
---|
| 1497 | // Calcul de la largeur d'impression pour chaque element
|
---|
| 1498 | int fprtw = os.precision()+7;
|
---|
| 1499 | int prtw = 5;
|
---|
| 1500 |
|
---|
| 1501 | if ( (typeid(T) == typeid( int_4 )) || (typeid(T) == typeid( uint_4 )) ) prtw = 8;
|
---|
| 1502 | else if ( (typeid(T) == typeid( int_8 )) || (typeid(T) == typeid( uint_8 )) ) prtw = 11;
|
---|
| 1503 | else if ( typeid(T) == typeid( r_4 ) ) prtw = fprtw;
|
---|
| 1504 | else if ( typeid(T) == typeid( r_8 ) ) prtw = fprtw;
|
---|
| 1505 | else if ( typeid(T) == typeid(complex<r_4>) ) prtw = fprtw;
|
---|
| 1506 | else if ( typeid(T) == typeid(complex<r_8>) ) prtw = fprtw;
|
---|
| 1507 |
|
---|
| 1508 |
|
---|
[1156] | 1509 | sa_size_t k0,k1,k2,k3,k4;
|
---|
[772] | 1510 | for(k4=0; k4<size_[4]; k4++) {
|
---|
[2788] | 1511 | if ((size_[4] > 1) && !ascd)
|
---|
| 1512 | os << "\n ----- Dimension 5 (U) K4= " << k4 << endl;
|
---|
[772] | 1513 | for(k3=0; k3<size_[3]; k3++) {
|
---|
[2788] | 1514 | if ((size_[3] > 1) && !ascd)
|
---|
| 1515 | os << "\n ----- Dimension 4 (T) K3= " << k3 << endl;
|
---|
[772] | 1516 | for(k2=0; k2<size_[2]; k2++) {
|
---|
[2788] | 1517 | if ((size_[2] > 1) && !ascd)
|
---|
| 1518 | os << "\n ----- Dimension 3 (Z) K2= " << k2 << endl;
|
---|
[772] | 1519 | for(k1=0; k1<size_[1]; k1++) {
|
---|
[2788] | 1520 | if ( (size_[1] > 1) && (size_[0] > 10) && !ascd)
|
---|
| 1521 | os << "----- Dimension 2 (Y) K1= " << k1 << endl;
|
---|
[772] | 1522 | for(k0=0; k0<size_[0]; k0++) {
|
---|
[1550] | 1523 | if(k0 > 0) os << " ";
|
---|
[2752] | 1524 | os << setw(prtw) << Elem(k0, k1, k2, k3, k4); npr++;
|
---|
[2788] | 1525 | if (npr >= (sa_size_t) maxprt) {
|
---|
[772] | 1526 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 1527 | }
|
---|
| 1528 | }
|
---|
| 1529 | os << endl;
|
---|
| 1530 | }
|
---|
| 1531 | }
|
---|
| 1532 | }
|
---|
| 1533 | }
|
---|
[813] | 1534 | os << endl;
|
---|
[2756] | 1535 | //compile pas sur OSF-cxx os.flags(ioflg); // reset stream io flags
|
---|
[772] | 1536 | }
|
---|
| 1537 |
|
---|
[1517] | 1538 | //! Fill the array, decoding the ASCII input stream
|
---|
| 1539 | /*!
|
---|
| 1540 | \param is : input stream (ASCII)
|
---|
[1558] | 1541 | \param nr : Number of non empty (or comment) lines in stream (return value)
|
---|
| 1542 | \param nc : Number of columns (= ntot/nlines) (return value)
|
---|
[2286] | 1543 | \param clm : Lines starting with clm character are treated as comment lines
|
---|
| 1544 | \param sep : word separator in lines
|
---|
[1558] | 1545 | \return Number of decoded elements
|
---|
[2286] | 1546 | */
|
---|
[1517] | 1547 | template <class T>
|
---|
[2286] | 1548 | sa_size_t TArray<T>::ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
|
---|
| 1549 | char clm, const char* sep)
|
---|
[1517] | 1550 | {
|
---|
[1550] | 1551 | EnumeratedSequence es;
|
---|
[2286] | 1552 | sa_size_t n = es.FillFromFile(is, nr, nc, clm, sep);
|
---|
[1558] | 1553 | if ( (n < 1) || (nr < 1) || (nc < 1) ) return(n);
|
---|
| 1554 | if (!IsAllocated()) {
|
---|
| 1555 | sa_size_t sz[2];
|
---|
| 1556 | if (arrtype_ == 2) { // C'est un vecteur
|
---|
| 1557 | sz[0] = sz[1] = 1;
|
---|
| 1558 | sz[veceli_] = n;
|
---|
| 1559 | }
|
---|
| 1560 | else {
|
---|
| 1561 | sz[RowsKA()] = nr;
|
---|
| 1562 | sz[ColsKA()] = nc;
|
---|
| 1563 | }
|
---|
| 1564 | ReSize(2, sz);
|
---|
| 1565 | }
|
---|
| 1566 | SetSeq(es);
|
---|
| 1567 | cout << "TArray<T>::ReadASCII()/Info: " << n << " elements read from stream "
|
---|
| 1568 | << " (Row,Col= " << nr << "," << nc << ")" << endl;
|
---|
| 1569 | return(n);
|
---|
[1517] | 1570 | }
|
---|
[772] | 1571 |
|
---|
[1517] | 1572 | //! Writes the array content to the output stream, (in ASCII)
|
---|
| 1573 | /*!
|
---|
| 1574 | \param os : output stream (ASCII)
|
---|
[1558] | 1575 | \sa Print
|
---|
[1517] | 1576 | */
|
---|
| 1577 | template <class T>
|
---|
| 1578 | void TArray<T>::WriteASCII(ostream& os) const
|
---|
| 1579 | {
|
---|
[1550] | 1580 | Print(os, Size(), false, true);
|
---|
[1517] | 1581 | }
|
---|
[772] | 1582 |
|
---|
[1517] | 1583 |
|
---|
| 1584 |
|
---|
[772] | 1585 | ///////////////////////////////////////////////////////////////
|
---|
| 1586 | ///////////////////////////////////////////////////////////////
|
---|
| 1587 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
[804] | 1588 | /*
|
---|
[772] | 1589 | #pragma define_template TArray<uint_1>
|
---|
[804] | 1590 | */
|
---|
[772] | 1591 | #pragma define_template TArray<uint_2>
|
---|
[2927] | 1592 | #pragma define_template TArray<uint_4>
|
---|
[1543] | 1593 | #pragma define_template TArray<uint_8>
|
---|
[2927] | 1594 | #pragma define_template TArray<int_2>
|
---|
[772] | 1595 | #pragma define_template TArray<int_4>
|
---|
| 1596 | #pragma define_template TArray<int_8>
|
---|
| 1597 | #pragma define_template TArray<r_4>
|
---|
| 1598 | #pragma define_template TArray<r_8>
|
---|
| 1599 | #pragma define_template TArray< complex<r_4> >
|
---|
| 1600 | #pragma define_template TArray< complex<r_8> >
|
---|
| 1601 | #endif
|
---|
| 1602 |
|
---|
| 1603 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[804] | 1604 | /*
|
---|
| 1605 | template class TArray<uint_1>;
|
---|
| 1606 | */
|
---|
[772] | 1607 | template class TArray<uint_2>;
|
---|
[2927] | 1608 | template class TArray<uint_4>;
|
---|
[1543] | 1609 | template class TArray<uint_8>;
|
---|
[2927] | 1610 | template class TArray<int_2>;
|
---|
[772] | 1611 | template class TArray<int_4>;
|
---|
| 1612 | template class TArray<int_8>;
|
---|
| 1613 | template class TArray<r_4>;
|
---|
| 1614 | template class TArray<r_8>;
|
---|
| 1615 | template class TArray< complex<r_4> >;
|
---|
| 1616 | template class TArray< complex<r_8> >;
|
---|
| 1617 | #endif
|
---|
| 1618 |
|
---|
[3234] | 1619 | } // FIN namespace SOPHYA
|
---|