src/Malenki/Math/Matrix.php
public function multiply($mix)
    {
        if (!$this->multiplyAllow($mix)) {
            throw new \RuntimeException('Invalid number or matrix has not right number of rows.');
        }

        if ($mix instanceof Matrix) {
            $out = new self($this->size->rows, $mix->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrOutRow = array();

                for ($c = 0; $c < $mix->cols; $c++) {
                    $arrCol = $mix->getCol($c);
                    $arrRow = $this->getRow($r);

                    $arrItem = array();
                    $hasComplex = false;

                    foreach ($arrCol as $k => $v) {
                        if ($arrRow[$k] instanceof Complex) {
                            $arrItem[] = $arrRow[$k]->multiply($v);
                            $hasComplex = true;
                        } elseif ($v instanceof Complex) {
                            $arrItem[] = $v->multiply($arrRow[$k]);
                            $hasComplex = true;
                        } else {
                            $arrItem[] = $arrRow[$k] * $v;
                        }
                    }

                    if ($hasComplex) {
                        $sum = new Complex(0, 0);

                        foreach ($arrItem as $item) {
                            if (is_numeric($item)) {
                                $item = new Complex($item, 0);
                            }

                            $sum = $item->add($sum);
                        }

                        $arrOutRow[] = $sum;
                    } else {
                        $arrOutRow[] = array_sum($arrItem);
                    }
                }

                $out->addRow($arrOutRow);
            }

            return $out;
        }

        if (is_numeric($mix) || $mix instanceof Complex) {
            $out = new self($this->size->rows, $this->size->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrRow = $this->getRow($r);

                foreach ($arrRow as $k => $v) {
                    if (is_numeric($mix)) {
                        if ($v instanceof Complex) {
                            $arrRow[$k] = $v->multiply($mix);
                        } else {
                            $arrRow[$k] = $mix * $v;
                        }
                    } else {
                        $arrRow[$k] = $mix->multiply($v);
                    }
                }

                $out->addRow($arrRow);
            }

            return $out;
        }
    }
public function multiply($mix)
    {
        if (!$this->multiplyAllow($mix)) {
            throw new \RuntimeException('Invalid number or matrix has not right number of rows.');
        }

        if ($mix instanceof Matrix) {
            $out = new self($this->size->rows, $mix->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrOutRow = array();

                for ($c = 0; $c < $mix->cols; $c++) {
                    $arrCol = $mix->getCol($c);
                    $arrRow = $this->getRow($r);

                    $arrItem = array();
                    $hasComplex = false;

                    foreach ($arrCol as $k => $v) {
                        if ($arrRow[$k] instanceof Complex) {
                            $arrItem[] = $arrRow[$k]->multiply($v);
                            $hasComplex = true;
                        } elseif ($v instanceof Complex) {
                            $arrItem[] = $v->multiply($arrRow[$k]);
                            $hasComplex = true;
                        } else {
                            $arrItem[] = $arrRow[$k] * $v;
                        }
                    }

                    if ($hasComplex) {
                        $sum = new Complex(0, 0);

                        foreach ($arrItem as $item) {
                            if (is_numeric($item)) {
                                $item = new Complex($item, 0);
                            }

                            $sum = $item->add($sum);
                        }

                        $arrOutRow[] = $sum;
                    } else {
                        $arrOutRow[] = array_sum($arrItem);
                    }
                }

                $out->addRow($arrOutRow);
            }

            return $out;
        }

        if (is_numeric($mix) || $mix instanceof Complex) {
            $out = new self($this->size->rows, $this->size->cols);

            for ($r = 0; $r < $this->size->rows; $r++) {
                $arrRow = $this->getRow($r);

                foreach ($arrRow as $k => $v) {
                    if (is_numeric($mix)) {
                        if ($v instanceof Complex) {
                            $arrRow[$k] = $v->multiply($mix);
                        } else {
                            $arrRow[$k] = $mix * $v;
                        }
                    } else {
                        $arrRow[$k] = $mix->multiply($v);
                    }
                }

                $out->addRow($arrRow);
            }

            return $out;
        }
    }
Multiplies current matrix to another one or to a scalar.

@throw \RuntimeException If argument is not valid number or if the given
matrix has not the right numbers of rows.
@param  mixed  $mix Number (Real or Complex) or Matrix
@access public
@return Matrix