# 2.2.3.9.31 matrixbase::GetLowerTriangular

## Description

Get a lower triangular matrix from this matrix.

## Syntax

int GetLowerTriangular( matrixbase & mbResult, int nNumDiagonal = 0 )

## Parameters

mbResult
[output] The result matrix containing only the lower part
nNumDiagonal
[input] Default 0 is the main diagonal, > 0 is above the
main diagonal, and < 0 is below the main diagonal.

## Return

Returns 0 on success and a non-zero error code on failure.

-1=Internal cast error.

## Examples

EX1

void matrixbase_GetLowerTriangular_ex1()
{
matrix<double> mat1 = {
{ 1, 1, 99,  1,  1},
{99, 1,  1,  1,  1},
{ 1, 1,  1,  1,  1}
};
mat1[1][0]=NANUM;  // set row=2,col=1 to NANUM
mat1[0][2]=NANUM;  // set row=1,col=3 to NANUM
matrix<double> mat2;

int rc = mat1.GetLowerTriangular(mat2);
if(rc!=0)
printf(" Error: GetLowerTriangular failed. Error Code=%d\n",rc);
else{
printf("The matrix is:\n");
for(int ii=0; ii< mat2.GetNumRows(); ii++){
for(int jj=0; jj< mat2.GetNumCols(); jj++)
printf("%g  ", mat2[ii][jj]);
printf("\r\n");
}
}
}

EX2

// Convert a matrix to the lower triangular matrix
void matrixbase_GetLowerTriangular_ex1()
{
matrix<double> mat1 = {
{ 1, 1, 99,  1,  1},
{99, 1,  1,  1,  1},
{ 1, 1,  1,  1,  1}
};
mat1[1][0]=NANUM;  // set row=2,col=1 to NANUM
mat1[0][2]=NANUM;  // set row=1,col=3 to NANUM

//   Input matrix is:
//        { 1, 1, --,  1,  1}
//        {--, 1,  1,  1,  1}
//        { 1, 1,  1,  1,  1}

//   LowerTriangular matrix is:
//        { 1, 0,  0,  0,  0}
//        {--, 1,  0,  0,  0}
//        { 1, 1,  1,  0,  0}

//   LowerTriangular matrix from the 1th diagonal below the main diagonalis:
//        { 0, 0,  0,  0,  0}
//        {--, 0,  0,  0,  0}
//        { 1, 1,  0,  0,  0}

MatrixPage MatPg1;
MatPg1.Create("Origin");
MatrixLayer MatLy1 = MatPg1.Layers(0);
Matrix Mat1(MatLy1);
Mat1 = mat1;
printf("  Original matrix is %s.\n",Mat1.GetName());

MatrixPage MatPg2;
MatPg2.Create("Origin");
MatrixLayer MatLy2 = MatPg2.Layers(0);
Matrix Mat2(MatLy2);

int rc=Mat1.GetLowerTriangular(Mat2); //Put the lower triangular matrix of Mat1 to Mat2
if(rc!=0)
printf("  Error: GetLowerTriangular failed. Error Code=%d\n",rc);
else
{
printf("  Lower triangular matrix is %s.\n",Mat2.GetName());
printf("  Note that the NANUM in the lower triangular area is preserved,\n");
printf("  Note that the NANUM in the upper triangular area became 0,\n");
}

MatrixPage MatPg3;
MatPg3.Create("Origin");
MatrixLayer MatLy3 = MatPg3.Layers(0);
Matrix Mat3(MatLy3);

rc=Mat1.GetLowerTriangular(Mat3,-1); //Put the lower triangular matrix of Mat1 to Mat3 from the 1th diagonal below the main diagonal
if(rc!=0)
printf("  Error: GetLowerTriangular failed. Error Code=%d\n",rc);
else
{
printf("  Lower triangular matrix is %s.\n",Mat3.GetName());
printf("  Note that the NANUM in the lower triangular area is preserved,\n");
printf("  Note that the NANUM in the upper triangular area became 0,\n");
}

}

## Remark

Get a lower triangular matrix from this matrix. This matrix and the result matrix may be the same.