Esempio n. 1
0
 public function testTranslateWillEventuallyReturnAMatrix()
 {
     $mA = new Matrix([[1, 2], [3, 4]]);
     $f = function ($x, $y, $v) {
         return [$x + 1, $y + 1, $v + 1];
     };
     $vS = $this->object->fromMatrix($mA)->translate($f);
     $rebasedM = $vS->toMatrix();
     $this->assertEquals([[2, 3], [4, 5]], $rebasedM->toArray());
     $notRebasedM = $vS->toMatrix(false);
     $this->assertEquals([1 => [1 => 2, 2 => 3], 2 => [1 => 4, 2 => 5]], $notRebasedM->toArray());
 }
Esempio n. 2
0
 /**
  * Assembles Q using the single givens rotations.
  * 
  * @return  matrix  Q
  */
 public function getQ()
 {
     // Q is an m x m matrix if m is the maximum of the number of rows and thenumber of columns.
     $m = max($this->_matrix->columns(), $this->_matrix->rows());
     $Q = new Matrix($m, $m);
     $Q->setAll(0.0);
     // Begin with the identity matrix.
     for ($i = 0; $i < min($Q->rows(), $Q->columns()); $i++) {
         $Q->set($i, $i, 1.0);
     }
     // Got backwards through all householder transformations and apply them on
     for ($k = $this->_matrix->columns() - 1; $k >= 0; $k--) {
         for ($j = $k; $j < $Q->columns(); $j++) {
             // First compute w^T(j) = v^T * Q(j) where Q(j) is the j-th column of Q.
             $w = $Q->get($k, $j) * 1.0;
             for ($i = $k + 1; $i < $Q->rows(); $i++) {
                 $w += $this->_matrix->get($i, $k) * $Q->get($i, $j);
             }
             // Now : Q(i,j) = Q(i,j) - tau(k)*v(i)*w(j).
             $Q->set($k, $j, $Q->get($k, $j) - $this->_tau->get($k) * 1.0 * $w);
             for ($i = $k + 1; $i < $Q->rows(); $i++) {
                 $Q->set($i, $j, $Q->get($i, $j) - $this->_tau->get($k) * $this->_matrix->get($i, $k) * $w);
             }
         }
     }
     return $Q;
 }
Esempio n. 3
0
 /**
  * Build the inner product of two vectors.
  *
  * @param	vector	$a
  * @param	vector	$b
  * @return	vector	inner product
  */
 public static function inner($a, $b)
 {
     new Assertion($a instanceof Vector, 'Given first vector not of class Vector.');
     new Assertion($b instanceof Vector, 'Given second vector not of class Vector.');
     new Assertion($a->size() == $b->size(), 'Dimensions do not match.');
     $size = $a->size();
     $result = 0;
     for ($i = 0; $i < $size; $i++) {
         $result += $a->get($i) * $b->get($i);
     }
     return $result;
 }
Esempio n. 4
0
File: LU.php Progetto: hoenirvili/cn
 /**
  * Gets the row permutation.
  * 
  * @return  vector  permutation
  */
 public function getPermutation()
 {
     return $this->_permutation->copy();
 }