removed direct references to RealMatrixImpl and use factory
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@728186 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
735e84a26a
commit
136ffc1054
|
@ -22,8 +22,8 @@ import java.util.Arrays;
|
|||
import org.apache.commons.math.linear.InvalidMatrixException;
|
||||
import org.apache.commons.math.linear.LUDecompositionImpl;
|
||||
import org.apache.commons.math.linear.LUSolver;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
|
||||
/**
|
||||
* Base class for implementing estimators.
|
||||
|
@ -183,8 +183,8 @@ public abstract class AbstractEstimator implements Estimator {
|
|||
try {
|
||||
// compute the covariances matrix
|
||||
RealMatrix inverse =
|
||||
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(jTj, false))).getInverse();
|
||||
return ((RealMatrixImpl) inverse).getDataRef();
|
||||
new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj))).getInverse();
|
||||
return inverse.getData();
|
||||
} catch (InvalidMatrixException ime) {
|
||||
throw new EstimationException("unable to compute covariances: singular problem",
|
||||
null);
|
||||
|
|
|
@ -22,8 +22,8 @@ import java.io.Serializable;
|
|||
import org.apache.commons.math.linear.InvalidMatrixException;
|
||||
import org.apache.commons.math.linear.LUDecompositionImpl;
|
||||
import org.apache.commons.math.linear.LUSolver;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
import org.apache.commons.math.linear.RealVector;
|
||||
import org.apache.commons.math.linear.RealVectorImpl;
|
||||
|
||||
|
@ -112,8 +112,7 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
|
|||
double[] grad = new double[parameters.length];
|
||||
RealVectorImpl bDecrement = new RealVectorImpl(parameters.length);
|
||||
double[] bDecrementData = bDecrement.getDataRef();
|
||||
RealMatrixImpl wGradGradT = new RealMatrixImpl(parameters.length, parameters.length);
|
||||
double[][] wggData = wGradGradT.getDataRef();
|
||||
RealMatrix wGradGradT = MatrixUtils.createRealMatrix(parameters.length, parameters.length);
|
||||
|
||||
// iterate until convergence is reached
|
||||
double previous = Double.POSITIVE_INFINITY;
|
||||
|
@ -122,7 +121,7 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
|
|||
// build the linear problem
|
||||
incrementJacobianEvaluationsCounter();
|
||||
RealVector b = new RealVectorImpl(parameters.length);
|
||||
RealMatrix a = new RealMatrixImpl(parameters.length, parameters.length);
|
||||
RealMatrix a = MatrixUtils.createRealMatrix(parameters.length, parameters.length);
|
||||
for (int i = 0; i < measurements.length; ++i) {
|
||||
if (! measurements [i].isIgnored()) {
|
||||
|
||||
|
@ -137,10 +136,9 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
|
|||
|
||||
// build the contribution matrix for measurement i
|
||||
for (int k = 0; k < parameters.length; ++k) {
|
||||
double[] wggRow = wggData[k];
|
||||
double gk = grad[k];
|
||||
for (int l = 0; l < parameters.length; ++l) {
|
||||
wggRow[l] = weight * gk * grad[l];
|
||||
wGradGradT.setEntry(k, l, weight * gk * grad[l]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -96,38 +96,35 @@ class BiDiagonalTransformer implements Serializable {
|
|||
final int p = main.length;
|
||||
final int diagOffset = (m >= n) ? 0 : 1;
|
||||
final double[] diagonal = (m >= n) ? main : secondary;
|
||||
final double[][] uData = new double[m][m];
|
||||
cachedU = MatrixUtils.createRealMatrix(m, m);
|
||||
|
||||
// fill up the part of the matrix not affected by Householder transforms
|
||||
for (int k = m - 1; k >= p; --k) {
|
||||
uData[k][k] = 1;
|
||||
cachedU.setEntry(k, k, 1);
|
||||
}
|
||||
|
||||
// build up first part of the matrix by applying Householder transforms
|
||||
for (int k = p - 1; k >= diagOffset; --k) {
|
||||
final double[] hK = householderVectors[k];
|
||||
uData[k][k] = 1;
|
||||
cachedU.setEntry(k, k, 1);
|
||||
if (hK[k - diagOffset] != 0.0) {
|
||||
for (int j = k; j < m; ++j) {
|
||||
double alpha = 0;
|
||||
for (int i = k; i < m; ++i) {
|
||||
alpha -= uData[i][j] * householderVectors[i][k - diagOffset];
|
||||
alpha -= cachedU.getEntry(i, j) * householderVectors[i][k - diagOffset];
|
||||
}
|
||||
alpha /= diagonal[k - diagOffset] * hK[k - diagOffset];
|
||||
|
||||
for (int i = k; i < m; ++i) {
|
||||
uData[i][j] -= alpha * householderVectors[i][k - diagOffset];
|
||||
cachedU.addToEntry(i, j, -alpha * householderVectors[i][k - diagOffset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (diagOffset > 0) {
|
||||
uData[0][0] = 1;
|
||||
cachedU.setEntry(0, 0, 1);
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedU = new RealMatrixImpl(uData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
@ -145,24 +142,20 @@ class BiDiagonalTransformer implements Serializable {
|
|||
|
||||
final int m = householderVectors.length;
|
||||
final int n = householderVectors[0].length;
|
||||
double[][] bData = new double[m][n];
|
||||
cachedB = MatrixUtils.createRealMatrix(m, n);
|
||||
for (int i = 0; i < main.length; ++i) {
|
||||
double[] bDataI = bData[i];
|
||||
bDataI[i] = main[i];
|
||||
cachedB.setEntry(i, i, main[i]);
|
||||
if (m < n) {
|
||||
if (i > 0) {
|
||||
bDataI[i - 1] = secondary[i - 1];
|
||||
cachedB.setEntry(i, i - 1, secondary[i - 1]);
|
||||
}
|
||||
} else {
|
||||
if (i < main.length - 1) {
|
||||
bDataI[i + 1] = secondary[i];
|
||||
cachedB.setEntry(i, i + 1, secondary[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedB = new RealMatrixImpl(bData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
@ -184,38 +177,35 @@ class BiDiagonalTransformer implements Serializable {
|
|||
final int p = main.length;
|
||||
final int diagOffset = (m >= n) ? 1 : 0;
|
||||
final double[] diagonal = (m >= n) ? secondary : main;
|
||||
final double[][] vData = new double[n][n];
|
||||
cachedV = MatrixUtils.createRealMatrix(n, n);
|
||||
|
||||
// fill up the part of the matrix not affected by Householder transforms
|
||||
for (int k = n - 1; k >= p; --k) {
|
||||
vData[k][k] = 1;
|
||||
cachedV.setEntry(k, k, 1);
|
||||
}
|
||||
|
||||
// build up first part of the matrix by applying Householder transforms
|
||||
for (int k = p - 1; k >= diagOffset; --k) {
|
||||
final double[] hK = householderVectors[k - diagOffset];
|
||||
vData[k][k] = 1;
|
||||
cachedV.setEntry(k, k, 1);
|
||||
if (hK[k] != 0.0) {
|
||||
for (int j = k; j < n; ++j) {
|
||||
double beta = 0;
|
||||
for (int i = k; i < n; ++i) {
|
||||
beta -= vData[i][j] * hK[i];
|
||||
beta -= cachedV.getEntry(i, j) * hK[i];
|
||||
}
|
||||
beta /= diagonal[k - diagOffset] * hK[k];
|
||||
|
||||
for (int i = k; i < n; ++i) {
|
||||
vData[i][j] -= beta * hK[i];
|
||||
cachedV.addToEntry(i, j, -beta * hK[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (diagOffset > 0) {
|
||||
vData[0][0] = 1;
|
||||
cachedV.setEntry(0, 0, 1);
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedV = new RealMatrixImpl(vData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
|
|
@ -228,7 +228,17 @@ public class EigenDecompositionImpl implements EigenDecomposition {
|
|||
throws InvalidMatrixException {
|
||||
|
||||
if (cachedV == null) {
|
||||
cachedV = getVT().transpose();
|
||||
|
||||
if (eigenvectors == null) {
|
||||
findEigenVectors();
|
||||
}
|
||||
|
||||
final int m = eigenvectors.length;
|
||||
cachedV = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int k = 0; k < m; ++k) {
|
||||
cachedV.setColumnVector(k, eigenvectors[k]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
@ -239,18 +249,9 @@ public class EigenDecompositionImpl implements EigenDecomposition {
|
|||
/** {@inheritDoc} */
|
||||
public RealMatrix getD()
|
||||
throws InvalidMatrixException {
|
||||
|
||||
if (cachedD == null) {
|
||||
|
||||
final int m = eigenvalues.length;
|
||||
final double[][] sData = new double[m][m];
|
||||
for (int i = 0; i < m; ++i) {
|
||||
sData[i][i] = eigenvalues[i];
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedD = new RealMatrixImpl(sData, false);
|
||||
|
||||
cachedD = MatrixUtils.createRealDiagonalMatrix(eigenvalues);
|
||||
}
|
||||
return cachedD;
|
||||
}
|
||||
|
@ -265,14 +266,12 @@ public class EigenDecompositionImpl implements EigenDecomposition {
|
|||
findEigenVectors();
|
||||
}
|
||||
|
||||
final double[][] vtData = new double[eigenvectors.length][];
|
||||
for (int k = 0; k < eigenvectors.length; ++k) {
|
||||
vtData[k] = eigenvectors[k].getData();
|
||||
final int m = eigenvectors.length;
|
||||
cachedVt = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int k = 0; k < m; ++k) {
|
||||
cachedVt.setRowVector(k, eigenvectors[k]);
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedVt = new RealMatrixImpl(vtData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
|
|
@ -147,7 +147,7 @@ public class EigenSolver implements DecompositionSolver {
|
|||
}
|
||||
}
|
||||
|
||||
return new RealMatrixImpl(bp, false);
|
||||
return MatrixUtils.createRealMatrix(bp);
|
||||
|
||||
}
|
||||
|
||||
|
@ -203,7 +203,7 @@ public class EigenSolver implements DecompositionSolver {
|
|||
invI[j] = invIJ;
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(invData, false);
|
||||
return MatrixUtils.createRealMatrix(invData);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -178,12 +178,14 @@ public class LUDecompositionImpl implements LUDecomposition {
|
|||
throws IllegalStateException {
|
||||
if ((cachedL == null) && !singular) {
|
||||
final int m = pivot.length;
|
||||
final double[][] lData = new double[m][m];
|
||||
cachedL = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
System.arraycopy(lu[i], 0, lData[i], 0, i);
|
||||
lData[i][i] = 1.0;
|
||||
final double[] luI = lu[i];
|
||||
for (int j = 0; j < i; ++j) {
|
||||
cachedL.setEntry(i, j, luI[j]);
|
||||
}
|
||||
cachedL.setEntry(i, i, 1.0);
|
||||
}
|
||||
cachedL = new RealMatrixImpl(lData, false);
|
||||
}
|
||||
return cachedL;
|
||||
}
|
||||
|
@ -193,11 +195,13 @@ public class LUDecompositionImpl implements LUDecomposition {
|
|||
throws IllegalStateException {
|
||||
if ((cachedU == null) && !singular) {
|
||||
final int m = pivot.length;
|
||||
final double[][] uData = new double[m][m];
|
||||
cachedU = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
System.arraycopy(lu[i], i, uData[i], i, m - i);
|
||||
final double[] luI = lu[i];
|
||||
for (int j = i; j < m; ++j) {
|
||||
cachedU.setEntry(i, j, luI[j]);
|
||||
}
|
||||
}
|
||||
cachedU = new RealMatrixImpl(uData, false);
|
||||
}
|
||||
return cachedU;
|
||||
}
|
||||
|
@ -207,11 +211,10 @@ public class LUDecompositionImpl implements LUDecomposition {
|
|||
throws IllegalStateException {
|
||||
if ((cachedP == null) && !singular) {
|
||||
final int m = pivot.length;
|
||||
final double[][] pData = new double[m][m];
|
||||
cachedP = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
pData[i][pivot[i]] = 1.0;
|
||||
cachedP.setEntry(i, pivot[i], 1.0);
|
||||
}
|
||||
cachedP = new RealMatrixImpl(pData, false);
|
||||
}
|
||||
return cachedP;
|
||||
}
|
||||
|
|
|
@ -201,7 +201,7 @@ public class LUSolver implements DecompositionSolver {
|
|||
}
|
||||
}
|
||||
|
||||
return new RealMatrixImpl(bp, false);
|
||||
return MatrixUtils.createRealMatrix(bp);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -153,20 +153,16 @@ public class QRDecompositionImpl implements QRDecomposition {
|
|||
// R is supposed to be m x n
|
||||
final int n = qrt.length;
|
||||
final int m = qrt[0].length;
|
||||
double[][] r = new double[m][n];
|
||||
cachedR = MatrixUtils.createRealMatrix(m, n);
|
||||
|
||||
// copy the diagonal from rDiag and the upper triangle of qr
|
||||
for (int row = Math.min(m, n) - 1; row >= 0; row--) {
|
||||
double[] rRow = r[row];
|
||||
rRow[row] = rDiag[row];
|
||||
cachedR.setEntry(row, row, rDiag[row]);
|
||||
for (int col = row + 1; col < n; col++) {
|
||||
rRow[col] = qrt[col][row];
|
||||
cachedR.setEntry(row, col, qrt[col][row]);
|
||||
}
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedR = new RealMatrixImpl(r, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
@ -192,7 +188,7 @@ public class QRDecompositionImpl implements QRDecomposition {
|
|||
// QT is supposed to be m x m
|
||||
final int n = qrt.length;
|
||||
final int m = qrt[0].length;
|
||||
double[][] qT = new double[m][m];
|
||||
cachedQT = MatrixUtils.createRealMatrix(m, m);
|
||||
|
||||
/*
|
||||
* Q = Q1 Q2 ... Q_m, so Q is formed by first constructing Q_m and then
|
||||
|
@ -200,31 +196,27 @@ public class QRDecompositionImpl implements QRDecomposition {
|
|||
* succession to the result
|
||||
*/
|
||||
for (int minor = m - 1; minor >= Math.min(m, n); minor--) {
|
||||
qT[minor][minor]=1;
|
||||
cachedQT.setEntry(minor, minor, 1.0);
|
||||
}
|
||||
|
||||
for (int minor = Math.min(m,n)-1; minor >= 0; minor--){
|
||||
for (int minor = Math.min(m, n)-1; minor >= 0; minor--){
|
||||
final double[] qrtMinor = qrt[minor];
|
||||
qT[minor][minor] = 1;
|
||||
cachedQT.setEntry(minor, minor, 1.0);
|
||||
if (qrtMinor[minor] != 0.0) {
|
||||
for (int col = minor; col < m; col++) {
|
||||
final double[] qTCol = qT[col];
|
||||
double alpha = 0;
|
||||
for (int row = minor; row < m; row++) {
|
||||
alpha -= qTCol[row] * qrtMinor[row];
|
||||
alpha -= cachedQT.getEntry(col, row) * qrtMinor[row];
|
||||
}
|
||||
alpha /= rDiag[minor] * qrtMinor[minor];
|
||||
|
||||
for (int row = minor; row < m; row++) {
|
||||
qTCol[row] -= alpha * qrtMinor[row];
|
||||
cachedQT.addToEntry(col, row, -alpha * qrtMinor[row]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedQT = new RealMatrixImpl(qT, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
@ -240,17 +232,13 @@ public class QRDecompositionImpl implements QRDecomposition {
|
|||
|
||||
final int n = qrt.length;
|
||||
final int m = qrt[0].length;
|
||||
double[][] hData = new double[m][n];
|
||||
cachedH = MatrixUtils.createRealMatrix(m, n);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
final double[] hDataI = hData[i];
|
||||
for (int j = 0; j < Math.min(i + 1, n); ++j) {
|
||||
hDataI[j] = qrt[j][i] / -rDiag[j];
|
||||
cachedH.setEntry(i, j, qrt[j][i] / -rDiag[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedH = new RealMatrixImpl(hData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
|
|
@ -136,7 +136,7 @@ public class QRSolver implements DecompositionSolver {
|
|||
}
|
||||
}
|
||||
|
||||
return new RealMatrixImpl(xData, false);
|
||||
return MatrixUtils.createRealMatrix(xData);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1112,38 +1112,40 @@ public class RealVectorImpl implements RealVector, Serializable {
|
|||
return outerProduct((RealVectorImpl) v);
|
||||
} catch (ClassCastException cce) {
|
||||
checkVectorDimensions(v);
|
||||
double[][] out = new double[data.length][data.length];
|
||||
final int m = data.length;
|
||||
final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
for (int j = 0; j < data.length; j++) {
|
||||
out[i][j] = data[i] * v.getEntry(j);
|
||||
out.setEntry(i, j, data[i] * v.getEntry(j));
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(out);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealMatrix outerProduct(double[] v)
|
||||
throws IllegalArgumentException {
|
||||
checkVectorDimensions(v.length);
|
||||
double[][] out = new double[data.length][data.length];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
for (int j = 0; j < data.length; j++) {
|
||||
out[i][j] = data[i] * v[j];
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the outer product.
|
||||
* @param v vector with which outer product should be computed
|
||||
* @return the square matrix outer product between instance and v
|
||||
* @exception IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public RealMatrixImpl outerProduct(RealVectorImpl v)
|
||||
public RealMatrix outerProduct(RealVectorImpl v)
|
||||
throws IllegalArgumentException {
|
||||
return (RealMatrixImpl) outerProduct(v.data);
|
||||
return outerProduct(v.data);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealMatrix outerProduct(double[] v)
|
||||
throws IllegalArgumentException {
|
||||
checkVectorDimensions(v.length);
|
||||
final int m = data.length;
|
||||
final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
for (int j = 0; j < data.length; j++) {
|
||||
out.setEntry(i, j, data[i] * v[j]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
|
|
|
@ -157,7 +157,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
|
|||
iData[i] = new double[n];
|
||||
}
|
||||
cachedU =
|
||||
transformer.getU().multiply(new RealMatrixImpl(iData, false));
|
||||
transformer.getU().multiply(MatrixUtils.createRealMatrix(iData));
|
||||
} else {
|
||||
// the tridiagonal matrix is B.Bt, where B is lower bidiagonal
|
||||
cachedU = transformer.getU().multiply(eigenDecomposition.getV());
|
||||
|
@ -189,14 +189,8 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
|
|||
|
||||
if (cachedS == null) {
|
||||
|
||||
final int p = singularValues.length;
|
||||
final double[][] sData = new double[p][p];
|
||||
for (int i = 0; i < p; ++i) {
|
||||
sData[i][i] = singularValues[i];
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedS = new RealMatrixImpl(sData, false);
|
||||
cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues);
|
||||
|
||||
}
|
||||
return cachedS;
|
||||
|
@ -243,7 +237,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
|
|||
iData[i] = new double[m];
|
||||
}
|
||||
cachedV =
|
||||
transformer.getV().multiply(new RealMatrixImpl(iData, false));
|
||||
transformer.getV().multiply(MatrixUtils.createRealMatrix(iData));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -115,17 +115,15 @@ public class SingularValueSolver implements DecompositionSolver {
|
|||
throw new IllegalArgumentException("Incorrect row dimension");
|
||||
}
|
||||
|
||||
final RealMatrixImpl w = (RealMatrixImpl) decomposition.getUT().multiply(b);
|
||||
final double[][] wData = w.getDataRef();
|
||||
final RealMatrix w = decomposition.getUT().multiply(b);
|
||||
for (int i = 0; i < singularValues.length; ++i) {
|
||||
final double si = singularValues[i];
|
||||
if (si == 0) {
|
||||
throw new SingularMatrixException();
|
||||
}
|
||||
final double inv = 1.0 / si;
|
||||
final double[] wi = wData[i];
|
||||
for (int j = 0; j < b.getColumnDimension(); ++j) {
|
||||
wi[j] *= inv;
|
||||
w.multiplyEntry(i, j, inv);
|
||||
}
|
||||
}
|
||||
return decomposition.getV().multiply(w);
|
||||
|
|
|
@ -175,6 +175,36 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
|||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void addToEntry(int row, int column, double increment)
|
||||
throws MatrixIndexException {
|
||||
checkRowIndex(row);
|
||||
checkColumnIndex(column);
|
||||
final int key = computeKey(row, column);
|
||||
final double value = entries.get(key) + increment;
|
||||
if (value == 0.0) {
|
||||
entries.remove(key);
|
||||
} else {
|
||||
entries.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void multiplyEntry(int row, int column, double factor)
|
||||
throws MatrixIndexException {
|
||||
checkRowIndex(row);
|
||||
checkColumnIndex(column);
|
||||
final int key = computeKey(row, column);
|
||||
final double value = entries.get(key) * factor;
|
||||
if (value == 0.0) {
|
||||
entries.remove(key);
|
||||
} else {
|
||||
entries.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the key to access a matrix element
|
||||
* @param row row index of the matrix element
|
||||
|
|
|
@ -106,38 +106,33 @@ class TriDiagonalTransformer implements Serializable {
|
|||
if (cachedQt == null) {
|
||||
|
||||
final int m = householderVectors.length;
|
||||
final double[][] qtData = new double[m][m];
|
||||
cachedQt = MatrixUtils.createRealMatrix(m, m);
|
||||
|
||||
// build up first part of the matrix by applying Householder transforms
|
||||
for (int k = m - 1; k >= 1; --k) {
|
||||
final double[] hK = householderVectors[k - 1];
|
||||
final double inv = 1.0 / (secondary[k - 1] * hK[k]);
|
||||
qtData[k][k] = 1;
|
||||
cachedQt.setEntry(k, k, 1);
|
||||
if (hK[k] != 0.0) {
|
||||
final double[] qtK = qtData[k];
|
||||
double beta = 1.0 / secondary[k - 1];
|
||||
qtK[k] = 1 + beta * hK[k];
|
||||
cachedQt.setEntry(k, k, 1 + beta * hK[k]);
|
||||
for (int i = k + 1; i < m; ++i) {
|
||||
qtK[i] = beta * hK[i];
|
||||
cachedQt.setEntry(k, i, beta * hK[i]);
|
||||
}
|
||||
for (int j = k + 1; j < m; ++j) {
|
||||
final double[] qtJ = qtData[j];
|
||||
beta = 0;
|
||||
for (int i = k + 1; i < m; ++i) {
|
||||
beta += qtJ[i] * hK[i];
|
||||
beta += cachedQt.getEntry(j, i) * hK[i];
|
||||
}
|
||||
beta *= inv;
|
||||
qtJ[k] = beta * hK[k];
|
||||
cachedQt.setEntry(j, k, beta * hK[k]);
|
||||
for (int i = k + 1; i < m; ++i) {
|
||||
qtJ[i] += beta * hK[i];
|
||||
cachedQt.addToEntry(j, i, beta * hK[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
qtData[0][0] = 1;
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedQt = new RealMatrixImpl(qtData, false);
|
||||
cachedQt.setEntry(0, 0, 1);
|
||||
|
||||
}
|
||||
|
||||
|
@ -155,21 +150,17 @@ class TriDiagonalTransformer implements Serializable {
|
|||
if (cachedT == null) {
|
||||
|
||||
final int m = main.length;
|
||||
double[][] tData = new double[m][m];
|
||||
cachedT = MatrixUtils.createRealMatrix(m, m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
double[] tDataI = tData[i];
|
||||
tDataI[i] = main[i];
|
||||
cachedT.setEntry(i, i, main[i]);
|
||||
if (i > 0) {
|
||||
tDataI[i - 1] = secondary[i - 1];
|
||||
cachedT.setEntry(i, i - 1, secondary[i - 1]);
|
||||
}
|
||||
if (i < main.length - 1) {
|
||||
tDataI[i + 1] = secondary[i];
|
||||
cachedT.setEntry(i, i + 1, secondary[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// cache the matrix for subsequent calls
|
||||
cachedT = new RealMatrixImpl(tData, false);
|
||||
|
||||
}
|
||||
|
||||
// return the cached matrix
|
||||
|
|
|
@ -18,8 +18,8 @@
|
|||
package org.apache.commons.math.random;
|
||||
|
||||
import org.apache.commons.math.DimensionMismatchException;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
|
||||
/**
|
||||
* A {@link RandomVectorGenerator} that generates vectors with with
|
||||
|
@ -251,9 +251,11 @@ implements RandomVectorGenerator {
|
|||
}
|
||||
|
||||
// build the root matrix
|
||||
root = new RealMatrixImpl(order, rank);
|
||||
root = MatrixUtils.createRealMatrix(order, rank);
|
||||
for (int i = 0; i < order; ++i) {
|
||||
System.arraycopy(b[i], 0, root.getDataRef()[index[i]], 0, rank);
|
||||
for (int j = 0; j < rank; ++j) {
|
||||
root.setEntry(index[i], j, b[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -286,7 +288,7 @@ implements RandomVectorGenerator {
|
|||
private double[] mean;
|
||||
|
||||
/** Permutated Cholesky root of the covariance matrix. */
|
||||
private RealMatrixImpl root;
|
||||
private RealMatrix root;
|
||||
|
||||
/** Rank of the covariance matrix. */
|
||||
private int rank;
|
||||
|
|
|
@ -20,8 +20,8 @@ import java.io.Serializable;
|
|||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.DimensionMismatchException;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
|
||||
/**
|
||||
* Returns the covariance matrix of the available vectors.
|
||||
|
@ -83,17 +83,16 @@ public class VectorialCovariance implements Serializable {
|
|||
public RealMatrix getResult() {
|
||||
|
||||
int dimension = sums.length;
|
||||
RealMatrixImpl result = new RealMatrixImpl(dimension, dimension);
|
||||
RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension);
|
||||
|
||||
if (n > 1) {
|
||||
double[][] resultData = result.getDataRef();
|
||||
double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
|
||||
int k = 0;
|
||||
for (int i = 0; i < dimension; ++i) {
|
||||
for (int j = 0; j <= i; ++j) {
|
||||
double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
|
||||
resultData[i][j] = e;
|
||||
resultData[j][i] = e;
|
||||
result.setEntry(i, j, e);
|
||||
result.setEntry(j, i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,10 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.regression;
|
||||
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
import org.apache.commons.math.linear.RealVector;
|
||||
import org.apache.commons.math.linear.RealVectorImpl;
|
||||
|
||||
/**
|
||||
* Abstract base class for implementations of MultipleLinearRegression.
|
||||
|
@ -31,7 +33,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
protected RealMatrix X;
|
||||
|
||||
/** Y sample data. */
|
||||
protected RealMatrix Y;
|
||||
protected RealVector Y;
|
||||
|
||||
/**
|
||||
* Loads model x and y sample data from a flat array of data, overriding any previous sample.
|
||||
|
@ -52,8 +54,8 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
x[i][j] = data[pointer++];
|
||||
}
|
||||
}
|
||||
this.X = new RealMatrixImpl(x);
|
||||
this.Y = new RealMatrixImpl(y);
|
||||
this.X = MatrixUtils.createRealMatrix(x);
|
||||
this.Y = new RealVectorImpl(y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +64,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
* @param y the [n,1] array representing the y sample
|
||||
*/
|
||||
protected void newYSampleData(double[] y) {
|
||||
this.Y = new RealMatrixImpl(y);
|
||||
this.Y = new RealVectorImpl(y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,7 +73,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
* @param x the [n,k] array representing the x sample
|
||||
*/
|
||||
protected void newXSampleData(double[][] x) {
|
||||
this.X = new RealMatrixImpl(x);
|
||||
this.X = MatrixUtils.createRealMatrix(x);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,17 +122,14 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
public double[] estimateRegressionParameters() {
|
||||
RealMatrix b = calculateBeta();
|
||||
return b.getColumn(0);
|
||||
return calculateBeta().getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double[] estimateResiduals() {
|
||||
RealMatrix b = calculateBeta();
|
||||
RealMatrix e = Y.subtract(X.multiply(b));
|
||||
return e.getColumn(0);
|
||||
return Y.subtract(X.operate(calculateBeta())).getData();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,7 +151,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
*
|
||||
* @return beta
|
||||
*/
|
||||
protected abstract RealMatrix calculateBeta();
|
||||
protected abstract RealVector calculateBeta();
|
||||
|
||||
/**
|
||||
* Calculates the beta variance of multiple linear regression in matrix
|
||||
|
@ -179,9 +178,8 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
*
|
||||
* @return The residuals [n,1] matrix
|
||||
*/
|
||||
protected RealMatrix calculateResiduals() {
|
||||
RealMatrix b = calculateBeta();
|
||||
return Y.subtract(X.multiply(b));
|
||||
protected RealVector calculateResiduals() {
|
||||
return Y.subtract(X.operate(calculateBeta()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,8 +18,9 @@ package org.apache.commons.math.stat.regression;
|
|||
|
||||
import org.apache.commons.math.linear.LUDecompositionImpl;
|
||||
import org.apache.commons.math.linear.LUSolver;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
import org.apache.commons.math.linear.RealVector;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -68,7 +69,7 @@ public class GLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @param omega the [n,n] array representing the covariance
|
||||
*/
|
||||
protected void newCovarianceData(double[][] omega){
|
||||
this.Omega = new RealMatrixImpl(omega);
|
||||
this.Omega = MatrixUtils.createRealMatrix(omega);
|
||||
this.OmegaInverse = null;
|
||||
}
|
||||
|
||||
|
@ -91,12 +92,12 @@ public class GLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* </pre>
|
||||
* @return beta
|
||||
*/
|
||||
protected RealMatrix calculateBeta() {
|
||||
protected RealVector calculateBeta() {
|
||||
RealMatrix OI = getOmegaInverse();
|
||||
RealMatrix XT = X.transpose();
|
||||
RealMatrix XTOIX = XT.multiply(OI).multiply(X);
|
||||
RealMatrix inverse = new LUSolver(new LUDecompositionImpl(XTOIX)).getInverse();
|
||||
return inverse.multiply(XT).multiply(OI).multiply(Y);
|
||||
return inverse.multiply(XT).multiply(OI).operate(Y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,9 +121,9 @@ public class GLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @return The Y variance
|
||||
*/
|
||||
protected double calculateYVariance() {
|
||||
RealMatrix u = calculateResiduals();
|
||||
RealMatrix sse = u.transpose().multiply(getOmegaInverse()).multiply(u);
|
||||
return sse.getTrace()/(X.getRowDimension()-X.getColumnDimension());
|
||||
final RealVector u = calculateResiduals();
|
||||
final double sse = u.dotProduct(getOmegaInverse().operate(u));
|
||||
return sse / (X.getRowDimension() - X.getColumnDimension());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,12 +16,14 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.regression;
|
||||
|
||||
import org.apache.commons.math.linear.DenseRealMatrix;
|
||||
import org.apache.commons.math.linear.LUDecompositionImpl;
|
||||
import org.apache.commons.math.linear.LUSolver;
|
||||
import org.apache.commons.math.linear.QRDecomposition;
|
||||
import org.apache.commons.math.linear.QRDecompositionImpl;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
import org.apache.commons.math.linear.RealVector;
|
||||
import org.apache.commons.math.linear.RealVectorImpl;
|
||||
|
||||
/**
|
||||
* <p>Implements ordinary least squares (OLS) to estimate the parameters of a
|
||||
|
@ -86,7 +88,7 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @param x the [n,k] array representing the x sample
|
||||
*/
|
||||
protected void newXSampleData(double[][] x) {
|
||||
this.X = new RealMatrixImpl(x);
|
||||
this.X = new DenseRealMatrix(x);
|
||||
qr = new QRDecompositionImpl(X);
|
||||
}
|
||||
|
||||
|
@ -95,9 +97,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
*
|
||||
* @return beta
|
||||
*/
|
||||
protected RealMatrix calculateBeta() {
|
||||
return solveUpperTriangular((RealMatrixImpl) qr.getR(),
|
||||
(RealMatrixImpl) qr.getQ().transpose().multiply(Y));
|
||||
protected RealVector calculateBeta() {
|
||||
return solveUpperTriangular(qr.getR(), qr.getQ().transpose().operate(Y));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,9 +122,9 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @return The Y variance
|
||||
*/
|
||||
protected double calculateYVariance() {
|
||||
RealMatrix u = calculateResiduals();
|
||||
RealMatrix sse = u.transpose().multiply(u);
|
||||
return sse.getTrace()/(X.getRowDimension()-X.getColumnDimension());
|
||||
final RealVector u = calculateResiduals();
|
||||
final double sse = u.dotProduct(u);
|
||||
return sse / (X.getRowDimension() - X.getColumnDimension());
|
||||
}
|
||||
|
||||
/** TODO: Find a home for the following methods in the linear package */
|
||||
|
@ -143,32 +144,25 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
*
|
||||
* @param coefficients upper-triangular coefficients matrix
|
||||
* @param constants column RHS constants matrix
|
||||
* @return solution matrix as a column matrix
|
||||
* @return solution matrix as a vector
|
||||
*
|
||||
*/
|
||||
private static RealMatrix solveUpperTriangular(RealMatrixImpl coefficients,
|
||||
RealMatrixImpl constants) {
|
||||
private static RealVector solveUpperTriangular(RealMatrix coefficients, RealVector constants) {
|
||||
if (!isUpperTriangular(coefficients, 1E-12)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Coefficients is not upper-triangular");
|
||||
}
|
||||
if (constants.getColumnDimension() != 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Constants not a column matrix.");
|
||||
}
|
||||
int length = coefficients.getColumnDimension();
|
||||
double[][] cons = constants.getDataRef();
|
||||
double[][] coef = coefficients.getDataRef();
|
||||
double x[] = new double[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
int index = length - 1 - i;
|
||||
double sum = 0;
|
||||
for (int j = index + 1; j < length; j++) {
|
||||
sum += coef[index][j] * x[j];
|
||||
sum += coefficients.getEntry(index, j) * x[j];
|
||||
}
|
||||
x[index] = (cons[index][0] - sum) / coef[index][index];
|
||||
x[index] = (constants.getEntry(index) - sum) / coefficients.getEntry(index, index);
|
||||
}
|
||||
return new RealMatrixImpl(x);
|
||||
return new RealVectorImpl(x);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -183,14 +177,13 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @return true if m is upper-triangular; false otherwise
|
||||
* @throws NullPointerException if m is null
|
||||
*/
|
||||
private static boolean isUpperTriangular(RealMatrixImpl m, double epsilon) {
|
||||
double[][] data = m.getDataRef();
|
||||
private static boolean isUpperTriangular(RealMatrix m, double epsilon) {
|
||||
int nCols = m.getColumnDimension();
|
||||
int nRows = m.getRowDimension();
|
||||
for (int r = 0; r < nRows; r++) {
|
||||
int bound = Math.min(r, nCols);
|
||||
for (int c = 0; c < bound; c++) {
|
||||
if (Math.abs(data[r][c]) > epsilon) {
|
||||
if (Math.abs(m.getEntry(r, c)) > epsilon) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,9 +40,9 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testDimensions() {
|
||||
checkdimensions(new RealMatrixImpl(testSquare, false));
|
||||
checkdimensions(new RealMatrixImpl(testNonSquare, false));
|
||||
checkdimensions(new RealMatrixImpl(testNonSquare, false).transpose());
|
||||
checkdimensions(MatrixUtils.createRealMatrix(testSquare));
|
||||
checkdimensions(MatrixUtils.createRealMatrix(testNonSquare));
|
||||
checkdimensions(MatrixUtils.createRealMatrix(testNonSquare).transpose());
|
||||
}
|
||||
|
||||
private void checkdimensions(RealMatrix matrix) {
|
||||
|
@ -59,9 +59,9 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testAEqualUSVt() {
|
||||
checkAEqualUSVt(new RealMatrixImpl(testSquare, false));
|
||||
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false));
|
||||
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false).transpose());
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
|
||||
}
|
||||
|
||||
private void checkAEqualUSVt(RealMatrix matrix) {
|
||||
|
@ -74,15 +74,15 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testUOrthogonal() {
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getU());
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getU());
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getU());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getU());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getU());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
|
||||
}
|
||||
|
||||
public void testVOrthogonal() {
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getV());
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getV());
|
||||
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getV());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getV());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getV());
|
||||
checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
|
||||
}
|
||||
|
||||
private void checkOrthogonal(RealMatrix m) {
|
||||
|
@ -92,9 +92,9 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testBBiDiagonal() {
|
||||
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getB());
|
||||
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getB());
|
||||
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getB());
|
||||
checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getB());
|
||||
checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getB());
|
||||
checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getB());
|
||||
}
|
||||
|
||||
private void checkBiDiagonal(RealMatrix m) {
|
||||
|
@ -117,17 +117,17 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
|
||||
public void testMatricesValues() {
|
||||
BiDiagonalTransformer transformer =
|
||||
new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false));
|
||||
new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare));
|
||||
final double s17 = Math.sqrt(17.0);
|
||||
RealMatrix uRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -8 / (5 * s17), 19 / (5 * s17) },
|
||||
{ -19 / (5 * s17), -8 / (5 * s17) }
|
||||
});
|
||||
RealMatrix bRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -3 * s17 / 5, 32 * s17 / 85 },
|
||||
{ 0.0, -5 * s17 / 17 }
|
||||
});
|
||||
RealMatrix vRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1.0, 0.0 },
|
||||
{ 0.0, -1.0 }
|
||||
});
|
||||
|
@ -148,9 +148,9 @@ public class BiDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testUpperOrLower() {
|
||||
assertTrue(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).isUpperBiDiagonal());
|
||||
assertTrue(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).isUpperBiDiagonal());
|
||||
assertFalse(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).isUpperBiDiagonal());
|
||||
assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).isUpperBiDiagonal());
|
||||
assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).isUpperBiDiagonal());
|
||||
assertFalse(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).isUpperBiDiagonal());
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
|
|
|
@ -43,19 +43,17 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
|
||||
public void testDimension1() {
|
||||
RealMatrix matrix =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
{ 1.5 }
|
||||
}, false);
|
||||
MatrixUtils.createRealMatrix(new double[][] { { 1.5 } });
|
||||
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
|
||||
assertEquals(1.5, ed.getEigenvalue(0), 1.0e-15);
|
||||
}
|
||||
|
||||
public void testDimension2() {
|
||||
RealMatrix matrix =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
{ 59.0, 12.0 },
|
||||
{ Double.NaN, 66.0 }
|
||||
}, false);
|
||||
MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 59.0, 12.0 },
|
||||
{ Double.NaN, 66.0 }
|
||||
});
|
||||
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
|
||||
assertEquals(75.0, ed.getEigenvalue(0), 1.0e-15);
|
||||
assertEquals(50.0, ed.getEigenvalue(1), 1.0e-15);
|
||||
|
@ -63,11 +61,11 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
|
||||
public void testDimension3() {
|
||||
RealMatrix matrix =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 39632.0, -4824.0, -16560.0 },
|
||||
{ Double.NaN, 8693.0, 7920.0 },
|
||||
{ Double.NaN, Double.NaN, 17300.0 }
|
||||
}, false);
|
||||
});
|
||||
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
|
||||
assertEquals(50000.0, ed.getEigenvalue(0), 3.0e-11);
|
||||
assertEquals(12500.0, ed.getEigenvalue(1), 3.0e-11);
|
||||
|
@ -76,12 +74,12 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
|
||||
public void testDimension4WithSplit() {
|
||||
RealMatrix matrix =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 0.784, -0.288, 0.000, 0.000 },
|
||||
{ Double.NaN, 0.616, 0.000, 0.000 },
|
||||
{ Double.NaN, Double.NaN, 0.164, -0.048 },
|
||||
{ Double.NaN, Double.NaN, Double.NaN, 0.136 }
|
||||
}, false);
|
||||
});
|
||||
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
|
||||
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
|
||||
assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15);
|
||||
|
@ -91,12 +89,12 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
|
||||
public void testDimension4WithoutSplit() {
|
||||
RealMatrix matrix =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 0.5608, -0.2016, 0.1152, -0.2976 },
|
||||
{ -0.2016, 0.4432, -0.2304, 0.1152 },
|
||||
{ 0.1152, -0.2304, 0.3088, -0.1344 },
|
||||
{ -0.2976, 0.1152, -0.1344, 0.3872 }
|
||||
}, false);
|
||||
});
|
||||
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
|
||||
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
|
||||
assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15);
|
||||
|
@ -213,7 +211,7 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
* Matrix with eigenvalues {8, -1, -1}
|
||||
*/
|
||||
public void testRepeatedEigenvalue() {
|
||||
RealMatrix repeated = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix repeated = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{3, 2, 4},
|
||||
{2, 0, 2},
|
||||
{4, 2, 3}
|
||||
|
@ -227,7 +225,7 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
* Matrix with eigenvalues {2, 0, 12}
|
||||
*/
|
||||
public void testDistinctEigenvalues() {
|
||||
RealMatrix distinct = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix distinct = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{3, 1, -4},
|
||||
{1, 3, -4},
|
||||
{-4, -4, 8}
|
||||
|
@ -368,7 +366,7 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
} while (norm2 * size < 0.01);
|
||||
}
|
||||
|
||||
return new RealMatrixImpl(data, false);
|
||||
return MatrixUtils.createRealMatrix(data);
|
||||
|
||||
}
|
||||
|
||||
|
@ -378,7 +376,7 @@ public class EigenDecompositionImplTest extends TestCase {
|
|||
for (int i = 0; i < Math.min(rows, columns); ++i) {
|
||||
dData[i][i] = diagonal[i];
|
||||
}
|
||||
return new RealMatrixImpl(dData, false);
|
||||
return MatrixUtils.createRealMatrix(dData);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ public class EigenSolverTest extends TestCase {
|
|||
/** test solve dimension errors */
|
||||
public void testSolveDimensionErrors() {
|
||||
EigenSolver es = new EigenSolver(new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN));
|
||||
RealMatrix b = new RealMatrixImpl(new double[2][2]);
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
|
||||
try {
|
||||
es.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -102,7 +102,7 @@ public class EigenSolverTest extends TestCase {
|
|||
|
||||
/** test solve */
|
||||
public void testSolve() {
|
||||
RealMatrix m = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 91, 5, 29, 32, 40, 14 },
|
||||
{ 5, 34, -1, 0, 2, -1 },
|
||||
{ 29, -1, 12, 9, 21, 8 },
|
||||
|
@ -112,7 +112,7 @@ public class EigenSolverTest extends TestCase {
|
|||
});
|
||||
EigenSolver es = new EigenSolver(new EigenDecompositionImpl(m, MathUtils.SAFE_MIN));
|
||||
assertEquals(184041, es.getDeterminant(), 2.0e-8);
|
||||
RealMatrix b = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1561, 269, 188 },
|
||||
{ 69, -21, 70 },
|
||||
{ 739, 108, 63 },
|
||||
|
@ -120,7 +120,7 @@ public class EigenSolverTest extends TestCase {
|
|||
{ 1624, 194, 107 },
|
||||
{ 796, 69, 36 }
|
||||
});
|
||||
RealMatrix xRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1, 2, 1 },
|
||||
{ 2, -1, 2 },
|
||||
{ 4, 2, 3 },
|
||||
|
|
|
@ -66,7 +66,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test dimensions */
|
||||
public void testDimensions() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
|
||||
LUDecomposition LU = new LUDecompositionImpl(matrix);
|
||||
assertEquals(testData.length, LU.getL().getRowDimension());
|
||||
assertEquals(testData.length, LU.getL().getColumnDimension());
|
||||
|
@ -80,7 +80,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
/** test non-square matrix */
|
||||
public void testNonSquare() {
|
||||
try {
|
||||
new LUDecompositionImpl(new RealMatrixImpl(new double[3][2], false));
|
||||
new LUDecompositionImpl(MatrixUtils.createRealMatrix(new double[3][2]));
|
||||
} catch (InvalidMatrixException ime) {
|
||||
// expected behavior
|
||||
} catch (Exception e) {
|
||||
|
@ -90,7 +90,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test PA = LU */
|
||||
public void testPAEqualLU() {
|
||||
RealMatrix matrix = new RealMatrixImpl(testData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
|
||||
LUDecomposition lu = new LUDecompositionImpl(matrix);
|
||||
RealMatrix l = lu.getL();
|
||||
RealMatrix u = lu.getU();
|
||||
|
@ -98,7 +98,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
double norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
|
||||
assertEquals(0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testDataMinus, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testDataMinus);
|
||||
lu = new LUDecompositionImpl(matrix);
|
||||
l = lu.getL();
|
||||
u = lu.getU();
|
||||
|
@ -114,14 +114,14 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
|
||||
assertEquals(0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(singular, false);
|
||||
matrix = MatrixUtils.createRealMatrix(singular);
|
||||
lu = new LUDecompositionImpl(matrix);
|
||||
assertTrue(lu.isSingular());
|
||||
assertNull(lu.getL());
|
||||
assertNull(lu.getU());
|
||||
assertNull(lu.getP());
|
||||
|
||||
matrix = new RealMatrixImpl(bigSingular, false);
|
||||
matrix = MatrixUtils.createRealMatrix(bigSingular);
|
||||
lu = new LUDecompositionImpl(matrix);
|
||||
assertTrue(lu.isSingular());
|
||||
assertNull(lu.getL());
|
||||
|
@ -132,7 +132,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that L is lower triangular with unit diagonal */
|
||||
public void testLLowerTriangular() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
|
||||
RealMatrix l = new LUDecompositionImpl(matrix).getL();
|
||||
for (int i = 0; i < l.getRowDimension(); i++) {
|
||||
assertEquals(l.getEntry(i, i), 1, entryTolerance);
|
||||
|
@ -144,7 +144,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that U is upper triangular */
|
||||
public void testUUpperTriangular() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
|
||||
RealMatrix u = new LUDecompositionImpl(matrix).getU();
|
||||
for (int i = 0; i < u.getRowDimension(); i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
|
@ -155,7 +155,7 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that P is a permutation matrix */
|
||||
public void testPPermutation() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
|
||||
RealMatrix p = new LUDecompositionImpl(matrix).getP();
|
||||
|
||||
RealMatrix ppT = p.multiply(p.transpose());
|
||||
|
@ -206,29 +206,29 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
/** test singular */
|
||||
public void testSingular() {
|
||||
LUDecomposition lu =
|
||||
new LUDecompositionImpl(new RealMatrixImpl(testData, false));
|
||||
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
|
||||
assertFalse(lu.isSingular());
|
||||
lu = new LUDecompositionImpl(new RealMatrixImpl(singular, false));
|
||||
lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular));
|
||||
assertTrue(lu.isSingular());
|
||||
lu = new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false));
|
||||
lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular));
|
||||
assertTrue(lu.isSingular());
|
||||
}
|
||||
|
||||
/** test matrices values */
|
||||
public void testMatricesValues1() {
|
||||
LUDecomposition lu =
|
||||
new LUDecompositionImpl(new RealMatrixImpl(testData, false));
|
||||
RealMatrix lRef = new RealMatrixImpl(new double[][] {
|
||||
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
|
||||
RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1.0, 0.0, 0.0 },
|
||||
{ 0.5, 1.0, 0.0 },
|
||||
{ 0.5, 0.2, 1.0 }
|
||||
});
|
||||
RealMatrix uRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 2.0, 5.0, 3.0 },
|
||||
{ 0.0, -2.5, 6.5 },
|
||||
{ 0.0, 0.0, 0.2 }
|
||||
});
|
||||
RealMatrix pRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix pRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 0.0, 1.0, 0.0 },
|
||||
{ 0.0, 0.0, 1.0 },
|
||||
{ 1.0, 0.0, 0.0 }
|
||||
|
@ -257,18 +257,18 @@ public class LUDecompositionImplTest extends TestCase {
|
|||
/** test matrices values */
|
||||
public void testMatricesValues2() {
|
||||
LUDecomposition lu =
|
||||
new LUDecompositionImpl(new RealMatrixImpl(luData, false));
|
||||
RealMatrix lRef = new RealMatrixImpl(new double[][] {
|
||||
new LUDecompositionImpl(MatrixUtils.createRealMatrix(luData));
|
||||
RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1.0, 0.0, 0.0 },
|
||||
{ 0.0, 1.0, 0.0 },
|
||||
{ 1.0 / 3.0, 0.0, 1.0 }
|
||||
});
|
||||
RealMatrix uRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 6.0, 9.0, 8.0 },
|
||||
{ 0.0, 5.0, 7.0 },
|
||||
{ 0.0, 0.0, 1.0 / 3.0 }
|
||||
});
|
||||
RealMatrix pRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix pRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 0.0, 0.0, 1.0 },
|
||||
{ 0.0, 1.0, 0.0 },
|
||||
{ 1.0, 0.0, 0.0 }
|
||||
|
|
|
@ -57,11 +57,11 @@ public class LUSolverTest extends TestCase {
|
|||
|
||||
/** test threshold impact */
|
||||
public void testThreshold() {
|
||||
final RealMatrix matrix = new RealMatrixImpl(new double[][] {
|
||||
final RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1.0, 2.0, 3.0},
|
||||
{ 2.0, 5.0, 3.0},
|
||||
{ 4.000001, 9.0, 9.0}
|
||||
}, false);
|
||||
});
|
||||
assertFalse(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-5)).isNonSingular());
|
||||
assertTrue(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-10)).isNonSingular());
|
||||
}
|
||||
|
@ -69,19 +69,19 @@ public class LUSolverTest extends TestCase {
|
|||
/** test singular */
|
||||
public void testSingular() {
|
||||
LUSolver lu =
|
||||
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
|
||||
new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
|
||||
assertTrue(lu.isNonSingular());
|
||||
lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false)));
|
||||
lu = new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)));
|
||||
assertFalse(lu.isNonSingular());
|
||||
lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false)));
|
||||
lu = new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular)));
|
||||
assertFalse(lu.isNonSingular());
|
||||
}
|
||||
|
||||
/** test solve dimension errors */
|
||||
public void testSolveDimensionErrors() {
|
||||
LUSolver solver =
|
||||
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[2][2]);
|
||||
new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -111,8 +111,8 @@ public class LUSolverTest extends TestCase {
|
|||
/** test solve singularity errors */
|
||||
public void testSolveSingularityErrors() {
|
||||
LUSolver solver =
|
||||
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[2][2]);
|
||||
new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -150,11 +150,11 @@ public class LUSolverTest extends TestCase {
|
|||
/** test solve */
|
||||
public void testSolve() {
|
||||
LUSolver solver =
|
||||
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[][] {
|
||||
new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1, 0 }, { 2, -5 }, { 3, 1 }
|
||||
});
|
||||
RealMatrix xRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 19, -71 }, { -6, 22 }, { -2, 9 }
|
||||
});
|
||||
|
||||
|
@ -188,10 +188,10 @@ public class LUSolverTest extends TestCase {
|
|||
|
||||
/** test determinant */
|
||||
public void testDeterminant() {
|
||||
assertEquals( -1, getDeterminant(new RealMatrixImpl(testData, false)), 1.0e-15);
|
||||
assertEquals(-10, getDeterminant(new RealMatrixImpl(luData, false)), 1.0e-14);
|
||||
assertEquals( 0, getDeterminant(new RealMatrixImpl(singular, false)), 1.0e-17);
|
||||
assertEquals( 0, getDeterminant(new RealMatrixImpl(bigSingular, false)), 1.0e-10);
|
||||
assertEquals( -1, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
|
||||
assertEquals(-10, getDeterminant(MatrixUtils.createRealMatrix(luData)), 1.0e-14);
|
||||
assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(singular)), 1.0e-17);
|
||||
assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(bigSingular)), 1.0e-10);
|
||||
}
|
||||
|
||||
private double getDeterminant(RealMatrix m) {
|
||||
|
|
|
@ -59,21 +59,21 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test dimensions */
|
||||
public void testDimensions() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
|
||||
QRDecomposition qr = new QRDecompositionImpl(matrix);
|
||||
assertEquals("3x3 Q size", qr.getQ().getRowDimension(), 3);
|
||||
assertEquals("3x3 Q size", qr.getQ().getColumnDimension(), 3);
|
||||
assertEquals("3x3 R size", qr.getR().getRowDimension(), 3);
|
||||
assertEquals("3x3 R size", qr.getR().getColumnDimension(), 3);
|
||||
|
||||
matrix = new RealMatrixImpl(testData4x3, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData4x3);
|
||||
qr = new QRDecompositionImpl(matrix);
|
||||
assertEquals("4x3 Q size", qr.getQ().getRowDimension(), 4);
|
||||
assertEquals("4x3 Q size", qr.getQ().getColumnDimension(), 4);
|
||||
assertEquals("4x3 R size", qr.getR().getRowDimension(), 4);
|
||||
assertEquals("4x3 R size", qr.getR().getColumnDimension(), 3);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x4, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x4);
|
||||
qr = new QRDecompositionImpl(matrix);
|
||||
assertEquals("3x4 Q size", qr.getQ().getRowDimension(), 3);
|
||||
assertEquals("3x4 Q size", qr.getQ().getColumnDimension(), 3);
|
||||
|
@ -83,24 +83,24 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test A = QR */
|
||||
public void testAEqualQR() {
|
||||
RealMatrix A = new RealMatrixImpl(testData3x3NonSingular, false);
|
||||
RealMatrix A = MatrixUtils.createRealMatrix(testData3x3NonSingular);
|
||||
QRDecomposition qr = new QRDecompositionImpl(A);
|
||||
RealMatrix Q = qr.getQ();
|
||||
RealMatrix R = qr.getR();
|
||||
double norm = Q.multiply(R).subtract(A).getNorm();
|
||||
assertEquals("3x3 nonsingular A = QR", 0, norm, normTolerance);
|
||||
|
||||
RealMatrix matrix = new RealMatrixImpl(testData3x3Singular, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
|
||||
qr = new QRDecompositionImpl(matrix);
|
||||
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
|
||||
assertEquals("3x3 singular A = QR", 0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x4, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x4);
|
||||
qr = new QRDecompositionImpl(matrix);
|
||||
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
|
||||
assertEquals("3x4 A = QR", 0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData4x3, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData4x3);
|
||||
qr = new QRDecompositionImpl(matrix);
|
||||
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
|
||||
assertEquals("4x3 A = QR", 0, norm, normTolerance);
|
||||
|
@ -108,28 +108,28 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test the orthogonality of Q */
|
||||
public void testQOrthogonal() {
|
||||
RealMatrix matrix = new RealMatrixImpl(testData3x3NonSingular, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
|
||||
RealMatrix q = new QRDecompositionImpl(matrix).getQ();
|
||||
RealMatrix qT = new QRDecompositionImpl(matrix).getQT();
|
||||
RealMatrix eye = MatrixUtils.createRealIdentityMatrix(3);
|
||||
double norm = qT.multiply(q).subtract(eye).getNorm();
|
||||
assertEquals("3x3 nonsingular Q'Q = I", 0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x3Singular, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
|
||||
q = new QRDecompositionImpl(matrix).getQ();
|
||||
qT = new QRDecompositionImpl(matrix).getQT();
|
||||
eye = MatrixUtils.createRealIdentityMatrix(3);
|
||||
norm = qT.multiply(q).subtract(eye).getNorm();
|
||||
assertEquals("3x3 singular Q'Q = I", 0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x4, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x4);
|
||||
q = new QRDecompositionImpl(matrix).getQ();
|
||||
qT = new QRDecompositionImpl(matrix).getQT();
|
||||
eye = MatrixUtils.createRealIdentityMatrix(3);
|
||||
norm = qT.multiply(q).subtract(eye).getNorm();
|
||||
assertEquals("3x4 Q'Q = I", 0, norm, normTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData4x3, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData4x3);
|
||||
q = new QRDecompositionImpl(matrix).getQ();
|
||||
qT = new QRDecompositionImpl(matrix).getQT();
|
||||
eye = MatrixUtils.createRealIdentityMatrix(4);
|
||||
|
@ -139,28 +139,28 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that R is upper triangular */
|
||||
public void testRUpperTriangular() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
|
||||
RealMatrix R = new QRDecompositionImpl(matrix).getR();
|
||||
for (int i = 0; i < R.getRowDimension(); i++)
|
||||
for (int j = 0; j < i; j++)
|
||||
assertEquals("R lower triangle", R.getEntry(i, j), 0,
|
||||
entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x3Singular, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
|
||||
R = new QRDecompositionImpl(matrix).getR();
|
||||
for (int i = 0; i < R.getRowDimension(); i++)
|
||||
for (int j = 0; j < i; j++)
|
||||
assertEquals("R lower triangle", R.getEntry(i, j), 0,
|
||||
entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x4, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x4);
|
||||
R = new QRDecompositionImpl(matrix).getR();
|
||||
for (int i = 0; i < R.getRowDimension(); i++)
|
||||
for (int j = 0; j < i; j++)
|
||||
assertEquals("R lower triangle", R.getEntry(i, j), 0,
|
||||
entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData4x3, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData4x3);
|
||||
R = new QRDecompositionImpl(matrix).getR();
|
||||
for (int i = 0; i < R.getRowDimension(); i++)
|
||||
for (int j = 0; j < i; j++)
|
||||
|
@ -170,25 +170,25 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that H is trapezoidal */
|
||||
public void testHTrapezoidal() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
|
||||
RealMatrix H = new QRDecompositionImpl(matrix).getH();
|
||||
for (int i = 0; i < H.getRowDimension(); i++)
|
||||
for (int j = i + 1; j < H.getColumnDimension(); j++)
|
||||
assertEquals(H.getEntry(i, j), 0, entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x3Singular, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
|
||||
H = new QRDecompositionImpl(matrix).getH();
|
||||
for (int i = 0; i < H.getRowDimension(); i++)
|
||||
for (int j = i + 1; j < H.getColumnDimension(); j++)
|
||||
assertEquals(H.getEntry(i, j), 0, entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData3x4, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData3x4);
|
||||
H = new QRDecompositionImpl(matrix).getH();
|
||||
for (int i = 0; i < H.getRowDimension(); i++)
|
||||
for (int j = i + 1; j < H.getColumnDimension(); j++)
|
||||
assertEquals(H.getEntry(i, j), 0, entryTolerance);
|
||||
|
||||
matrix = new RealMatrixImpl(testData4x3, false);
|
||||
matrix = MatrixUtils.createRealMatrix(testData4x3);
|
||||
H = new QRDecompositionImpl(matrix).getH();
|
||||
for (int i = 0; i < H.getRowDimension(); i++)
|
||||
for (int j = i + 1; j < H.getColumnDimension(); j++)
|
||||
|
@ -199,18 +199,18 @@ public class QRDecompositionImplTest extends TestCase {
|
|||
/** test matrices values */
|
||||
public void testMatricesValues() {
|
||||
QRDecomposition qr =
|
||||
new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false));
|
||||
RealMatrix qRef = new RealMatrixImpl(new double[][] {
|
||||
new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular));
|
||||
RealMatrix qRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -12.0 / 14.0, 69.0 / 175.0, -58.0 / 175.0 },
|
||||
{ -6.0 / 14.0, -158.0 / 175.0, 6.0 / 175.0 },
|
||||
{ 4.0 / 14.0, -30.0 / 175.0, -165.0 / 175.0 }
|
||||
});
|
||||
RealMatrix rRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix rRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -14.0, -21.0, 14.0 },
|
||||
{ 0.0, -175.0, 70.0 },
|
||||
{ 0.0, 0.0, 35.0 }
|
||||
});
|
||||
RealMatrix hRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix hRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 26.0 / 14.0, 0.0, 0.0 },
|
||||
{ 6.0 / 14.0, 648.0 / 325.0, 0.0 },
|
||||
{ -4.0 / 14.0, 36.0 / 325.0, 2.0 }
|
||||
|
|
|
@ -56,16 +56,16 @@ public class QRSolverTest extends TestCase {
|
|||
/** test rank */
|
||||
public void testRank() {
|
||||
QRSolver solver =
|
||||
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
|
||||
new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
|
||||
assertTrue(solver.isNonSingular());
|
||||
|
||||
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)));
|
||||
solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)));
|
||||
assertFalse(solver.isNonSingular());
|
||||
|
||||
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x4, false)));
|
||||
solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x4)));
|
||||
assertTrue(solver.isNonSingular());
|
||||
|
||||
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData4x3, false)));
|
||||
solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData4x3)));
|
||||
assertTrue(solver.isNonSingular());
|
||||
|
||||
}
|
||||
|
@ -73,8 +73,8 @@ public class QRSolverTest extends TestCase {
|
|||
/** test solve dimension errors */
|
||||
public void testSolveDimensionErrors() {
|
||||
QRSolver solver =
|
||||
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[2][2]);
|
||||
new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -104,8 +104,8 @@ public class QRSolverTest extends TestCase {
|
|||
/** test solve rank errors */
|
||||
public void testSolveRankErrors() {
|
||||
QRSolver solver =
|
||||
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[3][2]);
|
||||
new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -135,11 +135,11 @@ public class QRSolverTest extends TestCase {
|
|||
/** test solve */
|
||||
public void testSolve() {
|
||||
QRSolver solver =
|
||||
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[][] {
|
||||
new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -102, 12250 }, { 544, 24500 }, { 167, -36750 }
|
||||
});
|
||||
RealMatrix xRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1, 2515 }, { 2, 422 }, { -3, 898 }
|
||||
});
|
||||
|
||||
|
|
|
@ -1060,7 +1060,7 @@ public class RealVectorImplTest extends TestCase {
|
|||
double dot_2 = v1.dotProduct(v2_t);
|
||||
assertEquals("compare val ",32d, dot_2);
|
||||
|
||||
RealMatrixImpl m_outerProduct = v1.outerProduct(v2);
|
||||
RealMatrix m_outerProduct = v1.outerProduct(v2);
|
||||
assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
|
||||
|
||||
RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);
|
||||
|
|
|
@ -79,7 +79,7 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test dimensions */
|
||||
public void testDimensions() {
|
||||
RealMatrixImpl matrix = new RealMatrixImpl(testSquare, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(testSquare);
|
||||
final int m = matrix.getRowDimension();
|
||||
final int n = matrix.getColumnDimension();
|
||||
SingularValueDecomposition svd = new SingularValueDecompositionImpl(matrix);
|
||||
|
@ -94,9 +94,9 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test A = USVt */
|
||||
public void testAEqualUSVt() {
|
||||
checkAEqualUSVt(new RealMatrixImpl(testSquare, false));
|
||||
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false));
|
||||
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false).transpose());
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
|
||||
checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
|
||||
}
|
||||
|
||||
public void checkAEqualUSVt(final RealMatrix matrix) {
|
||||
|
@ -111,16 +111,16 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
|
||||
/** test that U is orthogonal */
|
||||
public void testUOrthogonal() {
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)).getU());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false)).getU());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false).transpose()).getU());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getU());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare)).getU());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
|
||||
}
|
||||
|
||||
/** test that V is orthogonal */
|
||||
public void testVOrthogonal() {
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)).getV());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false)).getV());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false).transpose()).getV());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getV());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare)).getV());
|
||||
checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
|
||||
}
|
||||
|
||||
public void checkOrthogonal(final RealMatrix m) {
|
||||
|
@ -132,16 +132,16 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
/** test matrices values */
|
||||
public void testMatricesValues1() {
|
||||
SingularValueDecomposition svd =
|
||||
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false));
|
||||
RealMatrix uRef = new RealMatrixImpl(new double[][] {
|
||||
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
|
||||
RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 3.0 / 5.0, -4.0 / 5.0 },
|
||||
{ 4.0 / 5.0, 3.0 / 5.0 }
|
||||
});
|
||||
RealMatrix sRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 3.0, 0.0 },
|
||||
{ 0.0, 1.0 }
|
||||
});
|
||||
RealMatrix vRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 4.0 / 5.0, 3.0 / 5.0 },
|
||||
{ 3.0 / 5.0, -4.0 / 5.0 }
|
||||
});
|
||||
|
@ -164,18 +164,18 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
/** test matrices values */
|
||||
public void testMatricesValues2() {
|
||||
|
||||
RealMatrix uRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 0.0 / 5.0, 3.0 / 5.0, 0.0 / 5.0 },
|
||||
{ -4.0 / 5.0, 0.0 / 5.0, -3.0 / 5.0 },
|
||||
{ 0.0 / 5.0, 4.0 / 5.0, 0.0 / 5.0 },
|
||||
{ -3.0 / 5.0, 0.0 / 5.0, 4.0 / 5.0 }
|
||||
});
|
||||
RealMatrix sRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 4.0, 0.0, 0.0 },
|
||||
{ 0.0, 3.0, 0.0 },
|
||||
{ 0.0, 0.0, 2.0 }
|
||||
});
|
||||
RealMatrix vRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 80.0 / 125.0, -60.0 / 125.0, 75.0 / 125.0 },
|
||||
{ 24.0 / 125.0, 107.0 / 125.0, 60.0 / 125.0 },
|
||||
{ -93.0 / 125.0, -24.0 / 125.0, 80.0 / 125.0 }
|
||||
|
@ -183,7 +183,7 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
|
||||
// check values against known references
|
||||
SingularValueDecomposition svd =
|
||||
new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false));
|
||||
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare));
|
||||
RealMatrix u = svd.getU();
|
||||
assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
|
||||
RealMatrix s = svd.getS();
|
||||
|
@ -201,7 +201,7 @@ public class SingularValueDecompositionImplTest extends TestCase {
|
|||
/** test condition number */
|
||||
public void testConditionNumber() {
|
||||
SingularValueDecompositionImpl svd =
|
||||
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false));
|
||||
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
|
||||
assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,8 +43,8 @@ public class SingularValueSolverTest extends TestCase {
|
|||
/** test solve dimension errors */
|
||||
public void testSolveDimensionErrors() {
|
||||
SingularValueSolver solver =
|
||||
new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[3][2]);
|
||||
new SingularValueSolver(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -74,12 +74,12 @@ public class SingularValueSolverTest extends TestCase {
|
|||
/** test solve singularity errors */
|
||||
public void testSolveSingularityErrors() {
|
||||
RealMatrix m =
|
||||
new RealMatrixImpl(new double[][] {
|
||||
MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1.0, 0.0 },
|
||||
{ 0.0, 0.0 }
|
||||
}, false);
|
||||
});
|
||||
SingularValueSolver solver = new SingularValueSolver(new SingularValueDecompositionImpl(m));
|
||||
RealMatrix b = new RealMatrixImpl(new double[2][2]);
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
|
||||
try {
|
||||
solver.solve(b);
|
||||
fail("an exception should have been thrown");
|
||||
|
@ -117,11 +117,11 @@ public class SingularValueSolverTest extends TestCase {
|
|||
/** test solve */
|
||||
public void testSolve() {
|
||||
SingularValueSolver solver =
|
||||
new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)));
|
||||
RealMatrix b = new RealMatrixImpl(new double[][] {
|
||||
new SingularValueSolver(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)));
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ 1, 2, 3 }, { 0, -5, 1 }
|
||||
});
|
||||
RealMatrix xRef = new RealMatrixImpl(new double[][] {
|
||||
RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
|
||||
{ -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
|
||||
{ 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.0 }
|
||||
});
|
||||
|
@ -157,7 +157,7 @@ public class SingularValueSolverTest extends TestCase {
|
|||
/** test condition number */
|
||||
public void testConditionNumber() {
|
||||
SingularValueDecompositionImpl svd =
|
||||
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false));
|
||||
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
|
||||
assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
|
||||
public void testNonSquare() {
|
||||
try {
|
||||
new TriDiagonalTransformer(new RealMatrixImpl(new double[3][2], false));
|
||||
new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
|
||||
fail("an exception should have been thrown");
|
||||
} catch (InvalidMatrixException ime) {
|
||||
// expected behavior
|
||||
|
@ -55,8 +55,8 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testAEqualQTQt() {
|
||||
checkAEqualQTQt(new RealMatrixImpl(testSquare5, false));
|
||||
checkAEqualQTQt(new RealMatrixImpl(testSquare3, false));
|
||||
checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare5));
|
||||
checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare3));
|
||||
}
|
||||
|
||||
private void checkAEqualQTQt(RealMatrix matrix) {
|
||||
|
@ -79,23 +79,23 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
modifiedData[i] = data[i].clone();
|
||||
Arrays.fill(modifiedData[i], 0, i, Double.NaN);
|
||||
}
|
||||
RealMatrix matrix = new RealMatrixImpl(modifiedData, false);
|
||||
RealMatrix matrix = MatrixUtils.createRealMatrix(modifiedData);
|
||||
TriDiagonalTransformer transformer = new TriDiagonalTransformer(matrix);
|
||||
RealMatrix q = transformer.getQ();
|
||||
RealMatrix qT = transformer.getQT();
|
||||
RealMatrix t = transformer.getT();
|
||||
double norm = q.multiply(t).multiply(qT).subtract(new RealMatrixImpl(data, false)).getNorm();
|
||||
double norm = q.multiply(t).multiply(qT).subtract(MatrixUtils.createRealMatrix(data)).getNorm();
|
||||
assertEquals(0, norm, 4.0e-15);
|
||||
}
|
||||
|
||||
public void testQOrthogonal() {
|
||||
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getQ());
|
||||
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getQ());
|
||||
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQ());
|
||||
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQ());
|
||||
}
|
||||
|
||||
public void testQTOrthogonal() {
|
||||
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getQT());
|
||||
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getQT());
|
||||
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQT());
|
||||
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQT());
|
||||
}
|
||||
|
||||
private void checkOrthogonal(RealMatrix m) {
|
||||
|
@ -105,8 +105,8 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testTTriDiagonal() {
|
||||
checkTriDiagonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getT());
|
||||
checkTriDiagonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getT());
|
||||
checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT());
|
||||
checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT());
|
||||
}
|
||||
|
||||
private void checkTriDiagonal(RealMatrix m) {
|
||||
|
@ -149,11 +149,11 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
double[] mainDiagnonal,
|
||||
double[] secondaryDiagonal) {
|
||||
TriDiagonalTransformer transformer =
|
||||
new TriDiagonalTransformer(new RealMatrixImpl(matrix, false));
|
||||
new TriDiagonalTransformer(MatrixUtils.createRealMatrix(matrix));
|
||||
|
||||
// check values against known references
|
||||
RealMatrix q = transformer.getQ();
|
||||
assertEquals(0, q.subtract(new RealMatrixImpl(qRef, false)).getNorm(), 1.0e-14);
|
||||
assertEquals(0, q.subtract(MatrixUtils.createRealMatrix(qRef)).getNorm(), 1.0e-14);
|
||||
|
||||
RealMatrix t = transformer.getT();
|
||||
double[][] tData = new double[mainDiagnonal.length][mainDiagnonal.length];
|
||||
|
@ -166,7 +166,7 @@ public class TriDiagonalTransformerTest extends TestCase {
|
|||
tData[i][i + 1] = secondaryDiagonal[i];
|
||||
}
|
||||
}
|
||||
assertEquals(0, t.subtract(new RealMatrixImpl(tData, false)).getNorm(), 1.0e-14);
|
||||
assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(tData)).getNorm(), 1.0e-14);
|
||||
|
||||
// check the same cached instance is returned the second time
|
||||
assertTrue(q == transformer.getQ());
|
||||
|
|
|
@ -98,6 +98,7 @@ public class NelderMeadTest
|
|||
} catch (ConvergenceException ce) {
|
||||
// expected behavior
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
fail("wrong exception caught: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -17,14 +17,16 @@
|
|||
|
||||
package org.apache.commons.math.random;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.apache.commons.math.DimensionMismatchException;
|
||||
import org.apache.commons.math.linear.MatrixUtils;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.RealMatrixImpl;
|
||||
import org.apache.commons.math.stat.descriptive.moment.VectorialCovariance;
|
||||
import org.apache.commons.math.stat.descriptive.moment.VectorialMean;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
public class CorrelatedRandomVectorGeneratorTest
|
||||
extends TestCase {
|
||||
|
||||
|
@ -48,7 +50,7 @@ extends TestCase {
|
|||
{ 2, 16, 38, -1 },
|
||||
{ 6, 2, -1, 197 }
|
||||
};
|
||||
RealMatrix covRM = new RealMatrixImpl(cov, false);
|
||||
RealMatrix covRM = MatrixUtils.createRealMatrix(cov);
|
||||
JDKRandomGenerator jg = new JDKRandomGenerator();
|
||||
jg.setSeed(5322145245211l);
|
||||
NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg);
|
||||
|
@ -99,24 +101,21 @@ extends TestCase {
|
|||
try {
|
||||
mean = new double[] { 0.0, 1.0, -3.0, 2.3};
|
||||
|
||||
RealMatrixImpl b = new RealMatrixImpl(4, 3);
|
||||
double[][] bData = b.getDataRef();
|
||||
RealMatrix b = MatrixUtils.createRealMatrix(4, 3);
|
||||
int counter = 0;
|
||||
for (int i = 0; i < bData.length; ++i) {
|
||||
double[] bi = bData[i];
|
||||
for (int i = 0; i < b.getRowDimension(); ++i) {
|
||||
for (int j = 0; j < b.getColumnDimension(); ++j) {
|
||||
bi[j] = 1.0 + 0.1 * ++counter;
|
||||
b.setEntry(i, j, 1.0 + 0.1 * ++counter);
|
||||
}
|
||||
}
|
||||
RealMatrix bbt = b.multiply(b.transpose());
|
||||
covariance = new RealMatrixImpl(mean.length, mean.length);
|
||||
double[][] covData = covariance.getDataRef();
|
||||
covariance = MatrixUtils.createRealMatrix(mean.length, mean.length);
|
||||
for (int i = 0; i < covariance.getRowDimension(); ++i) {
|
||||
covData[i][i] = bbt.getEntry(i, i);
|
||||
covariance.setEntry(i, i, bbt.getEntry(i, i));
|
||||
for (int j = 0; j < covariance.getColumnDimension(); ++j) {
|
||||
double s = bbt.getEntry(i, j);
|
||||
covData[i][j] = s;
|
||||
covData[j][i] = s;
|
||||
covariance.setEntry(i, j, s);
|
||||
covariance.setEntry(j, i, s);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,7 +144,7 @@ extends TestCase {
|
|||
}
|
||||
|
||||
private double[] mean;
|
||||
private RealMatrixImpl covariance;
|
||||
private RealMatrix covariance;
|
||||
private CorrelatedRandomVectorGenerator generator;
|
||||
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
|
|||
createRegression().newSampleData(y, x, null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
@Test(expected=ArrayIndexOutOfBoundsException.class)
|
||||
public void cannotAddNullCovarianceData() {
|
||||
createRegression().newSampleData(new double[]{}, new double[][]{}, null);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue