最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP使用數(shù)組實現(xiàn)矩陣數(shù)學運算的方法示例

 更新時間:2017年05月29日 11:31:09   作者:dengzonghuan  
這篇文章主要介紹了PHP使用數(shù)組實現(xiàn)矩陣數(shù)學運算的方法,結(jié)合具體實例形式分析了php基于數(shù)組實現(xiàn)矩陣表示與運算的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHP使用數(shù)組實現(xiàn)矩陣數(shù)學運算的方法。分享給大家供大家參考,具體如下:

矩陣運算就是對兩個數(shù)據(jù)表進行某種數(shù)學運算,并得到另一個數(shù)據(jù)表.
下面的例子中我們創(chuàng)建了一個基本完整的矩陣運算函數(shù)庫,以便用于矩陣操作的程序中.

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

<?php
// A Library of Matrix Math functions.
// All assume a Matrix defined by a 2 dimensional array, where the first
// index (array[x]) are the rows and the second index (array[x][y])
// are the columns
// First create a few helper functions
// A function to determine if a matrix is well formed. That is to say that
// it is perfectly rectangular with no missing values:
function _matrix_well_formed($matrix) {
  // If this is not an array, it is badly formed, return false.
  if (!(is_array($matrix))) {
    return false;
  } else {
    // Count the number of rows.
    $rows = count($matrix);
    // Now loop through each row:
    for ($r = 0; $r < $rows; $r++) {
      // Make sure that this row is set, and an array. Checking to
      // see if it is set is ensuring that this is a 0 based
      // numerically indexed array.
      if (!(isset($matrix[$r]) && is_array($matrix[$r]))) {
        return false;
      } else {
        // If this is row 0, calculate the columns in it:
        if ($r == 0) {
          $cols = count($matrix[$r]);
        // Ensure that the number of columns is identical else exit
        } elseif (count($matrix[$r]) != $cols) {
          return false;
        }
        // Now, loop through all the columns for this row
        for ($c = 0; $c < $cols; $c++) {
          // Ensure this entry is set, and a number
          if (!(isset($matrix[$r][$c]) &&
              is_numeric($matrix[$r][$c]))) {
            return false;
          }
        }
      }
    }
  }
  // Ok, if we actually made it this far, then we have not found
  // anything wrong with the matrix.
  return true;
}
// A function to return the rows in a matrix -
//  Does not check for validity, it assumes the matrix is well formed.
function _matrix_rows($matrix) {
  return count($matrix);
}
// A function to return the columns in a matrix -
//  Does not check for validity, it assumes the matrix is well formed.
function _matrix_columns($matrix) {
  return count($matrix[0]);
}
// This function performs operations on matrix elements, such as addition
// or subtraction. To use it, pass it 2 matrices, and the operation you
// wish to perform, as a string: '+', '-'
function matrix_element_operation($a, $b, $operation) {
  // Verify both matrices are well formed
  $valid = false;
  if (_matrix_well_formed($a) && _matrix_well_formed($b)) {
    // Make sure they have the same number of columns & rows
    $rows = _matrix_rows($a);
    $columns = _matrix_columns($a);
    if (($rows == _matrix_rows($b)) &&
        ($columns == _matrix_columns($b))) {
      // We have a valid setup for continuing with element math
      $valid = true;
    }
  }
  // If invalid, return false
  if (!($valid)) { return false; }
  // For each element in the matrices perform the operatoin on the
  // corresponding element in the other array to it:
  for ($r = 0; $r < $rows; $r++) {
    for ($c = 0; $c < $columns; $c++) {
      eval('$a[$r][$c] '.$operation.'= $b[$r][$c];');
    }
  }
  // Return the finished matrix:
  return $a;
}
// This function performs full matrix operations, such as matrix addition
// or matrix multiplication. As above, pass it to matrices and the
// operation: '*', '-', '+'
function matrix_operation($a, $b, $operation) {
  // Verify both matrices are well formed
  $valid = false;
  if (_matrix_well_formed($a) && _matrix_well_formed($b)) {
    // Make sure they have complementary numbers of rows and columns.
    // The number of rows in A should be the number of columns in B
    $rows = _matrix_rows($a);
    $columns = _matrix_columns($a);
    if (($columns == _matrix_rows($b)) &&
        ($rows == _matrix_columns($b))) {
      // We have a valid setup for continuing
      $valid = true;
    }
  }
  // If invalid, return false
  if (!($valid)) { return false; }
  // Create a blank matrix the appropriate size, initialized to 0
  $new = array_fill(0, $rows, array_fill(0, $rows, 0));
  // For each row in a ...
  for ($r = 0; $r < $rows; $r++) {
    // For each column in b ...
    for ($c = 0; $c < $rows; $c++) {
      // Take each member of column b, with each member of row a
      // and add the results, storing this in the new table:
      // Loop over each column in A ...
      for ($ac = 0; $ac < $columns; $ac++) {
        // Evaluate the operation
        eval('$new[$r][$c] += $a[$r][$ac] '.
          $operation.' $b[$ac][$c];');
      }
    }
  }
  // Return the finished matrix:
  return $new;
}
// A function to perform scalar operations. This means that you take the scalar value,
// and the operation provided, and apply it to every element.
function matrix_scalar_operation($matrix, $scalar, $operation) {
  // Verify it is well formed
  if (_matrix_well_formed($matrix)) {
    $rows = _matrix_rows($matrix);
    $columns = _matrix_columns($matrix);
    // For each element in the matrix, multiply by the scalar
    for ($r = 0; $r < $rows; $r++) {
      for ($c = 0; $c < $columns; $c++) {
        eval('$matrix[$r][$c] '.$operation.'= $scalar;');
      }
    }
    // Return the finished matrix:
    return $matrix;
  } else {
    // It wasn't well formed:
    return false;
  }
}
// A handy function for printing matrices (As an HTML table)
function matrix_print($matrix) {
  // Verify it is well formed
  if (_matrix_well_formed($matrix)) {
    $rows = _matrix_rows($matrix);
    $columns = _matrix_columns($matrix);
    // Start the table
    echo '<table>';
    // For each row in the matrix:
    for ($r = 0; $r < $rows; $r++) {
      // Begin the row:
      echo '<tr>';
      // For each column in this row
      for ($c = 0; $c < $columns; $c++) {
        // Echo the element:
        echo "<td>{$matrix[$r][$c]}</td>";
      }
      // End the row.
      echo '</tr>';
    }
    // End the table.
    echo "</table>/n";
  } else {
    // It wasn't well formed:
    return false;
  }
}
// Let's do some testing. First prepare some formatting:
echo "<mce:style><!--
table { border: 1px solid black; margin: 20px; }
td { text-align: center; }
--></mce:style><style mce_bogus="1">table { border: 1px solid black; margin: 20px; }
td { text-align: center; }</style>/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, '+'));
?>

PS:這里再為大家推薦幾款在線計算工具供大家參考使用:

在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)學運算技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php正則表達式用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • PHP實現(xiàn)的字符串匹配算法示例【sunday算法】

    PHP實現(xiàn)的字符串匹配算法示例【sunday算法】

    這篇文章主要介紹了PHP實現(xiàn)的字符串匹配算法,簡單描述了sunday算法的概念與原理,并結(jié)合實例形式分析了php基于sunday算法實現(xiàn)字符串匹配操作相關(guān)技巧,需要的朋友可以參考下
    2017-12-12
  • 一文搞懂PHP中的DI依賴注入

    一文搞懂PHP中的DI依賴注入

    依賴注入DI 其實本質(zhì)上是指對類的依賴通過構(gòu)造器完成 自動注入。本文將通過一些示例帶大家深入了解一下PHP中的DI依賴注入,需要的可以參考一下
    2022-08-08
  • 詳解php微信小程序消息推送配置

    詳解php微信小程序消息推送配置

    這篇文章主要介紹了php微信小程序消息推送配置,對微信小程序推送感興趣的同學,可以參考下
    2021-04-04
  • PHP實現(xiàn)動態(tài)獲取函數(shù)參數(shù)的方法示例

    PHP實現(xiàn)動態(tài)獲取函數(shù)參數(shù)的方法示例

    這篇文章主要介紹了PHP實現(xiàn)動態(tài)獲取函數(shù)參數(shù)的方法,結(jié)合實例形式分析了php針對函數(shù)參數(shù)操作func_num_args()、func_get_arg()及func_get_args()函數(shù)相關(guān)使用技巧,需要的朋友可以參考下
    2018-04-04
  • PHP中比較兩個對象的幾種方式小結(jié)

    PHP中比較兩個對象的幾種方式小結(jié)

    在PHP中,比較兩個對象并不是一件直接明了的事情,因為對象之間的比較通常依賴于它們的屬性和狀態(tài),而這些屬性和狀態(tài)可能非常復雜且多樣化,本文給大家總結(jié)了PHP中比較兩個對象的幾種方式,需要的朋友可以參考下
    2024-09-09
  • php Imagick獲取圖片RGB顏色值

    php Imagick獲取圖片RGB顏色值

    根據(jù)用戶上傳的圖片檢索出圖片的主要顏色值,再根據(jù)顏色搜索相關(guān)的圖片,使用Imagick的quantizeImage方法能夠很方便的取到圖片中平均的RGB值
    2014-07-07
  • php采用curl實現(xiàn)偽造IP來源的方法

    php采用curl實現(xiàn)偽造IP來源的方法

    這篇文章主要介紹了php采用curl實現(xiàn)偽造IP來源的方法,主要涉及使用curl的CURLOPT_REFERER參數(shù)實現(xiàn)該功能,需要的朋友可以參考下
    2014-11-11
  • 解析php中static,const與define的使用區(qū)別

    解析php中static,const與define的使用區(qū)別

    本篇文章是對php中static,const與define的使用區(qū)別進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 深入分析PHP優(yōu)化及注意事項

    深入分析PHP優(yōu)化及注意事項

    本篇文章是對PHP高效率寫法進行了詳細的分析介紹,總結(jié)的十分細致全面,對大家提升PHP水平很有幫助,需要的朋友參考下!
    2016-07-07
  • PHP實現(xiàn)下載斷點續(xù)傳的方法

    PHP實現(xiàn)下載斷點續(xù)傳的方法

    這篇文章主要介紹了PHP實現(xiàn)下載斷點續(xù)傳的方法,通過自定義函數(shù)來實現(xiàn)PHP的斷點續(xù)傳下載方法,涉及文件的常見操作與指針和緩沖的用法,代碼中備有較為詳盡的注釋便于閱讀和理解,需要的朋友可以參考下
    2014-11-11

最新評論

吴桥县| 邵武市| 曲靖市| 甘德县| 定日县| 图们市| 苍溪县| 花莲市| 枣庄市| 攀枝花市| 铜陵市| 龙里县| 威信县| 巍山| 汽车| 抚松县| 双峰县| 安多县| 玉龙| 奉贤区| 宿迁市| 三河市| 疏勒县| 敖汉旗| 宁晋县| 西宁市| 青州市| 疏勒县| 社旗县| 陵川县| 丹江口市| 昌邑市| 买车| 宜都市| 平山县| 万安县| 昂仁县| 西青区| 宣威市| 阿瓦提县| 诸暨市|