1
       2
       3
       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
      38
      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
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     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
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     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
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     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
/*
 *   Copyright (c) 2011, Michael Lehn
 *
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *   1) Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2) Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *   3) Neither the name of the FLENS development group nor the names of
 *      its contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Based on
      SUBROUTINE DLAHQR( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
     $                   ILOZ, IHIZ, Z, LDZ, INFO )
 *
 *  -- LAPACK auxiliary routine (version 3.2) --
 *     Univ. of Tennessee, Univ. of California Berkeley,
 *     Univ. of Colorado Denver and NAG Ltd..
 *     November 2006
 */

#ifndef FLENS_LAPACK_LA_LAHQR_TCC
#define FLENS_LAPACK_LA_LAHQR_TCC 1

#include <cmath>
#include <flens/blas/blas.h>
#include <flens/lapack/lapack.h>

namespace flens { namespace lapack {

//== generic lapack implementation =============================================

namespace generic {

template <typename IndexType, typename MH, typename VWR, typename VWI,
          typename MZ>
IndexType
lahqr_impl(bool                  wantT,
           bool                  wantZ,
           IndexType             iLo,
           IndexType             iHi,
           GeMatrix<MH>          &H,
           DenseVector<VWR>      &wr,
           DenseVector<VWI>      &wi,
           IndexType             iLoZ,
           IndexType             iHiZ,
           GeMatrix<MZ>          &Z)
{
    using std::abs;
    using std::max;
    using std::min;

    typedef typename GeMatrix<MH>::ElementType  T;

    const Underscore<IndexType>     _;
    const T                         Zero(0), One(1), Two(2);
    const T                         Dat1 = T(3)/T(4),
                                    Dat2 = T(-0.4375);
    const IndexType                 itMax = 30;
    const IndexType                 n = H.numRows();

    typedef typename GeMatrix<MH>::VectorView   VectorView;
    T           vBuffer[3];
    VectorView  v = typename VectorView::Engine(3, vBuffer);

//
//  Quick return if possible
//
    if (n==0) {
        return 0;
    }
    if (iLo==iHi) {
        wr(iLo) = H(iLo, iLo);
        wi(iLo) = Zero;
        return 0;
    }
//
//  ==== clear out the trash ====
//
    for (IndexType j=iLo; j<=iHi-3; ++j) {
        H(j+2, j) = Zero;
        H(j+3, j) = Zero;
    }
    if (iLo<=iHi-2) {
        H(iHi, iHi-2) = Zero;
    }

    const IndexType nh = iHi - iLo1;
//
//  Set machine-dependent constants for the stopping criterion.
//
    T safeMin = lamch<T>(SafeMin);
    T safeMax = One / safeMin;
    labad(safeMin, safeMax);
    const T ulp = lamch<T>(Precision);
    const T smallNum = safeMin*(T(nh)/ulp);
//
//  i1 and i2 are the indices of the first row and last column of H
//  to which transformations must be applied. If eigenvalues only are
//  being computed, i1 and i2 are set inside the main loop.
//
    IndexType i1 = -1, i2 = -1;
    if (wantT) {
        i11;
        i2 = n;
    }
//
//  The main loop begins here. Variable i is the loop index and decreases from
//  iHi to iLo in steps of 1 or 2. Each iteration of the loop works
//  with the active submatrix in rows and columns l to i.
//  Eigenvalues i+1 to iHi have already converged. Either l = iLo or
//  H(l,l-1) is negligible so that the matrix splits.
//
    IndexType i = iHi;
    while (true) {
        IndexType l = iLo;
        if (i<iLo) {
            break;
        }
//
//      Perform QR iterations on rows and columns iLo to i until a
//      submatrix of order 1 or 2 splits off at the bottom because a
//      subdiagonal element has become negligible.
//
        IndexType its;
        for (its=0; its<=itMax; ++its) {
//
//          Look for a single small subdiagonal element.
//
            IndexType k;
            for (k=i; k>=l+1; --k) {
                if (abs(H(k,k-1))<=smallNum) {
                    break;
                }
                T test = abs(H(k-1,k-1)) + abs(H(k,k));
                if (test==Zero) {
                    if (k-2>=iLo) {
                        test += abs(H(k-1,k-2));
                    }
                    if (k+1<=iHi) {
                        test += abs(H(k+1,k));
                    }
                }
//              ==== The following is a conservative small subdiagonal
//              .    deflation  criterion due to Ahues & Tisseur (LAWN 122,
//              .    1997). It has better mathematical foundation and
//              .    improves accuracy in some cases.  ====
                if (abs(H(k,k-1))<=ulp*test) {
                    const T ab = max(abs(H(k, k-1)), abs(H(k-1, k)));
                    const T ba = min(abs(H(k, k-1)), abs(H(k-1, k)));
                    const T aa = max(abs(H(k,   k)), abs(H(k-1, k-1)-H(k, k)));
                    const T bb = min(abs(H(k,   k)), abs(H(k-1, k-1)-H(k, k)));
                    const T s = aa + ab;
                    if (ba*(ab/s)<=max(smallNum, ulp*(bb*(aa/s)))) {
                        break;
                    }
                }
            }
            l = k;

            if (l>iLo) {
//
//              H(l,l-1) is negligible
//
                H(l, l-1) = Zero;
            }
//
//          Exit from loop if a submatrix of order 1 or 2 has split off.
//
            if (l>=i-1) {
                break;
            }
//
//          Now the active submatrix is in rows and columns l to i. If
//          eigenvalues only are being computed, only the active submatrix
//          need be transformed.
//
            if (!wantT) {
                i1 = l;
                i2 = i;
            }

            T H11, H12, H21, H22;
            T rt1r, rt2r, rt1i, rt2i;
            if (its==10) {
//
//              Exceptional shift.
//
                const T s = abs(H(l+1,l)) + abs(H(l+2, l+1));
                H11 = Dat1*s + H(l,l);
                H12 = Dat2*s;
                H21 = s;
                H22 = H11;
            } else if (its==20) {
//
//              Exceptional shift.
//
                const T s = abs(H(i,i-1)) + abs(H(i-1,i-2));
                H11 = Dat1*s + H(i,i);
                H12 = Dat2*s;
                H21 = s;
                H22 = H11;
            } else {
//
//              Prepare to use Francis' double shift
//              (i.e. 2nd degree generalized Rayleigh quotient)
//
                H11 = H(i-1, i-1);
                H21 = H(  i, i-1);
                H12 = H(i-1,   i);
                H22 = H(  i,   i);
            }

            const T s = abs(H11) + abs(H12) + abs(H21) + abs(H22);
            if (s==Zero) {
                rt1r = Zero;
                rt1i = Zero;
                rt2r = Zero;
                rt2i = Zero;
            } else {
                H11 /= s;
                H21 /= s;
                H12 /= s;
                H22 /= s;
                const T tr = (H11+H22) / Two;
                const T det = (H11-tr)*(H22-tr) - H12*H21;
                const T rtDisc = sqrt(abs(det));
                if (det>=Zero) {
//
//                  ==== complex conjugate shifts ====
//
                    rt1r = tr*s;
                    rt2r = rt1r;
                    rt1i = rtDisc*s;
                    rt2i = -rt1i;
                } else {
//
//                  ==== real shifts (use only one of them)  ====
//
                    rt1r = tr + rtDisc;
                    rt2r = tr - rtDisc;
                    if (abs(rt1r-H22)<=abs(rt2r-H22)) {
                        rt1r *= s;
                        rt2r = rt1r;
                    } else {
                        rt2r *= s;
                        rt1r = rt2r;
                    }
                    rt1i = Zero;
                    rt2i = Zero;
                }
            }
//
//          Look for two consecutive small subdiagonal elements.
//
            IndexType m;
            for (m=i-2; m>=l; --m) {
//              Determine the effect of starting the double-shift QR
//              iteration at row m, and see if this would make H(m,m-1)
//              negligible.  (The following uses scaling to avoid
//              overflows and most underflows.)
//
                T H21S = H(m+1,m);
                T s = abs(H(m,m)-rt2r) + abs(rt2i) + abs(H21S);

                H21S = H(m+1,m)/s;
                v(1) = H21S*H(m,m+1)
                     + (H(m,m)-rt1r)*((H(m,m)-rt2r)/s)
                     - rt1i*(rt2i/s);
                v(2) = H21S*(H(m,m) + H(m+1,m+1)-rt1r-rt2r);
                v(3) = H21S*H(m+2,m+1);

                s = abs(v(1)) + abs(v(2)) + abs(v(3));
                v(1) /= s;
                v(2) /= s;
                v(3) /= s;

                if (m==l) {
                    break;
                }

                const T value1 = abs(H(m,m-1))*(abs(v(2))+abs(v(3)));
                const T value2 = ulp*abs(v(1))
                                 *(abs(H(m-1,m-1))+abs(H(m,m))+abs(H(m+1,m+1)));
                if (value1<=value2) {
                    break;
                }
            }
//
//          Double-shift QR step
//
            for (k=m; k<=i-1; ++k) {
//
//              The first iteration of this loop determines a reflection G
//              from the vector v and applies it from left and right to H,
//              thus creating a nonzero bulge below the subdiagonal.
//
//              Each subsequent iteration determines a reflection G to
//              restore the Hessenberg form in the (k-1)th column, and thus
//              chases the bulge one step toward the bottom of the active
//              submatrix. 'nr' is the order of G.
//
                T   t1, t2, t3, v2, v3;

                IndexType nr = min(IndexType(3), i-k+1);
                if (k>m) {
                    v(_(1,nr)) = H(_(k,k+nr-1),k-1);
                }
                larfg(nr, v(1), v(_(2,nr)), t1);
                if (k>m) {
                    H(k,  k-1) = v(1);
                    H(k+1,k-1) = Zero;
                    if (k<i-1) {
                        H(k+2,k-1) = Zero;
                    }
                } else if (m>l) {
//                  ==== Use the following instead of
//                  .    H( K, K-1 ) = -H( K, K-1 ) to
//                  .    avoid a bug when v(2) and v(3)
//                  .    underflow. ====
                    H(k, k-1) *= (One-t1);
                }
                v2 = v(2);
                t2 = t1*v2;
                if (nr==3) {
                    v3 = v(3);
                    t3 = t1*v3;
//
//                  Apply G from the left to transform the rows of the matrix
//                  in columns k to i2.
//
                    for (IndexType j=k; j<=i2; ++j) {
                        const T sum = H(k,j) + v2*H(k+1,j) + v3*H(k+2,j);
                        H(k,   j) -= sum*t1;
                        H(k+1, j) -= sum*t2;
                        H(k+2, j) -= sum*t3;
                    }
//
//                  Apply G from the right to transform the columns of the
//                  matrix in rows i1 to min(k+3,i).
//
                    for (IndexType j=i1; j<=min(k+3,i); ++j) {
                        const T sum = H(j, k) + v2*H(j,k+1) + v3*H(j,k+2);
                        H(j, k  ) -= sum*t1;
                        H(j, k+1) -= sum*t2;
                        H(j, k+2) -= sum*t3;
                    }

                    if (wantZ) {
//
//                      Accumulate transformations in the matrix Z
//
                        for (IndexType j=iLoZ; j<=iHiZ; ++j) {
                            const T sum = Z(j, k) + v2*Z(j, k+1) + v3*Z(j, k+2);
                            Z(j, k  ) -= sum*t1;
                            Z(j, k+1) -= sum*t2;
                            Z(j, k+2) -= sum*t3;
                        }
                    }
                } else if (nr==2) {
//
//                  Apply G from the left to transform the rows of the matrix
//                  in columns K to I2.
//
                    for (IndexType j=k; j<=i2; ++j) {
                        const T sum = H(k, j) + v2*H(k+1, j);
                        H(k,   j) -= sum*t1;
                        H(k+1, j) -= sum*t2;
                    }
//
//                  Apply G from the right to transform the columns of the
//                  matrix in rows i1 to min(k+3,i).
//
                    for (IndexType j=i1; j<=i; ++j) {
                        const T sum = H(j, k) + v2*H(j, k+1);
                        H(j, k  ) -= sum*t1;
                        H(j, k+1) -= sum*t2;
                    }

                    if (wantZ) {
//
//                      Accumulate transformations in the matrix Z
//
                        for (IndexType j=iLoZ; j<=iHiZ; ++j) {
                            const T sum = Z(j, k) + v2*Z(j, k+1);
                            Z(j, k  ) -= sum*t1;
                            Z(j, k+1) -= sum*t2;
                        }
                    }
                }
            }
        }
//
//      Failure to converge in remaining number of iterations
//
        if (its>itMax) {
            return i;
        }

        if (l==i) {
//
//          H(I,I-1) is negligible: one eigenvalue has converged.
//
            wr(i) = H(i, i);
            wi(i) = Zero;
        } else if (l==i-1) {
//
//          H(I-1,I-2) is negligible: a pair of eigenvalues have converged.
//
//          Transform the 2-by-2 submatrix to standard Schur form,
//          and compute and store the eigenvalues.
//
            T cs, sn;
            lanv2(H(i-1,i-1), H(i-1,i), H(i,i-1), H(i,i),
                  wr(i-1), wi(i-1), wr(i), wi(i),
                  cs, sn);

            if (wantT) {
//
//              Apply the transformation to the rest of H.
//
                if (i2>i) {
                    const auto cols = _(i+1,i2);
                    blas::rot(H(i-1,cols), H(i,cols), cs, sn);
                }
                const auto rows = _(i1,i-2);
                blas::rot(H(rows, i-1), H(rows, i), cs, sn);
            }
            if (wantZ) {
//
//              Apply the transformation to Z.
//
                const auto rows = _(iLoZ, iHiZ);
                blas::rot(Z(rows, i-1), Z(rows, i), cs, sn);
            }
        }
//
//      return to start of the main loop with new value of I.
//
        i = l1;
    }
    return 0;
}

// namespace generic

//== interface for native lapack ===============================================

#ifdef USE_CXXLAPACK

namespace external {

template <typename IndexType, typename MH, typename VWR, typename VWI,
          typename MZ>
IndexType
lahqr_impl(bool                   wantT,
           bool                   wantZ,
           IndexType              iLo,
           IndexType              iHi,
           GeMatrix<MH>           &H,
           DenseVector<VWR>       &wr,
           DenseVector<VWI>       &wi,
           IndexType              iLoZ,
           IndexType              iHiZ,
           GeMatrix<MZ>           &Z)
{
    IndexType  info;
    info = cxxlapack::lahqr<IndexType>(wantT,
                                       wantZ,
                                       H.numRows(),
                                       iLo,
                                       iHi,
                                       H.data(),
                                       H.leadingDimension(),
                                       wr.data(),
                                       wi.data(),
                                       iLoZ,
                                       iHiZ,
                                       Z.data(),
                                       Z.leadingDimension());
    return info;
}

// namespace external

#endif // USE_CXXLAPACK

//== public interface ==========================================================

template <typename IndexType, typename MH, typename VWR, typename VWI,
          typename MZ>
IndexType
lahqr(bool                  wantT,
      bool                  wantZ,
      IndexType             iLo,
      IndexType             iHi,
      GeMatrix<MH>          &H,
      DenseVector<VWR>      &wr,
      DenseVector<VWI>      &wi,
      IndexType             iLoZ,
      IndexType             iHiZ,
      GeMatrix<MZ>          &Z)
{
    LAPACK_DEBUG_OUT("lahqr");

//
//  Test the input parameters
//
    using std::max;

    ASSERT(H.firstRow()==1);
    ASSERT(H.firstCol()==1);
    ASSERT(H.numRows()==H.numCols());
    ASSERT(wr.firstIndex()==1);
    ASSERT(wr.length()==H.numRows());
    ASSERT(wi.firstIndex()==1);
    ASSERT(wi.length()==H.numRows());
    ASSERT(wantZ || (Z.numRows()==H.numCols()));
    ASSERT(wantZ || (Z.numCols()==H.numCols()));

    // 1 <= ILO <= max(1,IHI); IHI <= N.
    ASSERT(1<=iLo);
    ASSERT(iLo<=max(IndexType(1), iHi));
    ASSERT(iHi<=H.numRows());

    // 1 <= ILOZ <= ILO; IHI <= IHIZ <= N.
    ASSERT(1<=iLoZ);
    ASSERT(iLoZ<=iLo);
    ASSERT(iHi<=iHiZ);
    ASSERT(iHiZ<=H.numRows());

//
//  Make copies of output arguments
//
#   ifdef CHECK_CXXLAPACK
    typename GeMatrix<MH>::NoView          H_org    = H;
    typename GeMatrix<MH>::NoView          Z_org    = Z;

    typename GeMatrix<MH>::NoView          _H       = H;
    typename DenseVector<VWR>::NoView      _wr      = wr;
    typename DenseVector<VWI>::NoView      _wi      = wi;
    typename GeMatrix<MZ>::NoView          _Z       = Z;
#   endif

//
//  Call implementation
//
    IndexType info = LAPACK_SELECT::lahqr_impl(wantT, wantZ, iLo,  iHi,
                                               H, wr, wi, iLoZ, iHiZ, Z);

//
//  Compare results
//
#   ifdef CHECK_CXXLAPACK
    IndexType _info = external::lahqr_impl(wantT, wantZ, iLo,  iHi,
                                           _H, _wr, _wi, iLoZ, iHiZ, _Z);

    bool failed = false;
    if (! isIdentical(H, _H, " H""_H")) {
        std::cerr << "CXXLAPACK:  H = " << H << std::endl;
        std::cerr << "F77LAPACK: _H = " << _H << std::endl;
        failed = true;
    }

    if (! isIdentical(wr, _wr, " wr""_wr")) {
        std::cerr << "CXXLAPACK:  wr = " << wr << std::endl;
        std::cerr << "F77LAPACK: _wr = " << _wr << std::endl;
        failed = true;
    }

    if (! isIdentical(wi, _wi, " wi""_wi")) {
        std::cerr << "CXXLAPACK:  wi = " << wi << std::endl;
        std::cerr << "F77LAPACK: _wi = " << _wi << std::endl;
        failed = true;
    }

    if (! isIdentical(Z, _Z, " Z""_Z")) {
        std::cerr << "CXXLAPACK:  Z = " << Z << std::endl;
        std::cerr << "F77LAPACK: _Z = " << _Z << std::endl;
        failed = true;
    }

    if (! isIdentical(info, _info, " info""_info")) {
        std::cerr << "CXXLAPACK:  info = " << info << std::endl;
        std::cerr << "F77LAPACK: _info = " << _info << std::endl;
        failed = true;
    }

    if (failed) {
        std::cerr << "H_org = " << H_org << std::endl;
        std::cerr << "Z_org = " << Z_org << std::endl;
        std::cerr << "wantT = " << wantT
                  << ", wantZ = " << wantZ
                  << ", iLo = " << iLo
                  << ", iHi = " << iHi
                  << std::endl;
        std::cerr << "error in: lahqr.tcc" << std::endl;
        ASSERT(0);
    } else {
//        std::cerr << "passed: lahqr.tcc" << std::endl;
    }
#   endif
    return info;
}

//-- forwarding ----------------------------------------------------------------
template <typename IndexType, typename MH, typename VWR, typename VWI,
          typename MZ>
IndexType
lahqr(bool          wantT,
      bool          wantZ,
      IndexType     iLo,
      IndexType     iHi,
      MH            &&H,
      VWR           &&wr,
      VWI           &&wi,
      IndexType     iLoZ,
      IndexType     iHiZ,
      MZ            &&Z)
{
    return lahqr(wantT, wantZ, iLo, iHi, H, wr, wi, iLoZ, iHiZ, Z);
}

} } // namespace lapack, flens

#endif // FLENS_LAPACK_LA_LAHQR_TCC