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
      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
      677
      678
      679
      680
      681
      682
      683
      684
      685
      686
      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
      761
      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
      853
      854
      855
      856
      857
      858
      859
      860
      861
      862
      863
      864
      865
      866
      867
      868
      869
      870
      871
      872
      873
      874
      875
      876
      877
      878
      879
      880
      881
      882
      883
      884
      885
      886
      887
      888
      889
      890
      891
      892
      893
      894
      895
      896
      897
      898
      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
      986
      987
      988
      989
      990
      991
      992
      993
      994
      995
      996
      997
      998
      999
     1000
     1001
     1002
     1003
     1004
     1005
     1006
     1007
     1008
     1009
     1010
     1011
     1012
     1013
     1014
     1015
     1016
     1017
     1018
     1019
     1020
     1021
     1022
     1023
     1024
     1025
     1026
     1027
     1028
     1029
     1030
     1031
     1032
     1033
     1034
     1035
     1036
     1037
     1038
     1039
     1040
     1041
     1042
     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
     1069
     1070
     1071
     1072
     1073
     1074
     1075
     1076
     1077
     1078
     1079
     1080
     1081
     1082
     1083
     1084
     1085
     1086
     1087
     1088
     1089
     1090
     1091
     1092
     1093
     1094
     1095
     1096
     1097
     1098
     1099
     1100
     1101
     1102
     1103
     1104
     1105
     1106
     1107
     1108
     1109
     1110
     1111
     1112
     1113
     1114
     1115
     1116
     1117
     1118
     1119
     1120
     1121
     1122
     1123
     1124
     1125
     1126
     1127
     1128
     1129
     1130
     1131
/*
 *   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 DTREVC( SIDE, HOWMNY, SELECT, N, T, LDT, VL, LDVL, VR,
      $                   LDVR, MM, M, WORK, INFO )
 *
 *  -- LAPACK routine (version 3.3.1) --
 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
 *  -- April 2011                                                      --
 */

#ifndef FLENS_LAPACK_IMPL_TREVC_TCC
#define FLENS_LAPACK_IMPL_TREVC_TCC 1

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

namespace flens { namespace lapack {

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

namespace generic {

template <typename VSELECT, typename MT, typename MVL, typename MVR,
          typename IndexType, typename VWORK>
void
trevc_impl(bool                          computeVL,
           bool                          computeVR,
           TREVC::Job                    howMany,
           DenseVector<VSELECT>          &select,
           const GeMatrix<MT>            &T,
           GeMatrix<MVL>                 &VL,
           GeMatrix<MVR>                 &VR,
           IndexType                     mm,
           IndexType                     &m,
           DenseVector<VWORK>            &work)
{

    using std::abs;
    using flens::max;

    typedef typename GeMatrix<MT>::ElementType  ElementType;

    // TODO: define a colmajor GeMatrixView
    typedef typename GeMatrix<MT>::View         GeMatrixView;

    const ElementType   Zero(0), One(1);

    const Underscore<IndexType> _;
    const IndexType n = T.numCols();

//
//    .. Local Arrays ..
//
    ElementType  _xData[4];
    GeMatrixView X = typename GeMatrixView::Engine(22, _xData2);

    GeMatrixView Work(n3, work, n);

//
//  Decode and test the input parameters
//
    const bool over     = howMany==TREVC::Backtransform;
    const bool someV    = howMany==TREVC::Selected;

//
//  Set M to the number of columns required to store the selected
//  eigenvectors, standardize the array SELECT if necessary, and
//  test MM.
//
    if (someV) {
        m0;
        bool pair = false;
        for (IndexType j=1; j<=n; ++j) {
            if (pair) {
                pair        = false;
                select(j)   = false;
            } else {
                if (j<n) {
                    if (T(j+1,j)==Zero) {
                        if (select(j)) {
                            ++m;
                        }
                    } else {
                        pairtrue;
                        if (select(j) || select(j+1)) {
                            select(j) = true;
                            m += 2;
                        }
                    }
                } else {
                    if (select(n)) {
                        ++m;
                    }
                }
            }
        }
    } else {
        m = n;
    }

    if (mm<m) {
        ASSERT(0);
    }
//
//   Quick return if possible.
//
    if (n==0) {
        return;
    }
//
//  Set the constants to control overflow.
//
    ElementType underflow = lamch<ElementType>(SafeMin);
    ElementType overflow  = One / underflow;
    labad(underflow, overflow);

    const ElementType ulp         = lamch<ElementType>(Precision);
    const ElementType smallNum    = underflow*(n/ulp);
    const ElementType bigNum      = (One-ulp)/smallNum;
//
//  Compute 1-norm of each column of strictly upper triangular
//  part of T to control overflow in triangular solver.
//
    work(1) = Zero;
    for (IndexType j=2; j<=n; ++j) {
        work(j) = Zero;
        for (IndexType i=1; i<=j-1; ++i) {
            work(j) += abs(T(i,j));
        }
    }
//
//  Index IP is used to specify the real or complex eigenvalue:
//    IP = 0, real eigenvalue,
//         1, first of conjugate complex pair: (wr,wi)
//        -1, second of conjugate complex pair: (wr,wi)
//
    const IndexType n2 = 2*n;

    ElementType scale, xNorm, wr, wi;

    if (computeVR) {
//
//      Compute right eigenvectors.
//

        IndexType ip = 0;
        IndexType is = m;
        for (IndexType ki=n; ki>=1; --ki) {

            if (ip==1) {
                ip0;
                continue;
            }
            if (ki!=1 && T(ki,ki-1)!=Zero) {
                ip = -1;
            }

            if (someV) {
                if (ip==0) {
                    if (!select(ki)) {
                        if (ip==1) {
                            ip0;
                        }
                        if (ip==-1) {
                            ip1;
                        }
                        continue;
                    }
                } else {
                    if (!select(ki-1)) {
                        if (ip==1) {
                            ip0;
                        }
                        if (ip==-1) {
                            ip1;
                        }
                        continue;
                    }
                }
            }
//
//          Compute the KI-th eigenvalue (WR,WI).
//
            wr = T(ki,ki);
            wi = Zero;
            if (ip!=0) {
                wi = sqrt(abs(T(ki,ki-1)))*sqrt(abs(T(ki-1,ki)));
            }
            const ElementType safeMin = max(ulp*(abs(wr)+abs(wi)), smallNum);

            if (ip==0) {
//
//              Real right eigenvector
//
                work(ki+n) = One;
//
//              Form right-hand side
//              TODO: do this by work(_(...)) = -T(_(...),ki)
//
                for (IndexType k=1; k<=ki-1; ++k) {
                    work(k+n) = -T(k,ki);
                }
//
//              Solve the upper quasi-triangular system:
//                 (T(1:KI-1,1:KI-1) - WR)*X = SCALE*WORK.
//
                IndexType jNext = ki1;
                for (IndexType j=ki-1; j>=1; --j) {
                    if (j>jNext) {
                        continue;
                    }
                    IndexType j1 = j;
                    IndexType j2 = j;
                    jNext = j1;
                    if (j>1) {
                        if (T(j,j-1)!=Zero) {
                            j1 = j1;
                            jNext = j2;
                        }
                    }

                    if (j1==j2) {
//
//                      1-by-1 diagonal block
//
                        laln2(false,
                              IndexType(1),
                              safeMin,
                              One,
                              T(_(j,j),_(j,j)),
                              One,
                              One,
                              Work(_(j,j), _(2,2)),
                              wr,
                              Zero,
                              X(_(1,1),_(1,1)),
                              scale,
                              xNorm);
//
//                      Scale X(1,1) to avoid overflow when updating
//                      the right-hand side.
//
                        if (xNorm>One) {
                            if (work(j)>bigNum/xNorm) {
                                X(1,1)  /= xNorm;
                                scale   /= xNorm;
                            }
                        }
//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(1+n,ki+n)) *= scale;
                        }
                        work(j+n) = X(1,1);
//
//                      Update right-hand side
//
                        work(_(1+n,j-1+n)) -= X(1,1)*T(_(1,j-1),j);

                    } else {
//
//                      2-by-2 diagonal block
//
                        laln2(false,
                              IndexType(1),
                              safeMin,
                              One,
                              T(_(j-1,j),_(j-1,j)),
                              One,
                              One,
                              Work(_(j-1,j), _(2,2)),
                              wr,
                              Zero,
                              X(_(1,2),_(1,1)),
                              scale,
                              xNorm);

//
//                      Scale X(1,1) and X(2,1) to avoid overflow when
//                      updating the right-hand side.
//
                        if (xNorm>One) {
                            const ElementType beta = max(work(j-1), work(j));
                            if (beta>bigNum/xNorm) {
                                X(1,1) /= xNorm;
                                X(2,1) /= xNorm;
                                scale  /= xNorm;
                            }
                        }
//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(1+n,ki+n)) *= scale;
                        }
                        work(j-1+n) = X(1,1);
                        work(j+n)   = X(2,1);
//
//                      Update right-hand side
//
                        work(_(1+n,j-2+n)) -= X(1,1)*T(_(1,j-2),j-1);
                        work(_(1+n,j-2+n)) -= X(2,1)*T(_(1,j-2),j);
                    }
                }
//
//              Copy the vector x or Q*x to VR and normalize.
//
                if (!over) {
                    VR(_(1,ki),is) = work(_(1+n,ki+n));

                    const IndexType ii = blas::iamax(VR(_(1,ki),is));
                    const ElementType reMax = One / abs(VR(ii,is));
                    VR(_(1,ki),is) *= reMax;

                    VR(_(ki+1,n),is) = Zero;
                } else {
                    if (ki>1) {
                        blas::mv(NoTrans, One,
                                 VR(_,_(1,ki-1)),
                                 work(_(1+n,n+ki-1)),
                                 work(ki+n),
                                 VR(_,ki));
                    }
                    const IndexType ii = blas::iamax(VR(_,ki));
                    const ElementType reMax = One / abs(VR(ii,ki));
                    VR(_,ki) *= reMax;
                }

            } else {
//
//              Complex right eigenvector.
//
//              Initial solve
//                [ (T(KI-1,KI-1) T(KI-1,KI) ) - (WR + I* WI)]*X = 0.
//                [ (T(KI,KI-1)   T(KI,KI)   )               ]
//
                if (abs(T(ki-1,ki))>=abs(T(ki,ki-1))) {
                    work(ki-1+n)  = One;
                    work(ki+n2)   = wi / T(ki-1,ki);
                } else {
                    work(ki-1+n ) = -wi / T(ki,ki-1);
                    work(ki+n2 )  = One;
                }
                work(ki+n) = Zero;
                work(ki-1+n2) = Zero;
//
//              Form right-hand side
//
                for (IndexType k=1; k<=ki-2; ++k) {
                    work(k+n)   = -work(ki-1+n) * T(k,ki-1);
                    work(k+n2)  = -work(ki+n2)  * T(k,ki);
                }
//
//              Solve upper quasi-triangular system:
//              (T(1:KI-2,1:KI-2) - (WR+i*WI))*X = SCALE*(WORK+i*WORK2)
//
                IndexType jNext = ki2;
                for (IndexType j=ki-2; j>=1; --j) {
                    if (j>jNext) {
                        continue;
                    }
                    IndexType j1 = j;
                    IndexType j2 = j;
                    jNext = j1;
                    if (j>1) {
                        if (T(j,j-1)!=Zero) {
                            j1 = j1;
                            jNext = j2;
                        }
                    }

                    if (j1==j2) {
//
//                      1-by-1 diagonal block
//
                        laln2(false,
                              IndexType(2),
                              safeMin,
                              One,
                              T(_(j,j),_(j,j)),
                              One,
                              One,
                              Work(_(j,j), _(2,3)),
                              wr,
                              wi,
                              X(_(1,1),_(1,2)),
                              scale,
                              xNorm);
//
//                      Scale X(1,1) and X(1,2) to avoid overflow when
//                      updating the right-hand side.
//
                        if (xNorm>One) {
                            if (work(j)>bigNum/xNorm) {
                                X(1,1) /= xNorm;
                                X(1,2) /= xNorm;
                                scale /= xNorm;
                            }
                        }
//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(1+n,ki+n))   *= scale;
                            work(_(1+n2,ki+n2)) *= scale;
                        }
                        work(j+n)   = X(1,1);
                        work(j+n2)  = X(1,2);
//
//                      Update the right-hand side
//
                        work(_(1+n,j-1+n))   -= X(1,1)*T(_(1,j-1),j);
                        work(_(1+n2,j-1+n2)) -= X(1,2)*T(_(1,j-1),j);

                    } else {
//
//                      2-by-2 diagonal block
//
                        laln2(false,
                              IndexType(2),
                              safeMin,
                              One,
                              T(_(j-1,j),_(j-1,j)),
                              One,
                              One,
                              Work(_(j-1,j), _(2,3)),
                              wr,
                              wi,
                              X(_(1,2),_(1,2)),
                              scale,
                              xNorm);

//
//                      Scale X to avoid overflow when updating
//                      the right-hand side.
//
                        if (xNorm>One) {
                            const ElementType beta = max(work(j-1), work(j));
                            if (beta>bigNum/xNorm) {
                                const ElementType rec = One / xNorm;
                                X(1,1) *= rec;
                                X(1,2) *= rec;
                                X(2,1) *= rec;
                                X(2,2) *= rec;
                                scale *= rec;
                            }
                        }
//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(1+n,ki+n))   *= scale;
                            work(_(1+n2,ki+n2)) *= scale;
                        }
                        work(j-1+n)  = X(1,1);
                        work(j+n)    = X(2,1);
                        work(j-1+n2) = X(1,2);
                        work(j+n2)   = X(2,2);
//
//                      Update the right-hand side
//
                        work(_(1+n,j-2+n)) -= X(1,1)*T(_(1,j-2),j-1);
                        work(_(1+n,j-2+n)) -= X(2,1)*T(_(1,j-2),j);

                        work(_(1+n2,j-2+n2)) -= X(1,2)*T(_(1,j-2),j-1);
                        work(_(1+n2,j-2+n2)) -= X(2,2)*T(_(1,j-2),j);
                    }
                }
//
//              Copy the vector x or Q*x to VR and normalize.
//
                if (!over) {
                    VR(_(1,ki),is-1) = work(_(1+n,ki+n));
                    VR(_(1,ki),is)   = work(_(1+n2,ki+n2));

                    ElementType eMax = Zero;
                    for (IndexType k=1; k<=ki; ++k) {
                        eMax = max(eMax, abs(VR(k,is-1))+abs(VR(k,is)));
                    }

                    const ElementType reMax = One / eMax;
                    VR(_(1,ki),is-1)  *= reMax;
                    VR(_(1,ki),is)    *= reMax;

                    VR(_(ki+1,n),is-1)  = Zero;
                    VR(_(ki+1,n),is)    = Zero;

                } else {

                    if (ki>2) {
                        blas::mv(NoTrans, One,
                                 VR(_,_(1,ki-2)),
                                 work(_(1+n,ki-2+n)),
                                 work(ki-1+n),
                                 VR(_,ki-1));
                        blas::mv(NoTrans, One,
                                 VR(_,_(1,ki-2)),
                                 work(_(n2+1,n2+ki-2)),
                                 work(ki+n2),
                                 VR(_,ki));
                    } else {
                        VR(_,ki-1)  *= work(ki-1+n);
                        VR(_,ki)    *= work(ki+n2);
                    }

                    ElementType eMax = Zero;
                    for (IndexType k=1; k<=n; ++k) {
                        eMax = max(eMax, abs(VR(k,ki-1)) + abs(VR(k,ki)));
                    }
                    const ElementType reMax = One / eMax;
                    VR(_,ki-1)  *= reMax;
                    VR(_,ki)    *= reMax;
                }
            }

