Linear algebraic matrix multiplication, A * B

@param B another matrix
@return Matrix product, A * B
@throws IllegalArgumentException Matrix inner dimensions must agree.
337781
public Matrix times(Matrix B)
    {
        if (B.m != n)
        {
            throw new IllegalArgumentException("Matrix inner dimensions must agree.");
        }
        Matrix X = new Matrix(m, B.n);
        double[][] C = X.getArray();
        double[] Bcolj = new double[n];
        for (int j = 0; j < B.n; j++)
        {
            for (int k = 0; k < n; k++)
            {
                Bcolj[k] = B.A[k][j];
            }
            for (int i = 0; i < m; i++)
            {
                double[] Arowi = A[i];
                double s = 0;
                for (int k = 0; k < n; k++)
                {
                    s += Arowi[k] * Bcolj[k];
                }
                C[i][j] = s;
            }
        }
        return X;
    }
902695
public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2)
    {
        // Check bounds
        if (matrix1[0].length != matrix2.length)
        {
            throw new SecretShareException("The matrices do not have compatible size");
        }

        // Create result matrix
        E[][] result = //(E[][]) new Number[matrix1.length][matrix2[0].length];
                create(matrix1.length, matrix2[0].length);

        // Perform multiplication of two matrices
        for (int i = 0; i < result.length; i++)
        {
            for (int j = 0; j < result[0].length; j++)
            {
                result[i][j] = zero();

                for (int k = 0; k < matrix1[0].length; k++)
                {
                    result[i][j] = add(result[i][j],
                                   multiply(matrix1[i][k], matrix2[k][j]));
                }
            }
        }

        return result;
    }
883380
public Matrix multiply(double k)
    {
        double pv[][] = new double[nRows][nCols]; // product values

        // Compute values of the product.
        for (int r = 0; r < nRows; ++r) {
            for (int c = 0; c < nCols; ++c) {
                pv[r][c] = k*values[r][c];
            }
        }

        return new Matrix(pv);
    }
926648
public static FloatMatrix solve(FloatMatrix A, FloatMatrix B) {
		A.assertSquare();
		FloatMatrix X = B.dup();
		int[] ipiv = new int[B.rows];
		SimpleBlas.gesv(A.dup(), ipiv, X);
		return X;
	}
852542
public void solve(double[] b, double[] x) {
        if (b.length != qr.nrows()) {
            throw new IllegalArgumentException(String.format("Row dimensions do not agree: A is %d x %d, but B is %d x 1", qr.nrows(), qr.nrows(), b.length));
        }

        if (x.length != qr.ncols()) {
            throw new IllegalArgumentException("A and x dimensions don't match.");
        }

        if (singular) {
            throw new RuntimeException("Matrix is rank deficient.");
        }

        double[] B = b.clone();
        solve(Matrix.newInstance(B));
        System.arraycopy(B, 0, x, 0, x.length);
    }
111244
public double[] solve(double[] b, double[] x) {
        if (b.length != n) {
            throw new IllegalArgumentException(String.format("Row dimensions do not agree: A is %d x %d, but b is %d x 1", n, n, b.length));
        }

        if (b.length != x.length) {
            throw new IllegalArgumentException("b and x dimensions do not agree.");
        }

        if (au == null) {
            decompose();
        }
        System.arraycopy(b, 0, x, 0, n);

        int mm = m1 + m2 + 1;
        int l = m1;

        for (int k = 0; k < n; k++) {
            int j = index[k] - 1;

            if (j != k) {
                // swap x[k] and x[j]
                double swap = x[k];
                x[k] = x[j];
                x[j] = swap;
            }

            if (l < n) {
                l++;
            }

            for (j = k + 1; j < l; j++) {
                x[j] -= al[k][j - k - 1] * x[k];
            }
        }

        l = 1;
        for (int i = n - 1; i >= 0; i--) {
            double dum = x[i];
            for (int k = 1; k < l; k++) {
                dum -= au[i][k] * x[k + i];
            }
            x[i] = dum / au[i][0];
            if (l < mm) {
                l++;
            }
        }

        return x;
    }
510117
public double dot(Vector b) {
        if ((b == null) || (size != b.size)) return Double.NaN;

        double result = 0;
        int i;
        for (i = 0; i < size; i++)
            result += vector[i] * b.vector[i];
        return result;
    }

    /**
     * Cross product, only well definited in R^3
     */
    public Vector cross(Vector b) {
        if ((b == null) || (size != 3) || (b.size != 3)) return null;

        Vector result = new Vector(3);
        result.vector[0] = vector[1] * b.vector[2] - vector[2] * b.vector[1];
        result.vector[1] = vector[2] * b.vector[0] - vector[0] * b.vector[2];
        result.vector[2] = vector[0] * b.vector[1] - vector[1] * b.vector[0];
        return result;
    }

    /**
     * Return the length from this vector
     */
    public double length() {
        double value = 0;
        for (int i = 0; i < size; i++)
            value += vector[i] * vector[i];
        return Math.sqrt(value);
    }

    /**
     * Normalize this vector
     */
    public Vector normalize() {
        Vector result = new Vector(size);
        double length = length();
        for (int i = 0; i < size; i++)
            result.vector[i] = vector[i] / length;
        return result;
    }

    /**
     * Negates this vector
     */
    public Vector negate() {
        Vector result = new Vector(size);
        for (int i = 0; i < size; i++)
            result.vector[i] = -vector[i];
        return result;
    }

    /**
     *  Copy a vector
     */
    public Vector duplicate() {
        Vector result = new Vector(size);
        int i;
        for (i = 0; i < size; i++)
            result.vector[i] = vector[i];
        return result;
    }

    /**
     *  Return a vector as a string
     */
    @Override
    public String toString() {
        int i;
        StringBuffer str = new StringBuffer();
        str.append("[ ");
        for (i = 0; i < (size - 1); i++)
            str.append(vector[i] + " ");
        str.append(vector[size - 1] + " ]");
        return str.toString();
    }
}
725036
public Matrix solve(Matrix B)
    {
        //Solve  A x = L L^T x = b, for x 
        
        //First solve L y = b
        Matrix y = forwardSub(L, B);
        //Sole L^T x = y
        Matrix x = backSub(L, y);
        
        return x;
    }
618891
public Matrix solve (Matrix B) throws SingularityException {
      if (B.getRowCount() != m) {
         throw new IllegalArgumentException("Matrix row dimensions must agree.");
      }
      if (!this.isNonsingular()) {
         throw new SingularityException("Matrix is singular.");
      }

      // Copy right hand side with pivoting
      int nx = B.getColumnCount();
      double[][] X = new double[piv.length][nx];
		for (int i=0; i<piv.length; i++) {
			for (int j=0; j<nx; j++) {
				X[i][j]=B.getQuick(piv[i], j);
			}
		}

      // Solve L*Y = B(piv,:)
      for (int k = 0; k < n; k++) {
         for (int i = k+1; i < n; i++) {
            for (int j = 0; j < nx; j++) {
               X[i][j] -= X[k][j]*LU[i][k];
            }
         }
      }
      // Solve U*X = Y;
      for (int k = n-1; k >= 0; k--) {
         for (int j = 0; j < nx; j++) {
            X[k][j] /= LU[k][k];
         }
         for (int i = 0; i < k; i++) {
            for (int j = 0; j < nx; j++) {
               X[i][j] -= X[k][j]*LU[i][k];
            }
         }
      }
      return MatrixFactory.createMatrix(X);
   }
1275229
protected function multiplyMatrix(Matrix $b) : Matrix
    {
        if ($this->n !== $b->m()) {
            throw new DimensionalityMismatchException('Vector A requires'
                . " $this->n rows but Matrix B has {$b->m()}.");
        }

        $c = [];

        foreach ($b as $i => $row) {
            $valueA = $this->a[$i];

            $temp = [];

            foreach ($row as $valueB) {
                $temp[] = $valueA * $valueB;
            }

            $c[] = $temp;
        }

        return Matrix::quick($c);
    }
1153322
public static DoubleMatrix2D subdiagonalMultiply(final DoubleMatrix2D A, final DoubleMatrix2D B){
		final int r = A.rows();
		final int rc = A.columns();
		final int c = B.columns();

		if(r != c){
			throw new IllegalArgumentException("The result must be square");
		}

		boolean useSparsity = A instanceof SparseDoubleMatrix2D;
		DoubleFactory2D F2 = (useSparsity)? DoubleFactory2D.sparse : DoubleFactory2D.dense; 
		final DoubleMatrix2D ret = F2.make(r, c);

		if(useSparsity){
			IntIntDoubleFunction myFunct = new IntIntDoubleFunction() {
				@Override
				public double apply(int t, int s, double pts) {
					int i = t;
					for (int j = 0; j < i + 1; j++) {
						ret.setQuick(i, j, ret.getQuick(i, j) + pts * B.getQuick(s, j));
					}
					return pts;
				}
			};

			//view A row by row
			A.forEachNonZero(myFunct);
		}else{
			for (int i = 0; i < r; i++) {
				for (int j = 0; j < i + 1; j++) {
					double s = 0;
					for (int k = 0; k < rc; k++) {
						s += A.getQuick(i, k) * B.getQuick(k, j);
					}
					ret.setQuick(i, j, s);
				}
			}
		}

		return ret;
	}
1253729
public boolean eq(Matrix B) {
        Matrix A = this;
        if (B.M != A.M || B.N != A.N) {
            throw new RuntimeException("Illegal matrix dimensions.");
        }
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                if (A.data[i][j] != B.data[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }
1339486
protected function multiplyColumnVector(ColumnVector $b) : self
    {
        if ($b->m() !== $this->m) {
            throw new DimensionalityMismatchException('Matrix A requires'
                . " $this->m rows but Vector B has {$b->m()}.");
        }

        $c = [];

        foreach ($this->a as $i => $row) {
            $valueB = $b[$i];
            
            $temp = [];

            foreach ($row as $valueA) {
                $temp[] = $valueA * $valueB;
            }

            $c[] = $temp;
        }

        return self::quick($c);
    }
937483
private Matrix getThisSideMatrix(Matrix B)
    {
        if(this.canBeMutated())
            return this.clone();
        else//so far, only other option in JSAT is a dense matrix
        {
            DenseMatrix dm = new DenseMatrix(rows(), cols());
            dm.mutableAdd(this);
            return dm;
        }
    }
880140
public static <T extends Transform> T multiply (AffineTransform a, AffineTransform b, T into) {
        return multiply(a.m00, a.m01, a.m10, a.m11, a.tx, a.ty,
                        b.m00, b.m01, b.m10, b.m11, b.tx, b.ty, into);
    }
927880
public void solve(DenseMatrix B) {
        if (B.nrows() != L.nrows()) {
            throw new IllegalArgumentException(String.format("Row dimensions do not agree: A is %d x %d, but B is %d x %d", L.nrows(), L.ncols(), B.nrows(), B.ncols()));
        }

        int n = B.nrows();
        int nrhs = B.ncols();

        // Solve L*Y = B;
        for (int k = 0; k < n; k++) {
            for (int j = 0; j < nrhs; j++) {
                for (int i = 0; i < k; i++) {
                    B.sub(k, j, B.get(i, j) * L.get(k, i));
                }
                B.div(k, j, L.get(k, k));
            }
        }

        // Solve L'*X = Y;
        for (int k = n - 1; k >= 0; k--) {
            for (int j = 0; j < nrhs; j++) {
                for (int i = k + 1; i < n; i++) {
                    B.sub(k, j, B.get(i, j) * L.get(i, k));
                }
                B.div(k, j, L.get(k, k));
            }
        }
    }
1051600
public DenseMatrix solve(DenseMatrix B) throws MatrixNotSPDException {
        if (notspd)
            throw new MatrixNotSPDException();
        if (B.numRows() != n)
            throw new IllegalArgumentException("B.numRows() != n");

        intW info = new intW(0);
        if (upper)
            LAPACK.getInstance().dpbtrs(UpLo.Upper.netlib(), n, kd,
                    B.numColumns(), Cu.getData(), Matrices.ld(kd + 1),
                    B.getData(), Matrices.ld(n), info);
        else
            LAPACK.getInstance().dpbtrs(UpLo.Lower.netlib(), n, kd,
                    B.numColumns(), Cl.getData(), Matrices.ld(kd + 1),
                    B.getData(), Matrices.ld(n), info);

        if (info.val < 0)
            throw new IllegalArgumentException();

        return B;
    }