MATRIX MULTIPLICATION!! DONE!!

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

MATRIX MULTIPLICATION!! DONE!!

Post 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>";
}
      
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Parse error: parse error, unexpected '{' in /home/muot/public_html/test/test.php on line 5
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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???
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I have PHP 4.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Works fine here, as pasted (no parse error). Congrats!
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Post Reply