How to use arrays to implement matrix mathematical operations in PHP

墨辰丷
Release: 2023-03-27 10:54:02
Original
1656 people have browsed it

This article mainly introduces the method of PHP using arrays to implement matrix mathematical operations, and analyzes the related operating skills of PHP based on arrays to implement matrix representation and operations based on specific examples. Friends in need can refer to the following

The details are as follows:

Matrix operation is to perform some kind of mathematical operation on two data tables and obtain another data table.
In the following example, we have created a basically complete matrix operation function library so that Programs for matrix operations.

From PHP5 in Practice (U.S.)Elliott III & Jonathan D.Eisenhamer

';
    // For each row in the matrix:
    for ($r = 0; $r < $rows; $r++) {
      // Begin the row:
      echo '';
      // For each column in this row
      for ($c = 0; $c < $columns; $c++) {
        // Echo the element:
        echo "{$matrix[$r][$c]}";
      }
      // End the row.
      echo '';
    }
    // End the table.
    echo "/n";
  } else {
    // It wasn't well formed:
    return false;
  }
}
// Let's do some testing. First prepare some formatting:
echo "/n";
// Now let's test element operations. We need identical sized matrices:
$m1 = array(
  array(5, 3, 2),
  array(3, 0, 4),
  array(1, 5, 2),
  );
$m2 = array(
  array(4, 9, 5),
  array(7, 5, 0),
  array(2, 2, 8),
  );
// Element addition should give us: 9  12   7
//                 10   5   4
//                  3   7  10
matrix_print(matrix_element_operation($m1, $m2, '+'));
// Element subtraction should give us:   1  -6  -3
//                    -4  -5   4
//                    -1   3  -6
matrix_print(matrix_element_operation($m1, $m2, '-'));
// Do a scalar multiplication on the 2nd matrix:  8 18 10
//                        14 10  0
//                         4  4 16
matrix_print(matrix_scalar_operation($m2, 2, '*'));
// Define some matrices for full matrix operations.
// Need to be complements of each other:
$m3 = array(
  array(1, 3, 5),
  array(-2, 5, 1),
  );
$m4 = array(
  array(1, 2),
  array(-2, 8),
  array(1, 1),
  );
// Matrix multiplication gives: 0  31
//                -11  37
matrix_print(matrix_operation($m3, $m4, '*'));
// Matrix addition gives:   9 20
//              4 15
matrix_print(matrix_operation($m3, $m4, '+'));
?>
Copy after login

Related recommendations:

Example of how PHP implements clockwise printing of matrix (spiralmatrix)

PHP implements graph adjacencyMatrixRepresentation and traversal algorithm

PHP implements two-dimensional arrayMatrixMethods and cases of transposition operation

The above is the detailed content of How to use arrays to implement matrix mathematical operations in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!