用Java玩矩阵

用简单的循环控制语句实现矩阵的基本运算
没有写注释,有难理解的地方可以问我

package processor;

import java.util.Scanner;

public class Main {
    static boolean check = true;
    public static void main(String[] args) {
        while (check) {
            makeChoice();
        }
    }

    public static void add() {      //A矩阵和B矩阵相加
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter size of first matrix: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        System.out.println("Enter first matrix: ");
        if (rowA <= 1 || columnA <= 1) {
            System.out.println("The operation cannot be performed.");
            return;
        }
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        System.out.print("Enter size of second matrix: ");
        int rowB = scanner.nextInt();
        int columnB = scanner.nextInt();
        System.out.println("Enter second matrix: ");
        if (rowB <= 1 || columnB <= 1) {
            System.out.println("The operation cannot be performed.");
            return;
        }
        double[][] B = new double[rowB][columnB];
        for (int i = 0; i < rowB; i ++) {
            for (int j = 0; j < columnB; j ++) {
                B[i][j] = scanner.nextDouble();
            }
        }
        if (rowA == rowB && columnA == columnB) {
            double[][] C = new double[rowA][columnA];
            System.out.println("The result is: ");
            for (int i = 0; i < rowB; i ++) {
                for (int j = 0; j < columnB; j ++) {
                    C[i][j] = A[i][j] + B[i][j];
                    System.out.print(C[i][j] + " ");
                }
                System.out.println();
            }
        } else {
            System.out.println("ERROR");
        }
        System.out.println();
    }

