Beispiel #1
0
 /**
  * @inheritDoc
  */
 public function derive(NumericMatrix $mA, $extra = null)
 {
     if ($mA->is('empty')) {
         return TypeFactory::createInt(0);
     }
     if ($mA->is('singleitem')) {
         return $mA->get(1, 1);
     }
     $calc = new Calculator();
     return array_reduce($mA->toArray(), function ($c1, $row) use($calc) {
         return array_reduce($row, function ($carry, $item) use($calc) {
             return $calc->add($item, $carry);
         }, $c1);
     }, TypeFactory::createInt(0));
 }
 /**
  * Find the derivative
  *
  * @param NumericMatrix $mA
  * @param IntType $extra The current row to find the next weighted random row from
  *
  * @throws MathMatrixException
  * @throws NotMarkovException
  *
  * @return IntType
  */
 public function derive(NumericMatrix $mA, $extra = null)
 {
     if (!$mA->is('Markov')) {
         throw new NotMarkovException();
     }
     if (!$extra instanceof IntType) {
         throw new MathMatrixException('The extra parameter is not an IntType');
     }
     $this->comp = new Comparator();
     $this->calc = new Calculator();
     $this->zero = TypeFactory::createInt(0);
     return $this->nextWeightedRandom($mA, $extra);
 }
Beispiel #3
0
 /**
  * Find tr(M)
  *
  * @param NumericMatrix $mA
  * @param mixed $extra
  * @return numeric
  *
  * @throws Chippyash/Math/Matrix/Exceptions/UndefinedComputationException
  */
 public function derive(NumericMatrix $mA, $extra = null)
 {
     if ($mA->is('singleitem')) {
         return $mA->get(1, 1);
     }
     $this->assertMatrixIsNotEmpty($mA, 'No trace for empty matrix')->assertMatrixIsSquare($mA, 'No trace for non-square matrix');
     $tr = new FloatType(0);
     $size = $mA->rows();
     $data = $mA->toArray();
     $calc = new Calculator();
     for ($x = 0; $x < $size; $x++) {
         $tr = $calc->add($tr, $data[$x][$x]);
     }
     return $tr;
 }
Beispiel #4
0
 /**
  * Divide a mtix by another
  *
  * @param NumericMatrix $mA First matrix operand - required
  * @param NumericMatrix $extra Second Matrix operand - required
  *
  * @return NumericMatrix
  */
 public function compute(NumericMatrix $mA, $extra = null)
 {
     $this->assertParameterIsMatrix($extra, 'Parameter is not a matrix');
     if ($mA->is('empty')) {
         $mA = $this->createCorrectMatrixType($mA, [1]);
     }
     if ($extra->is('empty')) {
         $extra = $this->createCorrectMatrixType($extra, [1]);
     }
     $this->assertMatrixColumnsAreEqual($mA, $extra)->assertMatrixRowsAreEqual($mA, $extra);
     $fI = new Invert();
     $mI = $fI->transform($extra);
     $mul = new MM();
     return $mul($mA, $mI);
 }
Beispiel #5
0
 /**
  * Multiply each member of the matrix by single scalar value and return result
  * Boolean values are converted to 0 (false) and 1 (true).  Use the logical
  * computations if required.
  * String values must conform to the requirements of a rational string number
  * i.e. '2/3', else an exception will be thrown
  *
  * @param Matrix $mA First matrix to act on - required
  * @param scalar $extra value to add
  *
  * @return Matrix
  *
  * @throws Chippyash/Matrix/Exceptions/ComputationException
  */
 public function compute(NumericMatrix $mA, $extra = null)
 {
     if ($mA->is('empty')) {
         return $this->createCorrectMatrixType($mA);
     }
     $scalar = $this->createCorrectScalarType($mA, $extra);
     $data = $mA->toArray();
     $lx = $mA->columns();
     $ly = $mA->rows();
     $calc = new Calculator();
     for ($row = 0; $row < $ly; $row++) {
         for ($col = 0; $col < $lx; $col++) {
             $data[$row][$col] = $calc->mul($data[$row][$col], $scalar);
         }
     }
     return $this->createCorrectMatrixType($mA, $data);
 }
 /**
  * @inheritDoc
  */
 public function compute(NumericMatrix $mA, $extra = null)
 {
     $this->assertParameterIsMatrix($extra, 'Parameter is not a matrix');
     if ($mA->is('empty') || $extra->is('empty')) {
         return $this->createCorrectMatrixType($mA, []);
     }
     $this->assertMatrixColumnsAreEqual($mA, $extra)->assertMatrixRowsAreEqual($mA, $extra);
     $data = (new ZMatrix())->create([$mA->rows(), $mA->columns()])->toArray();
     $dA = $mA->toArray();
     $dB = $extra->toArray();
     $cols = $mA->columns();
     $rows = $mA->rows();
     $calc = new Calculator();
     for ($row = 0; $row < $rows; $row++) {
         for ($col = 0; $col < $cols; $col++) {
             $data[$row][$col] = $this->doCompute($dA[$row][$col], $dB[$row][$col], $calc);
         }
     }
     return $this->createCorrectMatrixType($mA, $data);
 }
Beispiel #7
0
 /**
  * Massage where mA is rectangle
  * @param \Chippyash\Math\Matrix\NumericMatrix $mA
  * @param \Chippyash\Math\Matrix\NumericMatrix $mB
  * @param array $product
  * @return \Chippyash\Math\Matrix\NumericMatrix
  */
 protected function massageRectangle(NumericMatrix $mA, NumericMatrix $mB, array $product)
 {
     if ($mB->is('rectangle') && $mA->rows() < $mB->rows()) {
         return $this->createCorrectMatrixType($mA, [[$product[0][0], $product[0][1]], [$product[1][0], $product[1][1]]]);
     }
     return $this->createCorrectMatrixType($mA, $product);
 }
Beispiel #8
0
 /**
  * Set determinant of original matrix if it is square
  */
 protected function setDeterminant(NumericMatrix $mA)
 {
     if (!$mA->is('square')) {
         //determinant undefined for non square matrix
         $this->set('Det', null);
         return;
     }
     if ($mA->is('empty')) {
         $this->set('Det', $this->createCorrectScalarType($mA, 1));
         return;
     }
     $det = $this->createCorrectScalarType($mA, $this->pivsign);
     $LU = $this->LU->toArray();
     $calc = new Calculator();
     $this->set('Det', function () use($det, $LU, $calc) {
         $c = count($LU);
         for ($j = 0; $j < $c; $j++) {
             $det = $calc->mul($det, $LU[$j][$j]);
         }
         return $det;
     });
 }