This file is indexed.

/usr/include/trilinos/AnasaziTraceMinRitzOp.hpp is in libtrilinos-anasazi-dev 12.12.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
// @HEADER
// ***********************************************************************
//
//                 Anasazi: Block Eigensolvers Package
//                 Copyright 2004 Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// 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 Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "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 SANDIA CORPORATION OR THE
// 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.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER

/*! \file AnasaziTraceMinRitzOp.hpp
 *  Defines several operators for use with TraceMin
*/

#ifndef TRACEMIN_RITZ_OP_HPP
#define TRACEMIN_RITZ_OP_HPP

#include "AnasaziConfigDefs.hpp"
#include "AnasaziMatOrthoManager.hpp"
#include "AnasaziMinres.hpp"
#include "AnasaziTraceMinBase.hpp"

#ifdef HAVE_ANASAZI_BELOS
  #include "BelosMultiVecTraits.hpp"
  #include "BelosLinearProblem.hpp"
  #include "BelosPseudoBlockGmresSolMgr.hpp"
  #include "BelosOperator.hpp"
  #ifdef HAVE_ANASAZI_TPETRA
    #include "BelosTpetraAdapter.hpp"
  #endif
  #ifdef HAVE_ANASAZI_EPETRA
    #include "BelosEpetraAdapter.hpp"
  #endif
#endif

#include "Teuchos_RCP.hpp"
#include "Teuchos_SerialDenseSolver.hpp"
#include "Teuchos_ScalarTraitsDecl.hpp"


using Teuchos::RCP;

namespace Anasazi {
namespace Experimental {



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Abstract base class for all operators
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
class TraceMinOp
{
public:
  virtual ~TraceMinOp() { };
  virtual void Apply(const MV& X, MV& Y) const =0;
  virtual void removeIndices(const std::vector<int>& indicesToRemove) =0;
};



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Defines a projector
// Applies P_i to each individual vector x_i
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
class TraceMinProjOp
{
  typedef Anasazi::MultiVecTraits<Scalar,MV>    MVT;
  const Scalar ONE; 

public:
  // Constructors
  TraceMinProjOp(const Teuchos::RCP<const MV> X, const Teuchos::RCP<const OP> B, Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> >  orthman = Teuchos::null);
  TraceMinProjOp(const Teuchos::RCP<const MV> X, const Teuchos::RCP<const OP> B, Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> >  orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs);

  // Destructor
  ~TraceMinProjOp();

  // Applies the projector to a multivector
  void Apply(const MV& X, MV& Y) const;

  // Called by MINRES when certain vectors converge
  void removeIndices(const std::vector<int>& indicesToRemove);

private:  
  Teuchos::Array< Teuchos::RCP<const MV> > projVecs_;
  Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman_;
  Teuchos::RCP<const OP> B_;

#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  RCP<Teuchos::Time> ProjTime_;
#endif
};


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// This class defines an operator A + \sigma B
// This is used to apply shifts within TraceMin
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
class TraceMinRitzOp : public TraceMinOp<Scalar,MV,OP>
{
  template <class Scalar_, class MV_, class OP_> friend class TraceMinProjRitzOp;
  template <class Scalar_, class MV_, class OP_> friend class TraceMinProjRitzOpWithPrec;
  template <class Scalar_, class MV_, class OP_> friend class TraceMinProjectedPrecOp;

  typedef Anasazi::MultiVecTraits<Scalar,MV>     MVT;
  typedef Anasazi::OperatorTraits<Scalar,MV,OP>  OPT;
  const Scalar ONE;  
  const Scalar ZERO;

public:
  // constructors for standard/generalized EVP
  TraceMinRitzOp(const Teuchos::RCP<const OP>& A, const Teuchos::RCP<const OP>& B = Teuchos::null, const Teuchos::RCP<const OP>& Prec = Teuchos::null);

  // Destructor
  ~TraceMinRitzOp() { };

  // sets the Ritz shift
  void setRitzShifts(std::vector<Scalar> shifts) {ritzShifts_ = shifts;};

  Scalar getRitzShift(const int subscript) { return ritzShifts_[subscript]; };

  Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > getPrec() { return Prec_; };

  // sets the tolerances for the inner solves
  void setInnerTol(const std::vector<Scalar>& tolerances) { tolerances_ = tolerances; };

  void getInnerTol(std::vector<Scalar>& tolerances) const { tolerances = tolerances_; };

