/* Class Matrix
*
* Defines a matrix and includes the methods needed
* for standard matrix manipulations, e.g. multiplation,
* and related procedures, e.g. solution of linear
* simultaneous equations
*
* See class ComplexMatrix and PhasorMatrix for complex matrix arithmetic
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: June 2002
* UPDATES: 21 April 2004, 19 January 2005, 1 May 2005
* UPDATES: 16 February 2006 Set methods corrected thanks to Myriam Servi�res, Equipe IVC , Ecole Polytechnique de l'universit� de Nantes, Laboratoire IRCCyN/UMR CNRS
* UPDATES: 31 March 2006 Norm methods corrected thanks to Jinshan Wu, University of British Columbia
* UPDATES: 22 April 2006 getSubMatrix methods corrected thanks to Joachim Wesner
* UPDATES: 1 July 2007 row matrix, column matrix and division added
* UPDATES: 17 July 2007 solution of overdetermined linear equations and eigen values and vectros of a symmetric matrix added
* UPDATES: 18 August 2007 zero replacement in LU decompostion made a variable that the user may set
* UPDATES: 7 October 2007 get row and column numbers methods revised
* UPDATES: 27 February 2008 symmetric eigen method corrected - thanks to Mike Kroutikov
* UPDATES: 7 April 2008 and 5 July 2008 code tidying
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web page:
* http://www.ee.ucl.ac.uk/~mflanaga/java/Matrix.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2002 - 2008 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
* Permission to use, copy and modify this software and its documentation for
* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
*
* Dr Michael Thomas Flanagan makes no representations about the suitability
* or fitness of the software for any or for a particular purpose.
* Michael Thomas Flanagan shall not be liable for any damages suffered
* as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/
package flanagan.math;
import flanagan.analysis.Regression;
import flanagan.analysis.RegressionFunction;
public class Matrix{
private int numberOfRows = 0; // number of rows
private int numberOfColumns = 0; // number of columns
private double matrix[][] = null; // 2-D Matrix
private double hessenberg[][] = null; // 2-D Hessenberg equivalent
private boolean hessenbergDone = false; // = true when Hessenberg matrix calculated
private int permutationIndex[] = null; // row permutation index
private double rowSwapIndex = 1.0D; // row swap index
private double[] eigenValues = null; // eigen values of the matrix
private double[][] eigenVector = null; // eigen vectors of the matrix
private double[] sortedEigenValues = null; // eigen values of the matrix sorted into descending order
private double[][] sortedEigenVector = null; // eigen vectors of the matrix sorted to matching descending eigen value order
private int numberOfRotations = 0; // number of rotations in Jacobi transformation
private int maximumJacobiIterations = 100; // maximum number of Jacobi iterations
private boolean eigenDone = false; // = true when eigen values and vectors calculated
private boolean matrixCheck = true; // check on matrix status
// true - no problems encountered in LU decomposition
// false - attempted a LU decomposition on a singular matrix
private boolean supressErrorMessage = false; // true - LU decompostion failure message supressed
private double tiny = 1.0e-100; // small number replacing zero in LU decomposition
// CONSTRUCTORS
// Construct a numberOfRows x numberOfColumns matrix of variables all equal to zero
public Matrix(int numberOfRows, int numberOfColumns){
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
this.matrix = new double[numberOfRows][numberOfColumns];
this.permutationIndex = new int[numberOfRows];
for(int i=0;i<numberOfRows;i++)this.permutationIndex[i]=i;
}
// Construct a numberOfRows x numberOfColumns matrix of variables all equal to the number const
public Matrix(int numberOfRows, int numberOfColumns, double constant){
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
this.matrix = new double[numberOfRows][numberOfColumns];
for(int i=0;i<numberOfRows;i++){
for(int j=0;j<numberOfColumns;j++)this.matrix[i][j]=constant;
}
this.permutationIndex = new int[numberOfRows];
for(int i=0;i<numberOfRows;i++)this.permutationIndex[i]=i;
}
// Construct matrix with a reference to an existing numberOfRows x numberOfColumns 2-D array of variables
public Matrix(double[][] twoD){
this.numberOfRows = twoD.length;
this.numberOfColumns = twoD[0].length;
for(int i=0; i<numberOfRows; i++){
if(twoD[i].length!=numberOfColumns)throw new IllegalArgumentException("All rows must have the same length");
}
this.matrix = twoD.clone();
this.permutationIndex = new int[numberOfRows];
for(int i=0;i<numberOfRows;i++)this.permutationIndex[i]=i;
}
// Construct matrix with a reference to the 2D matrix and permutation index of an existing Matrix bb.
public Matrix(Matrix bb){
this.numberOfRows = bb.numberOfRows;
this.numberOfColumns = bb.numberOfColumns;
this.matrix = bb.matrix.clone();
this.permutationIndex = bb.permutationIndex.clone();
this.rowSwapIndex = bb.rowSwapIndex;
}
// METHODS
// SET VALUES
// reset value of tiny used to replace zero in LU decompostions
// If not set: 1e-100 used
public void resetLUzero(double zeroValue){
this.tiny = zeroValue;
}
// Set the matrix with a copy of an existing numberOfRows x numberOfColumns 2-D matrix of variables
public void setTwoDarray(double[][] aarray){
if(this.numberOfRows != aarray.length)throw new IllegalArgumentException("row length of this Matrix differs from that of the 2D array argument");
if(this.numberOfColumns != aarray[0].length)throw new IllegalArgumentException("column length of this Matrix differs from that of the 2D array argument");
for(int i=0; i<numberOfRows; i++){
if(aarray[i].length!=numberOfColumns)throw new IllegalArgumentException("All rows must have the same length");
for(int j=0; j<numberOfColumns; j++){
this.matrix[i][j]=aarray[i][j];
}
}
}
// Set an individual array element
// i = row index
// j = column index
// aa = value of the element
public void setElement(int i, int j, double aa){
this.matrix[i][j]=aa;
}
// Set a sub-matrix starting with row index i, column index j
public void setSubMatrix(int i, int j, double[][] subMatrix){
int k = subMatrix.length;
int l = subMatrix[0].length;
if(i>k)throw new IllegalArgumentException("row indices inverted");
if(j>l)throw new IllegalArgumentException("column indices inverted");
int n=k-i+1, m=l-j+1;
for(int p=0; p<n; p++){
for(int q=0; q<m; q++){
this.matrix[i+p][j+q] = subMatrix[p][q];
没有合适的资源?快使用搜索试试~ 我知道了~
矩阵运算Matrix,java实现
共1个文件
java:1个
5星 · 超过95%的资源 需积分: 46 159 下载量 100 浏览量
2008-09-26
11:38:54
上传
评论 4
收藏 9KB RAR 举报
温馨提示
关于矩阵的相关运算的类库,java编写的。可以直接调用,包括矩逆阵,矩阵相乘,求特征值,特征向量等基本操作运算
资源推荐
资源详情
资源评论
收起资源包目录
Matrix.rar (1个子文件)
Matrix.java 63KB
共 1 条
- 1
dnnyyq
- 粉丝: 21
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页