/** * @inheritDoc */ protected function doCompute(NumericTypeInterface $a, NumericTypeInterface $b, Calculator $calc) { if ($this->getComparator()->compare($b, TypeFactory::createInt(0)) === 0) { return null; } return $calc->div($a, $b); }
/** * Divide each member of the matrix by single scalar value and return result * * @param NumericMatrix $mA First matrix to act on - required * @param numeric $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); if ($this->isZero($scalar)) { throw new ComputationException('Divisor == zero'); } $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->div($data[$row][$col], $scalar); } } return $this->createCorrectMatrixType($mA, $data); }
/** * LU Decomposition constructor. * * @param \Chippyash\Math\Matrix\NumericMatrix $mA */ protected function LUDecomposition(NumericMatrix $mA) { // Use a "left-looking", dot-product, Crout/Doolittle algorithm. $LU = $mA->toArray(); $m = $this->rows = $mA->rows(); $n = $this->cols = $mA->columns(); for ($i = 0; $i < $m; $i++) { $this->piv[$i] = $i; } $this->pivsign = 1; $LUrowi = []; $LUcolj = []; $calc = new Calculator(); $comp = new Comparator(); $zeroInt = $this->createCorrectScalarType($mA, 0); // Outer loop. for ($j = 0; $j < $n; $j++) { // Make a copy of the j-th column to localize references. for ($i = 0; $i < $m; $i++) { $LUcolj[$i] =& $LU[$i][$j]; } // Apply previous transformations. for ($i = 0; $i < $m; $i++) { $LUrowi = $LU[$i]; // Most of the time is spent in the following dot product. $kmax = min($i, $j); $s = clone $zeroInt; for ($k = 0; $k < $kmax; $k++) { $s = $calc->add($s, $calc->mul($LUrowi[$k], $LUcolj[$k])); } $LUcolj[$i] = $calc->sub($LUcolj[$i], $s); $LUrowi[$j] = $LUcolj[$i]; } // Find pivot and exchange if necessary. $p = $j; for ($i = $j + 1; $i < $m; $i++) { if ($comp->gt($LUcolj[$i]->abs(), $LUcolj[$p]->abs())) { $p = $i; } } if ($p != $j) { for ($k = 0; $k < $n; $k++) { //swap $t = $LU[$p][$k]; $LU[$p][$k] = $LU[$j][$k]; $LU[$j][$k] = $t; } $k = $this->piv[$p]; $this->piv[$p] = $this->piv[$j]; $this->piv[$j] = $k; $this->pivsign = $this->pivsign * -1; } // Compute multipliers. if ($j < $m && $comp->neq($LU[$j][$j], $zeroInt)) { for ($i = $j + 1; $i < $m; $i++) { $LU[$i][$j] = $calc->div($LU[$i][$j], $LU[$j][$j]); } } } $this->set('LU', $this->createCorrectMatrixType($mA, $LU)); }