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