  void setMaxIts(const int maxits) { maxits_ = maxits; };
  
  int getMaxIts() const { return maxits_; };

  // applies A+\sigma B to a vector
  void Apply(const MV& X, MV& Y) const;

  // returns (A+\sigma B)\X
  void ApplyInverse(const MV& X, MV& Y);

  void removeIndices(const std::vector<int>& indicesToRemove);

private:  
  Teuchos::RCP<const OP> A_, B_;
  Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > Prec_;

  int maxits_;
  std::vector<Scalar> ritzShifts_;
  std::vector<Scalar> tolerances_;

  Teuchos::RCP< PseudoBlockMinres< Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> > > solver_;

#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  RCP<Teuchos::Time> PetraMultTime_, AopMultTime_;
#endif
};



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Defines an operator P (A + \sigma B) P
// Used for TraceMin with the projected iterative solver
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
class TraceMinProjRitzOp : public TraceMinOp<Scalar,MV,OP>
{
  typedef Anasazi::MultiVecTraits<Scalar,MV>                                MVT;
  typedef Anasazi::OperatorTraits<Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> >  OPT;

public:
  // constructors for standard/generalized EVP
  TraceMinProjRitzOp(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, const Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman = Teuchos::null);
  TraceMinProjRitzOp(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, const Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs);

  // applies P (A+\sigma B) P to a vector
  void Apply(const MV& X, MV& Y) const;

  // returns P(A+\sigma B)P\X
  // this is not const due to the clumsiness with amSolving
  void ApplyInverse(const MV& X, MV& Y);

  void removeIndices(const std::vector<int>& indicesToRemove);

private:  
  Teuchos::RCP< TraceMinRitzOp<Scalar,MV,OP> > Op_;
  Teuchos::RCP< TraceMinProjOp<Scalar,MV,OP> > projector_;

  Teuchos::RCP< PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> > > solver_;
};



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Defines a preconditioner to be used with our projection method
// Because we're using projected CG/minres/gmres, this preconditioner has to do projection as well
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: Should this be public?
template <class Scalar, class MV, class OP>
class TraceMinProjectedPrecOp : public TraceMinOp<Scalar,MV,OP>
{
  typedef Anasazi::MultiVecTraits<Scalar,MV>     MVT;
  typedef Anasazi::OperatorTraits<Scalar,MV,OP>  OPT;
  const Scalar ONE; 

public:
  // constructors for standard/generalized EVP
  TraceMinProjectedPrecOp(const Teuchos::RCP<const OP> Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman = Teuchos::null);
  TraceMinProjectedPrecOp(const Teuchos::RCP<const OP> Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs);

  ~TraceMinProjectedPrecOp();

  void Apply(const MV& X, MV& Y) const;

  void removeIndices(const std::vector<int>& indicesToRemove);

private:  
  Teuchos::RCP<const OP> Op_;
  Teuchos::Array< Teuchos::RCP<const MV> > projVecs_;

  Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman_;
  Teuchos::RCP<const OP> B_;
};



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Defines a preconditioner to be used with our projection method
// Because we're using projected CG/minres/gmres, this preconditioner has to do projection as well
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_ANASAZI_BELOS
template <class Scalar, class MV, class OP>
class TraceMinProjRitzOpWithPrec : public TraceMinOp<Scalar,MV,OP>
{
  typedef Anasazi::MultiVecTraits<Scalar,MV>                                MVT;
  typedef Anasazi::OperatorTraits<Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> >  OPT;
  const Scalar ONE; 

public:
  // constructors for standard/generalized EVP
  TraceMinProjRitzOpWithPrec(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman = Teuchos::null);
  TraceMinProjRitzOpWithPrec(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs);

  void Apply(const MV& X, MV& Y) const;

  // returns P(A+\sigma B)P\X
  // this is not const due to the clumsiness with amSolving
  void ApplyInverse(const MV& X, MV& Y);

  void removeIndices(const std::vector<int>& indicesToRemove);

private:  
  Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > Op_;
  Teuchos::RCP<TraceMinProjectedPrecOp<Scalar,MV,OP> > Prec_;

  Teuchos::RCP< Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> > > solver_;
  Teuchos::RCP< Belos::LinearProblem<Scalar,MV,TraceMinOp<Scalar,MV,OP> > > problem_;
};
#endif

}} // end of namespace



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Operator traits classes
// Required to use user-defined operators with a Krylov solver in Belos
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace Anasazi
{
template <class Scalar, class MV, class OP>
class OperatorTraits <Scalar, MV, Experimental::TraceMinOp<Scalar,MV,OP> >
  {
  public:
    static void
    Apply (const Experimental::TraceMinOp<Scalar,MV,OP>& Op,
           const MV& x,
           MV& y) {Op.Apply(x,y);};

    //! Whether Op implements applying the transpose.
    static bool
    HasApplyTranspose (const Experimental::TraceMinOp<Scalar,MV,OP>& Op) {return true;};
  };
}


#ifdef HAVE_ANASAZI_BELOS
namespace Belos
{
template <class Scalar, class MV, class OP>
class OperatorTraits <Scalar, MV, Anasazi::Experimental::TraceMinOp<Scalar,MV,OP> >
  {
  public:
    static void
    Apply (const Anasazi::Experimental::TraceMinOp<Scalar,MV,OP>& Op,
           const MV& x,
           MV& y,  Belos::ETrans trans = NOTRANS) {Op.Apply(x,y);};

    //! Whether Op implements applying the transpose.
    static bool
    HasApplyTranspose (const Anasazi::Experimental::TraceMinOp<Scalar,MV,OP>& Op) {return true;};
  };
}
#endif



namespace Anasazi
{
template <class Scalar, class MV, class OP>
class OperatorTraits <Scalar, MV, Experimental::TraceMinRitzOp<Scalar,MV,OP> >
  {
  public:
    static void
    Apply (const Experimental::TraceMinRitzOp<Scalar,MV,OP>& Op,
           const MV& x,
           MV& y) {Op.Apply(x,y);};

    //! Whether Op implements applying the transpose.
    static bool
    HasApplyTranspose (const Experimental::TraceMinRitzOp<Scalar,MV,OP>& Op) {return true;};
  };
}



namespace Anasazi
{
template <class Scalar, class MV, class OP>
class OperatorTraits <Scalar, MV, Experimental::TraceMinProjRitzOp<Scalar,MV,OP> >
  {
  public:
    static void
    Apply (const Experimental::TraceMinProjRitzOp<Scalar,MV,OP>& Op,
           const MV& x,
           MV& y) {Op.Apply(x,y);};

    //! Whether Op implements applying the transpose.
    static bool
    HasApplyTranspose (const Experimental::TraceMinProjRitzOp<Scalar,MV,OP>& Op) {return true;};
  };
}



namespace Anasazi
{
template <class Scalar, class MV, class OP>
class OperatorTraits <Scalar, MV, Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP> >
  {
  public:
    static void
    Apply (const Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP>& Op,
           const MV& x,
           MV& y) {Op.Apply(x,y);};

    //! Whether Op implements applying the transpose.
    static bool
    HasApplyTranspose (const Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP>& Op) {return true;};
  };
}



