Example #1
0
function pleac_Multiplying_Matrices()
{
    // PHP offers no native support for matrices. However, a 'Math_Matrix' class
    // is available for download from PEAR: [http://pear.php.net/package/Math_Matrix].
    // Note the following 'include' directives are required:
    //
    //  include_once('Math/Matrix.php');
    $a = new Math_Matrix(array(array(3, 2, 3), array(5, 9, 8)));
    $b = new Math_Matrix(array(array(4, 7), array(9, 3), array(8, 1)));
    echo $a->toString() . "\n";
    echo $b->toString() . "\n";
    // NOTE: When I installed this package I had to rename the 'clone' method else
    // it would not load, so I chose to rename it to 'clone_', and this usage is
    // shown below. This bug may well be fixed by the time you obtain this package
    $c = $a->clone_();
    $c->multiply($b);
    echo $c->toString() . "\n";
}
Example #2
0
 public function availableHeightFrom($x, $y)
 {
     // get the column
     $col = $this->matrix->getCol($x);
     // make array start at correct y pos
     for ($i = 0; $i < $y; $i++) {
         array_shift($col);
     }
     foreach ($col as $k => $v) {
         if ($v > -1) {
             return $k;
         }
     }
     // entire height available
     return $this->ysize - $y;
 }
Example #3
0
 /**
  * Solves a system of linear equations: Ax = b
  *
  * A system such as:
  * <pre>
  *     a11*x1 + a12*x2 + ... + a1n*xn = b1
  *     a21*x1 + a22*x2 + ... + a2n*xn = b2
  *     ...
  *     ak1*x1 + ak2*x2 + ... + akn*xn = bk
  * </pre>
  * can be rewritten as:
  * <pre>
  *     Ax = b
  * </pre>
  * where: 
  * - A is matrix of coefficients (aij, i=1..k, j=1..n), 
  * - b a vector of values (bi, i=1..k),
  * - x the vector of unkowns (xi, i=1..n)
  * Using: x = (Ainv)*b
  * where:
  * - Ainv is the inverse of A
  *
  * @static
  * @access public
  * @param object Math_Matrix $a the matrix of coefficients
  * @param object Math_Vector $b the vector of values
  * @return mixed a Math_Vector object on succcess, PEAR_Error otherwise
  * @see vectorMultiply()
  */
 function solve($a, $b)
 {
     // check that the vector classes are defined
     if (!Math_Matrix::isMatrix($a) && !Math_VectorOp::isVector($b)) {
         return PEAR::raiseError('Incorrect parameters, expecting a Math_Matrix and a Math_Vector');
     }
     $e = $a->invert();
     if (PEAR::isError($e)) {
         return $e;
     }
     return $a->vectorMultiply($b);
 }
