以下是一个基于Java语言的编程示例,实现了上述要求:
通用矩阵类 Matrix:public class Matrix { private double[][] data; // 数据域 // 默认无参数构造方法 public Matrix() { } // 创建指定二维数组具体值的矩阵的构造方法 public Matrix(double[][] data) { this.data = data; } // 创建指定矩阵维数(m×n)的随机数的矩阵的构造方法 public Matrix(int m, int n) { this.data = new double[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { data[i][j] = Math.random(); } } } // 获取矩阵维数的访问器方法 public int getRows() { return data.length; } public int getCols() { return data[0].length; } // 返回矩阵的字符串表示 @Override public String toString() { StringBuilder result = new StringBuilder(); result.append("Matrix " + getRows() + "x" + getCols() + ":\n"); for (int i = 0; i < getRows(); i++) { for (int j = 0; j < getCols(); j++) { result.append(data[i][j] + "\t"); } result.append("\n"); } return result.toString(); } // 矩阵加法 public Matrix add(Matrix matrix) throws MatrixDimensionMismatchException { if (getRows() != matrix.getRows() || getCols() != matrix.getCols()) { throw new MatrixDimensionMismatchException(this, matrix); } double[][] resultData = new double[getRows()][getCols()]; for (int i = 0; i < getRows(); i++) { for (int j = 0; j < getCols(); j++) { resultData[i][j] = data[i][j] + matrix.data[i][j]; } } return new Matrix(resultData); } // 矩阵相乘 public Matrix multiply(Matrix matrix) throws MatrixDimensionMismatchException { if (getCols() != matrix.getRows()) { throw new MatrixDimensionMismatchException(this, matrix); } double[][] resultData = new double[getRows()][matrix.getCols()]; for (int i = 0; i < getRows(); i++) { for (int j = 0; j < matrix.getCols(); j++) { double sum = 0; for (int k = 0; k < getCols(); k++) { sum += data[i][k] * matrix.data[k][j]; } resultData[i][j] = sum; } } return new Matrix(resultData); } // 矩阵转置 public Matrix transpose() { double[][] resultData = new double[getCols()][getRows()]; for (int i = 0; i < getCols(); i++) { for (int j = 0; j < getRows(); j++) { resultData[i][j] = data[j][i]; } } return new Matrix(resultData); } // 数除矩阵 public Matrix divide(double number) { double[][] resultData = new double[getRows()][getCols()]; for (int i = 0; i < getRows(); i++) { for (int j = 0; j < getCols(); j++) { resultData[i][j] = data[i][j] / number; } } return new Matrix(resultData); } // 获取某列向量 public Matrix getColumn(int col) throws IndexOutOfBoundsException { if (col < 0 || col >= getCols()) { throw new IndexOutOfBoundsException("Invalid column index: " + col); } double[][] resultData = new double[getRows()][1]; for (int i = 0; i < getRows(); i++) { resultData[i][0] = data[i][col]; } return new Matrix(resultData); } // 获取某行向量 public Matrix getRow(int row) throws IndexOutOfBoundsException { if (row < 0 || row >= getRows()) { throw new IndexOutOfBoundsException("Invalid row index: " + row); } double[][] resultData = new double[1][getCols()]; for (int j = 0; j < getCols(); j++) { resultData[0][j] = data[row][j]; } return new Matrix(resultData); }}
方阵类 SquareMatrix:public class SquareMatrix extends Matrix { // 调用父类构造方法 public SquareMatrix(int n) { super(n, n); } // 求主对角线和的方法 public double sumOfMainDiagonal() { double sum = 0; for (int i = 0; i < getRows(); i++) { sum += super.data[i][i]; } return sum; }}
矩阵维度不匹配时的自定义异常类 MatrixDimensionMismatchException:public class MatrixDimensionMismatchException extends Exception { public MatrixDimensionMismatchException(Matrix matrix1, Matrix matrix2) { super("Matrix dimension mismatch: (" + matrix1.getRows() + "x" + matrix1.getCols() + ") and (" + matrix2.getRows() + "x" + matrix2.getCols() + ")"); }}
测试类 Main:public class Main { public static void main(String[] args) { Matrix matrix1 = new Matrix(new double[][]{{1, 2}, {3, 4}}); Matrix matrix2 = new Matrix(new double[][]{{5, 6}, {7, 8}}); try { System.out.println("Matrix1:"); System.out.println(matrix1); System.out.println("Matrix2:"); System.out.println(matrix2); System.out.println("Matrix1 + Matrix2:"); System.out.println(matrix1.add(matrix2)); System.out.println("Matrix1 * Matrix2:"); System.out.println(matrix1.multiply(matrix2)); System.out.println("Transpose of Matrix1:"); System.out.println(matrix1.transpose()); System.out.println("Matrix1 / 2:"); System.out.println(matrix1.divide(2)); System.out.println("Column 1 of Matrix1:"); System.out.println(matrix1.getColumn(0)); System.out.println("Row 0 of Matrix1:"); System.out.println(matrix1.getRow(0)); SquareMatrix squareMatrix = new SquareMatrix(3); System.out.println("SquareMatrix:"); System.out.println(squareMatrix); System.out.println("Sum of main diagonal of SquareMatrix:"); System.out.println(squareMatrix.sumOfMainDiagonal()); } catch (MatrixDimensionMismatchException e) { System.out.println(e.getMessage()); } catch (IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } }}
上述代码中创建了一个通用矩阵类 Matrix 和一个方阵类 SquareMatrix,以及一个自定义异常类 MatrixDimensionMismatchException。测试类 Main 中展示了如何使用这些类进行矩阵相关操作,并处理可能抛出的异常。
请根据实际需求修改代码,例如添加其他所需的方法或属性,适配不同的问题场景。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |