脚本宝典收集整理的这篇文章主要介绍了PHP使用数组实现矩阵数学运算的方法示例,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了PHP使用数组实现矩阵数学运算的方法。分享给大家供大家参考,具体如下:
矩阵运算就是对两个数据表进行某种数学运算,并得到另一个数据表. 下面的例子中我们创建了一个基本完整的矩阵运算函数库,以便用于矩阵操作的程序中.
来自 PHP5 in PRactice (U.S.)Elliott III & Jonathan D.eisenhamer
brush:PHp;"> <>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,$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,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="">
以上是脚本宝典为你收集整理的PHP使用数组实现矩阵数学运算的方法示例全部内容,希望文章能够帮你解决PHP使用数组实现矩阵数学运算的方法示例所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。