Symmetric Matrix Example Using PHP

Symmetric Matrix Example Using PHP

  • PHP
  • 2 mins read

A symmetric matrix is a square matrix (same number of rows and columns) that is equal to its transpose. In this tutorial, I am giving a Symmetric Matrix example using the PHP program.

Symmetric Matrix Example Using PHP Program

 

Here is a PHP program, for checking where a matrix is symmetric or not:

<html>
    <body>
        <?php
echo "<b>The matrix is given below:-</b>"."<br>";
$a=array(array());
$b=array(array());
$c=array(array());
$rows=4;
$cols=4;
$m=1;
$n=1;
for($i=0;$i<$rows;$i=$i+1)
{
    for($j=0;$j<$cols;$j=$j+1)
    {
        $a[$i][$j]=$m;
        echo $a[$i][$j]." ";
        $m=$m+1;
    }
    echo "<br>";
}
echo "<b>The transpose of the matrix is given below:-</b><br>";
for($i=0;$i<$rows;$i=$i+1)
{
    for($j=0;$j<$cols;$j=$j+1)
    {
        $b[$i][$j]=$a[$j][$i];
        echo $b[$i][$j]." ";
        $n=$n*1;
    }
    echo "<br>";
}
$count=0;
for($i=0;$i<$rows;$i=$i+1)
{
    for($j=0;$j<$cols;$j=$j+1)
    {
        if($a[$i][$j]==$b[$i][$j])
        {
            $count=$count+1;
        }
    }
}
if($count==$rows*$cols)
{
    echo "Yes, the matrix is a symmetric matrix";
}
else
{
    echo "The matrix is not a symmetric matrix";
}
        ?>
    </body>
</html>

Output:

The matrix is given below:-
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
The transpose of the matrix is given below:-
1 5 9 13 
2 6 10 14 
3 7 11 15 
4 8 12 16 
The matrix is not a symmetric matrix

In this program firstly we have created a matrix and then displayed the transpose of that matrix. After that, we have checked between values of each array element of two matrices and used a variable $count. Here using a condition (here $count==$rows*$cols), we have displayed the message "The matrix is not a symmetric matrix".