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
https://forums.phpfreaks.com/topic/74409-solving-determinates-in-php/
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.

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);
?>

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.

I agree completely.

 

Most of us would have thought the term "abbrivations abbreviations" pretty straightforward too, so why query it, having just abbreviated "differential equations" to "diffy Qs"

 

All corbin wanted was confirmation that that was what you meant.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.