// @(#)root/quadp:$Id$
// Author: Eddy Offermann   May 2004

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

/*************************************************************************
 * Parts of this file are copied from the OOQP distribution and          *
 * are subject to the following license:                                 *
 *                                                                       *
 * COPYRIGHT 2001 UNIVERSITY OF CHICAGO                                  *
 *                                                                       *
 * The copyright holder hereby grants you royalty-free rights to use,    *
 * reproduce, prepare derivative works, and to redistribute this software*
 * to others, provided that any changes are clearly documented. This     *
 * software was authored by:                                             *
 *                                                                       *
 *   E. MICHAEL GERTZ      gertz@mcs.anl.gov                             *
 *   Mathematics and Computer Science Division                           *
 *   Argonne National Laboratory                                         *
 *   9700 S. Cass Avenue                                                 *
 *   Argonne, IL 60439-4844                                              *
 *                                                                       *
 *   STEPHEN J. WRIGHT     swright@cs.wisc.edu                           *
 *   Computer Sciences Department                                        *
 *   University of Wisconsin                                             *
 *   1210 West Dayton Street                                             *
 *   Madison, WI 53706   FAX: (608)262-9777                              *
 *                                                                       *
 * Any questions or comments may be directed to one of the authors.      *
 *                                                                       *
 * ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES OF   *
 * ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, AND     *
 * OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A CONTRACT   *
 * WITH THE DEPARTMENT OF ENERGY.                                        *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TQpLinSolverSparse                                                   //
//                                                                      //
// Implements the aspects of the solvers for dense general QP           //
// formulation that are specific to the dense case.                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "Riostream.h"
#include "TQpLinSolverSparse.h"

ClassImp(TQpLinSolverSparse)

//______________________________________________________________________________
TQpLinSolverSparse::TQpLinSolverSparse(TQpProbSparse *factory,TQpDataSparse *data) :
                    TQpLinSolverBase(factory,data)
{
// Constructor

   const Int_t n = factory->fNx+factory->fMy+factory->fMz;
   fKkt.ResizeTo(n,n);

   if (fMy > 0) data->PutAIntoAt(fKkt,fNx,    0);
   if (fMz > 0) data->PutCIntoAt(fKkt,fNx+fMy,0);

   // trick to make sure that A and C are inserted symmetrically
   if (fMy > 0 || fMz > 0) {
      TMatrixDSparse tmp(TMatrixDSparse::kTransposed,fKkt);
      fKkt += tmp;
   }

   data->PutQIntoAt(fKkt,0,0);
}


//______________________________________________________________________________
TQpLinSolverSparse::TQpLinSolverSparse(const TQpLinSolverSparse &another) :
TQpLinSolverBase(another)
{
// Copy constructor

   *this = another;
}


//______________________________________________________________________________
void TQpLinSolverSparse::Factor(TQpDataBase *prob,TQpVar *vars)
{
// Sets up the matrix for the main linear system in "augmented system" form.

   TQpLinSolverBase::Factor(prob,vars);
   fSolveSparse.SetMatrix(fKkt);
}


//______________________________________________________________________________
void TQpLinSolverSparse::PutXDiagonal(TVectorD &xdiag)
{
// Places the diagonal resulting from the bounds on x into the augmented system matrix

   TMatrixDSparseDiag diag(fKkt);
   for (Int_t i = 0; i < xdiag.GetNrows(); i++)
      diag[i] = xdiag[i];
}


//______________________________________________________________________________
void TQpLinSolverSparse::PutZDiagonal(TVectorD &zdiag)
{
// Places the diagonal resulting from the bounds on Cx into the augmented system matrix

   TMatrixDSparseDiag diag(fKkt);
   for (Int_t i = 0; i < zdiag.GetNrows(); i++)
      diag[i+fNx+fMy] = zdiag[i];
}


//______________________________________________________________________________
void TQpLinSolverSparse::SolveCompressed(TVectorD &compressedRhs)
{
// Perform the actual solve using the factors produced in factor.
// rhs on input contains the aggregated right-hand side of the augmented system;
//  on output contains the solution in aggregated form .

   fSolveSparse.Solve(compressedRhs);
}


//______________________________________________________________________________
TQpLinSolverSparse &TQpLinSolverSparse::operator=(const TQpLinSolverSparse &source)
{
// Assignment operator

   if (this != &source) {
      TQpLinSolverBase::operator=(source);
      fKkt.ResizeTo(source.fKkt); fKkt = source.fKkt;
      fSolveSparse = source.fSolveSparse;
   }
   return *this;
}
 TQpLinSolverSparse.cxx:1
 TQpLinSolverSparse.cxx:2
 TQpLinSolverSparse.cxx:3
 TQpLinSolverSparse.cxx:4
 TQpLinSolverSparse.cxx:5
 TQpLinSolverSparse.cxx:6
 TQpLinSolverSparse.cxx:7
 TQpLinSolverSparse.cxx:8
 TQpLinSolverSparse.cxx:9
 TQpLinSolverSparse.cxx:10
 TQpLinSolverSparse.cxx:11
 TQpLinSolverSparse.cxx:12
 TQpLinSolverSparse.cxx:13
 TQpLinSolverSparse.cxx:14
 TQpLinSolverSparse.cxx:15
 TQpLinSolverSparse.cxx:16
 TQpLinSolverSparse.cxx:17
 TQpLinSolverSparse.cxx:18
 TQpLinSolverSparse.cxx:19
 TQpLinSolverSparse.cxx:20
 TQpLinSolverSparse.cxx:21
 TQpLinSolverSparse.cxx:22
 TQpLinSolverSparse.cxx:23
 TQpLinSolverSparse.cxx:24
 TQpLinSolverSparse.cxx:25
 TQpLinSolverSparse.cxx:26
 TQpLinSolverSparse.cxx:27
 TQpLinSolverSparse.cxx:28
 TQpLinSolverSparse.cxx:29
 TQpLinSolverSparse.cxx:30
 TQpLinSolverSparse.cxx:31
 TQpLinSolverSparse.cxx:32
 TQpLinSolverSparse.cxx:33
 TQpLinSolverSparse.cxx:34
 TQpLinSolverSparse.cxx:35
 TQpLinSolverSparse.cxx:36
 TQpLinSolverSparse.cxx:37
 TQpLinSolverSparse.cxx:38
 TQpLinSolverSparse.cxx:39
 TQpLinSolverSparse.cxx:40
 TQpLinSolverSparse.cxx:41
 TQpLinSolverSparse.cxx:42
 TQpLinSolverSparse.cxx:43
 TQpLinSolverSparse.cxx:44
 TQpLinSolverSparse.cxx:45
 TQpLinSolverSparse.cxx:46
 TQpLinSolverSparse.cxx:47
 TQpLinSolverSparse.cxx:48
 TQpLinSolverSparse.cxx:49
 TQpLinSolverSparse.cxx:50
 TQpLinSolverSparse.cxx:51
 TQpLinSolverSparse.cxx:52
 TQpLinSolverSparse.cxx:53
 TQpLinSolverSparse.cxx:54
 TQpLinSolverSparse.cxx:55
 TQpLinSolverSparse.cxx:56
 TQpLinSolverSparse.cxx:57
 TQpLinSolverSparse.cxx:58
 TQpLinSolverSparse.cxx:59
 TQpLinSolverSparse.cxx:60
 TQpLinSolverSparse.cxx:61
 TQpLinSolverSparse.cxx:62
 TQpLinSolverSparse.cxx:63
 TQpLinSolverSparse.cxx:64
 TQpLinSolverSparse.cxx:65
 TQpLinSolverSparse.cxx:66
 TQpLinSolverSparse.cxx:67
 TQpLinSolverSparse.cxx:68
 TQpLinSolverSparse.cxx:69
 TQpLinSolverSparse.cxx:70
 TQpLinSolverSparse.cxx:71
 TQpLinSolverSparse.cxx:72
 TQpLinSolverSparse.cxx:73
 TQpLinSolverSparse.cxx:74
 TQpLinSolverSparse.cxx:75
 TQpLinSolverSparse.cxx:76
 TQpLinSolverSparse.cxx:77
 TQpLinSolverSparse.cxx:78
 TQpLinSolverSparse.cxx:79
 TQpLinSolverSparse.cxx:80
 TQpLinSolverSparse.cxx:81
 TQpLinSolverSparse.cxx:82
 TQpLinSolverSparse.cxx:83
 TQpLinSolverSparse.cxx:84
 TQpLinSolverSparse.cxx:85
 TQpLinSolverSparse.cxx:86
 TQpLinSolverSparse.cxx:87
 TQpLinSolverSparse.cxx:88
 TQpLinSolverSparse.cxx:89
 TQpLinSolverSparse.cxx:90
 TQpLinSolverSparse.cxx:91
 TQpLinSolverSparse.cxx:92
 TQpLinSolverSparse.cxx:93
 TQpLinSolverSparse.cxx:94
 TQpLinSolverSparse.cxx:95
 TQpLinSolverSparse.cxx:96
 TQpLinSolverSparse.cxx:97
 TQpLinSolverSparse.cxx:98
 TQpLinSolverSparse.cxx:99
 TQpLinSolverSparse.cxx:100
 TQpLinSolverSparse.cxx:101
 TQpLinSolverSparse.cxx:102
 TQpLinSolverSparse.cxx:103
 TQpLinSolverSparse.cxx:104
 TQpLinSolverSparse.cxx:105
 TQpLinSolverSparse.cxx:106
 TQpLinSolverSparse.cxx:107
 TQpLinSolverSparse.cxx:108
 TQpLinSolverSparse.cxx:109
 TQpLinSolverSparse.cxx:110
 TQpLinSolverSparse.cxx:111
 TQpLinSolverSparse.cxx:112
 TQpLinSolverSparse.cxx:113
 TQpLinSolverSparse.cxx:114
 TQpLinSolverSparse.cxx:115
 TQpLinSolverSparse.cxx:116
 TQpLinSolverSparse.cxx:117
 TQpLinSolverSparse.cxx:118
 TQpLinSolverSparse.cxx:119
 TQpLinSolverSparse.cxx:120
 TQpLinSolverSparse.cxx:121
 TQpLinSolverSparse.cxx:122
 TQpLinSolverSparse.cxx:123
 TQpLinSolverSparse.cxx:124
 TQpLinSolverSparse.cxx:125
 TQpLinSolverSparse.cxx:126
 TQpLinSolverSparse.cxx:127
 TQpLinSolverSparse.cxx:128
 TQpLinSolverSparse.cxx:129
 TQpLinSolverSparse.cxx:130
 TQpLinSolverSparse.cxx:131
 TQpLinSolverSparse.cxx:132
 TQpLinSolverSparse.cxx:133
 TQpLinSolverSparse.cxx:134
 TQpLinSolverSparse.cxx:135
 TQpLinSolverSparse.cxx:136
 TQpLinSolverSparse.cxx:137
 TQpLinSolverSparse.cxx:138
 TQpLinSolverSparse.cxx:139
 TQpLinSolverSparse.cxx:140
 TQpLinSolverSparse.cxx:141
 TQpLinSolverSparse.cxx:142
 TQpLinSolverSparse.cxx:143