1 /*
  2  *   Copyright (c) 2011, 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 /* Based on
 34  *
 35        SUBROUTINE DLAUU2( UPLO, N, A, LDA, INFO )
 36  *
 37  *  -- LAPACK auxiliary routine (version 3.3.1) --
 38  *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
 39  *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
 40  *  -- April 2011  
 41  */
 42 
 43 #ifndef FLENS_LAPACK_GESV_LAUU2_TCC
 44 #define FLENS_LAPACK_GESV_LAUU2_TCC 1
 45 
 46 #include <algorithm>
 47 #include <flens/blas/blas.h>
 48 #include <flens/lapack/lapack.h>
 49 
 50 #include <flens/lapack/interface/include/f77lapack.h>
 51 
 52 namespace flens { namespace lapack {
 53 
 54 //== generic lapack implementation =============================================
 55 
 56 template <typename MA>
 57 void
 58 lauu2_generic(TrMatrix<MA> &A)
 59 {
 60     typedef typename TrMatrix<MA>::ElementType   ElementType;
 61     typedef typename TrMatrix<MA>::IndexType     IndexType;
 62 
 63     const ElementType            One(1);
 64     const IndexType              n = A.dim();
 65     const Underscore<IndexType>  _;
 66 //
 67 //  Quick return if possible
 68 //
 69     if (n==0) {
 70         return;
 71     }
 72 
 73     if (A.upLo()==Upper) {
 74 //
 75 //      Compute the product U * U**T.
 76 //
 77         for (IndexType i=1; i<=n; ++i) {
 78             const ElementType a22 = A(i,i);
 79             if (i<n) {
 80                 A(i,i) = A(i,_(i,n)) * A(i,_(i,n));
 81 
 82                 const auto range1 = _(1,i-1);
 83                 const auto range2 = i;
 84                 const auto range3 = _(i+1,n);
 85 
 86                 auto a12       = A(range1,range2);
 87                 const auto A13 = A(range1,range3);
 88                 const auto a23 = A(range2,range3);
 89                 blas::mv(NoTrans, One, A13, a23, a22, a12);
 90             } else {
 91                 A(_,n) *= a22;
 92             }
 93         }
 94     } else {
 95 //
 96 //      Compute the product L**T * L.
 97 //
 98         for (IndexType i=1; i<=n; ++i) {
 99             const ElementType a22 = A(i,i);
100             if (i<n) {
101                 A(i,i) = A(_(i,n),i) * A(_(i,n),i);
102 
103                 const auto range1 = _(1,i-1);
104                 const auto range2 = i;
105                 const auto range3 = _(i+1,n);
106 
107                 auto a21       = A(range2,range1);
108                 const auto A31 = A(range3,range1);
109                 const auto a32 = A(range3,range2);
110 
111                 blas::mv(Trans, One, A31, a32, a22, a21);
112             } else {
113                 A(n,_) *= a22;
114             }
115         }
116     }
117 }
118 
119 //== interface for native lapack ===============================================
120 
121 #ifdef CHECK_CXXLAPACK
122 
123 template <typename MA>
124 void
125 lauu2_native(TrMatrix<MA> &A)
126 {
127     typedef typename TrMatrix<MA>::ElementType ElementType;
128 
129     const char     UPLO(A.upLo());
130     const INTEGER  N     = A.dim();
131     const INTEGER  LDA   = A.leadingDimension();
132     INTEGER        INFO;
133 
134     if (IsSame<ElementType,double>::value) {
135         LAPACK_DECL(dlauu2)(&UPLO,
136                             &N,
137                             A.data(),
138                             &LDA,
139                             &INFO);
140     } else {
141         ASSERT(0);
142     }
143     ASSERT(INFO==0);
144 }
145 
146 #endif // CHECK_CXXLAPACK
147 
148 //== public interface ==========================================================
149 
150 template <typename MA>
151 void
152 lauu2(TrMatrix<MA> &A)
153 {
154     typedef typename TrMatrix<MA>::IndexType    IndexType;
155 
156 //
157 //  Test the input parameters
158 //
159     ASSERT(A.firstRow()==1);
160     ASSERT(A.firstCol()==1);
161 
162 #   ifdef CHECK_CXXLAPACK
163 //
164 //  Make copies of output arguments
165 //
166     typename TrMatrix<MA>::NoView       A_org      = A;
167 #   endif
168 
169 //
170 //  Call implementation
171 //
172     lauu2_generic(A);
173 
174 #   ifdef CHECK_CXXLAPACK
175 //
176 //  Make copies of generic results
177 //
178     typename TrMatrix<MA>::NoView       A_generic      = A;
179 //
180 //  Restore output arguments
181 //
182     A = A_org;
183 
184 //
185 //  Compare results
186 //
187     lauu2_native(A);
188 
189     bool failed = false;
190     if (! isIdentical(A_generic, A, "A_generic""_A")) {
191         std::cerr << "A_org = " << A_org << std::endl;
192         std::cerr << "CXXLAPACK: A_generic = " << A_generic << std::endl;
193         std::cerr << "F77LAPACK: A = " << A << std::endl;
194         failed = true;
195     }
196 
197     if (failed) {
198         ASSERT(0);
199     }
200 
201 #   endif
202 }
203 
204 //-- forwarding ----------------------------------------------------------------
205 template <typename MA>
206 void
207 lauu2(MA &&A)
208 {
209     lauu2(A);
210 }
211 
212 } } // namespace lapack, flens
213 
214 #endif // FLENS_LAPACK_GESV_LAUU2_TCC