欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现矩阵加减乘除及转制等运算功能示例

程序员文章站 2023-12-16 21:53:10
本文实例讲述了java实现矩阵加减乘除及转制等运算功能。分享给大家供大家参考,具体如下: java初学,编写矩阵预算程序,当做工具,以便以后写算法时使用。 pu...

本文实例讲述了java实现矩阵加减乘除及转制等运算功能。分享给大家供大家参考,具体如下:

java初学,编写矩阵预算程序,当做工具,以便以后写算法时使用。

public class matrixoperation {
  public static int[][] add(int[][] matrix_a, int[][] matrix_b) {
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    if (row != matrix_b.length || col != matrix_b[0].length) {
      system.out.println("fault");
    } else {
      for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
          result[i][j] = matrix_a[i][j] + matrix_b[i][j];
        }
      }
    }
    return result;
  }
  public static int[][] sub(int[][] matrix_a, int[][] matrix_b) {
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    if (row != matrix_b.length || col != matrix_b[0].length) {
      system.out.println("fault");
    } else {
      for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
          result[i][j] = matrix_a[i][j] - matrix_b[i][j];
        }
      }
    }
    return result;
  }
  public static int[][] dot(int[][] matrix_a, int[][] matrix_b) {
    /*
     * matrix_a's dimention m*p matrix_b's dimention p*n. return dimention
     * m*n
     */
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    if (col != matrix_b.length) {
      system.out.println("fault");
    } else {
      for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
          result[i][j] = 0;
          for (int k = 0; k < col; k++) {
            result[i][j] += matrix_a[i][k] * matrix_b[k][j];
          }
        }
      }
    }
    return result;
  }
  public static int[][] dot(int[][] matrix_a, int b) {
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        result[i][j] = matrix_a[i][j] * b;
      }
    }
    return result;
  }
  public static int[][] mul(int[][] matrix_a, int[][] matrix_b) {
    /*
     * matrix_a's dimention m*n matrix_b's dimention m*n. return dimention
     * m*n
     */
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    if (row != matrix_b.length || col != matrix_b[0].length) {
      system.out.println("fault");
    } else {
      for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
          result[i][j] = matrix_a[i][j] * matrix_b[i][j];
        }
      }
    }
    return result;
  }
  public static int[][] transport(int[][] matrix_a) {
    int row = matrix_a.length;
    int col = matrix_a[0].length;
    int[][] result = new int[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        result[j][i] = matrix_a[i][j];
      }
    }
    return result;
  }
  public static void print(int[][] matrix) {
    int row = matrix.length;
    int col = matrix[0].length;
    for (int i = 0; i < row; i++) {
      system.out.print("[");
      for (int j = 0; j < col; j++) {
        system.out.print(matrix[i][j]);
        if (j != col - 1) {
          system.out.print(", ");
        }
      }
      system.out.print("]\n");
    }
  }
  public static void main(string[] args) {
    int[][] a = { { 1, 2 }, { 3, 4 } };
    int[][] b = { { 7, 8 }, { 6, 5 } };
    int[][] c = add(a, b);
    system.out.println("测试结果如下:");
    system.out.println("matrix a = ");
    print(a);
    system.out.println("matrix b = ");
    print(b);
    system.out.println("matrix a + b = ");
    print(c);
    c = sub(a, b);
    system.out.println("matrix a - b = ");
    print(c);
    int[][] d = dot(a, b);
    system.out.println("matrix a dot b = ");
    print(d);
    int[][] e = dot(a, 3);
    system.out.println("matrix a * 3 = ");
    print(e);
    int[][] f = transport(a);
    system.out.println("matrix a.t = ");
    print(f);
    int[][] g = mul(a, b);
    system.out.println("matrix a * b = ");
    print(g);
  }
}

运行结果:

Java实现矩阵加减乘除及转制等运算功能示例

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: