source: Sophya/trunk/SophyaLib/TArray/tarray.cc@ 1013

Last change on this file since 1013 was 976, checked in by ansari, 25 years ago

modifs doc cmv 27/4/00

File size: 25.0 KB
Line 
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>
7#include <math.h>
8#include "pexceptions.h"
9#include "tarray.h"
10
11/*!
12 \class SOPHYA::TArray
13 \ingroup TArray
14 Class for template arrays
15
16 This class implements arrays of dimensions up to
17 \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS"
18*/
19
20// -------------------------------------------------------
21// Methodes de la classe
22// -------------------------------------------------------
23
24////////////////////////// Les constructeurs / destructeurs
25
26//! Default constructor
27template <class T>
28TArray<T>::TArray()
29 : BaseArray() , mNDBlock()
30{
31}
32
33//! Constructor
34/*!
35 \param ndim : number of dimensions (less or equal to
36 \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS")
37 \param siz[ndim] : size along each dimension
38 \param step : step (same for all dimensions)
39 */
40template <class T>
41TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, uint_4 step)
42 : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1))
43{
44 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, uint_4)";
45 if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
46}
47
48//! Constructor
49/*!
50 \param nx,ny,nz,nt,nu : sizes along first, second, third, fourth and fifth dimension
51 */
52template <class T>
53TArray<T>::TArray(uint_4 nx, uint_4 ny, uint_4 nz, uint_4 nt, uint_4 nu)
54 : BaseArray() , mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)*((nt>0)?nt:1)*((nu>0)?nu:1))
55{
56 uint_4 size[BASEARRAY_MAXNDIMS];
57 size[0] = nx; size[1] = ny; size[2] = nz;
58 size[3] = nt; size[4] = nu;
59 int ndim = 1;
60 if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) && (size[4] > 0) ) ndim = 5;
61 else if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) ) ndim = 4;
62 else if ((size[1] > 0) && (size[2] > 0)) ndim = 3;
63 else if (size[1] > 0) ndim = 2;
64 else ndim = 1;
65 string exmsg = "TArray<T>::TArray(uint_4, uint_4, uint_4)";
66 if (!UpdateSizes(ndim, size, 1, 0, exmsg)) throw( ParmError(exmsg) );
67}
68
69//! Constructor
70/*!
71 \param ndim : number of dimensions
72 \param siz[ndim] : size along each dimension
73 \param db : datas are given by this NDataBlock
74 \param share : if true, data are shared, if false they are copied
75 \param step : step (same for all dimensions) in data block
76 \param offset : offset for first element in data block
77 */
78template <class T>
79TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, NDataBlock<T> & db, bool share, uint_4 step, uint_8 offset)
80 : BaseArray() , mNDBlock(db, share)
81{
82 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, NDataBlock<T> & ... )";
83 if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
84
85}
86
87//! Constructor
88/*!
89 \param ndim : number of dimensions
90 \param siz[ndim] : size along each dimension
91 \param values : datas are given by this pointer
92 \param share : if true, data are shared, if false they are copied
93 \param step : step (same for all dimensions) in data block
94 \param offset : offset for first element in data block
95 \param br : if not NULL, dats are bridge with other datas
96 \sa NDataBlock
97 */
98template <class T>
99TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, T* values, uint_4 step, uint_8 offset, Bridge* br)
100 : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br)
101{
102 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, T* ... )";
103 if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
104}
105
106//! Constructor by copy
107/*!
108 \warning datas are \b SHARED with \b a.
109 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
110 */
111template <class T>
112TArray<T>::TArray(const TArray<T>& a)
113 : BaseArray() , mNDBlock(a.mNDBlock)
114{
115 string exmsg = "TArray<T>::TArray(const TArray<T>&)";
116 if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
117 if (a.mInfo) mInfo = new DVList(*(a.mInfo));
118}
119
120//! Constructor by copy
121/*!
122 \param share : if true, data are shared, if false they are copied
123 */
124template <class T>
125TArray<T>::TArray(const TArray<T>& a, bool share)
126 : BaseArray() , mNDBlock(a.mNDBlock, share)
127{
128 string exmsg = "TArray<T>::TArray(const TArray<T>&, bool)";
129 if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
130 if (a.mInfo) mInfo = new DVList(*(a.mInfo));
131}
132
133//! Destructor
134template <class T>
135TArray<T>::~TArray()
136{
137}
138
139////////////////////////// Les methodes de copie/share
140
141//! Set array equal to \b a and return *this
142/*!
143 \warning Datas are copied (cloned) from \b a.
144 \sa NDataBlock::operator=(const NDataBlock<T>&)
145*/
146template <class T>
147TArray<T>& TArray<T>::Set(const TArray<T>& a)
148{
149 if (this == &a) return(*this);
150 if (a.NbDimensions() < 1)
151 throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! ");
152 if (NbDimensions() < 1) CloneOrShare(a);
153 else CopyElt(a);
154 return(*this);
155}
156
157//! Clone array \b a
158template <class T>
159void TArray<T>::Clone(const TArray<T>& a)
160{
161 string exmsg = "TArray<T>::Clone()";
162 if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
163 mNDBlock.Clone(a.mNDBlock);
164 if (mInfo) {delete mInfo; mInfo = NULL;}
165 if (a.mInfo) mInfo = new DVList(*(a.mInfo));
166}
167
168//! Clone if \b a is not temporary, share if temporary
169/*! \sa NDataBlock::CloneOrShare(const NDataBlock<T>&) */
170template <class T>
171void TArray<T>::CloneOrShare(const TArray<T>& a)
172{
173 string exmsg = "TArray<T>::CloneOrShare()";
174 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );
175 mNDBlock.CloneOrShare(a.mNDBlock);
176}
177
178//! Share data with a
179template <class T>
180void TArray<T>::Share(const TArray<T>& a)
181{
182 string exmsg = "TArray<T>::Share()";
183 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );
184 mNDBlock.Share(a.mNDBlock);
185}
186
187
188//! Resize array
189/*!
190 \param ndim : number of dimensions
191 \param siz[ndim] : size along each dimension
192 \param step : step (same for all dimensions)
193 */
194template <class T>
195void TArray<T>::ReSize(uint_4 ndim, uint_4 * siz, uint_4 step)
196{
197 string exmsg = "TArray<T>::ReSize()";
198 if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
199 mNDBlock.ReSize(totsize_);
200}
201
202//! Re-allocate space for array
203/*!
204 \param ndim : number of dimensions
205 \param siz[ndim] : size along each dimension
206 \param step : step (same for all dimensions)
207 \param force : if true re-allocation is forced, if not it occurs
208 only if the required space is greater than the old one.
209 */
210template <class T>
211void TArray<T>::Realloc(uint_4 ndim, uint_4 * siz, uint_4 step, bool force)
212{
213 string exmsg = "TArray<T>::Realloc()";
214 if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
215 mNDBlock.ReSize(totsize_);
216}
217
218
219//! Compact dimensions in one or more is equal to 1.
220template <class T>
221TArray<T>& TArray<T>::CompactAllDimensions()
222{
223 CompactAllDim();
224 return(*this);
225}
226
227//! Compact dimensions if the last one is equal to 1.
228template <class T>
229TArray<T>& TArray<T>::CompactTrailingDimensions()
230{
231 CompactTrailingDim();
232 return(*this);
233}
234
235inline double _SqrtRz_(double x) { return sqrt(x); } // Pb avec SGI-CC - $CHECK$ - Reza 04/2000
236
237//! Give value (in \b double) for element at position \b ip..
238template <class T>
239double TArray<T>::ValueAtPosition(uint_8 ip) const
240{
241#ifdef SO_BOUNDCHECKING
242 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
243#endif
244 return( (double)(*(mNDBlock.Begin()+Offset(ip))) );
245}
246
247//! Give value (in \b double) for element at position \b ip..
248/*!
249 For complex values, we return the module of the complex number
250*/
251double TArray< complex<r_4> >::ValueAtPosition(uint_8 ip) const
252{
253#ifdef SO_BOUNDCHECKING
254 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
255#endif
256 complex<r_4> c = *(mNDBlock.Begin()+Offset(ip));
257 double cr = (double)(c.real());
258 double ci = (double)(c.imag());
259 return( _SqrtRz_(cr*cr+ci*ci) );
260}
261
262//! Give value (in \b double) for element at position \b ip..
263/*!
264 For complex values, we return the module of the complex number
265*/
266double TArray< complex<r_8> >::ValueAtPosition(uint_8 ip) const
267{
268#ifdef SO_BOUNDCHECKING
269 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
270#endif
271 complex<r_8> c = *(mNDBlock.Begin()+Offset(ip));
272 double cr = (double)(c.real());
273 double ci = (double)(c.imag());
274 return( _SqrtRz_(cr*cr+ci*ci) );
275}
276
277//! Return array with elements packed
278/*!
279 \param force : if true, pack elements in a new array.
280 If false and array is already packed, return
281 an array that share data with the current one.
282 \return packed array
283 */
284template <class T>
285TArray<T> TArray<T>::PackElements(bool force) const
286{
287 if (NbDimensions() < 1)
288 throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
289 if ( !force && (AvgStep() == 1) ) {
290 TArray<T> ra;
291 ra.Share(*this);
292 ra.SetTemp(true);
293 return(ra);
294 }
295 else {
296 TArray<T> ra(ndim_, size_, 1);
297 ra.CopyElt(*this);
298 ra.SetTemp(true);
299 return(ra);
300 }
301}
302
303// SubArrays
304// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
305//! Extract a sub-array
306/*!
307 \param rx,ry,rz,rt,ru : range of extraction along dimensions
308 \sa Range
309 */
310template <class T>
311TArray<T> TArray<T>::SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const
312{
313 if (NbDimensions() < 1)
314 throw RangeCheckError("TArray<T>::operator () (Range, ...) - Not Allocated Array ! ");
315 uint_4 ndim = 0;
316 uint_4 size[BASEARRAY_MAXNDIMS];
317 uint_4 step[BASEARRAY_MAXNDIMS];
318 uint_4 pos[BASEARRAY_MAXNDIMS];
319 size[0] = rx.Size();
320 size[1] = ry.Size();
321 size[2] = rz.Size();
322 size[3] = rt.Size();
323 size[4] = ru.Size();
324
325 step[0] = rx.Step();
326 step[1] = ry.Step();
327 step[2] = rz.Step();
328 step[3] = rt.Step();
329 step[4] = ru.Step();
330
331 pos[0] = rx.Start();
332 pos[1] = ry.Start();
333 pos[2] = rz.Start();
334 pos[3] = rt.Start();
335 pos[4] = ru.Start();
336
337 ndim = ndim_;
338 TArray<T> ra;
339 UpdateSubArraySizes(ra, ndim, size, pos, step);
340 ra.DataBlock().Share(this->DataBlock());
341 ra.SetTemp(true);
342 return(ra);
343}
344
345// ...... Operation de calcul sur les tableaux ......
346// ------- Attention --------
347// Boucles normales prenant en compte les steps ....
348// Possibilite de // , vectorisation
349
350//! Fill TArray with Sequence \b seq
351/*!
352 \param seq : sequence to fill the array
353 \sa Sequence
354 */
355template <class T>
356TArray<T>& TArray<T>::SetSeq(Sequence seq)
357{
358 if (NbDimensions() < 1)
359 throw RangeCheckError("TArray<T>::SetSeq(Sequence ) - Not Allocated Array ! ");
360 T * pe;
361 uint_8 j,k;
362 if (AvgStep() > 0) { // regularly spaced elements
363 uint_8 step = AvgStep();
364 pe = Data();
365 for(k=0; k<totsize_; k++ ) pe[k*step] = (T) seq(k);
366 }
367 else { // Non regular data spacing ...
368 // uint_4 ka = MaxSizeKA();
369 uint_4 ka = 0;
370 uint_8 step = Step(ka);
371 uint_8 gpas = Size(ka);
372 uint_8 naxa = Size()/Size(ka);
373 for(j=0; j<naxa; j++) {
374 pe = mNDBlock.Begin()+Offset(ka,j);
375 for(k=0; k<gpas; k++) pe[k*step] = (T) seq(j*gpas+k);
376 }
377 }
378 return(*this);
379}
380
381// >>>> Operations avec 2nd membre de type scalaire
382
383//! Fill an array with a constant value \b x
384template <class T>
385TArray<T>& TArray<T>::SetT(T x)
386{
387 if (NbDimensions() < 1)
388 throw RangeCheckError("TArray<T>::SetT(T ) - Not Allocated Array ! ");
389 T * pe;
390 uint_8 j,k;
391 if (AvgStep() > 0) { // regularly spaced elements
392 uint_8 step = AvgStep();
393 uint_8 maxx = totsize_*step;
394 pe = Data();
395 for(k=0; k<maxx; k+=step ) pe[k] = x;
396 }
397 else { // Non regular data spacing ...
398 uint_4 ka = MaxSizeKA();
399 uint_8 step = Step(ka);
400 uint_8 gpas = Size(ka)*step;
401 uint_8 naxa = Size()/Size(ka);
402 for(j=0; j<naxa; j++) {
403 pe = mNDBlock.Begin()+Offset(ka,j);
404 for(k=0; k<gpas; k+=step) pe[k] = x;
405 }
406 }
407 return(*this);
408}
409
410//! Add a constant value \b x to an array
411template <class T>
412TArray<T>& TArray<T>::Add(T x)
413{
414 if (NbDimensions() < 1)
415 throw RangeCheckError("TArray<T>::Add(T ) - Not Allocated Array ! ");
416 T * pe;
417 uint_8 j,k;
418 if (AvgStep() > 0) { // regularly spaced elements
419 uint_8 step = AvgStep();
420 uint_8 maxx = totsize_*step;
421 pe = Data();
422 for(k=0; k<maxx; k+=step ) pe[k] += x;
423 }
424 else { // Non regular data spacing ...
425 uint_4 ka = MaxSizeKA();
426 uint_8 step = Step(ka);
427 uint_8 gpas = Size(ka)*step;
428 uint_8 naxa = Size()/Size(ka);
429 for(j=0; j<naxa; j++) {
430 pe = mNDBlock.Begin()+Offset(ka,j);
431 for(k=0; k<gpas; k+=step) pe[k] += x;
432 }
433 }
434 return(*this);
435}
436
437//! Substract a constant value \b x to an array
438/*!
439Substract a constant from the *this = *this-x
440\param fginv == true : Perfoms the inverse subtraction (*this = x-(*this))
441*/
442template <class T>
443TArray<T>& TArray<T>::Sub(T x, bool fginv)
444{
445 if (NbDimensions() < 1)
446 throw RangeCheckError("TArray<T>::Sub(T ) - Not Allocated Array ! ");
447 T * pe;
448 uint_8 j,k;
449 if (AvgStep() > 0) { // regularly spaced elements
450 uint_8 step = AvgStep();
451 uint_8 maxx = totsize_*step;
452 pe = Data();
453 if (fginv)
454 for(k=0; k<maxx; k+=step ) pe[k] = x-pe[k];
455 else
456 for(k=0; k<maxx; k+=step ) pe[k] -= x;
457 }
458 else { // Non regular data spacing ...
459 uint_4 ka = MaxSizeKA();
460 uint_8 step = Step(ka);
461 uint_8 gpas = Size(ka)*step;
462 uint_8 naxa = Size()/Size(ka);
463 for(j=0; j<naxa; j++) {
464 pe = mNDBlock.Begin()+Offset(ka,j);
465 if (fginv)
466 for(k=0; k<gpas; k+=step) pe[k] = x-pe[k];
467 else
468 for(k=0; k<gpas; k+=step) pe[k] -= x;
469 }
470 }
471 return(*this);
472}
473
474//! Multiply an array by a constant value \b x
475template <class T>
476TArray<T>& TArray<T>::Mul(T x)
477{
478 if (NbDimensions() < 1)
479 throw RangeCheckError("TArray<T>::Mul(T ) - Not Allocated Array ! ");
480 T * pe;
481 uint_8 j,k;
482 if (AvgStep() > 0) { // regularly spaced elements
483 uint_8 step = AvgStep();
484 uint_8 maxx = totsize_*step;
485 pe = Data();
486 for(k=0; k<maxx; k+=step ) pe[k] *= x;
487 }
488 else { // Non regular data spacing ...
489 uint_4 ka = MaxSizeKA();
490 uint_8 step = Step(ka);
491 uint_8 gpas = Size(ka)*step;
492 uint_8 naxa = Size()/Size(ka);
493 for(j=0; j<naxa; j++) {
494 pe = mNDBlock.Begin()+Offset(ka,j);
495 for(k=0; k<gpas; k+=step) pe[k] *= x;
496 }
497 }
498 return(*this);
499}
500
501//! Divide an array by a constant value \b x
502/*!
503Divide the array by a constant *this = *this/x
504\param fginv == true : Perfoms the inverse division (*this = x/(*this))
505*/
506template <class T>
507TArray<T>& TArray<T>::Div(T x, bool fginv)
508{
509 if (NbDimensions() < 1)
510 throw RangeCheckError("TArray<T>::Div(T ) - Not Allocated Array ! ");
511 if (!fginv && (x == (T) 0) )
512 throw MathExc("TArray<T>::Div(T ) - Divide by zero ! ");
513 T * pe;
514 uint_8 j,k;
515 if (AvgStep() > 0) { // regularly spaced elements
516 uint_8 step = AvgStep();
517 uint_8 maxx = totsize_*step;
518 pe = Data();
519 if (fginv)
520 for(k=0; k<maxx; k+=step ) pe[k] = x/pe[k];
521 else
522 for(k=0; k<maxx; k+=step ) pe[k] /= x;
523 }
524 else { // Non regular data spacing ...
525 uint_4 ka = MaxSizeKA();
526 uint_8 step = Step(ka);
527 uint_8 gpas = Size(ka)*step;
528 uint_8 naxa = Size()/Size(ka);
529 for(j=0; j<naxa; j++) {
530 pe = mNDBlock.Begin()+Offset(ka,j);
531 if (fginv)
532 for(k=0; k<gpas; k+=step) pe[k] = x/pe[k];
533 else
534 for(k=0; k<gpas; k+=step) pe[k] /= x;
535 }
536 }
537 return(*this);
538}
539
540
541
542
543// >>>> Operations avec 2nd membre de type tableau
544//! Add two TArrays
545template <class T>
546TArray<T>& TArray<T>::AddElt(const TArray<T>& a)
547{
548 if (NbDimensions() < 1)
549 throw RangeCheckError("TArray<T>::AddElt(const TArray<T>& ) - Not Allocated Array ! ");
550 if (!CompareSizes(a))
551 throw(SzMismatchError("TArray<T>::AddElt(const TArray<T>&) SizeMismatch")) ;
552
553 T * pe;
554 const T * pea;
555 uint_8 j,k,ka;
556 if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
557 uint_8 step = AvgStep();
558 uint_8 stepa = a.AvgStep();
559 uint_8 maxx = totsize_*step;
560 pe = Data();
561 pea = a.Data();
562 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] += pea[ka] ;
563 }
564 else { // Non regular data spacing ...
565 uint_4 ax = MaxSizeKA();
566 uint_8 step = Step(ax);
567 uint_8 stepa = a.Step(ax);
568 uint_8 gpas = Size(ax)*step;
569 uint_8 naxa = Size()/Size(ax);
570 for(j=0; j<naxa; j++) {
571 pe = mNDBlock.Begin()+Offset(ax,j);
572 pea = a.DataBlock().Begin()+a.Offset(ax,j);
573 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] += pea[ka];
574 }
575 }
576 return(*this);
577}
578
579//! Substract two TArrays
580/*!
581Substract two TArrays *this = *this-a
582\param fginv == true : Perfoms the inverse subtraction (*this = a-(*this))
583*/
584template <class T>
585TArray<T>& TArray<T>::SubElt(const TArray<T>& a, bool fginv)
586{
587 if (NbDimensions() < 1)
588 throw RangeCheckError("TArray<T>::SubElt(const TArray<T>& ) - Not Allocated Array ! ");
589 if (!CompareSizes(a))
590 throw(SzMismatchError("TArray<T>::SubElt(const TArray<T>&) SizeMismatch")) ;
591
592 T * pe;
593 const T * pea;
594 uint_8 j,k,ka;
595 if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
596 uint_8 step = AvgStep();
597 uint_8 stepa = a.AvgStep();
598 uint_8 maxx = totsize_*step;
599 pe = Data();
600 pea = a.Data();
601 if (fginv)
602 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]-pe[k] ;
603 else
604 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] -= pea[ka] ;
605 }
606 else { // Non regular data spacing ...
607 uint_4 ax = MaxSizeKA();
608 uint_8 step = Step(ax);
609 uint_8 stepa = a.Step(ax);
610 uint_8 gpas = Size(ax)*step;
611 uint_8 naxa = Size()/Size(ax);
612 for(j=0; j<naxa; j++) {
613 pe = mNDBlock.Begin()+Offset(ax,j);
614 pea = a.DataBlock().Begin()+a.Offset(ax,j);
615 if (fginv)
616 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]-pe[k] ;
617 else
618 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] -= pea[ka];
619 }
620 }
621 return(*this);
622}
623
624
625//! Multiply two TArrays (elements by elements)
626template <class T>
627TArray<T>& TArray<T>::MulElt(const TArray<T>& a)
628{
629 if (NbDimensions() < 1)
630 throw RangeCheckError("TArray<T>::MulElt(const TArray<T>& ) - Not Allocated Array ! ");
631 if (!CompareSizes(a))
632 throw(SzMismatchError("TArray<T>::MulElt(const TArray<T>&) SizeMismatch")) ;
633
634 T * pe;
635 const T * pea;
636 uint_8 j,k,ka;
637 if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
638 uint_8 step = AvgStep();
639 uint_8 stepa = a.AvgStep();
640 uint_8 maxx = totsize_*step;
641 pe = Data();
642 pea = a.Data();
643 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] *= pea[ka] ;
644 }
645 else { // Non regular data spacing ...
646 uint_4 ax = MaxSizeKA();
647 uint_8 step = Step(ax);
648 uint_8 stepa = a.Step(ax);
649 uint_8 gpas = Size(ax)*step;
650 uint_8 naxa = Size()/Size(ax);
651 for(j=0; j<naxa; j++) {
652 pe = mNDBlock.Begin()+Offset(ax,j);
653 pea = a.DataBlock().Begin()+a.Offset(ax,j);
654 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] *= pea[ka];
655 }
656 }
657 return(*this);
658}
659
660
661//! Divide two TArrays (elements by elements)
662/*!
663Divide two TArrays *this = (*this)/a
664\param fginv == true : Perfoms the inverse division (*this = a/(*this))
665*/
666template <class T>
667TArray<T>& TArray<T>::DivElt(const TArray<T>& a, bool fginv)
668{
669 if (NbDimensions() < 1)
670 throw RangeCheckError("TArray<T>::DivElt(const TArray<T>& ) - Not Allocated Array ! ");
671 if (!CompareSizes(a))
672 throw(SzMismatchError("TArray<T>::DivElt(const TArray<T>&) SizeMismatch")) ;
673
674 T * pe;
675 const T * pea;
676 uint_8 j,k,ka;
677 if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
678 uint_8 step = AvgStep();
679 uint_8 stepa = a.AvgStep();
680 uint_8 maxx = totsize_*step;
681 pe = Data();
682 pea = a.Data();
683 if (fginv)
684 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]/pe[k];
685 else
686 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] /= pea[ka] ;
687 }
688 else { // Non regular data spacing ...
689 uint_4 ax = MaxSizeKA();
690 uint_8 step = Step(ax);
691 uint_8 stepa = a.Step(ax);
692 uint_8 gpas = Size(ax)*step;
693 uint_8 naxa = Size()/Size(ax);
694 for(j=0; j<naxa; j++) {
695 pe = mNDBlock.Begin()+Offset(ax,j);
696 pea = a.DataBlock().Begin()+a.Offset(ax,j);
697 if (fginv)
698 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]/pe[k];
699 else
700 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] /= pea[ka];
701 }
702 }
703 return(*this);
704}
705
706//! Copy elements of \b a
707template <class T>
708TArray<T>& TArray<T>::CopyElt(const TArray<T>& a)
709{
710 if (NbDimensions() < 1)
711 throw RangeCheckError("TArray<T>::CopyElt(const TArray<T>& ) - Not Allocated Array ! ");
712 if (!CompareSizes(a))
713 throw(SzMismatchError("TArray<T>::MultElt(const TArray<T>&) SizeMismatch")) ;
714
715 T * pe;
716 const T * pea;
717 uint_8 j,k,ka;
718 if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
719 uint_8 step = AvgStep();
720 uint_8 stepa = a.AvgStep();
721 uint_8 maxx = totsize_*step;
722 pe = Data();
723 pea = a.Data();
724 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka] ;
725 }
726 else { // Non regular data spacing ...
727 uint_4 ax = MaxSizeKA();
728 uint_8 step = Step(ax);
729 uint_8 stepa = a.Step(ax);
730 uint_8 gpas = Size(ax)*step;
731 uint_8 naxa = Size()/Size(ax);
732 for(j=0; j<naxa; j++) {
733 pe = mNDBlock.Begin()+Offset(ax,j);
734 pea = a.DataBlock().Begin()+a.Offset(ax,j);
735 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka];
736 }
737 }
738 return(*this);
739}
740
741
742// Somme et produit des elements
743//! Sum all elements
744template <class T>
745T TArray<T>::Sum() const
746{
747 if (NbDimensions() < 1)
748 throw RangeCheckError("TArray<T>::Sum() - Not Allocated Array ! ");
749 T ret=0;
750 const T * pe;
751 uint_8 j,k;
752 if (AvgStep() > 0) { // regularly spaced elements
753 uint_8 step = AvgStep();
754 uint_8 maxx = totsize_*step;
755 pe = Data();
756 for(k=0; k<maxx; k+=step ) ret += pe[k];
757 }
758 else { // Non regular data spacing ...
759 uint_4 ka = MaxSizeKA();
760 uint_8 step = Step(ka);
761 uint_8 gpas = Size(ka)*step;
762 uint_8 naxa = Size()/Size(ka);
763 for(j=0; j<naxa; j++) {
764 pe = mNDBlock.Begin()+Offset(ka,j);
765 for(k=0; k<gpas; k+=step) ret += pe[k] ;
766 }
767 }
768 return ret;
769}
770
771//! Multiply all elements
772template <class T>
773T TArray<T>::Product() const
774{
775 if (NbDimensions() < 1)
776 throw RangeCheckError("TArray<T>::Product() - Not Allocated Array ! ");
777 T ret=0;
778 const T * pe;
779 uint_8 j,k;
780 if (AvgStep() > 0) { // regularly spaced elements
781 uint_8 step = AvgStep();
782 uint_8 maxx = totsize_*step;
783 pe = Data();
784 for(k=0; k<maxx; k+=step ) ret *= pe[k];
785 }
786 else { // Non regular data spacing ...
787 uint_4 ka = MaxSizeKA();
788 uint_8 step = Step(ka);
789 uint_8 gpas = Size(ka)*step;
790 uint_8 naxa = Size()/Size(ka);
791 for(j=0; j<naxa; j++) {
792 pe = mNDBlock.Begin()+Offset(ka,j);
793 for(k=0; k<gpas; k+=step) ret *= pe[k] ;
794 }
795 }
796 return ret;
797}
798
799
800
801// ----------------------------------------------------
802// Impression, etc ...
803// ----------------------------------------------------
804
805//! Return a string that contain the type \b T of the array
806template <class T>
807string TArray<T>::InfoString() const
808{
809 string rs = "TArray<" ;
810 rs += typeid(T).name();
811 rs += "> ";
812 return(rs);
813}
814
815//! Print array
816/*!
817 \param os : output stream
818 \param maxprt : maximum numer of print
819 \param si : if true, display attached DvList
820 \sa SetMaxPrint
821 */
822template <class T>
823void TArray<T>::Print(ostream& os, int_4 maxprt, bool si) const
824{
825 if (maxprt < 0) maxprt = max_nprt_;
826 uint_4 npr = 0;
827 Show(os, si);
828 if (ndim_ < 1) return;
829 uint_4 k0,k1,k2,k3,k4;
830 for(k4=0; k4<size_[4]; k4++) {
831 if (size_[4] > 1) cout << "\n ----- Dimension 5 (U) K4= " << k4 << endl;
832 for(k3=0; k3<size_[3]; k3++) {
833 if (size_[3] > 1) cout << "\n ----- Dimension 4 (T) K3= " << k3 << endl;
834 for(k2=0; k2<size_[2]; k2++) {
835 if (size_[2] > 1) cout << "\n ----- Dimension 3 (Z) K2= " << k2 << endl;
836 for(k1=0; k1<size_[1]; k1++) {
837 if ( (size_[1] > 1) && (size_[0] > 10) ) cout << "----- Dimension 2 (Y) K1= " << k1 << endl;
838 for(k0=0; k0<size_[0]; k0++) {
839 if(k0 > 0) os << ", ";
840 os << Elem(k0, k1, k2, k3, k4); npr++;
841 if (npr >= (uint_4) maxprt) {
842 if (npr < totsize_) os << "\n .... " << endl; return;
843 }
844 }
845 os << endl;
846 }
847 }
848 }
849 }
850 os << endl;
851}
852
853
854
855///////////////////////////////////////////////////////////////
856///////////////////////////////////////////////////////////////
857#ifdef __CXX_PRAGMA_TEMPLATES__
858/*
859#pragma define_template TArray<uint_1>
860#pragma define_template TArray<int_2>
861#pragma define_template TArray<uint_4>
862#pragma define_template TArray<uint_8>
863*/
864#pragma define_template TArray<uint_2>
865#pragma define_template TArray<int_4>
866#pragma define_template TArray<int_8>
867#pragma define_template TArray<r_4>
868#pragma define_template TArray<r_8>
869#pragma define_template TArray< complex<r_4> >
870#pragma define_template TArray< complex<r_8> >
871#endif
872
873#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
874/*
875template class TArray<uint_1>;
876template class TArray<int_2>;
877template class TArray<uint_4>;
878template class TArray<uint_8>;
879*/
880template class TArray<uint_2>;
881template class TArray<int_4>;
882template class TArray<int_8>;
883template class TArray<r_4>;
884template class TArray<r_8>;
885template class TArray< complex<r_4> >;
886template class TArray< complex<r_8> >;
887#endif
888
889
Note: See TracBrowser for help on using the repository browser.