PHP利用數組實現矩陣數學運算的方法

墨辰丷
發布: 2023-03-27 10:54:02
原創
1657 人瀏覽過

這篇文章主要介紹了PHP使用陣列實現矩陣數學運算的方法,結合具體實例形式分析了php基於數組實現矩陣表示與運算的相關操作技巧,需要的朋友可以參考下

具體如下:

矩陣運算就是對兩個數據表進行某種數學運算,並得到另一個數據表.
下面的例子中我們創建了一個基本完整的矩陣運算函數庫,以便用於矩陣運算的程式中.

來自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, '+'));
?>
登入後複製

相關推薦:

PHP實作順時針列印矩陣(螺旋矩陣)的方法範例

PHP實作圖的鄰接矩陣表示及遍歷演算法

PHP實作二維陣列的矩陣轉置運算的方法及案例

以上是PHP利用數組實現矩陣數學運算的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!