Michielve 2024-11-29 16:20 采纳率: 0%
浏览 4

矩阵类运算(相关搜索:矩阵运算|构造函数|初始化)

用Java完成一个实现矩阵运算。这些任务包括创建一个Matrix类,该类具有各种方法来处理加法、减法、乘法、转置和行列式计算等操作。矩阵类实现
实现 Matrix 类的构造函数,用于处理基本矩阵操作。
构造函数 public Matrix(double[][] data) 用于初始化矩阵。如果输入数据为空或不是正确的 2D 数组(行长度不同),则抛出 IllegalArgumentException。

  • 写回答

1条回答 默认 最新

  • 趣享先生 优质创作者: 编程框架技术领域 2024-11-29 16:40
    关注

    该回答参考ChatGPT,由吾为汝整理提供如若有用,还请题主采纳!

    下面是一个用Java实现的 Matrix 类,包含了矩阵的基本运算,包括加法、减法、乘法、转置和行列式计算。该类的构造函数会检查输入数据的有效性,并在不符合要求时抛出 IllegalArgumentException

    Matrix 类实现

    public class Matrix {
        private double[][] data;
        private int rows;
        private int cols;
    
        // 构造函数
        public Matrix(double[][] data) {
            if (data == null || data.length == 0) {
                throw new IllegalArgumentException("输入数据不能为空");
            }
    
            rows = data.length;
            cols = data[0].length;
    
            // 检查每一行的长度是否相同
            for (int i = 1; i < rows; i++) {
                if (data[i].length != cols) {
                    throw new IllegalArgumentException("输入的二维数组行长度不一致");
                }
            }
    
            this.data = new double[rows][cols];
            for (int i = 0; i < rows; i++) {
                System.arraycopy(data[i], 0, this.data[i], 0, cols);
            }
        }
    
        // 矩阵加法
        public Matrix add(Matrix other) {
            if (this.rows != other.rows || this.cols != other.cols) {
                throw new IllegalArgumentException("矩阵维度不匹配,无法进行加法运算");
            }
    
            double[][] result = new double[rows][cols];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    result[i][j] = this.data[i][j] + other.data[i][j];
                }
            }
            return new Matrix(result);
        }
    
        // 矩阵减法
        public Matrix subtract(Matrix other) {
            if (this.rows != other.rows || this.cols != other.cols) {
                throw new IllegalArgumentException("矩阵维度不匹配,无法进行减法运算");
            }
    
            double[][] result = new double[rows][cols];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    result[i][j] = this.data[i][j] - other.data[i][j];
                }
            }
            return new Matrix(result);
        }
    
        // 矩阵乘法
        public Matrix multiply(Matrix other) {
            if (this.cols != other.rows) {
                throw new IllegalArgumentException("矩阵维度不匹配,无法进行乘法运算");
            }
    
            double[][] result = new double[this.rows][other.cols];
            for (int i = 0; i < this.rows; i++) {
                for (int j = 0; j < other.cols; j++) {
                    for (int k = 0; k < this.cols; k++) {
                        result[i][j] += this.data[i][k] * other.data[k][j];
                    }
                }
            }
            return new Matrix(result);
        }
    
        // 矩阵转置
        public Matrix transpose() {
            double[][] result = new double[cols][rows];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    result[j][i] = this.data[i][j];
                }
            }
            return new Matrix(result);
        }
    
        // 计算行列式(仅适用于2x2矩阵)
        public double determinant() {
            if (rows != cols) {
                throw new IllegalArgumentException("行列式仅适用于方阵");
            }
            if (rows == 2) {
                return this.data[0][0] * this.data[1][1] - this.data[0][1] * this.data[1][0];
            }
            throw new UnsupportedOperationException("行列式计算仅支持2x2矩阵");
        }
    
        // 打印矩阵
        public void print() {
            for (double[] row : data) {
                for (double value : row) {
                    System.out.printf("%8.2f ", value);
                }
                System.out.println();
            }
        }
    
        // 主方法用于测试
        public static void main(String[] args) {
            double[][] array1 = {{1, 2}, {3, 4}};
            double[][] array2 = {{5, 6}, {7, 8}};
    
            Matrix matrix1 = new Matrix(array1);
            Matrix matrix2 = new Matrix(array2);
    
            System.out.println("Matrix 1:");
            matrix1.print();
    
            System.out.println("Matrix 2:");
            matrix2.print();
    
            Matrix sum = matrix1.add(matrix2);
            System.out.println("Sum:");
            sum.print();
    
            Matrix difference = matrix1.subtract(matrix2);
            System.out.println("Difference:");
            difference.print();
    
            Matrix product = matrix1.multiply(matrix2);
            System.out.println("Product:");
            product.print();
    
            Matrix transpose = matrix1.transpose();
            System.out.println("Transpose of Matrix 1:");
            transpose.print();
    
            double det = matrix1.determinant();
            System.out.println("Determinant of Matrix 1: " + det);
        }
    }
    

    代码说明

    1. 构造函数public Matrix(double[][] data) 用于初始化矩阵,检查输入数据是否为空或行长度不一致。
    2. 矩阵运算
      • add(Matrix other):实现矩阵加法。
      • subtract(Matrix other):实现矩阵减法。
      • multiply(Matrix other):实现矩阵乘法。
      • transpose():实现矩阵转置。
      • determinant():计算2x2矩阵的行列式。
    3. 打印矩阵print() 方法用于输出矩阵的内容。
    4. 主方法:用于测试矩阵类的功能。

    使用说明

    可以通过创建 Matrix 对象并调用相应的方法来进行矩阵运算。希望这个实现能够满足你的需求!如果有其他问题或需要进一步的功能,请告诉我。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月29日