Vmath.cpp 31.6 KB
Newer Older
1
2
///////////////////////////////////////////////////////////////////////////////
//
Pavel Burovskiy's avatar
Pavel Burovskiy committed
3
// File: Vmath.hpp
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Collection of templated functions for vector mathematics
//
///////////////////////////////////////////////////////////////////////////////

#include <LibUtilities/BasicUtils/Vmath.hpp>
#include <LibUtilities/BasicConst/NektarUnivTypeDefs.hpp>
38
#include <LibUtilities/LibUtilitiesDeclspec.h>
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

namespace Vmath
{

    /***************** Math routines  ***************/

    /// \brief Fill a vector with a constant value
    template<class T>  void Fill( int n, const T alpha,  T *x, const int incx )
    {
        while( n-- )
        {
            *x = alpha;
             x += incx;
        }
    }

    template LIB_UTILITIES_EXPORT void Fill( int n, const Nektar::NekDouble alpha,  Nektar::NekDouble *x, const int incx );

    #define IM1   2147483563
    #define IM2   2147483399
    #define AM    (1.0/IM1)
    #define IMM1  (IM1-1)
    #define IA1   40014
    #define IA2   40692
    #define IQ1   53668
    #define IQ2   52774
    #define IR1   12211
    #define IR2   3791
    #define NTAB  32
    #define NDIV  (1+IMM1/NTAB)
    #define EPS   1.2e-7
    #define RNMX  (1.0-EPS)

    /// \brief Generates a number from ~Normal(0,1)
    template<class T> T ran2 (long* idum)
    /* ------------------------------------------------------------------------- *
     * Ran2 from NR 2e.  Returns a uniform random deviate between 0.0 &
     * 1.0 (exclusive of endpoints).  Call with idum a negative integer to
     * initialize; thereafter, do not alter idum between successive
     * deviates in a sequence.  RNMX should approximate the largest
     * floating value that is less than 1.
     * ------------------------------------------------------------------------- */
    {
      int         j;
      long        k;
      static long idum2=123456789;
      static long iy=0;
      static long iv[NTAB];
      T           temp;

      if (*idum <= 0) {
        if (-(*idum) < 1) *idum = 1;
        else              *idum = -(*idum);
        idum2 = (*idum);
        for (j=NTAB+7; j>=0; j--) {
          k = (*idum) / IQ1;
          *idum = IA1 * (*idum - k*IQ1) - k*IR1;
          if (*idum < 0) *idum += IM1;
          if (j < NTAB) iv[j] = *idum;
        }
        iy = iv[0];
      }

      k = (*idum) / IQ1;
      *idum = IA1*(*idum - k*IQ1) - k*IR1;
      if (*idum < 0) *idum += IM1;

      k = idum2 / IQ2;
      idum2 = IA2*(idum2 - k*IQ2) - k*IR2;
      if (idum2 < 0) idum2 += IM2;

      j = iy / NDIV;
      iy = iv[j] - idum2;
      iv[j] = *idum;
      if (iy < 1) iy += IMM1;

      if ((temp=AM*iy) > RNMX) return RNMX;
      else                     return temp;
    }

    #undef IM1
    #undef IM2
    #undef AM
    #undef IMM1
    #undef IA1
    #undef IA2
    #undef IQ1
    #undef IQ2
    #undef IR1
    #undef IR2
    #undef NTAB
    #undef NDIV
    #undef EPS
    #undef RNMX

Simon Clifford's avatar
Simon Clifford committed
134
    static boost::mutex mutex;
135
136
137
    template LIB_UTILITIES_EXPORT Nektar::NekDouble ran2 (long* idum);

    /// \brief Fills a vector with white noise.
138
    template<class T>  void FillWhiteNoise( int n, const T eps, T *x, const int incx, int outseed)
139
    {
Simon Clifford's avatar
Simon Clifford committed
140
141
        // Protect the static vars here and in ran2
        boost::mutex::scoped_lock l(mutex);
Douglas Serson's avatar
Douglas Serson committed
142
        long    seed = long(outseed);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        while( n-- )
        {
            static int     iset = 0;
            static T       gset;
            T              fac, rsq, v1, v2;

            if (iset == 0) {
              do {
                v1 = 2.0 * ran2<T> (&seed) - 1.0;
                v2 = 2.0 * ran2<T> (&seed) - 1.0;
                rsq = v1*v1 + v2*v2;
              } while (rsq >= 1.0 || rsq == 0.0);
              fac = sqrt(-2.0 * log (rsq) / rsq);
              gset = v1 * fac;
              iset = 1;
              *x = eps * v2 * fac;
            } else {
              iset = 0;
              *x = eps * gset;
            }
            x += incx;
        }
    }
166
    template  LIB_UTILITIES_EXPORT void FillWhiteNoise( int n, const Nektar::NekDouble eps, Nektar::NekDouble *x, const int incx, int outseed);
167
168
169
170
171

    /// \brief Multiply vector z = x*y
    template<class T>  void Vmul( int n, const T *x, const int incx, const T *y,
                                  const int incy,  T*z, const int incz)
    {
172
        ++n;
173
        if (incx == 1 && incy == 1 && incz == 1)
174
        {
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
            while( --n )
            {
                *z = (*x) * (*y);
                ++x;
                ++y;
                ++z;
            }
        }
        else
        {
            while( --n )
            {
                *z = (*x) * (*y);
                x += incx;
                y += incy;
                z += incz;
            }
192
193
194
195
196
197
198
199
200
201
        }
    }
    template  LIB_UTILITIES_EXPORT void Vmul( int n, const Nektar::NekDouble *x, const int incx, const Nektar::NekDouble *y,
                              const int incy,  Nektar::NekDouble*z, const int incz);

    /// \brief Scalar multiply  y = alpha*y

    template<class T>  void Smul( int n, const T alpha, const T *x, const int incx,
                                  T *y, const int incy)
    {
202
        ++n;
203
        if (incx == 1 && incy == 1)
204
        {
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
            while( --n )
            {
                *y = alpha * (*x);
                ++x;
                ++y;
            }
        }
        else
        {
            while( --n )
            {
                *y = alpha * (*x);
                x += incx;
                y += incy;
            }
220
221
222
223
224
225
226
227
228
229
        }
    }

    template  LIB_UTILITIES_EXPORT void Smul( int n, const Nektar::NekDouble alpha, const Nektar::NekDouble *x, const int incx,
                              Nektar::NekDouble *y, const int incy);

    /// \brief Multiply vector z = x/y
    template<class T>  void Vdiv( int n, const T *x, const int incx, const T *y,
                  const int incy,  T*z, const int incz)
    {
230
        ++n;
231
        if (incx == 1 && incy == 1)
232
        {
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            while( --n )
            {
                *z = (*x) / (*y);
                ++x;
                ++y;
                ++z;
            }
        }
        else
        {
            while( --n )
            {
                *z = (*x) / (*y);
                x += incx;
                y += incy;
                z += incz;
            }
250
251
252
253
254
255
256
257
258
259
        }
    }

    template LIB_UTILITIES_EXPORT void Vdiv( int n, const Nektar::NekDouble *x, const int incx, const Nektar::NekDouble *y,
              const int incy,  Nektar::NekDouble*z, const int incz);

    /// \brief Scalar multiply  y = alpha/y
    template<class T>  void Sdiv( int n, const T alpha, const T *x,
                                  const int incx, T *y, const int incy)
    {
260
        ++n;
261
        if (incx == 1 && incy == 1)
262
        {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
            while( --n )
            {
                *y = alpha / (*x);
                ++x;
                ++y;
            }
        }
        else
        {
            while( --n )
            {
                *y = alpha / (*x);
                x += incx;
                y += incy;
            }
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        }
    }

    template  LIB_UTILITIES_EXPORT void Sdiv( int n, const Nektar::NekDouble alpha, const Nektar::NekDouble *x,
                              const int incx, Nektar::NekDouble *y, const int incy);

    /// \brief Add vector z = x+y
    template<class T>  void Vadd( int n, const T *x, const int incx, const T *y,
                                  const int incy,  T *z, const int incz)
    {
        while( n-- )
        {
            *z = (*x) + (*y);
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template  LIB_UTILITIES_EXPORT void Vadd( int n, const Nektar::NekDouble *x, const int incx, const Nektar::NekDouble *y,
                              const int incy,  Nektar::NekDouble *z, const int incz);

    /// \brief Add vector y = alpha + x
    template<class T>  void Sadd( int n, const T alpha, const T *x,
                  const int incx, T *y, const int incy)
    {
304
        ++n;
305
        if (incx == 1 && incy == 1)
306
        {
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
            while( --n )
            {
                *y = alpha + (*x);
                ++x;
                ++y;
            }
        }
        else
        {
            while( --n )
            {
                *y = alpha + (*x);
                x += incx;
                y += incy;
            }
322
323
324
325
326
327
328
329
330
331
        }
    }

    template LIB_UTILITIES_EXPORT void Sadd( int n, const Nektar::NekDouble alpha, const Nektar::NekDouble *x,
              const int incx, Nektar::NekDouble *y, const int incy);

    /// \brief Subtract vector z = x-y
    template<class T>  void Vsub( int n, const T *x, const int incx, const T *y,
                                  const int incy,  T *z, const int incz)
    {
332
        ++n;
333
        if (incx == 1 && incy == 1 && incz == 1)
334
        {
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
            while( --n )
            {
                *z = (*x) - (*y);
                ++x;
                ++y;
                ++z;
            }
        }
        else
        {
            while( --n )
            {
                *z = (*x) - (*y);
                x += incx;
                y += incy;
                z += incz;
            }
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        }
    }

    template  LIB_UTILITIES_EXPORT void Vsub( int n, const Nektar::NekDouble *x, const int incx, const Nektar::NekDouble *y,
                              const int incy,  Nektar::NekDouble *z, const int incz);

    /// \brief Zero vector
    template<class T>  void Zero(int n, T *x, const int incx)
    {
        if(incx == 1)
        {
            std::memset(x,'\0', n*sizeof(T));
        }
        else
        {
            T zero = 0;
368
369
            ++n;
            while(--n)
370
371
372
373
374
375
376
377
            {
            *x = zero;
            x+=incx;
            }
        }
    }

    template  LIB_UTILITIES_EXPORT void Zero(int n, Nektar::NekDouble *x, const int incx);
378
379
    template  LIB_UTILITIES_EXPORT void Zero(int n, int *x, const int incx);
    template  LIB_UTILITIES_EXPORT void Zero(int n, long *x, const int incx);
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

    /// \brief Negate x = -x
    template<class T>  void Neg( int n, T *x, const int incx)
    {
        while( n-- )
        {
            *x = -(*x);
            x += incx;
        }
    }

    template  LIB_UTILITIES_EXPORT void Neg( int n, Nektar::NekDouble *x, const int incx);

    /// \brief sqrt y = sqrt(x)
    template<class T> void Vsqrt(int n, const T *x, const int incx,
                 T *y, const int incy)
    {
        while (n--)
        {
            *y  = sqrt( *x );
            x  += incx;
            y  += incy;
        }
    }

    template LIB_UTILITIES_EXPORT void Vsqrt(int n, const Nektar::NekDouble *x, const int incx,
             Nektar::NekDouble *y, const int incy);


    /// \brief vabs: y = |x|
    template<class T> void Vabs(int n, const T *x, const int incx,
                T *y, const int incy)
    {
        while( n-- )
        {
            *y = ( *x >0)? *x:-(*x);
            x += incx;
            y += incy;
        }
    }

    template LIB_UTILITIES_EXPORT void Vabs(int n, const Nektar::NekDouble *x, const int incx,
            Nektar::NekDouble *y, const int incy);


    /********** Triad  routines  ***********************/

    /// \brief  vvtvp (vector times vector plus vector): z = w*x + y
    template<class T> void Vvtvp(int n,
                                 const T *w, const int incw,
                                 const T *x, const int incx,
                                 const T *y, const int incy,
                                       T *z, const int incz)
    {
        while( n-- )
        {
            *z = (*w) * (*x) + (*y);
            w += incw;
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Vvtvp(int n,
                             const Nektar::NekDouble *w, const int incw,
                             const Nektar::NekDouble *x, const int incx,
                             const Nektar::NekDouble *y, const int incy,
                                   Nektar::NekDouble *z, const int incz);

    /// \brief vvtvm (vector times vector plus vector): z = w*x - y
    template<class T> void Vvtvm(int n, const T *w, const int incw, const T *x,
                 const int incx, const T *y, const int incy,
                 T *z, const int incz)
    {
        while( n-- )
        {
            *z = (*w) * (*x) - (*y);
            w += incw;
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Vvtvm(int n, const Nektar::NekDouble *w, const int incw, const Nektar::NekDouble *x,
             const int incx, const Nektar::NekDouble *y, const int incy,
             Nektar::NekDouble *z, const int incz);

469

470
    /// \brief  svtvp (scalar times vector plus vector): z = alpha*x + y
471
    template<class T> LIB_UTILITIES_EXPORT void Svtvp(int n, const T alpha, const T *x,
472
473
474
                 const int incx, const T *y, const int incy,
                 T *z, const int incz)
    {
475
        ++n;
476
        if (incx == 1 && incy == 1 && incz == 1)
477
        {
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
            while( --n )
            {
                *z = alpha * (*x) + (*y);
                ++x;
                ++y;
                ++z;
            }
        }
        else
        {
            while( --n )
            {
                *z = alpha * (*x) + (*y);
                x += incx;
                y += incy;
                z += incz;
            }
495
496
497
        }
    }

Pavel Burovskiy's avatar
Pavel Burovskiy committed
498
499
500
    template LIB_UTILITIES_EXPORT void Svtvp(int n, const Nektar::NekDouble alpha, const Nektar::NekDouble *x,
                 const int incx, const Nektar::NekDouble *y, const int incy,
                 Nektar::NekDouble *z, const int incz);
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

    /// \brief  svtvp (scalar times vector plus vector): z = alpha*x - y
    template<class T> void Svtvm(int n, const T alpha, const T *x,
                 const int incx, const T *y, const int incy,
                 T *z, const int incz)
    {
        while( n-- )
        {
            *z = alpha * (*x) - (*y);
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Svtvm(int n, const Nektar::NekDouble alpha, const Nektar::NekDouble *x,
             const int incx, const Nektar::NekDouble *y, const int incy,
             Nektar::NekDouble *z, const int incz);

    /// \brief  vvtvvtp (vector times vector plus vector times vector):
    // z = v*w + x*y
    template<class T> void Vvtvvtp (int n,
                                    const T* v, int incv,
                                    const T* w, int incw,
                                    const T* x, int incx,
                                    const T* y, int incy,
                                          T* z, int incz)
    {
        while( n-- )
        {
            *z = (*v) * (*w) + (*x) * (*y);
            v += incv;
            w += incw;
            x += incx;
            y += incy;
            z += incz;
        }
    }
    template LIB_UTILITIES_EXPORT void Vvtvvtp (int n,
                                const Nektar::NekDouble* v, int incv,
                                const Nektar::NekDouble* w, int incw,
                                const Nektar::NekDouble* x, int incx,
                                const Nektar::NekDouble* y, int incy,
                                      Nektar::NekDouble* z, int incz);


    /// \brief  vvtvvtm (vector times vector minus vector times vector):
    // z = v*w - x*y
    template<class T> void Vvtvvtm (int n,
                                    const T* v, int incv,
                                    const T* w, int incw,
                                    const T* x, int incx,
                                    const T* y, int incy,
                                          T* z, int incz)
    {
        while( n-- )
        {
            *z = (*v) * (*w) - (*x) * (*y);
            v += incv;
            w += incw;
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Vvtvvtm (int n,
                                const Nektar::NekDouble* v, int incv,
                                const Nektar::NekDouble* w, int incw,
                                const Nektar::NekDouble* x, int incx,
                                const Nektar::NekDouble* y, int incy,
                                      Nektar::NekDouble* z, int incz);

    /// \brief  vvtvvtp (scalar times vector plus scalar times vector):
    // z = alpha*x + beta*y
    template<class T> void Svtsvtp (int n,
                                    const T alpha,
                                    const T* x, int incx,
                                    const T beta,
                                    const T* y, int incy,
                                          T* z, int incz)
    {
        while( n-- )
        {
            *z = alpha * (*x) + beta * (*y);
            x += incx;
            y += incy;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Svtsvtp (int n,
                                const Nektar::NekDouble alpha,
                                const Nektar::NekDouble* x, int incx,
                                const Nektar::NekDouble beta,
                                const Nektar::NekDouble* y, int incy,
                                      Nektar::NekDouble* z, int incz);


    /// \brief  Vstvpp (scalar times vector plus vector plus vector):
    // z = v*w + x*y
    template<class T> void Vstvpp(int n,
                                  const T alpha,
                                  const T* v, int incv,
                                  const T* w, int incw,
                                  const T* x, int incx,
                                  T* z, int incz)
    {
        while( n-- )
        {
            *z = alpha * (*v) + (*w) + (*x);
            v += incv;
            w += incw;
            x += incx;
            z += incz;
        }
    }

    template LIB_UTILITIES_EXPORT void Vstvpp(int n,
                              const Nektar::NekDouble alpha,
                              const Nektar::NekDouble* v, int incv,
                              const Nektar::NekDouble* w, int incw,
                              const Nektar::NekDouble* x, int incx,
                              Nektar::NekDouble* z, int incz);

    /************ Misc routine from Veclib (and extras)  ************/

    /// \brief Gather vector z[i] = x[y[i]]
    template<class T>  void Gathr(int n, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
            *z++ = *(x + *y++);
        }
        return;
    }

    template LIB_UTILITIES_EXPORT  void Gathr(int n, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);


    /// \brief Gather vector z[i] = sign[i]*x[y[i]]
    template<class T>  void Gathr(int n, const T *sign, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
            *z++ = *(sign++) * (*(x + *y++));
        }
        return;
    }

    template LIB_UTILITIES_EXPORT  void Gathr(int n, const Nektar::NekDouble *sign, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);

    /// \brief Scatter vector z[y[i]] = x[i]
    template<class T>  void Scatr(int n, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
            *(z + *(y++)) = *(x++);
        }
    }

    template LIB_UTILITIES_EXPORT  void Scatr(int n, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);

    /// \brief Scatter vector z[y[i]] = sign[i]*x[i]
    template<class T>  void Scatr(int n, const T *sign, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
677
678
679
680
681
682
683
684
685
686
            if(*sign)
            {
                *(z + *(y++)) = *(sign++) * (*(x++));
            }
            else
            {
                x++;
                y++;
                sign++;
            }
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
        }
    }

    template LIB_UTILITIES_EXPORT  void Scatr(int n, const Nektar::NekDouble *sign, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);


    /// \brief Assemble z[y[i]] += x[i]; z should be zero'd first
    template<class T>  void Assmb(int n, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
            *(z + *(y++)) += *(x++);
        }
    }

    template LIB_UTILITIES_EXPORT  void Assmb(int n, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);

    /// \brief Assemble z[y[i]] += sign[i]*x[i]; z should be zero'd first
    template<class T>  void Assmb(int n, const T *sign, const T *x, const int *y,
                  T *z)
    {
        while (n--)
        {
            *(z + *(y++)) += *(sign++) * (*(x++));
        }
    }

    template LIB_UTILITIES_EXPORT  void Assmb(int n, const Nektar::NekDouble *sign, const Nektar::NekDouble *x, const int *y,
              Nektar::NekDouble *z);

    /************* Reduction routines  *****************/

    /// \brief Subtract return sum(x)
    template<class T>  T Vsum( int n, const T *x, const int incx)
    {

        T sum = 0;

        while( n-- )
        {
            sum += (*x);
            x += incx;
        }

        return sum;
    }

    template LIB_UTILITIES_EXPORT  Nektar::NekDouble Vsum( int n, const Nektar::NekDouble *x, const int incx);
    template LIB_UTILITIES_EXPORT  int Vsum( int n, const int *x, const int incx);

    /// \brief Return the index of the maximum element in x
    template<class T>  int Imax( int n, const T *x, const int incx)
    {

        int    i, indx = ( n > 0 ) ? 0 : -1;
        T      xmax = *x;

        for (i = 0; i < n; i++)
        {
            if (*x > xmax)
            {
            xmax = *x;
            indx = i;
            }
            x += incx;
        }

        return indx;
    }

    template LIB_UTILITIES_EXPORT  int Imax( int n, const Nektar::NekDouble *x, const int incx);
761
    template LIB_UTILITIES_EXPORT  int Imax( int n, const int *x, const int incx);
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

    /// \brief Return the maximum element in x -- called vmax to avoid
    /// conflict with max
    template<class T>  T Vmax( int n, const T *x, const int incx)
    {

        T  xmax = *x;

        while( n-- )
        {
            if (*x > xmax)
            {
            xmax = *x;
            }
            x += incx;
        }

        return xmax;
    }

    template LIB_UTILITIES_EXPORT  Nektar::NekDouble Vmax( int n, const Nektar::NekDouble *x, const int incx);
    template LIB_UTILITIES_EXPORT  int Vmax( int n, const int *x, const int incx);

    /// \brief Return the index of the maximum absolute element in x
    template<class T>  int Iamax( int n, const T *x, const int incx)
    {

        int    i, indx = ( n > 0 ) ? 0 : -1;
        T      xmax = *x;
        T      xm;

        for (i = 0; i < n; i++)
        {
            xm = (*x > 0)? *x: -*x;
            if (xm > xmax)
            {
            xmax = xm;
            indx = i;
            }
            x += incx;
        }

        return indx;
    }

    template LIB_UTILITIES_EXPORT int Iamax( int n, const Nektar::NekDouble *x, const int incx);

    /// \brief Return the maximum absolute element in x
    /// called vamax to avoid conflict with max
    template<class T>  T Vamax( int n, const T *x, const int incx)
    {

        T  xmax = *x;
        T  xm;

        while( n-- )
        {
            xm = (*x > 0)? *x: -*x;
            if (xm > xmax)
            {
            xmax = xm;
            }
            x += incx;
        }
        return xmax;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Vamax( int n, const Nektar::NekDouble *x, const int incx);


    /// \brief Return the index of the minimum element in x
    template<class T>  int Imin( int n, const T *x, const int incx)
    {

        int    i, indx = ( n > 0 ) ? 0 : -1;
        T      xmin = *x;

        for(i = 0;i < n;i++)
        {
            if( *x < xmin )
            {
            xmin = *x;
            indx = i;
            }
            x += incx;
        }

        return indx;
    }

    template LIB_UTILITIES_EXPORT int Imin( int n, const Nektar::NekDouble *x, const int incx);
853
    template LIB_UTILITIES_EXPORT  int Imin( int n, const int *x, const int incx);
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874

    /// \brief Return the minimum element in x - called vmin to avoid
    /// conflict with min
    template<class T>  T Vmin( int n, const T *x, const int incx)
    {

        T    xmin = *x;

        while( n-- )
        {
            if (*x < xmin)
            {
            xmin = *x;
            }
            x += incx;
        }

        return xmin;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Vmin( int n, const Nektar::NekDouble *x, const int incx);
875
    template LIB_UTILITIES_EXPORT int Vmin( int n, const int *x, const int incx);
876

877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
   /// \brief Return number of NaN elements of x
    template<class T>  int Nnan(int n, const T *x, const int incx)
    {

        int nNan = 0;

        while (n--)
        {
            if (*x != *x)
            {
                nNan++;
            }
            x += incx;
        }

        return nNan;
    }

    template LIB_UTILITIES_EXPORT int Nnan(int n, const Nektar::NekDouble *x, const int incx);
    template LIB_UTILITIES_EXPORT int Nnan(int n, const float *x, const int incx);
    template LIB_UTILITIES_EXPORT int Nnan(int n, const int *x, const int incx);

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    /// \brief  vvtvp (vector times vector times vector): z = w*x*y
    template<class T> T Dot(     int n,
                                 const T   *w,
                                 const T   *x)
    {
        T sum = 0;

        while( n-- )
        {
            sum += (*w) * (*x);
            ++w;
            ++x;
        }
        return sum;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Dot(     int n,
                             const Nektar::NekDouble   *w,
                             const Nektar::NekDouble   *x);

    /// \brief  vvtvp (vector times vector times vector): z = w*x*y
    template<class T> T Dot(     int n,
                                 const T   *w, const int incw,
                                 const T   *x, const int incx)
    {
        T sum = 0;

        while( n-- )
        {
            sum += (*w) * (*x);
            w += incw;
            x += incx;
        }
        return sum;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Dot(     int n,
                             const Nektar::NekDouble   *w, const int incw,
                             const Nektar::NekDouble   *x, const int incx);

    /// \brief  vvtvp (vector times vector times vector): z = w*x*y
    template<class T> T Dot2(    int n,
                                 const T   *w,
                                 const T   *x,
                                 const int *y)
    {
        T sum = 0;

        while( n-- )
        {
            sum += (*y == 1 ? (*w) * (*x) : 0 );
            ++w;
            ++x;
            ++y;
        }
        return sum;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Dot2(    int n,
                             const Nektar::NekDouble   *w,
                             const Nektar::NekDouble   *x,
                             const int *y);


    /// \brief  vvtvp (vector times vector times vector): z = w*x*y
    template<class T> T Dot2(    int n,
                                 const T   *w, const int incw,
                                 const T   *x, const int incx,
                                 const int *y, const int incy)
    {
        T sum = 0;

        while( n-- )
        {
            sum += (*y == 1 ? (*w) * (*x) : 0.0 );
            w += incw;
            x += incx;
            y += incy;
        }
        return sum;
    }

    template LIB_UTILITIES_EXPORT Nektar::NekDouble Dot2(    int n,
                             const Nektar::NekDouble   *w, const int incw,
                             const Nektar::NekDouble   *x, const int incx,
                             const int *y, const int incy);

986
/*
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
    // \brief copy one int vector to another
    void Vcopy(int n, const int *x, const int incx, int *y,
                             const int incy)
    {
        if( incx ==1 && incy == 1)
        {
            memcpy(y,x,n*sizeof(int));
        }
        else
        {
            while( n-- )
            {
                *y = *x;
                x += incx;
                y += incy;
            }
        }
    }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

    // \brief copy one unsigned int vector to another
    void Vcopy(int n, const unsigned int *x, const int incx, unsigned int *y,
                             const int incy)
    {
        if( incx ==1 && incy == 1)
        {
            memcpy(y,x,n*sizeof(int));
        }
        else
        {
            while( n-- )
            {
                *y = *x;
                x += incx;
                y += incy;
            }
        }
    }
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042

    // \brief copy one double vector to another
    void Vcopy(int n, const double *x, const int incx, double *y,
                             const int incy)
    {
        if( incx ==1 && incy == 1)
        {
            memcpy(y,x,n*sizeof(double));
        }
        else
        {
            while( n-- )
            {
                *y = *x;
                x += incx;
                y += incy;
            }
        }
    }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
*/

    // \brief copy one vector to another
    template<typename T>
    void Vcopy(int n, const T *x, const int incx,
                            T *y, const int incy)
    {
        if( incx ==1 && incy == 1)
        {
            memcpy(y,x,n*sizeof(T));
        }
        else
        {
            while( n-- )
            {
                *y = *x;
                x += incx;
                y += incy;
            }
        }
    }

    template  LIB_UTILITIES_EXPORT void  Vcopy( int n, const int *x, const int incx, int *y, const int incy);
    template  LIB_UTILITIES_EXPORT void  Vcopy( int n, const unsigned int *x, const int incx, unsigned int *y, const int incy);
    template  LIB_UTILITIES_EXPORT void  Vcopy( int n, const Nektar::NekDouble *x, const int incx, Nektar::NekDouble *y, const int incy);

1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089

    // \brief reverse the ordering of  vector to another
    template<class T>  void  Reverse( int n, const T *x, const int incx, T *y, const int incy)
    {
        int i;
        T store;
        int nloop = n/2;

        // copy value in case of n is odd number
        y[nloop] = x[nloop];

        for(i = 0; i < nloop; ++i)
        {
            store = x[n-1-i];
            y[n-1-i] = x[i];
            y[i] = store;
        }
    }

    template  LIB_UTILITIES_EXPORT void  Reverse( int n, const Nektar::NekDouble *x, const int incx, Nektar::NekDouble *y, const int incy);
}