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

/**
 *
 * 	@file ddss_dposv.c
 *
 * 	@brief LASs-DDSs ddss_dpotrf routine.
 *
 * 	LASs-DDSs is a software package provided by:
 * 	Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
 *
 * 	@author Pedro Valero-Lara pedro.valero@bsc.es
 * 	@date 2018-05-04
 * 	@reviewer 
 * 	@modified 
 *
 **/

/**
 *  
 *	@ingroup DDSS
 *   
 *	Solves a system of linear equations A \times X = B, where A is a m-by-m
 *	simmetric positive definite matrix and X and B are m-by-nrhs matrices.  
 *	The matrix A is descomposed as:
 *
 *		A = L \times L^T
 *		or
 *		A = U^T \times U
 *
 *	where L is a lower triangular matrix and U is an upper triangular matrix.
 *
**/

/**
 *	
 * 	@param[in]
 *	UPLO	enum DDSS_UPLO.
 *	        UPLO specifies the form of A is stored:
 *       	- Lower: Lower triangle of A is stored. The upper traingular part is
 *       	not referenced.
 *      	- Upper: Upper triangle of A is stored. The lower triangular part is
 *      	not referenced.
 *	
 *	@param[in]
 *	N       int.
 *          N specifies the order of the square matrix A. N >= 0.
 *    
 *	@param[in]
 *	NRHS    int.
 *          NRHS specifies the number of right-hand-sides (number 
 *			of columns of B). NRHS >= 0.
 *
 *	@param[in,out]
 *  A  		double *.
 *	    	A is a pointer to a positive definite matrix of dimension N by LDA. 
 *			On exit, if return value is Success, the matrix A
 * 			is overwriten by the factor U or L.
 *   
 *	@param[in]
 *  LDA     int.
 * 	    	LDA specifies the number of columns of A ( row-major order ).
 * 	    	LDA must be at least max( 1, N ).
 *
 *	@param[in,out]
 *  B  		double *.
 *	    	B is a pointer to a matrix of dimension N by NRHS, which stores the 
 *			right-hand-sides of the systems of linear equations.  
 *			(row-major order). On exit, if return value is Success, the matrix B
 * 			is overwriten by the solution matrix X.
 *   
 *	@param[in]
 *  LDB     int.
 * 	    	LDB specifies the number of columns of B ( row-major order ).
 * 	    	LDB must be at least max( 1, NRHS ).
 *
 **/

/**
 *
 *	@retval Success sucessful exit
 *	@retval NoSuccess unsucessful exit
 *
 **/

/**
 *
 *	@sa kdposv
 *
 **/ 

int ddss_dposv( enum DDSS_UPLO UPLO,
        int N, int NRHS,
        double *A, int LDA,
        double *B, int LDB )
{

	// Argument checking
	if ( ( UPLO != Upper ) && ( UPLO != Lower ) )
	{
		fprintf( stderr, "Illegal value of UPLO, in ddss_dposv code\n" );
		return NoSuccess;
	}
	
	if ( N < 0 )
	{
		fprintf( stderr, "Illegal value of N, in ddss_dposv code\n" );
		return NoSuccess;
	}
	
	if ( NRHS < 0 )
	{
		fprintf( stderr, "Illegal value of NRHS, in ddss_dposv code\n" );
		return NoSuccess;
	}

	if ( LDA < MAX( 1, N ) )
	{
		fprintf( stderr, "Illegal value of LDA, in ddss_dposv code\n" );
		return NoSuccess;
	}

	if ( LDB < MAX( 1, NRHS ) )
	{
		fprintf( stderr, "Illegal value of LDB, in ddss_dposv code\n" );
		return NoSuccess;
	}

	// Quick return
	if ( MAX( N, 0 ) == 0 || MAX( NRHS, 0 ) == 0 )
	{
		return Success;
	}
	
	return kdposv( UPLO, N, NRHS, A, LDA, B, LDB );
	
}
