Jump to content

Solving Determinates in PHP


cooldude832

Recommended Posts

I know it could be done, but image a 20x20 matrix, that is a ton of minors to do, anyone got an idea on discovering the most 0ed row/column to expand upon.  The expansion is fairly easily, but I was wondering if it could be made faster by finding the path of least resistance, or if that is just a waste of power?

Link to comment
Share on other sites

You could do something like this- it chooses the most 0ed column/row and from there you can continue.

 

<?php

//$matrix is a ($matrix_size)X($matrix_size) matrix

$matrix_size = 20;

$max1 = $max1_v = 0;
$max2 = $max2_v = 0;
$transpose = array();

for($i=0; $i<$matrix_size; $i++)
for($j=0; $j<$matrix_size; $j++)
	$transpose[$j][$i] = $matrix[$i][$j];

for($i=0; $i<$matrix_size; $i++)
if ($max1_v < $temp=count(array_keys($matrix[$i], 0)))
{
	$max1_v = $temp;
	$max1 = $i;
}

for($i=0; $i<$matrix_size; $i++)
{
if ($max2_v < $temp=count(array_keys($transpose[$i], 0)))
{
	$max2_v = $temp;
	$max2 = $i;
}
}


if($max1_v < $max2_v)  //Remember- the output is between 0 to ($matrix_size-1)
echo "Use column $max2";
else
echo "Use row $max1";

?>

 

Orio.

Link to comment
Share on other sites

try

<?php
function deter($a, $i = -1){
if ($i < 0) $i = count($a) -1;
if ($i==0) return $a[0][0];
$j = $i;
$sig = 1;
while ($a[$j][$i] == 0 and $j-- > -1);
if ($i != $j and $a[$j][$i] != 0) {
	$tmp = $a[$i];
	$a[$i] = $a[$j];
	$a[$j] = $tmp;
	$sig *= -1;
}
else if($a[$j][$i] == 0) return 0;
$b = $a[$i][$i];
for ($row = $i -1; $row >= 0; $row--){
	$c = -$a[$row][$i] / $b;
	for ($k = 0; $k < $i; $k++){
		$a[$row][$k] += $a[$i][$k] * $c;
	}
}
return $out = $b * deter($a, $i - 1) * $sig;
}
$matr = array(
array(0,2,0),
array(3,4,2),
array(6,0,0)
);
echo deter($matr);
?>

Link to comment
Share on other sites

Whoa.... We did that in Algebra 2 the other day (I'm in 10th grade lol)....

 

Hmmm would diagonals or minors be faster PHP wise.... I guess minors would be since it would be a lot less math (especially with 20x20 matrices and what not)....

 

Bleh idk....

 

Hmmm.... I contributed nothing to this thread.... oh well ;p.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.