Open In App

Php Program to check if matrix is lower triangular

Improve
Improve
Like Article
Like
Save
Share
Report

Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. 
 

Examples: 
 

Input : mat[4][4] = {{1, 0, 0, 0},
                     {1, 4, 0, 0},
                     {4, 6, 2, 0},
                     {0, 4, 7, 6}};
Output : Matrix is in lower triangular form.

Input : mat[4][4] = {{1, 0, 0, 0},
                     {4, 3, 0, 1},
                     {7, 9, 2, 0},
                     {8, 5, 3, 6}};
Output : Matrix is not in lower triangular form.

PHP




<?php
// PHP Program to check lower
// triangular matrix.
$N = 4;
 
// Function to check matrix is in
// lower triangular form or not.
function isLowerTriangularMatrix($mat)
{
    global $N;
    for ($i = 0; $i < $N; $i++)
        for ($j = $i + 1; $j < $N; $j++)
            if ($mat[$i][$j] != 0)
                return false;
    return true;
}
 
// Driver Code
$mat = array(array( 1, 0, 0, 0 ),
             array( 1, 4, 0, 0 ),
             array( 4, 6, 2, 0 ),
             array( 0, 4, 7, 6 ));
     
 
// Function call
if (isLowerTriangularMatrix($mat))
    echo("Yes");
else
    echo("No");
 
// This code is contributed by Ajit.
?>


Output: 
 

Yes

Time Complexity: O(n2), where n represents the number of rows and columns of the given matrix.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Please refer complete article on Program to check if matrix is lower triangular for more details!


Last Updated : 12 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads