# 2.2.3.9.42 matrixbase::GetUpperTriangular

## Description

Get an upper triangular matrix from this matrix.

## Syntax

int GetUpperTriangular( matrixbase & mbResult, int nNthDiagonal = 0 )

## Parameters

mbResult
[output] The result matrix containing only the lower part
nNthDiagonal
[input] The number specifying N-th diagonal, 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_GetUpperTriangular_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.GetUpperTriangular(mat2);
if(rc!=0)
printf(" Error: GetUpperTriangular 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 upper triangular matrix
void matrixbase_GetUpperTriangular_ex2()
{
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}

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

//   UpperTriangular matrix from 1th diagonal above the main diagonal is:
//        { 0, 1, --,  1,  1}
//        { 0, 0,  1,  1,  1}
//        { 0, 0,  0,  1,  1}

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);

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

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

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

## Remark

Get an upper triangular matrix from this matrix. This matrix and the result matrix may be the same.