📄 neuralmath.java
字号:
/* * $RCSfile: NeuralMath.java,v $ * $Revision: 1.6 $ * $Date: 2005/05/04 23:33:23 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Brasília * * This file is part of NeuralNetworkToolkit. * * NeuralNetworkToolkit is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NeuralNetworkToolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NeuralNetworkToolkit; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 - USA. */package neuralnetworktoolkit.math;import jama.Matrix;/** * Class that implements some numerical methods for neural networks * learning. Some methods uses linear algebra library <b>JAMA</b> * (<a href="http://math.nist.gov/javanumerics/jama"> * http://math.nist.gov/javanumerics/jama</a>). See JAMA documentation * for details. * * @version $Revision: 1.6 $ - $Date: 2005/05/04 23:33:23 $ * * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gonçalves</a> */public class NeuralMath { /** * Prints a matrix on screen. * * @param matrix Matrix to be printed. */ public static void printMatrix(double[][] matrix) { // TODO Create a self implementation. if (matrix != null) { Matrix m = new Matrix(matrix); m.print(10, 50); } } //printMatrix() /** * Obtains the diagonal matrix of a matrix. * * @param matrix Matrix to obtain the diagonal matrix. * * @return Diagonal matrix of the parameter. */ public static double[][] diagonalMatrix(double[][] matrix) { double[][] diagonal = new double[matrix.length][matrix[0].length]; for (int i = 0; i < diagonal.length; i++) { diagonal[i][i] = matrix[i][i]; } return diagonal; } //diagonalMatrix() /** * * @param matrix * @return */ public static double[] diagonalArray(double[][] matrix) { double[] diagonalArray; diagonalArray = new double[matrix.length]; for(int i = 0; i < matrix.length; i++) { diagonalArray[i] = matrix[i][i]; } return diagonalArray; } //diagonalArray() /** * Calculates the product of a transposed matrix by the original. * * @param matrix Matrix to calculate. * * @return Product of a transposed matrix by the original. */ public static double[][] transposeProduct(double[][] matrix) { double[][] product = new double[matrix[0].length][matrix[0].length]; double element; for (int column1 = 0; column1 < matrix[0].length; column1++) { element = 0; for (int column2 = 0; column2 < matrix[0].length; column2++) { element = 0; for (int line = 0; line < matrix.length; line++) element = element + matrix[line][column1] * matrix[line][column2]; product[column1][column2] = element; } } return product; } //transposeProduct() /** * Obtains the transposed matrix of a matrix. * * @param matrix Matrix to obtain the transposed matrix. * * @return Transposed matrix. */ public static double[][] calculateTransposed(double[][] matrix) { double[][] transposed = new double[matrix[0].length][matrix.length]; for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[0].length; j++) transposed[j][i] = matrix[i][j]; return transposed; } //calculateTransposed() /** * Calculates the sum of two matrices. * * @param originalMatrix First matrix to sum. * @param matrix Second matrix to sum. * * @return The sum of the two matrices. */ public static double[][] matrixSum(double[][] originalMatrix, double[][] matrix) { double[][] sum = new double[originalMatrix.length][originalMatrix[0].length]; for (int i = 0; i < originalMatrix.length; i++) { for (int j = 0; j < originalMatrix[0].length; j++) { sum[i][j] = originalMatrix[i][j] + matrix[i][j]; } } return sum; } //matrixSum() /** * * @param matrix * @param diagonal * @return */ public static double[][] matrixSumDiagonal(double[][] matrix, double[] diagonal) { double[][] newMatrix; newMatrix = (double[][]) matrix.clone(); for (int i = 0; i < newMatrix.length; i++) { newMatrix[i][i] = diagonal[i] + newMatrix[i][i]; } return newMatrix; } //matrixSumDiagonal() /** * * @param constant * @param matrix * @return */ public static double[][] matrixSumConstantDiagonal(double constant, double[][] matrix) { double[][] newMatrix; newMatrix = (double[][]) matrix.clone(); for (int i = 0; i < newMatrix.length; i++) { newMatrix[i][i] = constant + newMatrix[i][i]; } return newMatrix; } //matrixSumConstantDiagonal() /** * Calculates the multiplicative inverse of a matrix. * * @param matrix Matrix to invert. * * @return Inverse matrix. */ public static double[][] inverseMatrix(double[][] matrix) { Matrix toInverse = new Matrix(matrix); return toInverse.inverse().getArray(); } //inverseMatrix() /** * Calculates the multiplicative inverse of a matrix, using an * adaption of JAMA library to use Cholesky decomposition on the * calculus. <br><b>It is not a stable method! More tests be * needed. Avoid to use this!</b> * <br>Future versions of <code>INeuralMath</code> probably will * have an unique implementation for matrix inversion. * * @param matrix Matrix to invert. * * @return Inverse matrix. */ public static double[][] inverseCholMatrix(double[][] matrix) { Matrix toInverse = new Matrix(matrix); return toInverse.inverseCholesky().getArray(); } //inverseCholMatrix() /** * Calculates the product of two matrices, 'first' times 'second'. * * @param first First matrix of the product. * @param second Second matrix of the product. * * @return The product of the two matrices. */ public static double[][] matrixProduct(double[][] first, double[][] second) { // TODO Create a self implementation. Matrix product = new Matrix(first); return product.times(new Matrix(second)).getArray(); } //matrixProduct() /** * Calculates the product of a matrix by a constant. * * @param constant Constant of the product. * @param matrix Matrix of the product. * * @return Product of a matrix by a constant. */ public static double[][] constantTimesMatrix(double constant, double[][] matrix) { double[][] result = new double[matrix.length][matrix[0].length]; for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[0].length; j++) result[i][j] = constant * matrix[i][j]; return result; } //constantTimesMatrix() /** * * @param constant * @param matrix * @return */ public static double[][] constantTimesDiagonal(double constant, double[][] matrix) { double[][] newMatrix; newMatrix = (double[][]) matrix.clone(); for (int i = 0; i < newMatrix.length; i++) { newMatrix[i][i] = constant * newMatrix[i][i]; } return newMatrix; } //constantTimesDiagonal() /** * Calculates the product of a matrix by a constant. * * @param constant Constant of the product. * @param matrix Matrix of the product. * * @return Product of a matrix by a constant. */ public static double[][] constantTimesMatrix(double constant, double[] matrix) { double[][] result = new double[matrix.length][1]; for (int i = 0; i < matrix.length; i++) { result[i][0] = constant * matrix[i]; } return result; } //constantTimesMatrix() /** * * @param constant * @param array * @return */ public static double[] constantTimesArray(double constant, double[] array) { double[] newArray; newArray = new double[array.length]; for (int i = 0; i < array.length; i++) newArray[i] = constant * array[i]; return newArray; } //constantTimesArray() /** * Creates a identity matrix with dimensions <i>i</i> and <i>j</i>. * * @param i Lines dimension. * @param j Columns dimension. * * @return Identity matrix. */ public static double[][] identity(int i, int j) { // TODO Create a self implementation. return Matrix.identity(i, j).getArray(); } //identity() } //NeuralMath
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -