Page 1 of 1

MATRIX MULTIPLICATION!! DONE!!

Posted: Tue Jan 31, 2006 6:58 pm
by Charles256
Okay okay. Check it out! It's not perfect. It will be optimized. It will be OOPed and it will be all your dreams come true but for right now this does work. Take it, run with it! I'm sure I'm the only one that has ever needed it but in the off chance anybody else wants it here it is.

Code: Select all

<?php      
$matrix=array();
$matrix2=array();
function check_col($rows,$x)
{
    for ($i=0;$i<(count($rows)-1);$i++)
        {
            $o="".($i+1)."";
            $currentcols=explode(" ",$rows[$i]);
            $nextcols=explode(" ",$rows[$o]);
            if(count($currentcols)!=count($nextcols))
            {
                echo "The number of columns must be the same in every row. Try again.
                The error lies on row $o and ".($o+1)." in matrix $x. <a href='test.php'>Try again?</a>";
                die();
            }
            else
            {
                echo "The number of columns is the same in the following two rows in matrix $x.                Rows $o and ".($o+1).". <br />";
            }
            if ($i==(count($rows)-2))
            {
                echo "Second test completed on matrix $x. There are an equal number of columns in every row. <Br />";
            }
        }
}
function check_int($rows,$x)
{
    for ($i=0;$i<count($rows);$i++)
        {
            $colentries=explode(" ",$rows[$i]);
            for ($o=0;$o<count($colentries);$o++)
            {
                if ($x==1)
                {
                     global $matrix;
                     $matrix[$i][$o]=$colentries[$o];
                }
                else if ($x==2)
                {
                     global $matrix2;
                     $matrix2[$i][$o]=$colentries[$o];
                }
                    
                if (!(is_numeric($colentries[$o])))
                {
                    
                    echo "All entries must be integers. More than likely you have a trailin space which is causing this error. Hit the back button on your browser and edit your entries.";
                    die();
                }
                    
            }
        }
         echo " Third test passed. All entries in matrix $x are numeric.<br />";   
}
if (isset($_GET['process']))
{
    if ($_GET['process']=="yes")
    {
        $mat1=$_POST['mat1'];
        $mat2=$_POST['mat2'];
        $rows1=explode(";",$mat1);
        $rows2=explode(";",$mat2);
        $columns1=explode(" ",$rows1['0']);
        $columns2=explode(" ",$rows2['0']);
        if (count($rows2)!=count($columns1))
        {
            echo " The number of rows in matrix 2 must be equal to the number of columns in matrix 1. <a href='test.php'>Try again?</a>";
            die();
        }
        else
        {
            echo "First test passed. The number of rows in matrix two does equal the number of columns in matrix one.<br />";
        }
        check_col($rows1,1);
        check_col($rows2,2);
        check_int($rows1,1);
        check_int($rows2,2);
        echo "The resulting matrix will be a ".count($rows1)." by ".count($columns2)." matrix. <br /> Here is the resulting matrix :<br />";
        
        global $matrix,$matrix2;
        $array1=array();
        for ($x=0;$x<count($rows1);$x++)
        {
		  for ($y=0;$y<count($columns2);$y++)
		  {
		  	for ($z=0;$z<count($rows2);$z++)
			  {
			    $array1[]=$matrix[$x][$z]*$matrix2[$z][$y];			    
			  }  
		  }
		}
		$entry=0;
		$actualentry=1;
		for ($w=0; $w<count($array1);$w++)
		{
		  if ((($w % count($columns1))==0) && ($w != 0))
		  {
		    echo "Entry ".$actualentry.":";
		    echo "$entry <Br />";
		    $actualentry++;
		    $entry=0;
		  }
			$entry+=$array1[$w];
		}
		echo "Entry ".$actualentry.":";
		echo $entry;  
    }
}
else
{
 echo " Seperate rows with a ; and columns are seperated using spaces. Pretty intuitive,ain't it? Please remember to not put a space between rows. I.E. '2 4 6 7;2 4 6 8' is correct. However, '2 3 4 5; 3 2 3 3' is not correct.
        <form action='test.php?process=yes' method='POST'>
        Matrix 1:<input type='text' length='70' name='mat1'></input><br />
        Matrix 2:<input type='text' length='70' name='mat2'></input><br />
        <input type='submit'></input>
        </form>";
}
      
?>

Posted: Tue Jan 31, 2006 8:01 pm
by nickman013
Parse error: parse error, unexpected '{' in /home/muot/public_html/test/test.php on line 5

Posted: Tue Jan 31, 2006 8:02 pm
by Charles256
what version of PHP are you using? it's working on mine..... :-/ I mean..look at the code I pasted..how on earth could that cause a parse error on line 5???

Posted: Tue Jan 31, 2006 8:16 pm
by nickman013
I have PHP 4.

Posted: Tue Jan 31, 2006 8:28 pm
by Roja
Works fine here, as pasted (no parse error). Congrats!

Posted: Tue Jan 31, 2006 8:30 pm
by nickman013
yeah, i got it working too, it was my stupid BBEdit, it always finds a way to mess up copy and pasted scripts.