Example #4
0
// solution: <2,-2,5,3>
echo "\nSolving another Ax = b\n";
$a = new Math_Matrix($adata);
$b = new Math_Vector($bdata);
$x = Math_Matrix::solve($a, $b);
echo "\nA\n" . $a->toString('%8.4f') . "\n";
echo "B " . $b->toString() . "\n";
echo "Solution " . $x->toString() . "\n";
echo "\nSolving with error correction\n";
$x = Math_Matrix::solveEC($a, $b);
echo "EC Solution " . $x->toString() . "\n";
$Adata = array(array(1, 1, 2), array(2, 3, 4));
$Bdata = array(array(-1, 3), array(-3, 4), array(-5, 2));
$A = new Math_Matrix($Adata);
$A1 = $A->clone();
$B = new Math_Matrix($Bdata);
$B1 = $B->clone();
$A1->multiply($B1);
$B->multiply($A);
echo $A1->toString() . "\n";
echo $B->toString() . "\n";
/*
echo "\n";
$data = array (
            array(1,0,1),
            array(1,1,1),
            array(1,1,0),
        );
$m = new Math_Matrix($data);
echo $m->toString()."\n";
$det = $m->determinant();
 function testInvert()
 {
     $unit = Math_Matrix::makeUnit(4);
     $q = new Math_Matrix($this->data);
     $p = $q->clone();
     $q->invert();
     $p->multiply($q);
     $this->assertEquals($unit->getData(), $p->getData());
 }
 function testSolveEC()
 {
     $adata = array(array(-4.0, 3.0, -4.0, -1.0), array(-2.0, 0.0, -5.0, 3.0), array(-1.0, -1.0, -3.0, -4.0), array(-3.0, 2.0, 4.0, -1.0));
     $bdata = array(-37.0, -20.0, -27.0, 7.0);
     $res = array(2.0, -2.0, 5.0, 3.0);
     $a = new Math_Matrix($adata);
     $b = new Math_Vector($bdata);
     $x = Math_Matrix::solveEC($a, $b);
     $t = $x->getTuple();
     $this->assertEquals($res, $t->data);
 }
Example #7
0
<?php

// performs matrix functions to fit a template to a 3-point delineation
require_once $_SERVER['DOCUMENT_ROOT'] . '/include/main_func.php';
auth();
include_once "Math/Matrix.php";
$temPoints = $_POST['temPoints'];
$eyeclicks = $_POST['eyeclicks'];
$original = array(array($temPoints[0]['x'], $temPoints[0]['y'], 1.0), array($temPoints[1]['x'], $temPoints[1]['y'], 1.0), array($temPoints[2]['x'], $temPoints[2]['y'], 1.0));
$m = new Math_Matrix($original);
$xnew = array($eyeclicks[0]['x'], $eyeclicks[1]['x'], $eyeclicks[2]['x']);
$ynew = array($eyeclicks[0]['y'], $eyeclicks[1]['y'], $eyeclicks[2]['y']);
$xvector = new Math_Vector($xnew);
$yvector = new Math_Vector($ynew);
$m1 = $m->cloneMatrix();
$x = @Math_Matrix::solve($m1, $xvector);
$a = round($x->get(0), 3);
$b = round($x->get(1), 3);
$c = round($x->get(2), 3);
$m2 = $m->cloneMatrix();
$y = @Math_Matrix::solve($m2, $yvector);
$d = round($y->get(0), 3);
$e = round($y->get(1), 3);
$f = round($y->get(2), 3);
$variables = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d, 'e' => $e, 'f' => $f, 'fitPoints' => $_POST['fitPoints']);
echo json_encode($variables);
exit;
Example #8
0
File: Base.php Project: roojs/pear
 function transform()
 {
     if (empty($this->transform)) {
         return;
     }
     // do not transform tspans -- done by overwriting this...
     //if ($this->x === false) {
     //    return;
     // }
     // deal with transformation..
     $tr = $this->transform;
     if (preg_match('/scale\\(([0-9e.-]+),([0-9e.-]+)\\)/', $tr, $args)) {
         $xscale = $args[1];
         $yscale = $args[2];
         $method = 'scale';
     } else {
         if (preg_match('/matrix\\(([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+)\\)/', $tr, $args)) {
             array_shift($args);
             require_once 'Math/Matrix.php';
             $matrix = new Math_Matrix(array(array($args[0], $args[2], $args[4]), array($args[1], $args[3], $args[5]), array(0, 0, 1)));
             $method = 'matrix';
         } else {
             if (preg_match('/translate\\(([0-9e.-]+),([0-9e.-]+)\\)/', $tr, $args)) {
                 $x = $args[1];
                 $y = $args[2];
                 $method = 'translate';
             } else {
                 echo "<PRE>no match?";
                 print_r($this);
                 exit;
                 return;
             }
         }
     }
     //
     switch ($method) {
         case 'scale':
             $this->x *= $xscale;
             $this->y *= $yscale;
             if (empty($this->children)) {
                 return;
             }
             foreach (array_keys($this->children) as $i) {
                 if ($this->children[$i]->x === false) {
                     continue;
                     // echo "<PRE>";print_r($this);exit;
                 }
                 $this->children[$i]->x *= $xscale;
                 $this->children[$i]->y *= $yscale;
             }
             break;
         case 'matrix':
             $v = new Math_Vector(array($this->x, $this->y, 0));
             $r = $matrix->vectorMultiply($v);
             $r = $r->getData();
             $this->x = $r[0];
             $this->y = $r[1];
             //echo "<PRE>";var_dump(	$r);exit;
             if (empty($this->children)) {
                 return;
             }
             foreach (array_keys($this->children) as $i) {
                 if ($this->children[$i]->x === false) {
                     continue;
                     // echo "<PRE>";print_r($this);exit;
                 }
                 $v = new Math_Vector(array($this->children[$i]->x, $this->children[$i]->y, 0));
                 $r = $matrix->vectorMultiply($v);
                 $r = $r->getData();
                 $this->children[$i]->x = $r[0];
                 $this->children[$i]->y = $r[1];
             }
             break;
         case 'translate':
             if ($this->x !== false && $this->y !== false) {
                 $this->x += $x;
                 $this->y += $y;
             }
             if (empty($this->children)) {
                 return;
             }
             foreach (array_keys($this->children) as $i) {
                 if ($this->children[$i]->x === false || $this->children[$i]->y === false) {
                     continue;
                     // echo "<PRE>";print_r($this);exit;
                 }
                 $this->children[$i]->x += $x;
                 $this->children[$i]->y += $y;
             }
             break;
     }
 }
 function testSolveEC()
 {
     $adata = array(array(-4.0, 3.0, -4.0, -1.0), array(-2.0, 0.0, -5.0, 3.0), array(-1.0, -1.0, -3.0, -4.0), array(-3.0, 2.0, 4.0, -1.0));
     $bdata = array(-37.0, -20.0, -27.0, 7.0);
     $res = array(2.0, -2.0, 5.0, 3.0);
     $resVector = new Math_Vector($res);
     $a = new Math_Matrix($adata);
     $b = new Math_Vector($bdata);
     $x = Math_Matrix::solveEC($a, $b);
     $this->assertEquals($resVector->toString(), $x->toString());
 }
 function testMultiply()
 {
     $Adata = array(array(1, 1, 2), array(2, 3, 4));
     $Bdata = array(array(-1, 3), array(-3, 4), array(-5, 2));
     $ABdata = array(array(-14, 11), array(-31, 26));
     $BAdata = array(array(5, 8, 10), array(5, 9, 10), array(-1, 1, -2));
     $A = new Math_Matrix($Adata);
     $A1 = $A->clone();
     $B = new Math_Matrix($Bdata);
     $B1 = $B->clone();
     $A1->multiply($B);
     $B1->multiply($A);
     $AB = new Math_Matrix($ABdata);
     $BA = new Math_Matrix($BAdata);
     $this->assertEquals($A1->toString(), $AB->toString());
     $this->assertEquals($B1->toString(), $BA->toString());
 }
        );
$bdata = array(0,4,2);
// solution: <4,-2,-2>
*/
$adata = array(array(-4, 3, -4, -1), array(-2, 0, -5, 3), array(-1, -1, -3, -4), array(-3, 2, 4, -1));
$bdata = array(-37, -20, -27, 7);
// solution: <2,-2,5,3>
echo "\nSolving another Ax = b\n";
$a = new Math_Matrix($adata);
$b = new Math_Vector($bdata);
$x = Math_Matrix::solve($a, $b);
echo "\nA\n" . $a->toString('%8.4f') . "\n";
echo "B " . $b->toString() . "\n";
echo "Solution " . $x->toString() . "\n";
echo "\nSolving with error correction\n";
$x = Math_Matrix::solveEC($a, $b);
echo "EC Solution " . $x->toString() . "\n";
/*
echo "\n";
$data = array (
            array(1,0,1),
            array(1,1,1),
            array(1,1,0),
        );
$m = new Math_Matrix($data);
echo $m->toString()."\n";
$det = $m->determinant();
echo "Determinant = $det\n";
echo "Euclidean Norm = ".$m->norm()."\n";
echo "Normalized Determinant = ".$m->normalizedDeterminant()."\n";