It's free and you have access to premium codes!
Welcome back! Please login to your account.
Don't worry, we'll send you a message to help you to recover your acount.
Please check your email for instructions to activate your account.
Written by 20 November 2012
If you are a student or a researcher, you may need to offer some asthmatics in your website. This script has a class for doing some matrix operations such as Multiplying, Adding, Subtracting and Dividing. You can learn how to combine HTML + PHP + Mathematics with this code; Enjoy!
<!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->
<?php
$r='';
$M1='';
$M2='';
if (isset($_POST['Submit'])){
$M1=$_POST['m1'];
$M2=$_POST['m2'];
$m1=explode("\n",$M1);
$m2=explode("\n",$M2);
for ($i=0; $i<count($m1);$i++)
$a[$i]=explode(',',$m1[$i]);
for ($i=0; $i<count($m2);$i++)
$b[$i]=explode(',',$m2[$i]);
$op=$_POST['operation'];
$mat=new matrix;
switch ($op){
case 'Divide':
$b=$_POST['m2'];
$rr=$mat->divide($a,$b);
break;
case 'Multiply':
$rr=$mat->multiply($a,$b);
break;
case 'Add':
$rr=$mat->add($a,$b);
break;
case 'Subtract':
$rr=$mat->subtract($a,$b);
break;
}
for ($i=0;$i<count($rr);$i++){
for($j=0;$j<count($rr[$i]);$j++)
$r.=$rr[$i][$j].',';
$r=substr($r,0,strlen($r)-1)."\n";
}
}
class matrix{
public function multiply($m1,$m2){
$r=count($m1);
$c=count($m2[0]);
$p=count($m2);
if(count($m1[0])!=$p)die('Incompatible Matrixes');
for ($i=0;$i< $r;$i++){
for($j=0;$j<$c;$j++){
$m3[$i][$j]=0;
for($k=0;$k<$p;$k++){
$m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j];
}
}
}
return($m3);
}
public function add($m1,$m2){
if (count($m1)!=count($m2) || count($m1[0])!=count($m2[0])) die('Incompatible Matrixes');
for ($i=0;$i<count($m1);$i++){
for($j=0;$j<count($m1[0]);$j++){
$m3[$i][$j]=$m1[$i][$j]+$m2[$i][$j];
}
}
return($m3);
}
public function subtract($m1,$m2){
if (count($m1)!=count($m2) || count($m1[0])!=count($m2[0])) die('Incompatible Matrixes');
for ($i=0;$i<count($m1);$i++){
for($j=0;$j<count($m1[0]);$j++){
$m3[$i][$j]=$m1[$i][$j]-$m2[$i][$j];
}
}
return($m3);
}
public function divide($m1,$m2){ //$m2 must be num
if (is_array($m2)) die ('Pleae enter e number for deviding not an array');
for ($i=0;$i<count($m1);$i++){
for($j=0;$j<count($m1[0]);$j++){
$m3[$i][$j]=$m1[$i][$j]/$m2;
}
}
return($m3);
}
}
?>
<form method="post">
<div style="text-align: center">
<table style="border: 1px solid #000000;width: 543px" align="center">
<tr>
<td style=" padding: 5px; font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; width: 269px; color: #2214B9;border-style: solid;border-width: 1px; background-color: #FFFFD7;">
<strong>Matrix 1</strong></td>
<td style="padding: 5px;font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; color: #2214B9;border-style: solid;border-width: 1px; width: 270px; background-color: #FFFFD7;">
<strong>
Matrix 2</strong></td>
</tr>
<tr>
<td style=" padding: 5px; font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; width: 269px; color: #2214B9;border-style: solid;border-width: 1px; background-color: #D8FFD7;">
<textarea name="m1" style="height: 174px; width: 237px; font-size: 14pt; font-family: 'times New Roman', Times, serif;"><?php echo $M1?></textarea></td>
<td style="padding: 5px;font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; color: #2214B9;border-style: solid;border-width: 1px; width: 270px; background-color: #D8FFD7;">
<textarea cols="20" name="m2" rows="1" style="height: 174px; width: 237px; font-size: 14pt; font-family: 'times New Roman', Times, serif;"><?php echo $M2?></textarea></td>
</tr>
<tr>
<td style=" padding: 5px; font-family: 'Times New Roman', Times, serif;font-size: 20pt; text-align: center; color: #2214B9;border-style: solid;border-width: 1px; background-color: #D7E5FF;" colspan="2">
<select name="operation" style="width: 140px; font-size: 14pt; font-family: 'times New Roman', Times, serif">
<option selected="">Multiply</option>
<option>Divide</option>
<option>Add</option>
<option>Subtract</option>
</select><br><br style="font-size: 10pt">
<input name="Submit" style="width: 95px; height: 48px; margin-top: 0px; font-size: 20pt; font-family: 'times New Roman', Times, serif;" type="submit" value="Do"></td>
</tr>
</table>
<br>
<?php if (isset($_POST['Submit'])) {?>
<span style="font-family: 'Times New Roman', Times, serif; font-size: 20pt; color: #1A7D16">
Result of <?php echo $op?>ing: </span><br>
<textarea cols="20" name="TextArea3" rows="1" style="height: 174px; width: 237px; font-size: 14pt; font-family: 'times New Roman', Times, serif; background-color: #FFD7F6;"><?php print_r( $r)?></textarea></div>
<?php }?></form>
<div style="text-align: center">
<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
<a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>
Comments
Here you can leave us commments. Let us know what you think about this code tutorial!