Java实现矩阵

Java代码:


import java.util.Random;   
 
//矩阵类   
public class Matrix {
    private int[][] matrix;   
    Random random = new Random();   
 
    // 构造方法   
    public Matrix() {   
        matrix = new int[3][3];   
    }   
 
    public Matrix(int n) {   
        matrix = new int[n][n];     
        for (int i = 0; i < n; i++) {   
            for (int j = 0; j < n; j++) {   
                matrix[i][j] = random.nextInt(100);   
            }   
        }   
    }   
 
    public Matrix(int n, int m) {   
        matrix = new int[n][m];     
        for (int i = 0; i < n; i++) {   
            for (int j = 0; j < m; j++) {   
                matrix[i][j] = random.nextInt(100);   
            }   
        }   
    }   
 
    public int[][] getMatrix() {   
        return matrix;   
    }   
 
    // 输出矩阵中所有元素   
    public void output() {   
        for (int i = 0; i < matrix.length; i++) {   
            for (int j = 0; j < matrix[i].length; j++) {   
                System.out.print(matrix[i][j] + "\t");   
            }   
            System.out.println();   
        }   
    }   
 
    // 求一个矩阵的转置矩阵   
    public Matrix transpose() {
        int n = matrix.length;   
        int m = matrix[0].length;
        Matrix transMatrix = new Matrix(n, m);   
 
        for (int i = 0; i < n; i++) {   
            for (int j = 0; j < m; j++) {   
                transMatrix.getMatrix()[i][j] = matrix[j][i];   
            }   
        }   
        return transMatrix;   
    }   
 
    // 判断一个矩阵是否为上三角矩阵   
    public boolean isTriangular() {
        // 用相反的思路进行判断   
        for (int i = 1; i < matrix.length; i++) {   
            for (int j = 0; j < i; j++) {   
                if (matrix[i][j] != 0) {   
                    return false;   
                }   
            }   
        }   
        return true;   
    }   
 
    // 判断是否为对称矩阵   
    public boolean isSymmetry() {
        for (int i = 1; i < matrix.length; i++) {   
            for (int j = 0; j < matrix[i].length; j++) {   
                if (matrix[i][j] != matrix[j][i]) {   
                    return false;   
                }   
            }   
        }   
        return true;   
    }   
 
    // 矩阵的相加   
    public void add(Matrix b) {
        int[][] matrixOfB = b.getMatrix();
        int n = matrixOfB.length;   
        int m = matrixOfB[0].length;   
 
        if (n != matrix.length || m != matrix[0].length) {   
            System.out.println("矩阵的长度不一致,不能相加");   
        } else {   
            for (int i = 0; i < n; i++) {   
                for (int j = 0; j < m; j++) {   
                    matrix[i][j] += matrixOfB[i][j];   
                }   
            }
        }   
    }   
 
    public static void main(String[] args) {
        //测试   
        Matrix test1 = new Matrix(4);   
 
        System.out.println("原始矩阵");   
        test1.output();   
 
        Matrix transMatrix = test1.transpose();   
 
        System.out.println("转置矩阵");   
        transMatrix.output();   
 
        System.out.println("是否是上三角矩阵");   
        System.out.println(test1.isTriangular());   
 
        System.out.println("是否是对称矩阵");   
        System.out.println(test1.isSymmetry());   
 
        System.out.println("----------------------");   
 
        Matrix test2 = new Matrix();   
 
        test2.output();   
 
        System.out.println(test2.isTriangular());   
        System.out.println(test2.isSymmetry());   
           
        System.out.println("----------------------");   
           
        Matrix test3 = new Matrix(4);   
        Matrix test4 = new Matrix(4);   
           
        test3.add(test2);   
           
        System.out.println("矩阵1");   
        test3.output();   
        System.out.println("矩阵2");   
        test4.output();   
           
        System.out.println("矩阵相加");   
        test3.add(test4);   
           
        test3.output();   
    }   

}  


要使用Java实现矩阵运算,可以使用数组来表示矩阵,并编写相应的函数来实现不同的运算。以下是一个简单的示例代码,展示了如何实现矩阵的加法、减法和乘法运算: ```java public class MatrixOperations { public static void main(String[] args) { double[][] matrixA = { {1.0, 1.0, 1.0}, {1.0, 1.0, 1.0}, {1.0, 1.0, 1.0} }; double[][] matrixB = { {2.0, 2.0, 2.0}, {2.0, 2.0, 2.0}, {2.0, 2.0, 2.0} }; double[][] result = addMatrices(matrixA, matrixB); printMatrix(result); result = subtractMatrices(matrixA, matrixB); printMatrix(result); result = multiplyMatrices(matrixA, matrixB); printMatrix(result); } public static double[][] addMatrices(double[][] matrixA, double[][] matrixB) { int rows = matrixA.length; int cols = matrixA = matrixA[i][j + matrixB[i][j]; } } return result; } public static double[][] subtractMatrices(double[][] matrixA, double[][] matrixB) { int rows = matrixA.length; int cols = matrixA = matrixA[i][j - matrixB[i][j]; } } return result; } public static double[][] multiplyMatrices(double[][] matrixA, double[][] matrixB) { int rowsA = matrixA.length; int colsA = matrixA += matrixA[i][k * matrixB[k][j]; } } } return result; } public static void printMatrix(double[][] matrix) { int rows = matrix.length; int cols = matrix + " "); } System.out.println(); } System.out.println(); } } ``` 上述代码中,`addMatrices`函数实现矩阵的加法运算,`subtractMatrices`函数实现矩阵的减法运算,`multiplyMatrices`函数实现矩阵的乘法运算。`printMatrix`函数用于打印矩阵。 你可以根据需要修改矩阵的大小和内容,并调用相应的函数来执行不同的矩阵运算。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [用Java实现矩阵的基本运算(矩阵与数字相加,矩阵矩阵相加,矩阵矩阵相乘,矩阵与数字相乘)附有详细代码...](https://blog.csdn.net/m0_58941767/article/details/121137711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值