Open In App

How to Find Multiplication of Two Matrices of any Size in PHP?

Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product.

Below are the approaches to find the Multiplication of two Matrices of any size in PHP:

Using nested loops

This approach involves using nested for loops to iterate through the rows and columns of the matrices. For each element in the result matrix, the corresponding elements in the input matrices are multiplied and added together.

Example: This example uses nested for loops to find the Multiplication of two Matrices of any size In PHP.

<?php
function multiplyMatrices($matrix1, $matrix2) {
    $rows1 = count($matrix1);
    $cols1 = count($matrix1[0]);
    $cols2 = count($matrix2[0]);

    if ($cols1 != count($matrix2)) {
        return "Matrices cannot be multiplied. Number of 
                columns in the first matrix must be equal to 
                the number of rows in the second matrix.";
    }

    $result = array_fill(0, $rows1, array_fill(0, $cols2, 0));

    for ($i = 0; $i < $rows1; $i++) {
        for ($j = 0; $j < $cols2; $j++) {
            for ($k = 0; $k < $cols1; $k++) {
                $result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j];
            }
        }
    }

    return $result;
}

// Example matrices
$matrix1 = [
    [4, 2],
    [6, 5],
];

$matrix2 = [
    [3, 2],
    [7, 4],
];

$result = multiplyMatrices($matrix1, $matrix2);

echo "Matrix 1:\n";
foreach ($matrix1 as $row) {
    echo implode(" ", $row) . "\n";
}

echo "\nMatrix 2:\n";
foreach ($matrix2 as $row) {
    echo implode(" ", $row) . "\n";
}

echo "\nResult:\n";
foreach ($result as $row) {
    echo implode(" ", $row) . "\n";
}
?>

Output
Matrix 1:
4 2
6 5

Matrix 2:
3 2
7 4

Result:
26 16
53 32

Using the array_map

We are using array_map function that can be used to perform element-wise multiplication and addition. We can apply this function to each row of the first matrix, multiplying it by the second matrix, and then summing the results to get the final matrix.

Example: This example uses array_map to find Multiplication of two Matrices of any size In PHP.

<?php
function multiplyMatrices($matrix1, $matrix2) {
    $result = array_map(function ($row) use ($matrix2) {
        return array_map(function ($col) use ($row) {
            return array_sum(array_map(function ($a, $b) {
                return $a * $b;
            }, $row, $col));
        }, transpose($matrix2));
    }, $matrix1);

    return $result;
}

function transpose($matrix) {
    return array_map(null, ...$matrix);
}

// Example matrices
$matrix1 = [
    [2, 3],
    [4, 5],
];

$matrix2 = [
    [1, 2],
    [3, 4],
];

$result = multiplyMatrices($matrix1, $matrix2);

echo "Matrix 1:\n";
foreach ($matrix1 as $row) {
    echo implode(" ", $row) . "\n";
}

echo "\nMatrix 2:\n";
foreach ($matrix2 as $row) {
    echo implode(" ", $row) . "\n";
}

echo "\nResult:\n";
foreach ($result as $row) {
    echo implode(" ", $row) . "\n";
}
?>

Output
Matrix 1:
2 3
4 5

Matrix 2:
1 2
3 4

Result:
11 16
19 28
Article Tags :