Ejemplo n.º 1
0
  public Matrix matrixReverse() { // 求矩阵的逆
    int i, j, k;
    double change;
    Matrix matrix_reverse = new Matrix(this);

    for (i = 0; i < matrix_reverse.getLine(); i++)
      for (j = matrix_reverse.getRow(); j < 2 * matrix_reverse.getRow(); j++)
        matrix_reverse.setElement(i, j, (j == matrix_reverse.getRow() + i) ? 1 : 0);
    matrix_reverse.setRow(2 * matrix_reverse.getRow());
    matrix_reverse = matrix_reverse.matrixLineSimplify();
    /*将矩阵的左半边化为单位阵*/
    for (i = 0; i < matrix_reverse.getLine() - 1; i++)
      for (j = i + 1; j < matrix_reverse.getRow() / 2; j++) {
        if (matrix_reverse.getElement(i, j) == 0) continue;
        if (matrix_reverse.getElement(i, j) < 0)
          for (k = 0; k < matrix_reverse.getRow(); k++)
            matrix_reverse.setElement(i, k, -matrix_reverse.getElement(i, k));
        if (matrix_reverse.getElement(j, j) < 0)
          for (k = 0; k < matrix_reverse.getRow(); k++)
            matrix_reverse.setElement(j, k, -matrix_reverse.getElement(j, k));
        change = matrix_reverse.getElement(i, j) / matrix_reverse.getElement(j, j);
        for (k = 0; k < matrix_reverse.getRow(); k++)
          matrix_reverse.setElement(
              i, k, matrix_reverse.getElement(i, k) - matrix_reverse.getElement(j, k) * change);
      }
    matrix_reverse.setRow(matrix_reverse.getRow() / 2);
    for (i = 0; i < matrix_reverse.getLine(); i++)
      for (j = 0; j < matrix_reverse.getRow(); j++)
        matrix_reverse.setElement(i, j, matrix_reverse.getElement(i, j + matrix_reverse.getRow()));
    return matrix_reverse;
  }
Ejemplo n.º 2
0
  public Matrix matrixTranspose() { // 矩阵转置
    int i, j;
    Matrix trans_matrix = new Matrix();

    for (i = 0; i < getLine(); i++)
      for (j = 0; j < getRow(); j++) trans_matrix.setElement(j, i, this.getElement(i, j));
    trans_matrix.setLine(getLine());
    trans_matrix.setRow(getRow());
    trans_matrix.setAccuracy(getAccuracy());
    return trans_matrix;
  }
Ejemplo n.º 3
0
  public static Matrix matrixMinus(Matrix matrix1, Matrix matrix2) { // 矩阵减法
    int i, j;
    Matrix matrix = new Matrix();

    for (i = 0; i < matrix1.getLine(); i++)
      for (j = 0; j < matrix1.getRow(); j++)
        matrix.setElement(i, j, matrix1.getElement(i, j) - matrix2.getElement(i, j));
    matrix.setLine(matrix1.getLine());
    matrix.setRow(matrix1.getRow());
    matrix.setAccuracy(matrix1.getAccuracy());
    return matrix;
  }
Ejemplo n.º 4
0
  public static Matrix matrixCopy(Matrix matrix) { // 矩阵复制
    int i, j;
    Matrix matrix_copy = new Matrix();

    matrix_copy.setLine(matrix.getLine());
    matrix_copy.setRow(matrix.getRow());
    matrix_copy.setAccuracy(matrix.getAccuracy());
    matrix_copy.setName(matrix.getName());
    for (i = 0; i < matrix.getLine(); i++)
      for (j = 0; j < matrix.getRow(); j++) matrix_copy.setMatrixArray(matrix.getMatrixArray());
    return matrix_copy;
  }
Ejemplo n.º 5
0
  public static Matrix scanMatrix() { // 输入矩阵
    int i, j;
    Matrix matrix = new Matrix();
    Scanner scanner = new Scanner(System.in);

    matrix.setLine(scanner.nextInt());
    matrix.setRow(scanner.nextInt());
    matrix.setAccuracy(scanner.nextDouble());
    for (i = 0; i < matrix.getLine(); i++) {
      for (j = 0; j < matrix.getRow(); j++) matrix.setElement(i, j, scanner.nextDouble());
    }
    return matrix;
  }
Ejemplo n.º 6
0
  public static Matrix matrixMultiply(Matrix matrix1, Matrix matrix2) { // 矩阵乘法
    int i, j, k;
    Matrix matrix = new Matrix();

    matrix.setLine(matrix1.getLine());
    matrix.setRow(matrix2.getRow());
    matrix.setAccuracy(matrix1.getAccuracy());
    for (i = 0; i < matrix.getLine(); i++)
      for (j = 0; j < matrix.getRow(); j++) matrix.setElement(i, j, 0.0);
    for (i = 0; i < matrix.getLine(); i++)
      for (j = 0; j < matrix.getRow(); j++)
        for (k = 0; k < matrix1.getRow(); k++)
          matrix.setElement(
              i, j, matrix.getElement(i, j) + matrix1.getElement(i, k) * matrix2.getElement(k, j));
    return matrix;
  }