Exemple #1
0
 /**
  * Tests if Helper::isVector works with invalid vector
  */
 public function testCheckVectorInvalid()
 {
     $numArray = NumPHP::ones(3, 2);
     $this->assertFalse(Helper::isVector($numArray));
 }
Exemple #2
0
 /**
  * Back Substitution solves a linear system with a upper triangular matrix of
  * size n*n and a vector of size n or a matrix of size n*m
  *
  * @param NumArray $uMatrix   upper triangular matrix of size n*n
  * @param NumArray $numArray  vector of size n or matrix of size n*m
  *
  * @return NumArray
  *
  * @since 1.0.0
  */
 protected static function backSubstitution(NumArray $uMatrix, NumArray $numArray)
 {
     $shape = $numArray->getShape();
     if (Helper::isVector($numArray)) {
         $xVector = NumPHP::zerosLike($numArray);
         for ($i = $shape[0] - 1; $i >= 0; $i--) {
             $slice = sprintf("%d:%d", $i + 1, $shape[0]);
             $sum = $uMatrix->get($i, $slice)->dot($xVector->get($slice));
             $xVector->set($i, $numArray->get($i)->sub($sum)->div($uMatrix->get($i, $i)));
         }
         return $xVector;
     }
     // $numArray is a matrix
     $copy = clone $numArray;
     for ($i = 0; $i < $shape[1]; $i++) {
         $copy->set(':', $i, self::backSubstitution($uMatrix, $copy->get(':', $i)));
     }
     return $copy;
 }