/** * 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); }
/** * Carry out the actual multiplication using standard matrix multiplication * method. * * @param \Chippyash\Math\Matrix\NumericMatrix $mA * @param \Chippyash\Math\Matrix\NumericMatrix $mB * @return array * @throws ComputationException */ protected function doComputation(NumericMatrix $mA, NumericMatrix $mB) { $size = max([$mA->columns(), $mA->rows(), $mB->columns(), $mB->rows()]); $product = (new ZMatrix())->create([$size, $size])->toArray(); $dA = $mA->toArray(); $dB = $mB->toArray(); $zero = $this->createCorrectScalarType($mA, 0); $calc = new Calculator(); for ($i = 0; $i < $size; $i++) { for ($k = 0; $k < $size; $k++) { for ($j = 0; $j < $size; $j++) { $a = isset($dA[$i][$k]) ? $dA[$i][$k] : $zero; $b = isset($dB[$k][$j]) ? $dB[$k][$j] : $zero; $product[$i][$j] = $calc->add($product[$i][$j], $calc->mul($a, $b)); } } } return $product; }
/** * Inter row multiplication * * @param array $a * @param NumericTypeInterface $multiple * @param int $rowToMultiplyWith * @param int $rowToAddTo * @param \Chippyash\Math\Type\Calculator $calc */ protected function addMultipleOfOtherRowToRow(array &$a, $multiple, $rowToMultiplyWith, $rowToAddTo, Calculator $calc) { $numberOfColumns = count($a[0]); for ($l = 0; $l < $numberOfColumns; $l++) { $a[$rowToAddTo][$l] = $calc->add($a[$rowToAddTo][$l], $calc->mul($a[$rowToMultiplyWith][$l], $multiple)); } }
/** * @inheritDoc */ protected function doCompute(NumericTypeInterface $a, NumericTypeInterface $b, Calculator $calc) { return $calc->mul($a, $b); }
/** * 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; }); }