namespace Anasazi {
namespace Experimental {
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TraceMinProjOp implementations
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
TraceMinProjOp<Scalar,MV,OP>::TraceMinProjOp(const Teuchos::RCP<const MV> X, const Teuchos::RCP<const OP> B, Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) :
ONE(Teuchos::ScalarTraits<Scalar>::one())
{
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  ProjTime_ = Teuchos::TimeMonitor::getNewTimer("Anasazi: TraceMinProjOp::Apply()");
#endif

  B_ = B;
  orthman_ = orthman;
  if(orthman_ != Teuchos::null && B_ != Teuchos::null)
    orthman_->setOp(Teuchos::null);

  // Make it so X'BBX = I
  // If there is no B, this step is unnecessary because X'X = I already
  if(B_ != Teuchos::null)
  {
    int nvec = MVT::GetNumberVecs(*X);

    if(orthman_ != Teuchos::null)
    {
      // Want: X'X = I NOT X'MX = I
      Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
      orthman_->normalizeMat(*helperMV);
      projVecs_.push_back(helperMV);
    }
    else
    {
      std::vector<Scalar> normvec(nvec);
      MVT::MvNorm(*X,normvec);
      for(int i=0; i<nvec; i++)
        normvec[i] = ONE/normvec[i];
      Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
      MVT::MvScale(*helperMV,normvec);
      projVecs_.push_back(helperMV);
    }
  }
  else
    projVecs_.push_back(MVT::CloneCopy(*X));
}


template <class Scalar, class MV, class OP>
TraceMinProjOp<Scalar,MV,OP>::TraceMinProjOp(const Teuchos::RCP<const MV> X, const Teuchos::RCP<const OP> B, Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> >  orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs) :
ONE(Teuchos::ScalarTraits<Scalar>::one())
{
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  ProjTime_ = Teuchos::TimeMonitor::getNewTimer("Anasazi: TraceMinProjOp::Apply()");
#endif

  B_ = B;
  orthman_ = orthman;
  if(B_ != Teuchos::null)
    orthman_->setOp(Teuchos::null);

  projVecs_ = auxVecs;

  // Make it so X'BBX = I
  // If there is no B, this step is unnecessary because X'X = I already
  if(B_ != Teuchos::null)
  {
    // Want: X'X = I NOT X'MX = I
    Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
    orthman_->normalizeMat(*helperMV);
    projVecs_.push_back(helperMV);

  }
  else
    projVecs_.push_back(MVT::CloneCopy(*X));
}


// Destructor - make sure to reset the operator in the ortho manager
template <class Scalar, class MV, class OP>
TraceMinProjOp<Scalar,MV,OP>::~TraceMinProjOp()
{
  if(orthman_ != Teuchos::null)
    orthman_->setOp(B_);
}


// Compute Px = x - proj proj'x
template <class Scalar, class MV, class OP>
void TraceMinProjOp<Scalar,MV,OP>::Apply(const MV& X, MV& Y) const
{
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
    Teuchos::TimeMonitor lcltimer( *ProjTime_ );
#endif

  if(orthman_ != Teuchos::null)
  {
    MVT::Assign(X,Y);
    orthman_->projectMat(Y,projVecs_);
  }
  else
  {
    int nvec = MVT::GetNumberVecs(X);
    std::vector<Scalar> dotProducts(nvec);
    MVT::MvDot(*projVecs_[0],X,dotProducts);
    Teuchos::RCP<MV> helper = MVT::CloneCopy(*projVecs_[0]);
    MVT::MvScale(*helper,dotProducts);
    MVT::MvAddMv(ONE,X,-ONE,*helper,Y);
  }
}



template <class Scalar, class MV, class OP>
void TraceMinProjOp<Scalar,MV,OP>::removeIndices(const std::vector<int>& indicesToRemove)
{
  if (orthman_ == Teuchos::null) {
    const int nprojvecs = projVecs_.size();
    const int nvecs = MVT::GetNumberVecs(*projVecs_[nprojvecs-1]);
    const int numRemoving = indicesToRemove.size();
    std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);

    for (int i=0; i<nvecs; i++) {
      helper[i] = i;
    }

    std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());

    Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*projVecs_[nprojvecs-1],indicesToLeave);
    projVecs_[nprojvecs-1] = helperMV;
  }
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TraceMinRitzOp implementations
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
TraceMinRitzOp<Scalar,MV,OP>::TraceMinRitzOp(const Teuchos::RCP<const OP>& A, const Teuchos::RCP<const OP>& B, const Teuchos::RCP<const OP>& Prec) :
ONE(Teuchos::ScalarTraits<Scalar>::one()),
ZERO(Teuchos::ScalarTraits<Scalar>::zero())
{
  A_ = A;
  B_ = B;
  // TODO: maxits should not be hard coded
  maxits_ = 200;

#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  PetraMultTime_ = Teuchos::TimeMonitor::getNewTimer("Anasazi: TraceMinRitzOp: *Petra::Apply()");
  AopMultTime_ = Teuchos::TimeMonitor::getNewTimer("Anasazi: TraceMinRitzOp::Apply()");
#endif

  // create the operator for my minres solver
  Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(this);
  linSolOp.release();

  // TODO: This should support left and right prec
  if(Prec != Teuchos::null)    
    Prec_ = Teuchos::rcp( new TraceMinRitzOp<Scalar,MV,OP>(Prec) );
  
  // create my minres solver
  solver_ = Teuchos::rcp( new PseudoBlockMinres< Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> >(linSolOp,Prec_) );
}



template <class Scalar, class MV, class OP>
void TraceMinRitzOp<Scalar,MV,OP>::Apply(const MV& X, MV& Y) const
{
  int nvecs = MVT::GetNumberVecs(X);

#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
  Teuchos::TimeMonitor outertimer( *AopMultTime_ );
#endif

  // Y := A*X
  {
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
    Teuchos::TimeMonitor lcltimer( *PetraMultTime_ );
#endif

    OPT::Apply(*A_,X,Y);
  }

  // If we are a preconditioner, we're not using shifts
  if(ritzShifts_.size() > 0)
  {
    // Get the indices of nonzero Ritz shifts
    std::vector<int> nonzeroRitzIndices;
    nonzeroRitzIndices.reserve(nvecs);
    for(int i=0; i<nvecs; i++)
    {
      if(ritzShifts_[i] != ZERO)
        nonzeroRitzIndices.push_back(i);
    }

    // Handle Ritz shifts
    int numRitzShifts = nonzeroRitzIndices.size();
    if(numRitzShifts > 0)
    {
      // Get pointers to the appropriate parts of X and Y
      Teuchos::RCP<const MV> ritzX = MVT::CloneView(X,nonzeroRitzIndices);
      Teuchos::RCP<MV> ritzY = MVT::CloneViewNonConst(Y,nonzeroRitzIndices);

      // Get the nonzero Ritz shifts
      std::vector<Scalar> nonzeroRitzShifts(numRitzShifts);
      for(int i=0; i<numRitzShifts; i++)
        nonzeroRitzShifts[i] = ritzShifts_[nonzeroRitzIndices[i]];

      // Compute Y := AX + ritzShift BX
      if(B_ != Teuchos::null)
      {
        Teuchos::RCP<MV> BX = MVT::Clone(*ritzX,numRitzShifts);
        OPT::Apply(*B_,*ritzX,*BX);
        MVT::MvScale(*BX,nonzeroRitzShifts);
        MVT::MvAddMv(ONE,*ritzY,-ONE,*BX,*ritzY);
      }
      // Compute Y := AX + ritzShift X
      else
      {
        Teuchos::RCP<MV> scaledX = MVT::CloneCopy(*ritzX);
        MVT::MvScale(*scaledX,nonzeroRitzShifts);
        MVT::MvAddMv(ONE,*ritzY,-ONE,*scaledX,*ritzY);
      }
    }
  }
}



template <class Scalar, class MV, class OP>
void TraceMinRitzOp<Scalar,MV,OP>::ApplyInverse(const MV& X, MV& Y)
{
  int nvecs = MVT::GetNumberVecs(X);
  std::vector<int> indices(nvecs);
  for(int i=0; i<nvecs; i++)
    indices[i] = i;

  Teuchos::RCP<const MV> rcpX = MVT::CloneView(X,indices);
  Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);

  // Solve the linear system A*Y = X
  solver_->setTol(tolerances_);
  solver_->setMaxIter(maxits_);

  // Set solution and RHS
  solver_->setSol(rcpY);
  solver_->setRHS(rcpX);

  // Solve the linear system
  solver_->solve();  
}



template <class Scalar, class MV, class OP>
void TraceMinRitzOp<Scalar,MV,OP>::removeIndices(const std::vector<int>& indicesToRemove)
{
  int nvecs = tolerances_.size();
  int numRemoving = indicesToRemove.size();
  std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);
  std::vector<Scalar> helperS(nvecs-numRemoving);

  for(int i=0; i<nvecs; i++)
    helper[i] = i;

  std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());

  for(int i=0; i<nvecs-numRemoving; i++)
    helperS[i] = ritzShifts_[indicesToLeave[i]];
  ritzShifts_ = helperS;

  for(int i=0; i<nvecs-numRemoving; i++)
    helperS[i] = tolerances_[indicesToLeave[i]];
  tolerances_ = helperS;
}



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TraceMinProjRitzOp implementations
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
TraceMinProjRitzOp<Scalar,MV,OP>::TraceMinProjRitzOp(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, const Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) 
{ 
  Op_ = Op; 

  // Create the projector object
  projector_ = Teuchos::rcp( new TraceMinProjOp<Scalar,MV,OP>(projVecs, Op_->B_, orthman) );

  // create the operator for my minres solver
  Teuchos::RCP<TraceMinProjRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(this);
  linSolOp.release();

  // create my minres solver
  solver_ = Teuchos::rcp( new PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> >(linSolOp) );
}


template <class Scalar, class MV, class OP>
TraceMinProjRitzOp<Scalar,MV,OP>::TraceMinProjRitzOp(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, const Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs)
{
  Op_ = Op; 

  // Create the projector object
  projector_ = Teuchos::rcp( new TraceMinProjOp<Scalar,MV,OP>(projVecs, Op_->B_, orthman, auxVecs) );

  // create the operator for my minres solver
  Teuchos::RCP<TraceMinProjRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(this);
  linSolOp.release();

  // create my minres solver
  solver_ = Teuchos::rcp( new PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> >(linSolOp) );
}



// Y:= P (A+\sigma B) P X
template <class Scalar, class MV, class OP>
void TraceMinProjRitzOp<Scalar,MV,OP>::Apply(const MV& X, MV& Y) const
{
  int nvecs = MVT::GetNumberVecs(X);

//  // compute PX
//  Teuchos::RCP<MV> PX = MVT::Clone(X,nvecs);
//  projector_->Apply(X,*PX);

  // compute (A+\sigma B) P X
  Teuchos::RCP<MV> APX = MVT::Clone(X,nvecs);
  OPT::Apply(*Op_,X,*APX);

  // compute Y := P (A+\sigma B) P X
  projector_->Apply(*APX,Y);
}



template <class Scalar, class MV, class OP>
void TraceMinProjRitzOp<Scalar,MV,OP>::ApplyInverse(const MV& X, MV& Y)
{
  int nvecs = MVT::GetNumberVecs(X);
  std::vector<int> indices(nvecs);
  for(int i=0; i<nvecs; i++)
    indices[i] = i;

  Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);
  Teuchos::RCP<MV> PX = MVT::Clone(X,nvecs);
  projector_->Apply(X,*PX);

  // Solve the linear system A*Y = X
  solver_->setTol(Op_->tolerances_);
  solver_->setMaxIter(Op_->maxits_);

  // Set solution and RHS
  solver_->setSol(rcpY);
  solver_->setRHS(PX);

  // Solve the linear system
  solver_->solve();  
}



template <class Scalar, class MV, class OP>
void TraceMinProjRitzOp<Scalar,MV,OP>::removeIndices(const std::vector<int>& indicesToRemove)
{
  Op_->removeIndices(indicesToRemove);

  projector_->removeIndices(indicesToRemove);
}




#ifdef HAVE_ANASAZI_BELOS
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TraceMinProjRitzOpWithPrec implementations
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::TraceMinProjRitzOpWithPrec(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) :
ONE(Teuchos::ScalarTraits<Scalar>::one())
{
  Op_ = Op;

  // create the operator for the Belos solver
  Teuchos::RCP<TraceMinProjRitzOpWithPrec<Scalar,MV,OP> > linSolOp = Teuchos::rcp(this);
  linSolOp.release();

  // Create the linear problem
  problem_ = Teuchos::rcp(new Belos::LinearProblem< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());

  // Set the operator
  problem_->setOperator(linSolOp);

  // Set the preconditioner
  // TODO: This does not support right preconditioning
  Prec_ = Teuchos::rcp( new TraceMinProjectedPrecOp<Scalar,MV,OP>(Op_->Prec_->A_, projVecs, orthman) );
//  problem_->setLeftPrec(Prec_);

  // create the pseudoblock gmres solver
  // minres has trouble with the projected preconditioner
  solver_ = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
}


template <class Scalar, class MV, class OP>
TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::TraceMinProjRitzOpWithPrec(const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs) :
ONE(Teuchos::ScalarTraits<Scalar>::one())
{
  Op_ = Op;

  // create the operator for the Belos solver
  Teuchos::RCP<const TraceMinProjRitzOpWithPrec<Scalar,MV,OP> > linSolOp = Teuchos::rcp(this);
  linSolOp.release();

  // Create the linear problem
  problem_ = Teuchos::rcp(new Belos::LinearProblem< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());

  // Set the operator
  problem_->setOperator(linSolOp);

  // Set the preconditioner
  // TODO: This does not support right preconditioning
  Prec_ = Teuchos::rcp( new TraceMinProjectedPrecOp<Scalar,MV,OP>(Op_->Prec_->A_, projVecs, orthman, auxVecs) );
//  problem_->setLeftPrec(Prec_);

  // create the pseudoblock gmres solver
  // minres has trouble with the projected preconditioner
  solver_ = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
}


template <class Scalar, class MV, class OP>
void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::Apply(const MV& X, MV& Y) const
{
  int nvecs = MVT::GetNumberVecs(X);
  RCP<MV> Ydot = MVT::Clone(Y,nvecs);
  OPT::Apply(*Op_,X,*Ydot);
  Prec_->Apply(*Ydot,Y);
}


template <class Scalar, class MV, class OP>
void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::ApplyInverse(const MV& X, MV& Y)
{
  int nvecs = MVT::GetNumberVecs(X);
  std::vector<int> indices(nvecs);
  for(int i=0; i<nvecs; i++)
    indices[i] = i;

  Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);
  Teuchos::RCP<MV> rcpX = MVT::Clone(X,nvecs);
  
  Prec_->Apply(X,*rcpX);
  
  // Create the linear problem
  problem_->setProblem(rcpY,rcpX);

  // Set the problem for the solver
  solver_->setProblem(problem_);

  // Set up the parameters for gmres
  // TODO: Accept maximum number of iterations
  // TODO: Make sure single shift really means single shift
  // TODO: Look into fixing my problem with the deflation quorum
  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::rcp(new Teuchos::ParameterList());
  pl->set("Convergence Tolerance", Op_->tolerances_[0]);
  pl->set("Block Size", nvecs);
//  pl->set("Verbosity", Belos::IterationDetails + Belos::StatusTestDetails + Belos::Debug);
//  pl->set("Output Frequency", 1);
  pl->set("Maximum Iterations", Op_->getMaxIts());
  pl->set("Num Blocks", Op_->getMaxIts());
  solver_->setParameters(pl);

  // Solve the linear system
  solver_->solve();  
}


template <class Scalar, class MV, class OP>
void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::removeIndices(const std::vector<int>& indicesToRemove)
{
  Op_->removeIndices(indicesToRemove);

  Prec_->removeIndices(indicesToRemove);
}
#endif


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// TraceMinProjectedPrecOp implementations
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Scalar, class MV, class OP>
TraceMinProjectedPrecOp<Scalar,MV,OP>::TraceMinProjectedPrecOp(const Teuchos::RCP<const OP> Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) :
ONE (Teuchos::ScalarTraits<Scalar>::one())
{
  Op_ = Op;
  orthman_ = orthman;

  int nvecs = MVT::GetNumberVecs(*projVecs);
  Teuchos::RCP<MV> helperMV = MVT::Clone(*projVecs,nvecs);

  // Compute Prec \ projVecs
  OPT::Apply(*Op_,*projVecs,*helperMV);

  if(orthman_ != Teuchos::null)
  {
    // Set the operator for the inner products
    B_ = orthman_->getOp();
    orthman_->setOp(Op_);

    Teuchos::RCP<MV> locProjVecs = MVT::CloneCopy(*projVecs);

    // Normalize the vectors such that Y' Prec \ Y = I
    const int rank = orthman_->normalizeMat (*locProjVecs, Teuchos::null, helperMV);

    // FIXME (mfh 08 Aug 2014) Write a named exception for this case.
    TEUCHOS_TEST_FOR_EXCEPTION(rank != nvecs, std::runtime_error, "Belos::TraceMinProjectedPrecOp: constructor: Loss of orthogonality detected.");

    orthman_->setOp(Teuchos::null);
  }
  else
  {
    std::vector<Scalar> dotprods(nvecs);
    MVT::MvDot(*projVecs,*helperMV,dotprods);

    for(int i=0; i<nvecs; i++)
      dotprods[i] = ONE/sqrt(dotprods[i]);

    MVT::MvScale(*helperMV, dotprods);
  }
  
  projVecs_.push_back(helperMV);
}


template <class Scalar, class MV, class OP>
TraceMinProjectedPrecOp<Scalar,MV,OP>::TraceMinProjectedPrecOp(const Teuchos::RCP<const OP> Op, const Teuchos::RCP<const MV> projVecs, Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs) :
ONE(Teuchos::ScalarTraits<Scalar>::one())
{
  Op_ = Op;
  orthman_ = orthman;

  int nvecs;
  Teuchos::RCP<MV> locProjVecs;

  // Add the aux vecs to the projector
  if(auxVecs.size() > 0)
  {
    // Get the total number of vectors
    nvecs = MVT::GetNumberVecs(*projVecs);
    for(int i=0; i<auxVecs.size(); i++)
      nvecs += MVT::GetNumberVecs(*auxVecs[i]);

    // Allocate space for all of them
    locProjVecs = MVT::Clone(*projVecs, nvecs);

    // Copy the vectors over
    int startIndex = 0;
    std::vector<int> locind(nvecs);

    locind.resize(MVT::GetNumberVecs(*projVecs));
    for (size_t i = 0; i<locind.size(); i++) {
      locind[i] = startIndex + i;
    }
    startIndex += locind.size();
    MVT::SetBlock(*projVecs,locind,*locProjVecs);

    for (size_t i=0; i < static_cast<size_t> (auxVecs.size ()); ++i)
    {
      locind.resize(MVT::GetNumberVecs(*auxVecs[i]));
      for(size_t j=0; j<locind.size(); j++) locind[j] = startIndex + j;
      startIndex += locind.size();
      MVT::SetBlock(*auxVecs[i],locind,*locProjVecs);
    }
  }
  else
  {
    // Copy the vectors over
    nvecs = MVT::GetNumberVecs(*projVecs);
    locProjVecs = MVT::CloneCopy(*projVecs);
  }
  
  Teuchos::RCP<MV> helperMV = MVT::Clone(*projVecs,nvecs);

  // Compute Prec \ projVecs
  OPT::Apply(*Op_,*locProjVecs,*helperMV);
  
  // Set the operator for the inner products
  B_ = orthman_->getOp();
  orthman_->setOp(Op_);

  // Normalize the vectors such that Y' Prec \ Y = I
  const int rank = orthman_->normalizeMat(*locProjVecs,Teuchos::null,helperMV);
  
  projVecs_.push_back(helperMV);

//  helperMV->describe(*(Teuchos::VerboseObjectBase::getDefaultOStream()),Teuchos::VERB_EXTREME);

  // FIXME (mfh 08 Aug 2014) Write a named exception for this case.  
  TEUCHOS_TEST_FOR_EXCEPTION(
          rank != nvecs, std::runtime_error, 
    "Belos::TraceMinProjectedPrecOp: constructor: Loss of orthogonality detected.");

  orthman_->setOp(Teuchos::null);
}


template <class Scalar, class MV, class OP>
TraceMinProjectedPrecOp<Scalar,MV,OP>::~TraceMinProjectedPrecOp()
{
  if(orthman_ != Teuchos::null)
    orthman_->setOp(B_);
}



template <class Scalar, class MV, class OP>
void TraceMinProjectedPrecOp<Scalar,MV,OP>::Apply(const MV& X, MV& Y) const
{
  int nvecsX = MVT::GetNumberVecs(X);

  if(orthman_ != Teuchos::null)
  {
    // Y = M\X - proj proj' X
    int nvecsP = MVT::GetNumberVecs(*projVecs_[0]);
    OPT::Apply(*Op_,X,Y);
	
    Teuchos::RCP< Teuchos::SerialDenseMatrix<int,Scalar> > projX = Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,Scalar>(nvecsP,nvecsX));

    MVT::MvTransMv(ONE, *projVecs_[0], X, *projX);
	
    MVT::MvTimesMatAddMv(-ONE, *projVecs_[0], *projX, ONE, Y);
  }
  else
  {
    Teuchos::RCP<MV> MX = MVT::Clone(X,nvecsX);
    OPT::Apply(*Op_,X,*MX);
	
    std::vector<Scalar> dotprods(nvecsX);
    MVT::MvDot(*projVecs_[0], X, dotprods);
	
    Teuchos::RCP<MV> helper = MVT::CloneCopy(*projVecs_[0]);
    MVT::MvScale(*helper, dotprods);
    MVT::MvAddMv(ONE, *MX, -ONE, *helper, Y);
  }
}

  
template <class Scalar, class MV, class OP>
void TraceMinProjectedPrecOp<Scalar,MV,OP>::removeIndices(const std::vector<int>& indicesToRemove)
{
  if(orthman_ == Teuchos::null)
  {
    int nvecs = MVT::GetNumberVecs(*projVecs_[0]);
    int numRemoving = indicesToRemove.size();
    std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);

    for(int i=0; i<nvecs; i++)
      helper[i] = i;

    std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());

    Teuchos::RCP<const MV> helperMV = MVT::CloneCopy(*projVecs_[0],indicesToLeave);
    projVecs_[0] = helperMV;
  }
}

}} // end of namespace

#endif