Example #1
0
function simplextoarray($sm)
{
    for ($r = 0; $r < count($sm); $r++) {
        for ($c = 0; $c < count($sm[0]); $c++) {
            $sm[$r][$c] = fractionparse($sm[$r][$c]);
        }
    }
    return $sm;
}
Example #2
0
function matrixreduce($A, $rref = false, $frac = false)
{
    include_once "fractions.php";
    // number of rows
    $N = count($A);
    $M = count($A[0]);
    $pivots = array();
    if ($N > 10) {
        global $myrights;
        if ($myrights > 10) {
            echo "You really shouldn't use matrixreduce for matrices bigger than 10 rows.";
        }
    }
    for ($r = 0; $r < $N; $r++) {
        for ($c = 0; $c < $M; $c++) {
            $A[$r][$c] = fractionparse($A[$r][$c]);
        }
    }
    $r = 0;
    $c = 0;
    while ($r < $N && $c < $M) {
        if ($A[$r][$c][0] == 0) {
            //swap only if there's a 0 entry
            $max = $p;
            for ($i = $r + 1; $i < $N; $i++) {
                if (abs($A[$i][$c][0] / $A[$i][$c][1]) > abs($A[$max][$c][0] / $A[$max][$c][1])) {
                    $max = $i;
                }
            }
            if ($max != $p) {
                $temp = $A[$r];
                $A[$r] = $A[$max];
                $A[$max] = $temp;
            }
        }
        if (abs($A[$r][$c][0] / $A[$r][$c][1]) <= 1.0E-10) {
            $c++;
            continue;
        }
        //scale pivot row
        if ($rref) {
            $divisor = $A[$r][$c];
            for ($j = $c; $j < $M; $j++) {
                $A[$r][$j] = fractiondivide($A[$r][$j], $divisor);
            }
        }
        for ($i = $rref ? 0 : $r + 1; $i < $N; $i++) {
            if ($i == $r) {
                continue;
            }
            $mult = fractiondivide($A[$i][$c], $A[$r][$c]);
            if ($mult[0] == 0) {
                continue;
            }
            for ($j = $c; $j < $M; $j++) {
                //echo "Entry $i,$j:  ".fractionreduce($A[$i][$j]).' - '.fractionreduce( $mult).'*'.fractionreduce($A[$r][$j]).'<br/>';
                $A[$i][$j] = fractionsubtract($A[$i][$j], fractionmultiply($mult, $A[$r][$j]));
            }
        }
        $r++;
        $c++;
    }
    for ($r = 0; $r < $N; $r++) {
        for ($c = 0; $c < $M; $c++) {
            if ($frac) {
                $A[$r][$c] = fractionreduce($A[$r][$c]);
            } else {
                $A[$r][$c] = $A[$r][$c][0] / $A[$r][$c][1];
            }
        }
    }
    return $A;
}
Example #3
0
function fractionroot($f, $root = 2)
{
    if (!is_array($f)) {
        $f = fractionparse($f);
    }
    include_once "radicals.php";
    return reduceradicalfrac(1, $f[0] * $f[1], $f[1]);
}