⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gimatrix.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public void setLine(int i, GIMatrix line) throws IndexOutOfBoundsException, BadMatrixFormatException {	if ( (i < 0)||(i >= m) ) throw new IndexOutOfBoundsException();	if ((line.height() != 1) || (line.width() != n)) throw new BadMatrixFormatException();	for (int k = 0; k < n; k++) array[i][k] = line.getValueAt(0,k);    } // method setLine(int,Matrix)    /**     * Sets the column of the matrix at the specified index to a new value.     * @param j the column number     * @param column the colums to be placed at the specified index     * @exception IndexOutOfBoundsException if the given index is out of the matrix's range     * @exception BadMatrixFormatException in case the given Matrix is unproper to replace a column of this Matrix     */    public void setColumn(int j, GIMatrix column) throws IndexOutOfBoundsException, BadMatrixFormatException {	if ( (j < 0)||(j >= n) ) throw new IndexOutOfBoundsException();	if ((column.height() != m) || (column.width() != 1)) throw new BadMatrixFormatException();	for (int k = 0; k < m; k++) array[k][j] = column.getValueAt(k,0);    } // method setColumn(int,Matrix)    /**     * Returns the identity matrix.     * @param n the matrix's dimension (identity matrix is a square matrix)     * @return the identity matrix of format nxn     */    public static GIMatrix identity(int n) {	double[][] identity = new double[n][n];	for (int i = 0; i < n; i++) {	    for (int j = 0; j < i; j++) identity[i][j] = 0.0;	    identity[i][i] = 1.0;	    for (int j = i+1; j < n; j++) identity[i][j] = 0.0;	}	try { return new GIMatrix(identity); } // format is always OK anyway ...	catch (BadMatrixFormatException e) { return null; }    } // method identity(int)        /**     * Returns a null matrix (with zeros everywhere) of given dimensions.     * @param m number of lines     * @param n number of columns     * @return the zero (null) matrix of format mxn     */    public static GIMatrix zero(int m, int n) {	double[][] zero = new double[m][n];	for (int i = 0; i < m; i++)	    for (int j = 0; j < n; j++)		zero[i][j] = 0.0;	try { return new GIMatrix(zero); } // format is always OK anyway ...	catch (BadMatrixFormatException e) { return null; }    } // method zero(int,int)    /**     * Verifies if two given matrix are equal or not. The matrix must be of the same size and dimensions,     * otherwise an exception will be thrown.     * @param matrix the Matrix object to be compared to     * @return true if both matrix are equal element to element     * @exception BadMatrixFormatException if the given matrix doesn't have the same dimensions as this one     */    public boolean equals(GIMatrix matrix) throws BadMatrixFormatException {	if ( (height() != matrix.height())||(width() != matrix.width()) )	    throw new BadMatrixFormatException();	double[][] temp = matrix.getArrayValue();	for (int i = 0; i < m; i++)	    for (int j = 0; j < n; j++)		if (!(array[i][j]==temp[i][j])) return false;	return true;    } // method equals(Matrix)    /**     * Verifies if the matrix is square, that is if it has an equal number of lines and columns.     * @return true if this matrix is square     */    public boolean isSquare() { return (m == n); } // method isSquare()       /**     * Verifies if the matrix is symetric, that is if the matrix is equal to it's transpose.     * @return true if the matrix is symetric     * @exception BadMatrixFormatException if the matrix is not square     */    public boolean isSymmetric() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	// the loop looks in the lower half of the matrix to find non-symetric elements	for (int i = 1; i < m; i++) // starts at index 1 because index (0,0) always symmetric	    for (int j = 0; j < i; j++)		if (!(array[i][j]==array[j][i])) return false;	return true; // the matrix has passed the test    } //method isSymmetric()    // NOT OVER, LOOK MORE CAREFULLY FOR DEFINITION    /**     * Verifies if the matrix is antisymetric, that is if the matrix is equal to the opposite of     * it's transpose.     * @return true if the matrix is antisymetric     * @exception BadMatrixFormatException if the matrix is not square     */    public boolean isAntisymmetric() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	// the loop looks in the lower half of the matrix to find non-antisymetric elements	for (int i = 0; i < m; i++) // not as isSymmetric() loop	    for (int j = 0; j <= i; j++)		if (!(array[i][j]==-array[j][i])) return false;	return true; // the matrix has passed the test    } // method isAntisymmetric()    /**     * Verifies if the matrix is triangular superior or not. A triangular superior matrix has     * zero (0) values everywhere under it's diagonal.     * @return true if the matrix is triangular superior     * @exception BadMatrixFormatException if the matrix is not square     */    public boolean isTriangularSuperior() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	// the loop looks in the lower half of the matrix to find non-null elements	for (int i = 1; i < m; i++) // starts at index 1 because index (0,0) is on the diagonal	    for (int j = 0; j < i; j++)		if (!(array[i][j] == 0.0)) return false;	return true; // the matrix has passed the test    } // method isTriangularSuperior        /**     * Verifies if the matrix is triangular inferior or not. A triangular inferior matrix has     * zero (0) values everywhere upper it's diagonal.     * @return true if the matrix is triangular inferior     * @exception BadMatrixFormatException if the matrix is not square     */    public boolean isTriangularInferior() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	// the loop looks in the upper half of the matrix to find non-null elements	for (int i = 1; i < m; i++) // starts at index 1 because index (0,0) is on the diagonal	    for (int j = i; j < n; j++)		if (!(array[i][j] == 0.0)) return false;	return true; // the matrix has passed the test    } // method isTriangularInferior()    /**     * Verifies wether or not the matrix is diagonal. A diagonal matrix only has elements on its diagonal     * and zeros (0) at every other index. The matrix must be square.     * @return true if the matrix is diagonal     * @exception BadMatrixFormatException if the matrix is not square     */    public boolean isDiagonal() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	// the loop looks both halves of the matrix to find non-null elements	for (int i = 1; i < m; i++) // starts at index 1 because index (0,0) must not be checked	    for (int j = 0; j < i; j++)		if ( (!(array[i][j] == 0.0))||(!(array[j][i]== 0.0)) )		    return false; // not null	return true;    } // method isDiagonal        /**     * Verifies if the matrix is invertible or not by asking for its determinant.     * @return true if the matrix is invertible     * @exception BadMatrixFormatException if the matrix is not square      */    public boolean isInvertible() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	return (!(determinant() == 0)); // det != 0    } // method isInvertible()    /**     * Returns the transpose of this matrix. The transpose of a matrix A = {a(i,j)} is the matrix B = {b(i,j)}     * such that b(i,j) = a(j,i) for every i,j i.e. it is the symetrical reflexion of the matrix along its     * diagonal. The matrix must be square to use this method, otherwise an exception will be thrown.     * @return the matrix's transpose as a Matrix object     * @exception BadMatrixFormatException if the matrix is not square     */    public GIMatrix inverse() throws MatrixNotInvertibleException {	try {	    if (!isInvertible()) throw new MatrixNotInvertibleException();	} catch (BadMatrixFormatException e) {	    throw new MatrixNotInvertibleException();	}	GIMatrix I = identity(n); // Creates an identity matrix of same dimensions	GIMatrix table;	try {	    GIMatrix[][] temp = {{this,I}};	    table = new GIMatrix(temp);	} catch (BadMatrixFormatException e) { return null; } // never happens	table = table.GaussJordan(); // linear reduction method applied	double[][] inv = new double[m][n];	for (int i = 0; i < m; i++) // extracts inverse matrix	    for (int j = n; j < 2*n; j++) {		try { inv[i][j-n] = table.getValueAt(i,j); }		catch (IndexOutOfBoundsException e) { return null; } // never happens	    }	try { return new GIMatrix(inv); }	catch (BadMatrixFormatException e) { return null; } // never happens...    } // method inverse()        /**     * Gauss-Jordan algorithm. Returns the reduced-echeloned matrix of this matrix. The     * algorithm has not yet been optimised but since it is quite simple, it should not be     * a serious problem.     * @return the reduced matrix     */    public GIMatrix GaussJordan() {	GIMatrix tempMatrix= new GIMatrix(this);	try {	    int i = 0;	    int j = 0;	    int k = 0;	    boolean end = false;	    while ( (i < m) && (!end) ) {		boolean allZero = true; // true if all elements under line i are null (zero)		while (j < n) { // determination of the pivot		    for (k = i; k < m; k++) {			if (!(tempMatrix.getValueAt(k,j)== 0.0)) { // if an element != 0			    allZero = false;			    break;			} }		    if (allZero) j++;		    else break;		}		if (j == n) end = true;		else {		    if (k != i) tempMatrix = tempMatrix.invertLine(i,k);		    if (!(tempMatrix.getValueAt(i,j)== 1.0)) // if element != 1			tempMatrix = // A = L(i)(1/a(i,j))(A)			    tempMatrix.multiplyLine(i,1/tempMatrix.getValueAt(i,j));		    for (int q = 0; q < m; q++)			if (q != i) // A = L(q,i)(-a(q,j))(A)			tempMatrix = tempMatrix.addLine(q,i,-tempMatrix.getValueAt(q,j));		}		i++;	    }	    // normally here, r = i-1	    return tempMatrix;	} catch (IndexOutOfBoundsException e) { return null; } // never happens... well I hope ;)	/*	  From: LEROUX, P. Algebre lineaire: une approche matricielle.	  Modulo Editeur, 1983. p. 75. (In French)	*/    } // method GaussJordan()        /**     * Returns the transpose of this matrix. The transpose of a matrix A = {a(i,j)} is the matrix B = {b(i,j)}     * such that b(i,j) = a(j,i) for every i,j i.e. it is the symetrical reflexion of the matrix along its     * diagonal. The matrix must be square to use this method, otherwise an exception will be thrown.     * @return the matrix's transpose as a Matrix object     * @exception BadMatrixFormatException if the matrix is not square     */    public GIMatrix transpose() throws BadMatrixFormatException {	if (m != n) throw new BadMatrixFormatException();	double[][] transpose = new double[array.length][array[0].length];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -