PHP使用数组实现矩阵数学运算的方法示例
本文实例讲述了PHP使用数组实现矩阵数学运算的方法。分享给大家供大家参考,具体如下:
矩阵运算就是对两个数据表进行某种数学运算,并得到另一个数据表.
下面的例子中我们创建了一个基本完整的矩阵运算函数库,以便用于矩阵操作的程序中.
来自PHP5inPractice (U.S.)ElliottIII&JonathanD.Eisenhamer
';
//Foreachrowinthematrix:
for($r=0;$r<$rows;$r++){
//Begintherow:
echo'';
//Foreachcolumninthisrow
for($c=0;$c<$columns;$c++){
//Echotheelement:
echo"{$matrix[$r][$c]} ";
}
//Endtherow.
echo' ';
}
//Endthetable.
echo"/n";
}else{
//Itwasn'twellformed:
returnfalse;
}
}
//Let'sdosometesting.Firstpreparesomeformatting:
echo"table{border:1pxsolidblack;margin:20px;}
td{text-align:center;}/n";
//Nowlet'stestelementoperations.Weneedidenticalsizedmatrices:
$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),
);
//Elementadditionshouldgiveus:9127
//1054
//3710
matrix_print(matrix_element_operation($m1,$m2,'+'));
//Elementsubtractionshouldgiveus:1-6-3
//-4-54
//-13-6
matrix_print(matrix_element_operation($m1,$m2,'-'));
//Doascalarmultiplicationonthe2ndmatrix:81810
//14100
//4416
matrix_print(matrix_scalar_operation($m2,2,'*'));
//Definesomematricesforfullmatrixoperations.
//Needtobecomplementsofeachother:
$m3=array(
array(1,3,5),
array(-2,5,1),
);
$m4=array(
array(1,2),
array(-2,8),
array(1,1),
);
//Matrixmultiplicationgives:031
//-1137
matrix_print(matrix_operation($m3,$m4,'*'));
//Matrixadditiongives:920
//415
matrix_print(matrix_operation($m3,$m4,'+'));
?>
PS:这里再为大家推荐几款在线计算工具供大家参考使用:
在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue
在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。