Beispiel #1
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;
}
Beispiel #2
0
function simplexreadsolutionarray($sma, $type, $showfractions = 1)
{
    // process arguments -----------------------------------------------
    $type = verifytype("simplexreadsolution", $type, "max");
    if (is_null($type)) {
        return null;
    }
    // showfractions
    $showfractions = verifyshowfraction("simplexreadsolution", $showfractions, 1);
    // Done processing arguments ---------------------------------------
    //
    // if we have 3 constraints with 2 variables then we will have:
    //
    // x1,  x2,  s1,  s2,  s3,  f,  obj
    //  0    1    2    3    4   5    6
    // rows = 4 --> slacks = 4 (3 slacks + 1 objectives)
    // lastrow =3
    // cols = 7
    // lastcol = 6
    // startcol = 6-4 = 2  --> lastcol-rows  // start column of slacks
    // endcol   = 2+3 = 5  --> startcol + lastrow
    $solution = array();
    $rows = count($sma);
    $cols = count($sma[0]);
    $lastrow = $rows - 1;
    $lastcol = $cols - 1;
    $objectiveposition = $lastcol - 1;
    $dualplusobjective = count($sma) + 1;
    $var = $lastcol - $rows;
    // number of x variables
    if ($type == "min") {
        for ($c = 0; $c < $var; $c++) {
            $solution[$c] = 0;
            $columnsolutionfound = true;
            $zerorow = -1;
            // not found
            if ($sma[$lastrow][$c][0] == 0) {
                for ($r = 0; $r < $lastrow; $r++) {
                    if ($sma[$r][$c][0] != 0 && $sma[$r][$c][0] != 1) {
                        $columnsolutionfound = false;
                        break;
                        // This should break out of the for r loop
                    }
                    if ($sma[$r][$c][0] == $sma[$r][$c][1]) {
                        if ($zerorow != -1) {
                            $columnsolutionfound = false;
                            break;
                            // This should break out of the for r loop
                        } else {
                            $zerorow = $r;
                        }
                    }
                }
                if ($columnsolutionfound) {
                    if ($showfractions == 1) {
                        $solution[$c] = fractionreduce($sma[$zerorow][$lastcol]);
                    } else {
                        $solution[$c] = fractiontodecimal($sma[$zerorow][$lastcol]);
                    }
                }
            }
        }
        for ($c = $var; $c < $dualplusobjective + 1; $c++) {
            if ($showfractions == 1) {
                $solution[$c] = fractionreduce($sma[$lastrow][$c]);
            } else {
                $solution[$c] = fractiontodecimal($sma[$lastrow][$c]);
            }
        }
    } else {
        for ($c = 0; $c < $lastcol - 1; $c++) {
            $solution[$c] = 0;
            $columnsolutionfound = true;
            $zerorow = -1;
            // not found
            if ($sma[$lastrow][$c][0] == 0) {
                for ($r = 0; $r < $lastrow; $r++) {
                    if ($sma[$r][$c][0] != 0 && $sma[$r][$c][0] != 1) {
                        $columnsolutionfound = false;
                    }
                    if ($sma[$r][$c][0] == $sma[$r][$c][1]) {
                        if ($zerorow != -1) {
                            $columnsolutionfound = false;
                        } else {
                            $zerorow = $r;
                        }
                    }
                }
                if ($columnsolutionfound) {
                    if ($showfractions == 1) {
                        $solution[$c] = fractionreduce($sma[$zerorow][$lastcol]);
                    } else {
                        $solution[$c] = fractiontodecimal($sma[$zerorow][$lastcol]);
                    }
                }
            }
        }
    }
    if ($showfractions == 1) {
        $solution[$objectiveposition] = fractionreduce($sma[$lastrow][$lastcol]);
    } else {
        $solution[$objectiveposition] = fractiontodecimal($sma[$lastrow][$lastcol]);
    }
    $solution[$lastcol] = "Yes";
    // objective reached
    for ($c = 0; $c < count($sma[0]); $c++) {
        if ($sma[$lastrow][$c][0] < 0) {
            $solution[$lastcol] = "No";
            break;
        }
    }
    return $solution;
}
Beispiel #3
0
function fractionpower($f, $p)
{
    if (!is_array($f)) {
        $f = fractionparse($f);
    }
    $fracarr = false;
    if (is_array($args[0])) {
        $fracarr = true;
    }
    $num = pow($f[0], $p);
    $denom = pow($f[1], $p);
    return fractionreduce($num, $denom, $fracarr);
}