/*************************************************************************

Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.



Contributors:

    * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to

      pseudocode.



See subroutines comments for additional copyrights.



Redistribution and use in source and binary forms, with or without

modification, are permitted provided that the following conditions are

met:



- Redistributions of source code must retain the above copyright

  notice, this list of conditions and the following disclaimer.



- Redistributions in binary form must reproduce the above copyright

  notice, this list of conditions and the following disclaimer listed

  in this license in the documentation and/or other materials

  provided with the distribution.



- Neither the name of the copyright holders 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.

*************************************************************************/



#ifndef _hessenberg_h

#define _hessenberg_h



#include "capd/alglib/ap.h"


#include "capd/alglib/reflections.h"




/*************************************************************************

Reduction of a square matrix to  upper Hessenberg form: Q'*A*Q = H,

where Q is an orthogonal matrix, H - Hessenberg matrix.



Input parameters:

    A       -   matrix A with elements [0..N-1, 0..N-1]

    N       -   size of matrix A.



Output parameters:

    A       -   matrices Q and P in  compact form (see below).

    Tau     -   array of scalar factors which are used to form matrix Q.

                Array whose index ranges within [0..N-2]



Matrix H is located on the main diagonal, on the lower secondary  diagonal

and above the main diagonal of matrix A. The elements which are used to

form matrix Q are situated in array Tau and below the lower secondary

diagonal of matrix A as follows:



Matrix Q is represented as a product of elementary reflections



Q = H(0)*H(2)*...*H(n-2),



where each H(i) is given by



H(i) = 1 - tau * v * (v^T)



where tau is a scalar stored in Tau[I]; v - is a real vector,

so that v(0:i) = 0, v(i+1) = 1, v(i+2:n-1) stored in A(i+2:n-1,i).



  -- LAPACK routine (version 3.0) --

     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,

     Courant Institute, Argonne National Lab, and Rice University

     October 31, 1992

*************************************************************************/

void rmatrixhessenberg(ap::real_2d_array& a, int n, ap::real_1d_array& tau);





/*************************************************************************

Unpacking matrix Q which reduces matrix A to upper Hessenberg form



Input parameters:

    A   -   output of RMatrixHessenberg subroutine.

    N   -   size of matrix A.

    Tau -   scalar factors which are used to form Q.

            Output of RMatrixHessenberg subroutine.



Output parameters:

    Q   -   matrix Q.

            Array whose indexes range within [0..N-1, 0..N-1].



  -- ALGLIB --

     Copyright 2005 by Bochkanov Sergey

*************************************************************************/

void rmatrixhessenbergunpackq(const ap::real_2d_array& a,

     int n,

     const ap::real_1d_array& tau,

     ap::real_2d_array& q);





/*************************************************************************

Unpacking matrix H (the result of matrix A reduction to upper Hessenberg form)



Input parameters:

    A   -   output of RMatrixHessenberg subroutine.

    N   -   size of matrix A.



Output parameters:

    H   -   matrix H. Array whose indexes range within [0..N-1, 0..N-1].



  -- ALGLIB --

     Copyright 2005 by Bochkanov Sergey

*************************************************************************/

void rmatrixhessenbergunpackh(const ap::real_2d_array& a,

     int n,

     ap::real_2d_array& h);





/*************************************************************************

Obsolete 1-based subroutine.

See RMatrixHessenberg for 0-based replacement.

*************************************************************************/

void toupperhessenberg(ap::real_2d_array& a, int n, ap::real_1d_array& tau);





/*************************************************************************

Obsolete 1-based subroutine.

See RMatrixHessenbergUnpackQ for 0-based replacement.

*************************************************************************/

void unpackqfromupperhessenberg(const ap::real_2d_array& a,

     int n,

     const ap::real_1d_array& tau,

     ap::real_2d_array& q);





/*************************************************************************

Obsolete 1-based subroutine.

See RMatrixHessenbergUnpackH for 0-based replacement.

*************************************************************************/

void unpackhfromupperhessenberg(const ap::real_2d_array& a,

     int n,

     const ap::real_1d_array& tau,

     ap::real_2d_array& h);





#endif

