📄 matrix.java
字号:
* @param b the multiplication matrix
* @return the product matrix
*/
public final Matrix multiply(Matrix b) {
try {
return new Matrix(getMatrix().times(b.getMatrix()).getArrayCopy());
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Performs a (ridged) linear regression.
*
* @param y the dependent variable vector
* @param ridge the ridge parameter
* @return the coefficients
* @throws IllegalArgumentException if not successful
*/
public final double[] regression(Matrix y, double ridge) {
return getMatrix().regression(y.getMatrix(), ridge).getCoefficients();
}
/**
* Performs a weighted (ridged) linear regression.
*
* @param y the dependent variable vector
* @param w the array of data point weights
* @param ridge the ridge parameter
* @return the coefficients
* @throws IllegalArgumentException if the wrong number of weights were
* provided.
*/
public final double[] regression(Matrix y, double [] w, double ridge) {
return getMatrix().regression(y.getMatrix(), w, ridge).getCoefficients();
}
/**
* Returns the L part of the matrix.
* This does only make sense after LU decomposition.
*
* @return matrix with the L part of the matrix;
* @see #LUDecomposition()
*/
public Matrix getL() throws Exception {
int nr = numRows(); // num of rows
int nc = numColumns(); // num of columns
double[][] ld = new double[nr][nc];
for (int i = 0; i < nr; i++) {
for (int j = 0; (j < i) && (j < nc); j++) {
ld[i][j] = getElement(i, j);
}
if (i < nc) ld[i][i] = 1;
}
Matrix l = new Matrix(ld);
return l;
}
/**
* Returns the U part of the matrix.
* This does only make sense after LU decomposition.
*
* @return matrix with the U part of a matrix;
* @see #LUDecomposition()
*/
public Matrix getU() throws Exception {
int nr = numRows(); // num of rows
int nc = numColumns(); // num of columns
double[][] ud = new double[nr][nc];
for (int i = 0; i < nr; i++) {
for (int j = i; j < nc ; j++) {
ud[i][j] = getElement(i, j);
}
}
Matrix u = new Matrix(ud);
return u;
}
/**
* Performs a LUDecomposition on the matrix.
* It changes the matrix into its LU decomposition.
*
* @return the indices of the row permutation
*/
public int[] LUDecomposition() throws Exception {
// decompose
weka.core.matrix.LUDecomposition lu = m_Matrix.lu();
// singular? old class throws Exception!
if (!lu.isNonsingular())
throw new Exception("Matrix is singular");
weka.core.matrix.Matrix u = lu.getU();
weka.core.matrix.Matrix l = lu.getL();
// modify internal matrix
int nr = numRows();
int nc = numColumns();
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
if (j < i)
setElement(i, j, l.get(i, j));
else
setElement(i, j, u.get(i, j));
}
}
u = null;
l = null;
return lu.getPivot();
}
/**
* Solve A*X = B using backward substitution.
* A is current object (this). Note that this matrix will be changed!
* B parameter bb.
* X returned in parameter bb.
*
* @param bb first vector B in above equation then X in same equation.
*/
public void solve(double[] bb) throws Exception {
// solve
weka.core.matrix.Matrix x = m_Matrix.solve(
new weka.core.matrix.Matrix(bb, bb.length));
// move X into bb
int nr = x.getRowDimension();
for (int i = 0; i < nr; i++)
bb[i] = x.get(i, 0);
}
/**
* Performs Eigenvalue Decomposition using Householder QR Factorization
*
* Matrix must be symmetrical.
* Eigenvectors are return in parameter V, as columns of the 2D array.
* (Real parts of) Eigenvalues are returned in parameter d.
*
* @param V double array in which the eigenvectors are returned
* @param d array in which the eigenvalues are returned
* @throws Exception if matrix is not symmetric
*/
public void eigenvalueDecomposition(double[][] V, double[] d)
throws Exception {
// old class only worked with symmetric matrices!
if (!this.isSymmetric())
throw new Exception("EigenvalueDecomposition: Matrix must be symmetric.");
// perform eigenvalue decomposition
weka.core.matrix.EigenvalueDecomposition eig = m_Matrix.eig();
weka.core.matrix.Matrix v = eig.getV();
double[] d2 = eig.getRealEigenvalues();
// transfer data
int nr = numRows();
int nc = numColumns();
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
V[i][j] = v.get(i, j);
for (int i = 0; i < d2.length; i++)
d[i] = d2[i];
}
/**
* Returns sqrt(a^2 + b^2) without under/overflow.
*
* @param a length of one side of rectangular triangle
* @param b length of other side of rectangular triangle
* @return lenght of third side
*/
protected static double hypot(double a, double b) {
return weka.core.matrix.Maths.hypot(a, b);
}
/**
* converts the Matrix into a single line Matlab string: matrix is enclosed
* by parentheses, rows are separated by semicolon and single cells by
* blanks, e.g., [1 2; 3 4].
* @return the matrix in Matlab single line format
*/
public String toMatlab() {
return getMatrix().toMatlab();
}
/**
* creates a matrix from the given Matlab string.
* @param matlab the matrix in matlab format
* @return the matrix represented by the given string
* @see #toMatlab()
*/
public static Matrix parseMatlab(String matlab) throws Exception {
return new Matrix(weka.core.matrix.Matrix.parseMatlab(matlab).getArray());
}
/**
* Main method for testing this class.
*/
public static void main(String[] ops) {
double[] first = {2.3, 1.2, 5};
double[] second = {5.2, 1.4, 9};
double[] response = {4, 7, 8};
double[] weights = {1, 2, 3};
try {
// test eigenvaluedecomposition
double[][] m = {{1, 2, 3}, {2, 5, 6},{3, 6, 9}};
Matrix M = new Matrix(m);
int n = M.numRows();
double[][] V = new double[n][n];
double[] d = new double[n];
double[] e = new double[n];
M.eigenvalueDecomposition(V, d);
Matrix v = new Matrix(V);
// M.testEigen(v, d, );
// end of test-eigenvaluedecomposition
Matrix a = new Matrix(2, 3);
Matrix b = new Matrix(3, 2);
System.out.println("Number of columns for a: " + a.numColumns());
System.out.println("Number of rows for a: " + a.numRows());
a.setRow(0, first);
a.setRow(1, second);
b.setColumn(0, first);
b.setColumn(1, second);
System.out.println("a:\n " + a);
System.out.println("b:\n " + b);
System.out.println("a (0, 0): " + a.getElement(0, 0));
System.out.println("a transposed:\n " + a.transpose());
System.out.println("a * b:\n " + a.multiply(b));
Matrix r = new Matrix(3, 1);
r.setColumn(0, response);
System.out.println("r:\n " + r);
System.out.println("Coefficients of regression of b on r: ");
double[] coefficients = b.regression(r, 1.0e-8);
for (int i = 0; i < coefficients.length; i++) {
System.out.print(coefficients[i] + " ");
}
System.out.println();
System.out.println("Weights: ");
for (int i = 0; i < weights.length; i++) {
System.out.print(weights[i] + " ");
}
System.out.println();
System.out.println("Coefficients of weighted regression of b on r: ");
coefficients = b.regression(r, weights, 1.0e-8);
for (int i = 0; i < coefficients.length; i++) {
System.out.print(coefficients[i] + " ");
}
System.out.println();
a.setElement(0, 0, 6);
System.out.println("a with (0, 0) set to 6:\n " + a);
a.write(new java.io.FileWriter("main.matrix"));
System.out.println("wrote matrix to \"main.matrix\"\n" + a);
a = new Matrix(new java.io.FileReader("main.matrix"));
System.out.println("read matrix from \"main.matrix\"\n" + a);
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -