1 /*
  2  *   Copyright (c) 2009, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef CXXBLAS_LEVEL2_HPR2_TCC
 34 #define CXXBLAS_LEVEL2_HPR2_TCC 1
 35 
 36 #include <complex>
 37 #include <cxxblas/level1/level1.h>
 38 
 39 namespace cxxblas {
 40 
 41 template <typename IndexType, typename ALPHA, typename VX, typename VY,
 42           typename MA>
 43 void
 44 hpr2_generic(StorageOrder order, StorageUpLo upLo, Transpose conjugateA,
 45              IndexType n,
 46              const ALPHA &alpha,
 47              const VX *x, IndexType incX,
 48              const VY *y, IndexType incY,
 49              MA *A)
 50 {
 51     if (alpha==ALPHA(0)) {
 52         return;
 53     }
 54     if (order==ColMajor) {
 55         upLo = (upLo==Upper) ? Lower : Upper;
 56         conjugateA = Transpose(conjugateA^Conj);
 57         hpr2_generic(RowMajor, upLo, conjugateA,
 58                      n, alpha, x, incX, y, incY, A);
 59         return;
 60     }
 61     #ifdef CXXBLAS_USE_XERBLA
 62         // insert error check here
 63     #endif
 64     if (upLo==Upper) {
 65         if (conjugateA==Conj) {
 66             for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 67                 axpy_generic(n-i, conjugate(alpha*x[iX]),
 68                                   y+iY, incY,
 69                                   A+i*(2*n-i+1)/2, IndexType(1));
 70                 axpy_generic(n-i, alpha*conjugate(y[iY]),
 71                                   x+iX, incX,
 72                                   A+i*(2*n-i+1)/2, IndexType(1));
 73             }
 74         } else {
 75             for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 76                 acxpy_generic(n-i, alpha*x[iX],
 77                                    y+iY, incY,
 78                                    A+i*(2*n-i+1)/2, IndexType(1));
 79                 acxpy_generic(n-i, conjugate(alpha)*y[iY],
 80                                    x+iX, incX,
 81                                    A+i*(2*n-i+1)/2, IndexType(1));
 82             }
 83         }
 84         for (IndexType i=0; i<n; ++i) {
 85             A[i+i*(2*n-i-1)/2] = cxxblas::real(A[i+i*(2*n-i-1)/2]);
 86         }
 87     } else {
 88         if (conjugateA==Conj) {
 89             for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 90                 axpy_generic(i+1, conjugate(alpha*x[iX]),
 91                                   y, incY,
 92                                   A+i*(i+1)/2, IndexType(1));
 93                 axpy_generic(i+1, alpha*conjugate(y[iY]),
 94                                   x, incX,
 95                                   A+i*(i+1)/2, IndexType(1));
 96             }
 97         } else {
 98             for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 99                 acxpy_generic(i+1, alpha*x[iX],
100                                    y, incY,
101                                    A+i*(i+1)/2, IndexType(1));
102                 acxpy_generic(i+1, conjugate(alpha)*y[iY],
103                                    x, incX,
104                                    A+i*(i+1)/2, IndexType(1));
105             }
106         }
107         for (IndexType i=0; i<n; ++i) {
108             A[i+i*(i+1)/2] = cxxblas::real(A[i+i*(i+1)/2]);
109         }
110     }
111 }
112 
113 template <typename IndexType, typename ALPHA, typename VX, typename VY,
114           typename MA>
115 void
116 hpr2(StorageOrder order, StorageUpLo upLo,
117      IndexType n,
118      const ALPHA &alpha,
119      const VX *x, IndexType incX,
120      const VY *y, IndexType incY,
121      MA *A)
122 {
123     CXXBLAS_DEBUG_OUT("hpr2_generic");
124 
125     if (incX<0) {
126         x -= incX*(n-1);
127     }
128     if (incY<0) {
129         y -= incY*(n-1);
130     }
131     hpr2_generic(order, upLo, NoTrans, n, alpha, x, incX, y, incY, A);
132 }
133 
134 // namespace cxxblas
135 
136 #endif // CXXBLAS_LEVEL2_HPR2_TCC