/** * @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 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; }
/** * Return determinant of a 2X2 matrix * @link http://en.wikipedia.org/wiki/Matrix_determinant#2.C2.A0.C3.97.C2.A02_matrices * [[a, b] * [c, d]] * ad - bc * * @param \Chippyash\Math\Matrix\NumericMatrix $mA */ protected function det2(NumericMatrix $mA) { $c = $this->calc(); return $c->sub($c->mul($mA->get(1, 1), $mA->get(2, 2)), $c->mul($mA->get(1, 2), $mA->get(2, 1))); }