PHP Program to find the product of two matrices


February 9, 2022, Learn eTutorial
1162

How to find the product of two matrices?

To find the product of 2 two-dimensional arrays first we have to find the number of rows and columns in the array and multiply the corresponding row and column and two matrices and add them together. For example, If the arrays are

        2  3  8               1  5  1

a[] = 3  2  1     b[] =  6  1  5

        3  5  6               4  1  2

then the result will be:

2*1+3*6+8*4    2*5+3*1+8*1    2*1+3*5+8*2

3*1+2*6+1*4    3*5+2*1+1*1    3*1+2*5+1*5

3*1+5*6+6*4    3*5+5*1+6*1    3*1+5*5+6*2

which is

52   21   33
19   18   15
57   26   40

How to find the product of two matrices using PHP?

In this program, we are using the static values which are initialized in the code itself. First, we have to initialize 2 two-dimensional arrays a1 and a2 we have to assign the number of rows and columns into the variable row and col using the built-in function count(). Then we have to create an empty array mul[] to store the product of array a1[] and a2[]. Then assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to perform another loop for that first assign the value 0 into the variable k and perform the loop until the condition 'k < row' becomes false and increment the value of variable k in every iteration and in the block of the loop we have to assign the computed value of 'mul[i][j] + a1[i][k] * a2[k][j]' into the array mul[i][j]. After the completion of the loops, we can print the values of the array mul[] as the product of the array a1[] and a2[].

ALGORITHM

Step 1: Initialize 2 two-dimensional arrays a1[] and a2[] with values

Step 2: Assign the number of rows and columns into the variable row and col using the built-in function count()

Step 3: Create an empty array mul[] to store the product result of a1[] and a2[]

Step 4: Print the elements of the arrays a1[] and a2[] by using the for loop

Step 5: To find the product of the arrays a1[] and a2[] by performing the for loop for that first, assign the value 0 into the variable i and perform the sub-step until the condition 'i < row' becomes false and increment the value of variable i in every iteration

        (i) Assign the value 0 into the variable j and perform the sub-step ii until the condition 'j < col' becomes false and increment the value of variable j in              every iteration

        (ii) Assign the value 0 into the variable k and perform the sub-step until the condition 'k < row' becomes false and increment the value of variable k               in every iteration

            (a) Assign the computed value of 'mul[i][j] + a1[i][k] * a2[k][j]' into the array mul[i][j]

Step 6: Print the values of the array mul[] as the product of arrays a1[] and a2[]

PHP Source Code

                                          <?php
$a1 = array(
    array(1, 2, 9),
    array(4, 5, 6),
    array(7, 2, 3)
);
$a2 = array(
    array(9, 8, 7),
    array(6, 5, 4),
    array(3, 2, 1)
);
$row = count($a1);
$col = count($a1[0]);
$mul = array();
echo "The first matrix is: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a1[$i][$j] . " ";
    }
    echo "\n";
}
echo "The second matrix is: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a2[$i][$j] . " ";
    }
    echo "\n";
}
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        for ($k = 0; $k < $row; $k++) {
            $mul[$i][$j] = $mul[$i][$j] + $a1[$i][$k] * $a2[$k][$j];
        }
    }
}
echo "Product of two matrices is: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $mul[$i][$j] . "  ";
    }
    echo "\n";
}
?>
                                      

OUTPUT

The first matrix is:
1 2 9
4 5 6
7 2 3
The second matrix is:
9 8 7
6 5 4
3 2 1
Product of two matrices is:
48  36  24
84  69  54
84  72  60