    public static void multiplyDigital() {     //矩阵与数相乘
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter size of matrix: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        System.out.println("Enter matrix: ");
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        System.out.println("Enter constant: ");
        double multi = scanner.nextDouble();
        System.out.println("The result is: ");
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = A[i][j] * multi;
                System.out.print(A[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void multiplyMatrices() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter size of first matrix: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        System.out.println("Enter first matrix: ");
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        System.out.print("Enter size of second matrix: ");
        int rowB = scanner.nextInt();
        int columnB = scanner.nextInt();
        System.out.println("Enter second matrix: ");
        double[][] B = new double[rowB][columnB];
        for (int i = 0; i < rowB; i ++) {
            for (int j = 0; j < columnB; j ++) {
                B[i][j] = scanner.nextDouble();
            }
        }
        if (columnA == rowB) {
            System.out.println("The result is: ");
            for (int i = 0; i < rowA; i ++) {
                for (int m = 0; m < columnB; m ++) {
                    double num = 0;
                    for (int k = 0; k < rowB; k ++) {
                        num += A[i][k] * B[k][m];
                    }
                    System.out.print(num + " ");
                }
                System.out.println();
            }
        } else {
            System.out.println("ERROR");
        }
        System.out.println();
    }

    public static void transpose() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("1. Main diagonal\n2. Side diagonal\n" +
                "3. Vertical line\nrowA. Horizontal line\nYour choice: ");
        int choice = scanner.nextInt();
        System.out.print("Enter matrix size: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        System.out.println("Enter first matrix: ");
        if (rowA <= 1 || columnA <= 1) {
            System.out.println("The operation cannot be performed.");
            return;
        }
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        System.out.println("The result is: ");
        switch (choice) {
            case 1 :
                mainDiagonal(rowA,columnA,A);
                break;
            case 2 :
                sideDiagonal(rowA,columnA,A);
                break;
            case 3 :
                verticalLine(rowA,columnA,A);
                break;
            case 4 :
                horizontalLine(rowA,columnA,A);
                break;
        }
    }

    public static void horizontalLine(int rowA,int columnA,double[][] A) {
        double[][] B = new double[rowA][columnA];
        int count = rowA;
        for (int i = 0; i < rowA; i ++) {
            count --;
            for (int j = 0; j < columnA; j ++) {
                B[i][j] = A[count][j];
            }
        }
        for (int l = 0; l < columnA; l ++) {
            for (int k = 0; k < rowA; k ++) {
                System.out.print(B[l][k] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void verticalLine(int rowA,int columnA,double[][] A) {
        double[][] B = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            int count = columnA;
            for (int j = 0; j < columnA; j ++) {
                count --;
                B[i][j] = A[i][count];
            }
        }
        for (int l = 0; l < columnA; l ++) {
            for (int k = 0; k < rowA; k ++) {
                System.out.print(B[l][k] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void sideDiagonal(int rowA,int columnA,double[][] A) {
        double[][] B = new double[columnA][rowA];
        int checkColumn = rowA;
        for (int i = 0; i < rowA; i ++) {
            int checkRow = columnA;
            checkColumn --;
            for (int j = 0; j < columnA; j ++) {
                checkRow --;
                B[checkRow][checkColumn] = A[i][j];
            }
        }
        for (int l = 0; l < columnA; l ++) {
            for (int k = 0; k < rowA; k ++) {
                System.out.print(B[l][k] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void mainDiagonal(int rowA,int columnA,double[][] A) {
        for (int i = 0; i < columnA; i ++) {
            for (int j = 0; j < rowA; j ++) {
                System.out.print(A[j][i] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void calculateDet() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter size of matrix: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        System.out.println("Enter matrix: ");
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        System.out.println("The result is: ");
        System.out.println(c(rowA,A));
    }

    public static double c(int row,double[][] a) {
        double num = 0;
        if (row == 2) {
            return a[0][0] * a[1][1] - a[0][1] * a[1][0];
        } else {
            for (int i = 0; i < row; i ++) {
                    num += (1 - ((2 + i) % 2) * 2) * a[0][i] *
                            c(row - 1,create5(row - 1,a,i));
            }
        }
        return num;
    }

    public static double[][] create5(int row,double[][] a,int col) {
        double[][] newCD = new double[row][row];
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < row; j++) {
                if (j < col) {
                    newCD[i][j] = a[i + 1][j];
                } else {
                    newCD[i][j] = a[i + 1][j + 1];
                }
            }
        }
        return newCD;
    }

    public static double create6(int row,double[][] a,int rc,int col) {
        double[][] newCD = new double[row][row];
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < row; j ++) {
                if (j < col && i < rc) {
                    newCD[i][j] = a[i][j];
                } else if (j < col){
                    newCD[i][j] = a[i + 1][j];
                } else if (i < rc) {
                    newCD[i][j] = a[i][j + 1];
                } else {
                    newCD[i][j] = a[i + 1][j + 1];
                }
            }
        }
        if (row == 2) {
            return newCD[0][0] * newCD[1][1] - newCD[0][1] * newCD[1][0];
        }
        return c(row,newCD);
    }

    public static void inverse() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter size of matrix: ");
        int rowA = scanner.nextInt();
        int columnA = scanner.nextInt();
        if (rowA != columnA) {
            return;
        }
        System.out.println("Enter matrix: ");
        double[][] A = new double[rowA][columnA];
        for (int i = 0; i < rowA; i ++) {
            for (int j = 0; j < columnA; j ++) {
                A[i][j] = scanner.nextDouble();
            }
        }
        double num = c(rowA,A);
        if (num == 0) {
            System.out.println("This matrix doesn't have an inverse.");
            return;
        }
        double[][] B = new double[rowA][columnA];
        if (rowA > 1) {
            System.out.println("The result is: ");
            for (int i = 0; i < rowA; i ++) {
                for (int j = 0; j < columnA; j ++) {
                    B[j][i] = (1 - ((i + j + 2) % 2) * 2) *
                            create6(rowA - 1,A,i,j);
                }
            }
            for (int i = 0; i < rowA; i ++) {
                for (int j = 0; j < columnA; j ++) {
                    System.out.print(String.format("%.2f",B[i][j] / num) + " ");
                }
                System.out.println();
            }
        } else {
            System.out.println("error");
        }
        System.out.println();
    }

    public static void makeChoice() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1. Add matrices");
        System.out.println("2. Multiply matrix by a constant");
        System.out.println("3. Multiply matrices");
        System.out.println("4. Transpose matrix");
        System.out.println("5. Calculate a determinant");
        System.out.println("6. Inverse matrix");
        System.out.println("0. Exit");
        System.out.print("Your choice: ");
        int choice = scanner.nextInt();
        switch (choice) {
            case 1 :
                add();
                break;
            case 2 :
                multiplyDigital();
                break;
            case 3 :
                multiplyMatrices();
                break;
            case 4 :
                transpose();
                System.out.println();
                break;
            case 5 :
                calculateDet();
                break;
            case 6 :
                inverse();
                break;
            case 0 :
                check = false;
                break;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NIHILISTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值