📄 gimatrix.java
字号:
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) transpose[i][j] = array[j][i]; return new GIMatrix(transpose); } // method transpose() /** * Returns a matrix containing all of the diagonal elements of this matrix and zero (0) everywhere * else. This matrix is called the diagonal of the matrix. * @return the diagonal of the matrix * @exception BadMatrixFormatException if the matrix is not square */ public GIMatrix diagonal() throws BadMatrixFormatException { if (m != n) throw new BadMatrixFormatException(); double[][] diagonal = new double[array.length][array[0].length];; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (i == j) diagonal[i][j] = array[i][j]; else diagonal[i][j] = 0; } return new GIMatrix(diagonal); } // method diagonal() /** * Returns the resulting matrix of an elementary linear operation that consists of multiplying a * single line of the matrix by a constant. * @param i the line number * @param c the double constant that multiplies the line * @return the resulting Matrix object of the linear operation * @exception IndexOutOfBoundsException if the given index is out of the matrix's range */ public GIMatrix multiplyLine(int i, double c) throws IndexOutOfBoundsException { if ( (i < 0)||(i >= m) ) throw new IndexOutOfBoundsException(); double[][] temp = array; for (int k = 0; k < n; k++) temp[i][k] = c*temp[i][k]; // mutliply every member of the line by c try { return new GIMatrix(temp); } // format is always OK anyway ... catch (BadMatrixFormatException e) { return null; } } // method multiplyLine(int,int) /** * Returns the resulting matrix of an elementary linear operation that consists of inverting two lines. * @param i the first line number * @param j the second line number * @return the resulting Matrix object of the linear operation * @exception IndexOutOfBoundsException if the given index is out of the matrix's range */ public GIMatrix invertLine(int i, int j) throws IndexOutOfBoundsException { if ( (i < 0)||(i >= m)||(j < 0)||(j >= m) ) throw new IndexOutOfBoundsException(); double[][] temp = array; double[] tempLine = temp[j]; // temporary line temp[j] = temp[i]; temp[i] = tempLine; try { return new GIMatrix(temp); } // format is always OK anyway ... catch (BadMatrixFormatException e) { return null; } } // method invertLine(int,int) /** * Returns the resulting matrix of an elementary linear operation that consists of adding one line, * multiplied by some constant factor, to another line. * @param i the first line number * @param j the second line number (to be added to the first) * @param c the double constant that multiplies the first line * @return the resulting Matrix object of the linear operation * @exception IndexOutOfBoundsException if the given index is out of the matrix's range */ public GIMatrix addLine(int i, int j, double c) throws IndexOutOfBoundsException{ if ( (i < 0)||(i >= m)||(j < 0)||(j >= m) ) throw new IndexOutOfBoundsException(); double[][] temp = array; for (int k = 0; k < n; k++) temp[i][k] = temp[i][k]+c*temp[j][k]; // add multiplied element of i to element of j try { return new GIMatrix(temp); } // format is always OK anyway ... catch (BadMatrixFormatException e) { return null; } } // method addLine(int,int,double) /** * Addition from two matrices. */ public GIMatrix add(GIMatrix b) { if ((b==null) || (m!=b.m) || (n!=b.n)) return null; int i, j; GIMatrix result = new GIMatrix(m,n); for(i=0; i<m; i++) for(j=0; j<n; j++) result.array[i][j] = array[i][j]+b.array[i][j]; return result; } /** * Returns the result of the scalar multiplication of the matrix, that is the multiplication of every * of its elements by a given number. * @param c the constant by which the matrix is multiplied * @return the resulting matrix of the scalar multiplication */ public GIMatrix multiply(double c) { double[][] temp = array; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) temp[i][j] = c*temp[i][j]; try { return new GIMatrix(temp); } // format is always OK anyway ... catch (BadMatrixFormatException e) { return null; } } // method multiply(double) /** * Returns the result of the matricial multiplication of this matrix by another one. The matrix passed * as parameter <i>follows</i> this matrix in the multiplication, so for an example if the dimension of * the actual matrix is mxn, the dimension of the second one should be nxp in order for the multiplication * to be performed (otherwise an exception will be thrown) and the resulting matrix will have dimension mxp. * @param matrix the matrix following this one in the matricial multiplication * @return the resulting matrix of the matricial multiplication * @exception BadMatrixFormatException if the matrix passed in arguments has wrong dimensions */ public GIMatrix multiply(GIMatrix matrix) throws BadMatrixFormatException { if (n != matrix.height()) throw new BadMatrixFormatException(); // unsuitable dimensions int p = matrix.width(); double[][] temp = new double[m][p]; double[][] multiplied = matrix.getArrayValue(); for (int i = 0; i < m; i++) // line index of the first matrix for (int k = 0; k < p; k++) { // column index of the second matrix temp[i][k] = array[i][0]*multiplied[0][k]; // first multiplication for (int j = 1; j < n; j++) // sum of multiplications temp[i][k] = temp[i][k]+array[i][j]*multiplied[j][k]; } return new GIMatrix(temp); } // method multiply(Matrix) /** * Returns the determinant of this matrix. The matrix must be * square in order to use this method, otherwise an exception will be thrown. * <i>Warning: this algorithm is very unefficient and takes too much time to compute * with large matrices.</i> * @return the determinant of the matrix * @exception BadMatrixFormatException if the matrix is not square */ public double determinant() throws BadMatrixFormatException { if (m != n) throw new BadMatrixFormatException(); return det(array); // use of recursive method } // method determinant() // Method used for recursive determinant algorithm. Supposes the given array is square. private double det(double[][] mat) { if (mat.length == 1) return mat[0][0]; double temp = mat[0][0]*det(M(mat,0,0)); // (-1)^(0+0)*m[0][0]*det(M(i,j)) ... first assignation for (int k = 1; k < mat.length; k++) temp = temp+(det(M(mat,0,k))*((k % 2 == 0)?mat[0][k]:-mat[0][k])); // Note: ((0+k)%2 == 0)?1:-1 is equivalent to (-1)^(0+k) return temp; } // method det(double[][]) // Returns the minor of the array (supposed square) i.e. the array least its i-th line // and j-th column private double[][] M(double[][] mat, int i, int j) { double[][] temp = new double[mat.length-1][mat[0].length-1]; // "void minor" for (int k = 0; k < i; k++) { for (int h = 0; h < j; h++) temp[k][h] = mat[k][h]; for (int h = j+1; h < mat[0].length; h++) temp[k][h-1] = mat[k][h]; } for (int k = i+1; k < mat.length; k++) { for (int h = 0; h < j; h++) temp[k-1][h] = mat[k][h]; for (int h = j+1; h < mat[0].length; h++) temp[k-1][h-1] = mat[k][h]; } return temp; } // method M(double[][],int,int) /** * Returns the trace of this matrix, that is the sum of the elements of its diagonal. The matrix must be * square in order to use this method, otherwise an exception will be thrown. * @return the trace of the matrix * @exception BadMatrixFormatException if the matrix is not square */ public double trace() throws BadMatrixFormatException { if (m != n) throw new BadMatrixFormatException(); double trace = array[0][0]; for (int i = 1; i < m; i++) trace = trace+array[i][i]; return trace; } // method trace()// /**// * Returns the matrix as a String.// * The double numbers are printed using the form "p/q" or "a b/c" depending on the value of the boolean,// * and use a minimal number of spaces as specified. It is the user's responsibility to grant a number// * of spaces wide enough to get proper alignment.// * @see tatien.toolbox.double#toString// * @param simple must be true to get simple expression, false otherwise as specified// * @param spaces number of spaces// */// public String print(boolean simple, int spaces) {// String print = "";// for (int i = 0; i < m; i++) {// print = print + array[i][0].toString(simple,spaces);// for (int j = 1; j < n; j++) {// print = print + array[i][j].toString(simple,spaces);// }// print = print + "\n";// }// return print;// } // method print() // Verifies if the matrix is of good format when calling a constructor or setArrayValue private void verifyMatrixFormat(double[][] testedMatrix) throws BadMatrixFormatException { if ( (testedMatrix.length == 0)||(testedMatrix[0].length == 0) ) throw new BadMatrixFormatException(); int noOfColumns = testedMatrix[0].length; for (int i = 1; i < testedMatrix.length; i++) if (testedMatrix[i].length != noOfColumns) throw new BadMatrixFormatException(); } // method verifyMatrixFormat(double[][]) // In the case of the implementation of a table i.e. an array of matrices, verifies if the table is proper. private void verifyTableFormat(GIMatrix[][] testedTable) throws BadMatrixFormatException { if ( (testedTable.length == 0)||(testedTable[0].length == 0) ) throw new BadMatrixFormatException(); int noOfColumns = testedTable[0].length; int currentHeigth, currentWidth; for (int i = 0; i < testedTable.length; i++) { // verifies correspondence of m's (heigth) if (testedTable[i].length != noOfColumns) throw new BadMatrixFormatException(); currentHeigth = testedTable[i][0].height(); for (int j = 1; j < testedTable[0].length; j++) if (testedTable[i][j].height() != currentHeigth) throw new BadMatrixFormatException(); } for (int j = 0; j < testedTable[0].length; j++) { // verifies correspondence of n's (width) currentWidth = testedTable[0][j].width(); for (int i = 1; i < testedTable.length; i++) if (testedTable[i][j].width() != currentWidth) throw new BadMatrixFormatException(); } } // method verifyTableFormat(Matrix[][]) } // class Matrix
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -