Last Updated: February 25, 2016
·
568
· jesuscast

Polynomial Division Calculator

So currently I am making an integral calculator that you could check here: http://jesuscastaneda.net76.net/apcalc/HJBABJHBHDA123hjdhjshsnGF63.html

but I need to make several mathematical functions and one of them is a polynomial divisor in php that you could check right here

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)