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