Last active
August 29, 2015 14:02
-
-
Save jesuscast/63b8377079d653481af0 to your computer and use it in GitHub Desktop.
Calculates de division of two polynomials. ENTRY an array where the index i equals the term with degree i. EX array(1,4,3) = 3x^2+4x+1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function degree($polynomial){ | |
for($i = (count($polynomial)-1); $i>=0; $i--){ | |
if($polynomial[$i]!=0){ | |
return $i; | |
} | |
} | |
return 0; | |
} | |
function formatPolynomial($poly){ | |
$expression = ""; | |
for($i = (count($poly)-1); $i>=0; $i--){ | |
if($i!=(count($poly)-1)){ | |
if($poly[$i]>0){ | |
$expression .= "+"; | |
} | |
} | |
if($poly[$i]!=0){ | |
if($poly[$i]!=1){ | |
$expression.=$poly[$i]; | |
} | |
if($i!=1 and $i!=0){ | |
$expression.=("x^".$i); | |
} | |
elseif($i==1){ | |
$expression.=("x"); | |
} | |
} | |
} | |
if($expression[0]=="+"){ | |
$expression = substr($expression,1); | |
} | |
return $expression; | |
} | |
function polynomialDivision($numerator,$denominator){ | |
$numeratorDegree = degree($numerator); | |
$denominatorDegree = degree($denominator); | |
//Calculate to see if the division makes sense or not | |
if($denominatorDegree > $numeratorDegree){ | |
return array(); | |
} | |
elseif($numeratorDegree >= $denominatorDegree){ | |
$i = $numeratorDegree-$denominatorDegree; | |
//the array_pad is for given initial values and avoiding undefined indexes | |
$answer = array_pad(array(),$i+2,0); | |
$remainder = array_pad(array(),$denominatorDegree+1,0); | |
while($numeratorDegree >= $denominatorDegree){ | |
//we devide the highest degree term from the denominator | |
//by the highest term from the numerator | |
//so the highest term of the answer has a degree of | |
// dN-dD | |
$i = $numeratorDegree-$denominatorDegree; | |
//we add the term to the array | |
$answer[$i] = $numerator[$numeratorDegree]/$denominator[$denominatorDegree]; | |
//we make the new array so we could take the difference | |
$polyForDiff = array_pad(array(),$denominatorDegree+$i+2,0); | |
for($j = 0; $j<=$denominatorDegree; $j++){ | |
$polyForDiff[$j+$i] = $denominator[$j]*$answer[$i]; | |
} | |
//now we calculate the difference | |
for($j = 0; $j<=degree($polyForDiff); $j++){ | |
$numerator[$j] = $numerator[$j] - $polyForDiff[$j]; | |
} | |
//recalculate the new numerator degree | |
$numeratorDegree = degree($numerator); | |
} | |
return array($answer,$numerator); | |
} | |
return array(); | |
} | |
$aP = array(-4,0,-2,1); | |
$bP = array(-3,1); | |
$result = polynomialDivision($aP,$bP); | |
$a = formatPolynomial($aP); | |
$b = formatPolynomial($bP); | |
$c = formatPolynomial($result[0]); | |
$d = formatPolynomial($result[1]); | |
echo("(".$a.")/(".$b.") = (".$c.")*(".$b.")+(".$d.")\n"); | |
//(x^3-2x^2-4)/(x-3) = (x^2+x+3)*(x-3)+(5) | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment