#include "../include/lass.h"

/**
 *
 *  @file dgtsv.c
 *
 *  @brief LASs-DSSs dgtsv routine.
 *
 *  LASs-DSSs is a software package provided by:
 *  Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
 *
 *  @author Pedro Valero-Lara pedro.valero@bsc.es
 *  @date 2017-08-02
 *  @reviewer 
 *  @modified 
 *
 **/

/**
 *  
 *  @ingroup DSSS
 *   
 *  Solves the system of equations, where A is an N-by-N tridiagonal matrix: 
 *
 *      A * X = RHS
 *
**/

/**
 *  
 *  @param[in]
 *  N       int.
 *          N specifies the size of the vectors (DL, D, DU), which composed the
 *			tridiagonal matrix A, and the size of the vector RHS.
 *          N must be greater than zero.
 *    
 *  @param[in]
 *  DL      double *.
 *          DL is a pointer to the lower vector of the tridiagonal matrix A.  
 *
 *  @param[in]
 *  D       double *.
 *          D is a pointer to the diagonal vector of the tridiagonal matrix A.  
 *
 *  @param[in]
 *  DU      double *.
 *          DU is a pointer to the upper vector of the tridiagonal matrix A.  
 *  
 *	@param[in,out]
 *  RHS     double *.
 *          RHS is a pointer to the right-hand side vector of the equation.
 *          On exit, RHS is overwritten by the vector solution X.  
 *   
**/  

void dgtsvseq( int N, 
			double *DL, double *D, double *DU, 
			double *RHS )
{

	// Local variables
	int i;
	int last;
	
	last = N - 1;

    DU[0] /= D[0];
    RHS[0] /= D[0];

    for ( i = 1; i < last; i++ ) 
	{
        DU[i] /= D[i] - DL[i] * DU[i - 1];
        RHS[i] = ( RHS[i] - DL[i] * RHS[i - 1] ) 
					/ ( D[i] - DL[i] * DU[ i - 1 ]);
    }

    RHS[last] = (RHS[last] - DL[last] * RHS[last - 1]) 
					/ (D[last] - DL[last] * DU[last - 1]);

    for ( i = last - 1; i >= 0; i-- ) 
	{
        RHS[i] -= DU[i] * RHS[ i + 1];
    }

}
