Ejemplo n.º 1
0
  public Matrix<T, A> add(Matrix<T, A> right) throws MatrixException {
    Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);

    if (rowSize != right.rowSize || columnSize != right.columnSize)
      throw new MatrixException("Cannot add matrices of different sizes");

    setup(temp, rowSize, columnSize);

    for (int i = 0; i < rowSize; ++i) {
      for (int j = 0; j < columnSize; ++j)
        temp.matrix[i][j] = arithmetics.add(matrix[i][j], right.matrix[i][j]);
    }

    return temp;
  }
Ejemplo n.º 2
0
  public Matrix<T, A> multiply(Matrix<T, A> right) throws MatrixException {
    Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, right.columnSize);

    if (columnSize != right.rowSize)
      throw new MatrixException("Cannot multiply matrices of incompatible sizes");

    setup(temp, rowSize, right.columnSize);

    for (int i = 0; i < rowSize; ++i) {
      for (int j = 0; j < right.columnSize; ++j) {
        T sum = arithmetics.zero();
        for (int k = 0; k < columnSize; ++k) {
          sum = arithmetics.add(sum, arithmetics.multiply(matrix[i][k], right.matrix[k][j]));
        }
        temp.matrix[i][j] = sum;
      }
    }

    return temp;
  }