Пример #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";
}
Пример #2
0
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();
echo "Determinant = $det\n";
echo "Euclidean Norm = ".$m->norm()."\n";
echo "Normalized Determinant = ".$m->normalizedDeterminant()."\n";

echo "\n";
Пример #3
0
 public function create($array)
 {
     // test conditions
     // TODO: test min/max
     // test area
     if ($this->minArea * ($this->minWidth * $this->minHeight) >= $this->maxArea * ($this->maxWidth * $this->maxHeight)) {
         throw new Exception('Min/max area overlap, please adjust settings');
     }
     // create matrix
     $this->matrix = Math_Matrix::makeMatrix($this->ysize, $this->xsize, -1);
     // variable to define whether filling is active
     $fill = false;
     // return array
     $positions = array();
     foreach ($array as $k => $v) {
         // get next empty space
         $pos = $this->searchNextPosition();
         // fill horizontal?
         if ($pos[1] == 0) {
             // are we at the top?
             if ($pos[0] != 0) {
                 // are we not at [0,0] ?
                 if (rand(1, $this->fillTopProbability) == 1) {
                     // roll a dice for top position fill
                     // calculate fill position
                     $fill = $this->searchXBoundFrom($pos[0]) + $this->minWidth;
                     if ($this->debug) {
                         echo 'fill from top x=' . $fill . '<br/>';
                     }
                 } else {
                     $fill = false;
                 }
             }
         } elseif ($fill === false) {
             // fill from halfway?
             if (rand(1, $this->fillHalfwayProbability) == 1) {
                 // roll a dice halfway position fill
                 // calculate fill position
                 $fill = $this->searchXBoundFrom($pos[0]) + $this->minWidth;
                 if ($this->debug) {
                     echo 'fill x=' . $fill . '<br/>';
                 }
             }
         }
         // get dimensions
         $rand = $this->randomDimensions();
         $randWidth = $rand['width'];
         $randHeight = $rand['height'];
         if ($fill !== false) {
             $randWidth = $fill - $pos[0];
         }
         // height available?
         $heightAvailable = $this->availableHeightFrom($pos[0], $pos[1]);
         if ($heightAvailable <= $this->minHeight) {
             $height = $heightAvailable;
         } else {
             if ($heightAvailable <= $this->minHeight * 2) {
                 $height = $heightAvailable;
             } else {
                 $height = min($randHeight, $heightAvailable - $this->minHeight);
             }
         }
         // random width
         $width = $randWidth;
         // debug
         if ($this->debug) {
             echo $k . ': ' . $height . '(' . $heightAvailable . ')x' . $width . ' @ [' . $pos[0] . ', ' . $pos[1] . ']<br/>';
         }
         // set ids
         for ($x = $pos[0]; $x < $width + $pos[0]; $x++) {
             for ($y = $pos[1]; $y < $height + $pos[1]; $y++) {
                 $this->matrix->setElement($y, $x, $k);
             }
         }
         // set item position
         $positions[$k] = array('x' => $pos[0] * $this->multiplier, 'y' => $pos[1] * $this->multiplier, 'w' => $width * $this->multiplier, 'h' => $height * $this->multiplier);
     }
     // debug: print matrix
     if ($this->debug) {
         echo '<pre>' . $this->matrix->toString('%3.0f') . '</pre>';
         die;
     }
     // return array
     return $positions;
 }
 function testMakeHankel()
 {
     $data = array(array(1, 2, 3, 3), array(2, 3, 3, 5), array(3, 3, 5, 7));
     $c = array(1, 2, 3);
     $r = array(1, 3, 5, 7);
     $res = new Math_Matrix($data);
     $hankel = Math_Matrix::makeHankel($c, $r);
     $this->assertEquals($res->toString(), $hankel->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());
 }
<?php

/**
 * Example of using the Math_Matrix class
 * @author Jesus M. Castagnetto
 * 
 * $Id: ex_math_matrix.php,v 1.1 2003/05/16 22:28:18 jmcastagnetto Exp $
 */
require_once 'Math/Matrix.php';
$data = array(array(1.0, 2.0, 3.0, 4.0), array(5.0, 6.0, 7.0, 8.0), array(1.0, 4.0, 5.0, 7.0), array(2.0, 3.0, -3.0, 4.0));
$m = new Math_Matrix($data);
//print_r($m);
echo $m->toString() . "\n";
$det = $m->determinant();
echo "Determinant = {$det}\n";
echo "Trace = " . $m->trace() . "\n";
echo "Euclidean Norm = " . $m->norm() . "\n";
echo "Normalized Determinant = " . $m->normalizedDeterminant() . "\n";
echo "\nSubmatrix(1,1,2,2)\n";
$n = $m->getSubMatrix(1, 1, 2, 2);
echo $n->toString() . "\n";
$det = $n->determinant();
echo "Determinant = {$det}\n";
echo "Euclidean Norm = " . $n->norm() . "\n";
echo "Normalized Determinant = " . $n->normalizedDeterminant() . "\n";
echo "\nWriting original matrix\n";
$e = Math_Matrix::writeToFile($m, 'writetest.mat', 'csv');
echo "... Reading from file\n";
$p = Math_Matrix::readFromFile('writetest.mat', 'csv');
echo $p->toString() . "\n";
$det = $p->determinant();