            --is;
            if (ip!=0) {
                --is;
            }
            if (ip==1) {
                ip0;
            }
            if (ip==-1) {
                ip1;
            }
        }
    }

    if (computeVL) {
//
//      Compute left eigenvectors.
//
        IndexType ip = 0;
        IndexType is = 1;
        for (IndexType ki=1; ki<=n; ++ki) {

            if (ip==-1) {
                ip0;
                continue;
            }
            if (ki!=n && T(ki+1,ki)!=Zero) {
                ip1;
            }

            if (someV) {
                if (!select(ki)) {
                    if (ip==-1) {
                        ip0;
                    }
                    if (ip==1) {
                        ip = -1;
                    }
                    continue;
                }
            }
//
//          Compute the KI-th eigenvalue (WR,WI).
//
            ElementType wr = T(ki,ki);
            ElementType wi = Zero;
            if (ip!=0) {
                wi = sqrt(abs(T(ki,ki+1))) * sqrt(abs(T(ki+1,ki)));
            }
            const ElementType safeMin = max(ulp*(abs(wr)+abs(wi)), smallNum);

            if (ip==0) {
//
//              Real left eigenvector.
//
                work(ki+n) = One;
//
//              Form right-hand side
//
                for (IndexType k=ki+1; k<=n; ++k) {
                    work(k+n) = -T(ki,k);
                }
//
//              Solve the quasi-triangular system:
//                 (T(KI+1:N,KI+1:N) - WR)**T*X = SCALE*WORK
//
                ElementType vMax = One;
                ElementType vCrit = bigNum;

                IndexType jNext = ki1;
                for (IndexType j=ki+1; j<=n; ++j) {
                    if (j<jNext) {
                        continue;
                    }
                    IndexType j1 = j;
                    IndexType j2 = j;
                    jNext = j1;
                    if (j<n) {
                        if (T(j+1,j)!=Zero) {
                            j2 = j1;
                            jNext = j2;
                        }
                    }

                    if (j1==j2) {
//
//                      1-by-1 diagonal block
//
//                      Scale if necessary to avoid overflow when forming
//                      the right-hand side.
//
                        if (work(j)>vCrit) {
                            const ElementType rec = One / vMax;
                            work(_(ki+n,n2)) *= rec;
                            vMax = One;
                            vCrit = bigNum;
                        }

                        work(j+n) -= T(_(ki+1,j-1),j)*work(_(ki+1+n,n+j-1));
//
//                      Solve (T(J,J)-WR)**T*X = WORK
//
                        laln2(false,
                              IndexType(1),
                              safeMin,
                              One,
                              T(_(j,j),_(j,j)),
                              One,
                              One,
                              Work(_(j,j), _(2,2)),
                              wr,
                              Zero,
                              X(_(1,1),_(1,1)),
                              scale,
                              xNorm);

//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(ki+n,n2)) *= scale;
                        }
                        work(j+n) = X(1,1);
                        vMax = max(abs(work(j+n)), vMax);
                        vCrit = bigNum / vMax;

                    } else {
//
//                      2-by-2 diagonal block
//
//                      Scale if necessary to avoid overflow when forming
//                      the right-hand side.
//
                        const ElementType beta = max(work(j), work(j+1));
                        if (beta>vCrit) {
                            const ElementType rec = One / vMax;
                            work(_(ki+n,n2)) *= rec;
                            vMax = One;
                            vCrit = bigNum;
                        }

                        work(j+n)   -= T(_(ki+1,j-1),j)*work(_(ki+1+n,n+j-1));
                        work(j+1+n) -= T(_(ki+1,j-1),j+1)*work(_(ki+1+n,n+j-1));
//
//                      Solve
//                       [T(J,J)-WR   T(J,J+1)     ]**T * X = SCALE*( WORK1 )
//                       [T(J+1,J)    T(J+1,J+1)-WR]                ( WORK2 )
//
                        laln2(true,
                              IndexType(1),
                              safeMin,
                              One,
                              T(_(j,j+1),_(j,j+1)),
                              One,
                              One,
                              Work(_(j,j+1), _(2,2)),
                              wr,
                              Zero,
                              X(_(1,2),_(1,1)),
                              scale,
                              xNorm);

//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(ki+n,n2)) *= scale;
                        }
                        work(j+n)   = X(1,1);
                        work(j+1+n) = X(2,1);

                        vMax = max(abs(work(j+n)), abs(work(j+1+n)), vMax);
                        vCrit = bigNum / vMax;
                    }
                }
//
//              Copy the vector x or Q*x to VL and normalize.
//
                if (!over) {
                    VL(_(ki,n),is) = work(_(ki+n,n2));

                    const IndexType ii = blas::iamax(VL(_(ki,n),is)) + ki1;
                    const ElementType reMax = One / abs(VL(ii,is));
                    VL(_(ki,n),is) *= reMax;

                    VL(_(1,ki-1),is) = Zero;

                } else {

                    if (ki<n) {
                        blas::mv(NoTrans, One,
                                 VL(_,_(ki+1,n)),
                                 work(_(ki+1+n,n2)),
                                 work(ki+n),
                                 VL(_,ki));
                    }

                    const IndexType ii = blas::iamax(VL(_,ki));
                    const ElementType reMax = One / abs(VL(ii,ki));
                    VL(_,ki) *= reMax;

                }

            } else {
//
//              Complex left eigenvector.
//
//              Initial solve:
//                ((T(KI,KI)    T(KI,KI+1) )**T - (WR - I* WI))*X = 0.
//                ((T(KI+1,KI) T(KI+1,KI+1))                )
//
                if (abs(T(ki,ki+1))>=abs(T(ki+1,ki))) {
                    work(ki+n)    = wi / T(ki,ki+1);
                    work(ki+1+n2) = One;
                } else {
                    work(ki+n)    = One;
                    work(ki+1+n2) = -wi / T(ki+1,ki);
                }
                work(ki+1+n) = Zero;
                work(ki+n2)  = Zero;
//
//              Form right-hand side
//
                for (IndexType k=ki+2; k<=n; ++k) {
                    work(k+n)   = -work(ki+n)*T(ki,k);
                    work(k+n2)  = -work(ki+1+n2)*T(ki+1,k);
                }
//
//              Solve complex quasi-triangular system:
//              ( T(KI+2,N:KI+2,N) - (WR-i*WI) )*X = WORK1+i*WORK2
//
                ElementType vMax = One;
                ElementType vCrit = bigNum;

                IndexType jNext = ki2;
                for (IndexType j=ki+2; j<=n; ++j) {
                    if (j<jNext) {
                        continue;
                    }
                    IndexType j1 = j;
                    IndexType j2 = j;
                    jNext = j1;
                    if (j<n) {
                        if (T(j+1,j)!=Zero) {
                            j2 = j1;
                            jNext = j2;
                        }
                    }

                    if (j1==j2) {
//
//                      1-by-1 diagonal block
//
//                      Scale if necessary to avoid overflow when
//                      forming the right-hand side elements.
//
                        if (work(j)>vCrit) {
                            const ElementType rec = One / vMax;
                            work(_(ki+n,n2))    *= rec;
                            work(_(ki+n2,n2+n)) *= rec;
                            vMax = One;
                            vCrit = bigNum;
                        }

                        work(j+n)  -= T(_(ki+2,j-1),j)*work(_(ki+2+n,n+j-1));
                        work(j+n2) -= T(_(ki+2,j-1),j)*work(_(ki+2+n2,n2+j-1));
//
//                      Solve (T(J,J)-(WR-i*WI))*(X11+i*X12)= WK+I*WK2
//
                        laln2(true,
                              IndexType(2),
                              safeMin,
                              One,
                              T(_(j,j),_(j,j)),
                              One,
                              One,
                              Work(_(j,j), _(2,3)),
                              wr,
                              -wi,
                              X(_(1,1),_(1,2)),
                              scale,
                              xNorm);

//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(ki+n,n2))    *= scale;
                            work(_(ki+n2,n2+n)) *= scale;
                        }
                        work(j+n) =  X(1,1);
                        work(j+n2) = X(1,2);
                        vMax = max(abs(work(j+n)), abs(work(j+n2)), vMax);
                        vCrit = bigNum / vMax;

                    } else {
//
//                      2-by-2 diagonal block
//
//                      Scale if necessary to avoid overflow when forming
//                      the right-hand side elements.
//
                        const ElementType beta = max(work(j), work(j+1));
                        if (beta>vCrit) {
                            const ElementType rec = One / vMax;
                            work(_(ki+n,n2))    *= rec;
                            work(_(ki+n2,n2+n)) *= rec;
                            vMax = One;
                            vCrit = bigNum;
                        }

                        const auto _work1 = work(_(ki+2+n,n+j-1));
                        const auto _work2 = work(_(ki+2+n2,n2+j-1));

                        work(j+n)   -= T(_(ki+2,j-1),j)   * _work1;
                        work(j+n2)  -= T(_(ki+2,j-1),j)   * _work2;
                        work(j+1+n) -= T(_(ki+2,j-1),j+1) * _work1;
                        work(j+1+n2)-= T(_(ki+2,j-1),j+1) * _work2;
//
//                      Solve 2-by-2 complex linear equation
//                      ([T(j,j)   T(j,j+1)  ]**T-(wr-i*wi)*I)*X = SCALE*B
//                      ([T(j+1,j) T(j+1,j+1)]               )
//
                        laln2(true,
                              IndexType(2),
                              safeMin,
                              One,
                              T(_(j,j+1),_(j,j+1)),
                              One,
                              One,
                              Work(_(j,j+1), _(2,3)),
                              wr,
                              -wi,
                              X(_(1,2),_(1,2)),
                              scale,
                              xNorm);
//
//                      Scale if necessary
//
                        if (scale!=One) {
                            work(_(ki+n,n2))    *= scale;
                            work(_(ki+n2,n2+n)) *= scale;
                        }
                        work(j+n)    = X(1,1);
                        work(j+n2)   = X(1,2);
                        work(j+1+n)  = X(2,1);
                        work(j+1+n2) = X(2,2);
                        vMax = max(abs(X(1,1)), abs(X(1,2)),
                                   abs(X(2,1)), abs(X(2,2)),
                                   vMax);
                        vCrit = bigNum / vMax;

                    }
                }
//
//              Copy the vector x or Q*x to VL and normalize.
//
                if (!over) {
                    VL(_(ki,n),is)   = work(_(ki+n,n2));
                    VL(_(ki,n),is+1) = work(_(ki+n2,n2+n));

                    ElementType eMax = Zero;
                    for (IndexType k=ki; k<=n; ++k) {
                        eMax = max(eMax, abs(VL(k,is)) + abs(VL(k,is+1)));
                    }
                    const ElementType reMax = One / eMax;
                    VL(_(ki,n),is)   *= reMax;
                    VL(_(ki,n),is+1) *= reMax;

                    VL(_(1,ki-1),is)   = Zero;
                    VL(_(1,ki-1),is+1) = Zero;
                } else {
                    if (ki<n-1) {
                        blas::mv(NoTrans, One,
                                 VL(_,_(ki+2,n)),
                                 work(_(ki+2+n,n2)),
                                 work(ki+n),
                                 VL(_,ki));
                        blas::mv(NoTrans, One,
                                 VL(_,_(ki+2,n)),
                                 work(_(ki+2+n2,n2+n)),
                                 work(ki+1+n2),
                                 VL(_,ki+1));
                    } else {
                        VL(_,ki)    *= work(ki+n);
                        VL(_,ki+1)  *= work(ki+1+n2);
                    }

                    ElementType eMax = Zero;
                    for (IndexType k=1; k<=n; ++k) {
                        eMax = max(eMax, abs(VL(k,ki)) + abs(VL(k,ki+1)));
                    }
                    const ElementType reMax = One / eMax;
                    VL(_,ki)   *= reMax;
                    VL(_,ki+1) *= reMax;
                }
            }

            ++is;
            if (ip!=0) {
                ++is;
            }
            if (ip==-1) {
                ip0;
            }
            if (ip==1) {
                ip = -1;
            }
        }
    }
}

// namespace generic

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

#ifdef USE_CXXLAPACK

namespace external {

template <typename VSELECT, typename MT, typename MVL, typename MVR,
          typename IndexType, typename VWORK>
void
trevc_impl(bool                           computeVL,
           bool                           computeVR,
           TREVC::Job                     howMany,
           DenseVector<VSELECT>           &select,
           const GeMatrix<MT>             &T,
           GeMatrix<MVL>                  &VL,
           GeMatrix<MVR>                  &VR,
           IndexType                      mm,
           IndexType                      &m,
           DenseVector<VWORK>             &work)
{
    typedef typename GeMatrix<MT>::ElementType     ElementType;

    char side = 'N';
    if (computeVL && computeVR) {
        side'B';
    } else if (computeVL) {
        side'L';
    } else if (computeVR) {
        side'R';
    } else {
        ASSERT(0);
    }

    DenseVector<Array<IndexType> > _select = select;

    cxxlapack::trevc<IndexType>(side,
                                getF77Char(howMany),
                                _select.data(),
                                T.numRows(),
                                T.data(),
                                T.leadingDimension(),
                                VL.data(),
                                VL.leadingDimension(),
                                VR.data(),
                                VR.leadingDimension(),
                                mm,
                                m,
                                work.data());
}

// namespace external

#endif // USE_CXXLAPACK

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

template <typename VSELECT, typename MT, typename MVL, typename MVR,
          typename IndexType, typename VWORK>
void
trevc(bool                          computeVL,
      bool                          computeVR,
      TREVC::Job                    howMany,
      DenseVector<VSELECT>          &select,
      const GeMatrix<MT>            &T,
      GeMatrix<MVL>                 &VL,
      GeMatrix<MVR>                 &VR,
      IndexType                     mm,
      IndexType                     &m,
      DenseVector<VWORK>            &work)
{
    LAPACK_DEBUG_OUT("BEGIN: trevc");

    typedef typename GeMatrix<MT>::ElementType ElementType;
//
//  Test the input parameters
//
    ASSERT(T.firstRow()==1);
    ASSERT(T.firstCol()==1);
    ASSERT(T.numRows()==T.numCols());
    ASSERT(VL.firstRow()==1);
    ASSERT(VL.firstCol()==1);
    ASSERT(VR.firstRow()==1);
    ASSERT(VR.firstCol()==1);
#   ifdef CHECK_CXXLAPACK
//
//  Make copies of output arguments
//
    typename DenseVector<VSELECT>::NoView   select_org = select;
    typename GeMatrix<MVL>::NoView          VL_org     = VL;
    typename GeMatrix<MVR>::NoView          VR_org     = VR;
    typename DenseVector<VWORK>::NoView     work_org   = work;
#   endif

//
//  Call implementation
//
    LAPACK_SELECT::trevc_impl(computeVL, computeVR, howMany, select,
                              T, VL, VR, mm, m, work);

#   ifdef CHECK_CXXLAPACK
//
//  Make copies of results computed by the generic implementation
//
    typename DenseVector<VSELECT>::NoView   select_generic = select;
    typename GeMatrix<MVL>::NoView          VL_generic     = VL;
    typename GeMatrix<MVR>::NoView          VR_generic     = VR;
    typename DenseVector<VWORK>::NoView     work_generic   = work;
//
//  restore output arguments
//
    select = select_org;
    VL     = VL_org;
    VR     = VR_org;
    work   = work_org;
//
//  Compare results
//
    external::trevc_impl(computeVL, computeVR, howMany, select,
                         T, VL, VR, mm, m, work);

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

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

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

    if (failed) {
        ASSERT(0);
    }
#   endif
    LAPACK_DEBUG_OUT("END: trevc");
}

//-- forwarding ----------------------------------------------------------------
template <typename VSELECT, typename MT, typename MVL, typename MVR,
          typename IndexType, typename VWORK>
void
trevc(bool                          computeVL,
      bool                          computeVR,
      TREVC::Job                    howMany,
      VSELECT                       &&select,
      const MT                      &T,
      MVL                           &&VL,
      MVR                           &&VR,
      IndexType                     &&m,
      VWORK                         &&work)
{
    CHECKPOINT_ENTER;
    trevc(computeVL, computeVR, howMany, select, T, VL, VR, m, work);
    CHECKPOINT_LEAVE;
}

} } // namespace lapack, flens

#endif // FLENS_LAPACK_IMPL_TREVC_TCC