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:
Luc Maisonobe 2008-12-20 00:03:13 +00:00
parent 735e84a26a
commit 136ffc1054
33 changed files with 351 additions and 367 deletions

View File

@ -22,8 +22,8 @@ import java.util.Arrays;
import org.apache.commons.math.linear.InvalidMatrixException; import org.apache.commons.math.linear.InvalidMatrixException;
import org.apache.commons.math.linear.LUDecompositionImpl; import org.apache.commons.math.linear.LUDecompositionImpl;
import org.apache.commons.math.linear.LUSolver; 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.RealMatrix;
import org.apache.commons.math.linear.RealMatrixImpl;
/** /**
* Base class for implementing estimators. * Base class for implementing estimators.
@ -183,8 +183,8 @@ public abstract class AbstractEstimator implements Estimator {
try { try {
// compute the covariances matrix // compute the covariances matrix
RealMatrix inverse = RealMatrix inverse =
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(jTj, false))).getInverse(); new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj))).getInverse();
return ((RealMatrixImpl) inverse).getDataRef(); return inverse.getData();
} catch (InvalidMatrixException ime) { } catch (InvalidMatrixException ime) {
throw new EstimationException("unable to compute covariances: singular problem", throw new EstimationException("unable to compute covariances: singular problem",
null); null);

View File

@ -22,8 +22,8 @@ import java.io.Serializable;
import org.apache.commons.math.linear.InvalidMatrixException; import org.apache.commons.math.linear.InvalidMatrixException;
import org.apache.commons.math.linear.LUDecompositionImpl; import org.apache.commons.math.linear.LUDecompositionImpl;
import org.apache.commons.math.linear.LUSolver; 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.RealMatrix;
import org.apache.commons.math.linear.RealMatrixImpl;
import org.apache.commons.math.linear.RealVector; import org.apache.commons.math.linear.RealVector;
import org.apache.commons.math.linear.RealVectorImpl; import org.apache.commons.math.linear.RealVectorImpl;
@ -112,8 +112,7 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
double[] grad = new double[parameters.length]; double[] grad = new double[parameters.length];
RealVectorImpl bDecrement = new RealVectorImpl(parameters.length); RealVectorImpl bDecrement = new RealVectorImpl(parameters.length);
double[] bDecrementData = bDecrement.getDataRef(); double[] bDecrementData = bDecrement.getDataRef();
RealMatrixImpl wGradGradT = new RealMatrixImpl(parameters.length, parameters.length); RealMatrix wGradGradT = MatrixUtils.createRealMatrix(parameters.length, parameters.length);
double[][] wggData = wGradGradT.getDataRef();
// iterate until convergence is reached // iterate until convergence is reached
double previous = Double.POSITIVE_INFINITY; double previous = Double.POSITIVE_INFINITY;
@ -122,7 +121,7 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
// build the linear problem // build the linear problem
incrementJacobianEvaluationsCounter(); incrementJacobianEvaluationsCounter();
RealVector b = new RealVectorImpl(parameters.length); 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) { for (int i = 0; i < measurements.length; ++i) {
if (! measurements [i].isIgnored()) { if (! measurements [i].isIgnored()) {
@ -137,10 +136,9 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
// build the contribution matrix for measurement i // build the contribution matrix for measurement i
for (int k = 0; k < parameters.length; ++k) { for (int k = 0; k < parameters.length; ++k) {
double[] wggRow = wggData[k];
double gk = grad[k]; double gk = grad[k];
for (int l = 0; l < parameters.length; ++l) { for (int l = 0; l < parameters.length; ++l) {
wggRow[l] = weight * gk * grad[l]; wGradGradT.setEntry(k, l, weight * gk * grad[l]);
} }
} }

View File

@ -96,38 +96,35 @@ class BiDiagonalTransformer implements Serializable {
final int p = main.length; final int p = main.length;
final int diagOffset = (m >= n) ? 0 : 1; final int diagOffset = (m >= n) ? 0 : 1;
final double[] diagonal = (m >= n) ? main : secondary; 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 // fill up the part of the matrix not affected by Householder transforms
for (int k = m - 1; k >= p; --k) { 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 // build up first part of the matrix by applying Householder transforms
for (int k = p - 1; k >= diagOffset; --k) { for (int k = p - 1; k >= diagOffset; --k) {
final double[] hK = householderVectors[k]; final double[] hK = householderVectors[k];
uData[k][k] = 1; cachedU.setEntry(k, k, 1);
if (hK[k - diagOffset] != 0.0) { if (hK[k - diagOffset] != 0.0) {
for (int j = k; j < m; ++j) { for (int j = k; j < m; ++j) {
double alpha = 0; double alpha = 0;
for (int i = k; i < m; ++i) { 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]; alpha /= diagonal[k - diagOffset] * hK[k - diagOffset];
for (int i = k; i < m; ++i) { 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) { 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 // return the cached matrix
@ -145,24 +142,20 @@ class BiDiagonalTransformer implements Serializable {
final int m = householderVectors.length; final int m = householderVectors.length;
final int n = householderVectors[0].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) { for (int i = 0; i < main.length; ++i) {
double[] bDataI = bData[i]; cachedB.setEntry(i, i, main[i]);
bDataI[i] = main[i];
if (m < n) { if (m < n) {
if (i > 0) { if (i > 0) {
bDataI[i - 1] = secondary[i - 1]; cachedB.setEntry(i, i - 1, secondary[i - 1]);
} }
} else { } else {
if (i < main.length - 1) { 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 // return the cached matrix
@ -184,38 +177,35 @@ class BiDiagonalTransformer implements Serializable {
final int p = main.length; final int p = main.length;
final int diagOffset = (m >= n) ? 1 : 0; final int diagOffset = (m >= n) ? 1 : 0;
final double[] diagonal = (m >= n) ? secondary : main; 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 // fill up the part of the matrix not affected by Householder transforms
for (int k = n - 1; k >= p; --k) { 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 // build up first part of the matrix by applying Householder transforms
for (int k = p - 1; k >= diagOffset; --k) { for (int k = p - 1; k >= diagOffset; --k) {
final double[] hK = householderVectors[k - diagOffset]; final double[] hK = householderVectors[k - diagOffset];
vData[k][k] = 1; cachedV.setEntry(k, k, 1);
if (hK[k] != 0.0) { if (hK[k] != 0.0) {
for (int j = k; j < n; ++j) { for (int j = k; j < n; ++j) {
double beta = 0; double beta = 0;
for (int i = k; i < n; ++i) { 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]; beta /= diagonal[k - diagOffset] * hK[k];
for (int i = k; i < n; ++i) { for (int i = k; i < n; ++i) {
vData[i][j] -= beta * hK[i]; cachedV.addToEntry(i, j, -beta * hK[i]);
} }
} }
} }
} }
if (diagOffset > 0) { 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 // return the cached matrix

View File

@ -228,7 +228,17 @@ public class EigenDecompositionImpl implements EigenDecomposition {
throws InvalidMatrixException { throws InvalidMatrixException {
if (cachedV == null) { 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 // return the cached matrix
@ -239,18 +249,9 @@ public class EigenDecompositionImpl implements EigenDecomposition {
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealMatrix getD() public RealMatrix getD()
throws InvalidMatrixException { throws InvalidMatrixException {
if (cachedD == null) { 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 // cache the matrix for subsequent calls
cachedD = new RealMatrixImpl(sData, false); cachedD = MatrixUtils.createRealDiagonalMatrix(eigenvalues);
} }
return cachedD; return cachedD;
} }
@ -265,14 +266,12 @@ public class EigenDecompositionImpl implements EigenDecomposition {
findEigenVectors(); findEigenVectors();
} }
final double[][] vtData = new double[eigenvectors.length][]; final int m = eigenvectors.length;
for (int k = 0; k < eigenvectors.length; ++k) { cachedVt = MatrixUtils.createRealMatrix(m, m);
vtData[k] = eigenvectors[k].getData(); 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 // return the cached matrix

View File

@ -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; invI[j] = invIJ;
} }
} }
return new RealMatrixImpl(invData, false); return MatrixUtils.createRealMatrix(invData);
} }

View File

@ -178,12 +178,14 @@ public class LUDecompositionImpl implements LUDecomposition {
throws IllegalStateException { throws IllegalStateException {
if ((cachedL == null) && !singular) { if ((cachedL == null) && !singular) {
final int m = pivot.length; final int m = pivot.length;
final double[][] lData = new double[m][m]; cachedL = MatrixUtils.createRealMatrix(m, m);
for (int i = 0; i < m; ++i) { for (int i = 0; i < m; ++i) {
System.arraycopy(lu[i], 0, lData[i], 0, i); final double[] luI = lu[i];
lData[i][i] = 1.0; 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; return cachedL;
} }
@ -193,11 +195,13 @@ public class LUDecompositionImpl implements LUDecomposition {
throws IllegalStateException { throws IllegalStateException {
if ((cachedU == null) && !singular) { if ((cachedU == null) && !singular) {
final int m = pivot.length; final int m = pivot.length;
final double[][] uData = new double[m][m]; cachedU = MatrixUtils.createRealMatrix(m, m);
for (int i = 0; i < m; ++i) { 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; return cachedU;
} }
@ -207,11 +211,10 @@ public class LUDecompositionImpl implements LUDecomposition {
throws IllegalStateException { throws IllegalStateException {
if ((cachedP == null) && !singular) { if ((cachedP == null) && !singular) {
final int m = pivot.length; final int m = pivot.length;
final double[][] pData = new double[m][m]; cachedP = MatrixUtils.createRealMatrix(m, m);
for (int i = 0; i < m; ++i) { 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; return cachedP;
} }

View File

@ -201,7 +201,7 @@ public class LUSolver implements DecompositionSolver {
} }
} }
return new RealMatrixImpl(bp, false); return MatrixUtils.createRealMatrix(bp);
} }

View File

@ -153,20 +153,16 @@ public class QRDecompositionImpl implements QRDecomposition {
// R is supposed to be m x n // R is supposed to be m x n
final int n = qrt.length; final int n = qrt.length;
final int m = qrt[0].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 // copy the diagonal from rDiag and the upper triangle of qr
for (int row = Math.min(m, n) - 1; row >= 0; row--) { for (int row = Math.min(m, n) - 1; row >= 0; row--) {
double[] rRow = r[row]; cachedR.setEntry(row, row, rDiag[row]);
rRow[row] = rDiag[row];
for (int col = row + 1; col < n; col++) { 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 // return the cached matrix
@ -192,7 +188,7 @@ public class QRDecompositionImpl implements QRDecomposition {
// QT is supposed to be m x m // QT is supposed to be m x m
final int n = qrt.length; final int n = qrt.length;
final int m = qrt[0].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 * 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 * succession to the result
*/ */
for (int minor = m - 1; minor >= Math.min(m, n); minor--) { 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]; final double[] qrtMinor = qrt[minor];
qT[minor][minor] = 1; cachedQT.setEntry(minor, minor, 1.0);
if (qrtMinor[minor] != 0.0) { if (qrtMinor[minor] != 0.0) {
for (int col = minor; col < m; col++) { for (int col = minor; col < m; col++) {
final double[] qTCol = qT[col];
double alpha = 0; double alpha = 0;
for (int row = minor; row < m; row++) { for (int row = minor; row < m; row++) {
alpha -= qTCol[row] * qrtMinor[row]; alpha -= cachedQT.getEntry(col, row) * qrtMinor[row];
} }
alpha /= rDiag[minor] * qrtMinor[minor]; alpha /= rDiag[minor] * qrtMinor[minor];
for (int row = minor; row < m; row++) { 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 // return the cached matrix
@ -240,17 +232,13 @@ public class QRDecompositionImpl implements QRDecomposition {
final int n = qrt.length; final int n = qrt.length;
final int m = qrt[0].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) { for (int i = 0; i < m; ++i) {
final double[] hDataI = hData[i];
for (int j = 0; j < Math.min(i + 1, n); ++j) { 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 // return the cached matrix

View File

@ -136,7 +136,7 @@ public class QRSolver implements DecompositionSolver {
} }
} }
return new RealMatrixImpl(xData, false); return MatrixUtils.createRealMatrix(xData);
} }

View File

@ -1112,38 +1112,40 @@ public class RealVectorImpl implements RealVector, Serializable {
return outerProduct((RealVectorImpl) v); return outerProduct((RealVectorImpl) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
checkVectorDimensions(v); 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 i = 0; i < data.length; i++) {
for (int j = 0; j < data.length; j++) { 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. * Compute the outer product.
* @param v vector with which outer product should be computed * @param v vector with which outer product should be computed
* @return the square matrix outer product between instance and v * @return the square matrix outer product between instance and v
* @exception IllegalArgumentException if v is not the same size as this * @exception IllegalArgumentException if v is not the same size as this
*/ */
public RealMatrixImpl outerProduct(RealVectorImpl v) public RealMatrix outerProduct(RealVectorImpl v)
throws IllegalArgumentException { 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} */ /** {@inheritDoc} */

View File

@ -157,7 +157,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
iData[i] = new double[n]; iData[i] = new double[n];
} }
cachedU = cachedU =
transformer.getU().multiply(new RealMatrixImpl(iData, false)); transformer.getU().multiply(MatrixUtils.createRealMatrix(iData));
} else { } else {
// the tridiagonal matrix is B.Bt, where B is lower bidiagonal // the tridiagonal matrix is B.Bt, where B is lower bidiagonal
cachedU = transformer.getU().multiply(eigenDecomposition.getV()); cachedU = transformer.getU().multiply(eigenDecomposition.getV());
@ -189,14 +189,8 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
if (cachedS == null) { 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 // cache the matrix for subsequent calls
cachedS = new RealMatrixImpl(sData, false); cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues);
} }
return cachedS; return cachedS;
@ -243,7 +237,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio
iData[i] = new double[m]; iData[i] = new double[m];
} }
cachedV = cachedV =
transformer.getV().multiply(new RealMatrixImpl(iData, false)); transformer.getV().multiply(MatrixUtils.createRealMatrix(iData));
} }
} }

View File

@ -115,17 +115,15 @@ public class SingularValueSolver implements DecompositionSolver {
throw new IllegalArgumentException("Incorrect row dimension"); throw new IllegalArgumentException("Incorrect row dimension");
} }
final RealMatrixImpl w = (RealMatrixImpl) decomposition.getUT().multiply(b); final RealMatrix w = decomposition.getUT().multiply(b);
final double[][] wData = w.getDataRef();
for (int i = 0; i < singularValues.length; ++i) { for (int i = 0; i < singularValues.length; ++i) {
final double si = singularValues[i]; final double si = singularValues[i];
if (si == 0) { if (si == 0) {
throw new SingularMatrixException(); throw new SingularMatrixException();
} }
final double inv = 1.0 / si; final double inv = 1.0 / si;
final double[] wi = wData[i];
for (int j = 0; j < b.getColumnDimension(); ++j) { for (int j = 0; j < b.getColumnDimension(); ++j) {
wi[j] *= inv; w.multiplyEntry(i, j, inv);
} }
} }
return decomposition.getV().multiply(w); return decomposition.getV().multiply(w);

View File

@ -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 * Compute the key to access a matrix element
* @param row row index of the matrix element * @param row row index of the matrix element

View File

@ -106,38 +106,33 @@ class TriDiagonalTransformer implements Serializable {
if (cachedQt == null) { if (cachedQt == null) {
final int m = householderVectors.length; 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 // build up first part of the matrix by applying Householder transforms
for (int k = m - 1; k >= 1; --k) { for (int k = m - 1; k >= 1; --k) {
final double[] hK = householderVectors[k - 1]; final double[] hK = householderVectors[k - 1];
final double inv = 1.0 / (secondary[k - 1] * hK[k]); final double inv = 1.0 / (secondary[k - 1] * hK[k]);
qtData[k][k] = 1; cachedQt.setEntry(k, k, 1);
if (hK[k] != 0.0) { if (hK[k] != 0.0) {
final double[] qtK = qtData[k];
double beta = 1.0 / secondary[k - 1]; 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) { 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) { for (int j = k + 1; j < m; ++j) {
final double[] qtJ = qtData[j];
beta = 0; beta = 0;
for (int i = k + 1; i < m; ++i) { for (int i = k + 1; i < m; ++i) {
beta += qtJ[i] * hK[i]; beta += cachedQt.getEntry(j, i) * hK[i];
} }
beta *= inv; beta *= inv;
qtJ[k] = beta * hK[k]; cachedQt.setEntry(j, k, beta * hK[k]);
for (int i = k + 1; i < m; ++i) { for (int i = k + 1; i < m; ++i) {
qtJ[i] += beta * hK[i]; cachedQt.addToEntry(j, i, beta * hK[i]);
} }
} }
} }
} }
qtData[0][0] = 1; cachedQt.setEntry(0, 0, 1);
// cache the matrix for subsequent calls
cachedQt = new RealMatrixImpl(qtData, false);
} }
@ -155,21 +150,17 @@ class TriDiagonalTransformer implements Serializable {
if (cachedT == null) { if (cachedT == null) {
final int m = main.length; final int m = main.length;
double[][] tData = new double[m][m]; cachedT = MatrixUtils.createRealMatrix(m, m);
for (int i = 0; i < m; ++i) { for (int i = 0; i < m; ++i) {
double[] tDataI = tData[i]; cachedT.setEntry(i, i, main[i]);
tDataI[i] = main[i];
if (i > 0) { if (i > 0) {
tDataI[i - 1] = secondary[i - 1]; cachedT.setEntry(i, i - 1, secondary[i - 1]);
} }
if (i < main.length - 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 // return the cached matrix

View File

@ -18,8 +18,8 @@
package org.apache.commons.math.random; package org.apache.commons.math.random;
import org.apache.commons.math.DimensionMismatchException; 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.RealMatrix;
import org.apache.commons.math.linear.RealMatrixImpl;
/** /**
* A {@link RandomVectorGenerator} that generates vectors with with * A {@link RandomVectorGenerator} that generates vectors with with
@ -251,9 +251,11 @@ implements RandomVectorGenerator {
} }
// build the root matrix // build the root matrix
root = new RealMatrixImpl(order, rank); root = MatrixUtils.createRealMatrix(order, rank);
for (int i = 0; i < order; ++i) { 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; private double[] mean;
/** Permutated Cholesky root of the covariance matrix. */ /** Permutated Cholesky root of the covariance matrix. */
private RealMatrixImpl root; private RealMatrix root;
/** Rank of the covariance matrix. */ /** Rank of the covariance matrix. */
private int rank; private int rank;

View File

@ -20,8 +20,8 @@ import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.math.DimensionMismatchException; 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.RealMatrix;
import org.apache.commons.math.linear.RealMatrixImpl;
/** /**
* Returns the covariance matrix of the available vectors. * Returns the covariance matrix of the available vectors.
@ -83,17 +83,16 @@ public class VectorialCovariance implements Serializable {
public RealMatrix getResult() { public RealMatrix getResult() {
int dimension = sums.length; int dimension = sums.length;
RealMatrixImpl result = new RealMatrixImpl(dimension, dimension); RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension);
if (n > 1) { if (n > 1) {
double[][] resultData = result.getDataRef();
double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n)); double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
int k = 0; int k = 0;
for (int i = 0; i < dimension; ++i) { for (int i = 0; i < dimension; ++i) {
for (int j = 0; j <= i; ++j) { for (int j = 0; j <= i; ++j) {
double e = c * (n * productsSums[k++] - sums[i] * sums[j]); double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
resultData[i][j] = e; result.setEntry(i, j, e);
resultData[j][i] = e; result.setEntry(j, i, e);
} }
} }
} }

View File

@ -16,8 +16,10 @@
*/ */
package org.apache.commons.math.stat.regression; 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.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. * Abstract base class for implementations of MultipleLinearRegression.
@ -31,7 +33,7 @@ public abstract class AbstractMultipleLinearRegression implements
protected RealMatrix X; protected RealMatrix X;
/** Y sample data. */ /** 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. * 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++]; x[i][j] = data[pointer++];
} }
} }
this.X = new RealMatrixImpl(x); this.X = MatrixUtils.createRealMatrix(x);
this.Y = new RealMatrixImpl(y); this.Y = new RealVectorImpl(y);
} }
/** /**
@ -62,7 +64,7 @@ public abstract class AbstractMultipleLinearRegression implements
* @param y the [n,1] array representing the y sample * @param y the [n,1] array representing the y sample
*/ */
protected void newYSampleData(double[] y) { 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 * @param x the [n,k] array representing the x sample
*/ */
protected void newXSampleData(double[][] x) { 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} * {@inheritDoc}
*/ */
public double[] estimateRegressionParameters() { public double[] estimateRegressionParameters() {
RealMatrix b = calculateBeta(); return calculateBeta().getData();
return b.getColumn(0);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public double[] estimateResiduals() { public double[] estimateResiduals() {
RealMatrix b = calculateBeta(); return Y.subtract(X.operate(calculateBeta())).getData();
RealMatrix e = Y.subtract(X.multiply(b));
return e.getColumn(0);
} }
/** /**
@ -152,7 +151,7 @@ public abstract class AbstractMultipleLinearRegression implements
* *
* @return beta * @return beta
*/ */
protected abstract RealMatrix calculateBeta(); protected abstract RealVector calculateBeta();
/** /**
* Calculates the beta variance of multiple linear regression in matrix * 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 * @return The residuals [n,1] matrix
*/ */
protected RealMatrix calculateResiduals() { protected RealVector calculateResiduals() {
RealMatrix b = calculateBeta(); return Y.subtract(X.operate(calculateBeta()));
return Y.subtract(X.multiply(b));
} }
} }

View File

@ -18,8 +18,9 @@ package org.apache.commons.math.stat.regression;
import org.apache.commons.math.linear.LUDecompositionImpl; import org.apache.commons.math.linear.LUDecompositionImpl;
import org.apache.commons.math.linear.LUSolver; 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.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 * @param omega the [n,n] array representing the covariance
*/ */
protected void newCovarianceData(double[][] omega){ protected void newCovarianceData(double[][] omega){
this.Omega = new RealMatrixImpl(omega); this.Omega = MatrixUtils.createRealMatrix(omega);
this.OmegaInverse = null; this.OmegaInverse = null;
} }
@ -91,12 +92,12 @@ public class GLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
* </pre> * </pre>
* @return beta * @return beta
*/ */
protected RealMatrix calculateBeta() { protected RealVector calculateBeta() {
RealMatrix OI = getOmegaInverse(); RealMatrix OI = getOmegaInverse();
RealMatrix XT = X.transpose(); RealMatrix XT = X.transpose();
RealMatrix XTOIX = XT.multiply(OI).multiply(X); RealMatrix XTOIX = XT.multiply(OI).multiply(X);
RealMatrix inverse = new LUSolver(new LUDecompositionImpl(XTOIX)).getInverse(); 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 * @return The Y variance
*/ */
protected double calculateYVariance() { protected double calculateYVariance() {
RealMatrix u = calculateResiduals(); final RealVector u = calculateResiduals();
RealMatrix sse = u.transpose().multiply(getOmegaInverse()).multiply(u); final double sse = u.dotProduct(getOmegaInverse().operate(u));
return sse.getTrace()/(X.getRowDimension()-X.getColumnDimension()); return sse / (X.getRowDimension() - X.getColumnDimension());
} }
} }

View File

@ -16,12 +16,14 @@
*/ */
package org.apache.commons.math.stat.regression; 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.LUDecompositionImpl;
import org.apache.commons.math.linear.LUSolver; import org.apache.commons.math.linear.LUSolver;
import org.apache.commons.math.linear.QRDecomposition; import org.apache.commons.math.linear.QRDecomposition;
import org.apache.commons.math.linear.QRDecompositionImpl; import org.apache.commons.math.linear.QRDecompositionImpl;
import org.apache.commons.math.linear.RealMatrix; 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 * <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 * @param x the [n,k] array representing the x sample
*/ */
protected void newXSampleData(double[][] x) { protected void newXSampleData(double[][] x) {
this.X = new RealMatrixImpl(x); this.X = new DenseRealMatrix(x);
qr = new QRDecompositionImpl(X); qr = new QRDecompositionImpl(X);
} }
@ -95,9 +97,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
* *
* @return beta * @return beta
*/ */
protected RealMatrix calculateBeta() { protected RealVector calculateBeta() {
return solveUpperTriangular((RealMatrixImpl) qr.getR(), return solveUpperTriangular(qr.getR(), qr.getQ().transpose().operate(Y));
(RealMatrixImpl) qr.getQ().transpose().multiply(Y));
} }
/** /**
@ -121,9 +122,9 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
* @return The Y variance * @return The Y variance
*/ */
protected double calculateYVariance() { protected double calculateYVariance() {
RealMatrix u = calculateResiduals(); final RealVector u = calculateResiduals();
RealMatrix sse = u.transpose().multiply(u); final double sse = u.dotProduct(u);
return sse.getTrace()/(X.getRowDimension()-X.getColumnDimension()); return sse / (X.getRowDimension() - X.getColumnDimension());
} }
/** TODO: Find a home for the following methods in the linear package */ /** 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 coefficients upper-triangular coefficients matrix
* @param constants column RHS constants 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, private static RealVector solveUpperTriangular(RealMatrix coefficients, RealVector constants) {
RealMatrixImpl constants) {
if (!isUpperTriangular(coefficients, 1E-12)) { if (!isUpperTriangular(coefficients, 1E-12)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Coefficients is not upper-triangular"); "Coefficients is not upper-triangular");
} }
if (constants.getColumnDimension() != 1) {
throw new IllegalArgumentException(
"Constants not a column matrix.");
}
int length = coefficients.getColumnDimension(); int length = coefficients.getColumnDimension();
double[][] cons = constants.getDataRef();
double[][] coef = coefficients.getDataRef();
double x[] = new double[length]; double x[] = new double[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int index = length - 1 - i; int index = length - 1 - i;
double sum = 0; double sum = 0;
for (int j = index + 1; j < length; j++) { 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 * @return true if m is upper-triangular; false otherwise
* @throws NullPointerException if m is null * @throws NullPointerException if m is null
*/ */
private static boolean isUpperTriangular(RealMatrixImpl m, double epsilon) { private static boolean isUpperTriangular(RealMatrix m, double epsilon) {
double[][] data = m.getDataRef();
int nCols = m.getColumnDimension(); int nCols = m.getColumnDimension();
int nRows = m.getRowDimension(); int nRows = m.getRowDimension();
for (int r = 0; r < nRows; r++) { for (int r = 0; r < nRows; r++) {
int bound = Math.min(r, nCols); int bound = Math.min(r, nCols);
for (int c = 0; c < bound; c++) { for (int c = 0; c < bound; c++) {
if (Math.abs(data[r][c]) > epsilon) { if (Math.abs(m.getEntry(r, c)) > epsilon) {
return false; return false;
} }
} }

View File

@ -40,9 +40,9 @@ public class BiDiagonalTransformerTest extends TestCase {
} }
public void testDimensions() { public void testDimensions() {
checkdimensions(new RealMatrixImpl(testSquare, false)); checkdimensions(MatrixUtils.createRealMatrix(testSquare));
checkdimensions(new RealMatrixImpl(testNonSquare, false)); checkdimensions(MatrixUtils.createRealMatrix(testNonSquare));
checkdimensions(new RealMatrixImpl(testNonSquare, false).transpose()); checkdimensions(MatrixUtils.createRealMatrix(testNonSquare).transpose());
} }
private void checkdimensions(RealMatrix matrix) { private void checkdimensions(RealMatrix matrix) {
@ -59,9 +59,9 @@ public class BiDiagonalTransformerTest extends TestCase {
} }
public void testAEqualUSVt() { public void testAEqualUSVt() {
checkAEqualUSVt(new RealMatrixImpl(testSquare, false)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false).transpose()); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
} }
private void checkAEqualUSVt(RealMatrix matrix) { private void checkAEqualUSVt(RealMatrix matrix) {
@ -74,15 +74,15 @@ public class BiDiagonalTransformerTest extends TestCase {
} }
public void testUOrthogonal() { public void testUOrthogonal() {
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getU()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getU());
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getU()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getU());
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getU()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
} }
public void testVOrthogonal() { public void testVOrthogonal() {
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getV()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getV());
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getV()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getV());
checkOrthogonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getV()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
} }
private void checkOrthogonal(RealMatrix m) { private void checkOrthogonal(RealMatrix m) {
@ -92,9 +92,9 @@ public class BiDiagonalTransformerTest extends TestCase {
} }
public void testBBiDiagonal() { public void testBBiDiagonal() {
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).getB()); checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getB());
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).getB()); checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getB());
checkBiDiagonal(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).getB()); checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getB());
} }
private void checkBiDiagonal(RealMatrix m) { private void checkBiDiagonal(RealMatrix m) {
@ -117,17 +117,17 @@ public class BiDiagonalTransformerTest extends TestCase {
public void testMatricesValues() { public void testMatricesValues() {
BiDiagonalTransformer transformer = BiDiagonalTransformer transformer =
new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)); new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare));
final double s17 = Math.sqrt(17.0); 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) }, { -8 / (5 * s17), 19 / (5 * s17) },
{ -19 / (5 * s17), -8 / (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 }, { -3 * s17 / 5, 32 * s17 / 85 },
{ 0.0, -5 * s17 / 17 } { 0.0, -5 * s17 / 17 }
}); });
RealMatrix vRef = new RealMatrixImpl(new double[][] { RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 0.0 }, { 1.0, 0.0 },
{ 0.0, -1.0 } { 0.0, -1.0 }
}); });
@ -148,9 +148,9 @@ public class BiDiagonalTransformerTest extends TestCase {
} }
public void testUpperOrLower() { public void testUpperOrLower() {
assertTrue(new BiDiagonalTransformer(new RealMatrixImpl(testSquare, false)).isUpperBiDiagonal()); assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).isUpperBiDiagonal());
assertTrue(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false)).isUpperBiDiagonal()); assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).isUpperBiDiagonal());
assertFalse(new BiDiagonalTransformer(new RealMatrixImpl(testNonSquare, false).transpose()).isUpperBiDiagonal()); assertFalse(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).isUpperBiDiagonal());
} }
public static Test suite() { public static Test suite() {

View File

@ -43,19 +43,17 @@ public class EigenDecompositionImplTest extends TestCase {
public void testDimension1() { public void testDimension1() {
RealMatrix matrix = RealMatrix matrix =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] { { 1.5 } });
{ 1.5 }
}, false);
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN); EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.5, ed.getEigenvalue(0), 1.0e-15); assertEquals(1.5, ed.getEigenvalue(0), 1.0e-15);
} }
public void testDimension2() { public void testDimension2() {
RealMatrix matrix = RealMatrix matrix =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] {
{ 59.0, 12.0 }, { 59.0, 12.0 },
{ Double.NaN, 66.0 } { Double.NaN, 66.0 }
}, false); });
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN); EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(75.0, ed.getEigenvalue(0), 1.0e-15); assertEquals(75.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(50.0, ed.getEigenvalue(1), 1.0e-15); assertEquals(50.0, ed.getEigenvalue(1), 1.0e-15);
@ -63,11 +61,11 @@ public class EigenDecompositionImplTest extends TestCase {
public void testDimension3() { public void testDimension3() {
RealMatrix matrix = RealMatrix matrix =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] {
{ 39632.0, -4824.0, -16560.0 }, { 39632.0, -4824.0, -16560.0 },
{ Double.NaN, 8693.0, 7920.0 }, { Double.NaN, 8693.0, 7920.0 },
{ Double.NaN, Double.NaN, 17300.0 } { Double.NaN, Double.NaN, 17300.0 }
}, false); });
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN); EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(50000.0, ed.getEigenvalue(0), 3.0e-11); assertEquals(50000.0, ed.getEigenvalue(0), 3.0e-11);
assertEquals(12500.0, ed.getEigenvalue(1), 3.0e-11); assertEquals(12500.0, ed.getEigenvalue(1), 3.0e-11);
@ -76,12 +74,12 @@ public class EigenDecompositionImplTest extends TestCase {
public void testDimension4WithSplit() { public void testDimension4WithSplit() {
RealMatrix matrix = RealMatrix matrix =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] {
{ 0.784, -0.288, 0.000, 0.000 }, { 0.784, -0.288, 0.000, 0.000 },
{ Double.NaN, 0.616, 0.000, 0.000 }, { Double.NaN, 0.616, 0.000, 0.000 },
{ Double.NaN, Double.NaN, 0.164, -0.048 }, { Double.NaN, Double.NaN, 0.164, -0.048 },
{ Double.NaN, Double.NaN, Double.NaN, 0.136 } { Double.NaN, Double.NaN, Double.NaN, 0.136 }
}, false); });
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN); EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15); assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15); assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15);
@ -91,12 +89,12 @@ public class EigenDecompositionImplTest extends TestCase {
public void testDimension4WithoutSplit() { public void testDimension4WithoutSplit() {
RealMatrix matrix = RealMatrix matrix =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] {
{ 0.5608, -0.2016, 0.1152, -0.2976 }, { 0.5608, -0.2016, 0.1152, -0.2976 },
{ -0.2016, 0.4432, -0.2304, 0.1152 }, { -0.2016, 0.4432, -0.2304, 0.1152 },
{ 0.1152, -0.2304, 0.3088, -0.1344 }, { 0.1152, -0.2304, 0.3088, -0.1344 },
{ -0.2976, 0.1152, -0.1344, 0.3872 } { -0.2976, 0.1152, -0.1344, 0.3872 }
}, false); });
EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN); EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15); assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(0.4, ed.getEigenvalue(1), 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} * Matrix with eigenvalues {8, -1, -1}
*/ */
public void testRepeatedEigenvalue() { public void testRepeatedEigenvalue() {
RealMatrix repeated = new RealMatrixImpl(new double[][] { RealMatrix repeated = MatrixUtils.createRealMatrix(new double[][] {
{3, 2, 4}, {3, 2, 4},
{2, 0, 2}, {2, 0, 2},
{4, 2, 3} {4, 2, 3}
@ -227,7 +225,7 @@ public class EigenDecompositionImplTest extends TestCase {
* Matrix with eigenvalues {2, 0, 12} * Matrix with eigenvalues {2, 0, 12}
*/ */
public void testDistinctEigenvalues() { public void testDistinctEigenvalues() {
RealMatrix distinct = new RealMatrixImpl(new double[][] { RealMatrix distinct = MatrixUtils.createRealMatrix(new double[][] {
{3, 1, -4}, {3, 1, -4},
{1, 3, -4}, {1, 3, -4},
{-4, -4, 8} {-4, -4, 8}
@ -368,7 +366,7 @@ public class EigenDecompositionImplTest extends TestCase {
} while (norm2 * size < 0.01); } 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) { for (int i = 0; i < Math.min(rows, columns); ++i) {
dData[i][i] = diagonal[i]; dData[i][i] = diagonal[i];
} }
return new RealMatrixImpl(dData, false); return MatrixUtils.createRealMatrix(dData);
} }
} }

View File

@ -73,7 +73,7 @@ public class EigenSolverTest extends TestCase {
/** test solve dimension errors */ /** test solve dimension errors */
public void testSolveDimensionErrors() { public void testSolveDimensionErrors() {
EigenSolver es = new EigenSolver(new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN)); 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 { try {
es.solve(b); es.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -102,7 +102,7 @@ public class EigenSolverTest extends TestCase {
/** test solve */ /** test solve */
public void testSolve() { public void testSolve() {
RealMatrix m = new RealMatrixImpl(new double[][] { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
{ 91, 5, 29, 32, 40, 14 }, { 91, 5, 29, 32, 40, 14 },
{ 5, 34, -1, 0, 2, -1 }, { 5, 34, -1, 0, 2, -1 },
{ 29, -1, 12, 9, 21, 8 }, { 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)); EigenSolver es = new EigenSolver(new EigenDecompositionImpl(m, MathUtils.SAFE_MIN));
assertEquals(184041, es.getDeterminant(), 2.0e-8); assertEquals(184041, es.getDeterminant(), 2.0e-8);
RealMatrix b = new RealMatrixImpl(new double[][] { RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
{ 1561, 269, 188 }, { 1561, 269, 188 },
{ 69, -21, 70 }, { 69, -21, 70 },
{ 739, 108, 63 }, { 739, 108, 63 },
@ -120,7 +120,7 @@ public class EigenSolverTest extends TestCase {
{ 1624, 194, 107 }, { 1624, 194, 107 },
{ 796, 69, 36 } { 796, 69, 36 }
}); });
RealMatrix xRef = new RealMatrixImpl(new double[][] { RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
{ 1, 2, 1 }, { 1, 2, 1 },
{ 2, -1, 2 }, { 2, -1, 2 },
{ 4, 2, 3 }, { 4, 2, 3 },

View File

@ -66,7 +66,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test dimensions */ /** test dimensions */
public void testDimensions() { public void testDimensions() {
RealMatrixImpl matrix = new RealMatrixImpl(testData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
LUDecomposition LU = new LUDecompositionImpl(matrix); LUDecomposition LU = new LUDecompositionImpl(matrix);
assertEquals(testData.length, LU.getL().getRowDimension()); assertEquals(testData.length, LU.getL().getRowDimension());
assertEquals(testData.length, LU.getL().getColumnDimension()); assertEquals(testData.length, LU.getL().getColumnDimension());
@ -80,7 +80,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test non-square matrix */ /** test non-square matrix */
public void testNonSquare() { public void testNonSquare() {
try { try {
new LUDecompositionImpl(new RealMatrixImpl(new double[3][2], false)); new LUDecompositionImpl(MatrixUtils.createRealMatrix(new double[3][2]));
} catch (InvalidMatrixException ime) { } catch (InvalidMatrixException ime) {
// expected behavior // expected behavior
} catch (Exception e) { } catch (Exception e) {
@ -90,7 +90,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test PA = LU */ /** test PA = LU */
public void testPAEqualLU() { public void testPAEqualLU() {
RealMatrix matrix = new RealMatrixImpl(testData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
LUDecomposition lu = new LUDecompositionImpl(matrix); LUDecomposition lu = new LUDecompositionImpl(matrix);
RealMatrix l = lu.getL(); RealMatrix l = lu.getL();
RealMatrix u = lu.getU(); RealMatrix u = lu.getU();
@ -98,7 +98,7 @@ public class LUDecompositionImplTest extends TestCase {
double norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm(); double norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
assertEquals(0, norm, normTolerance); assertEquals(0, norm, normTolerance);
matrix = new RealMatrixImpl(testDataMinus, false); matrix = MatrixUtils.createRealMatrix(testDataMinus);
lu = new LUDecompositionImpl(matrix); lu = new LUDecompositionImpl(matrix);
l = lu.getL(); l = lu.getL();
u = lu.getU(); u = lu.getU();
@ -114,14 +114,14 @@ public class LUDecompositionImplTest extends TestCase {
norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm(); norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
assertEquals(0, norm, normTolerance); assertEquals(0, norm, normTolerance);
matrix = new RealMatrixImpl(singular, false); matrix = MatrixUtils.createRealMatrix(singular);
lu = new LUDecompositionImpl(matrix); lu = new LUDecompositionImpl(matrix);
assertTrue(lu.isSingular()); assertTrue(lu.isSingular());
assertNull(lu.getL()); assertNull(lu.getL());
assertNull(lu.getU()); assertNull(lu.getU());
assertNull(lu.getP()); assertNull(lu.getP());
matrix = new RealMatrixImpl(bigSingular, false); matrix = MatrixUtils.createRealMatrix(bigSingular);
lu = new LUDecompositionImpl(matrix); lu = new LUDecompositionImpl(matrix);
assertTrue(lu.isSingular()); assertTrue(lu.isSingular());
assertNull(lu.getL()); assertNull(lu.getL());
@ -132,7 +132,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test that L is lower triangular with unit diagonal */ /** test that L is lower triangular with unit diagonal */
public void testLLowerTriangular() { public void testLLowerTriangular() {
RealMatrixImpl matrix = new RealMatrixImpl(testData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix l = new LUDecompositionImpl(matrix).getL(); RealMatrix l = new LUDecompositionImpl(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) { for (int i = 0; i < l.getRowDimension(); i++) {
assertEquals(l.getEntry(i, i), 1, entryTolerance); assertEquals(l.getEntry(i, i), 1, entryTolerance);
@ -144,7 +144,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test that U is upper triangular */ /** test that U is upper triangular */
public void testUUpperTriangular() { public void testUUpperTriangular() {
RealMatrixImpl matrix = new RealMatrixImpl(testData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix u = new LUDecompositionImpl(matrix).getU(); RealMatrix u = new LUDecompositionImpl(matrix).getU();
for (int i = 0; i < u.getRowDimension(); i++) { for (int i = 0; i < u.getRowDimension(); i++) {
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
@ -155,7 +155,7 @@ public class LUDecompositionImplTest extends TestCase {
/** test that P is a permutation matrix */ /** test that P is a permutation matrix */
public void testPPermutation() { public void testPPermutation() {
RealMatrixImpl matrix = new RealMatrixImpl(testData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix p = new LUDecompositionImpl(matrix).getP(); RealMatrix p = new LUDecompositionImpl(matrix).getP();
RealMatrix ppT = p.multiply(p.transpose()); RealMatrix ppT = p.multiply(p.transpose());
@ -206,29 +206,29 @@ public class LUDecompositionImplTest extends TestCase {
/** test singular */ /** test singular */
public void testSingular() { public void testSingular() {
LUDecomposition lu = LUDecomposition lu =
new LUDecompositionImpl(new RealMatrixImpl(testData, false)); new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
assertFalse(lu.isSingular()); assertFalse(lu.isSingular());
lu = new LUDecompositionImpl(new RealMatrixImpl(singular, false)); lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular));
assertTrue(lu.isSingular()); assertTrue(lu.isSingular());
lu = new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false)); lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular));
assertTrue(lu.isSingular()); assertTrue(lu.isSingular());
} }
/** test matrices values */ /** test matrices values */
public void testMatricesValues1() { public void testMatricesValues1() {
LUDecomposition lu = LUDecomposition lu =
new LUDecompositionImpl(new RealMatrixImpl(testData, false)); new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
RealMatrix lRef = new RealMatrixImpl(new double[][] { RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 },
{ 0.5, 1.0, 0.0 }, { 0.5, 1.0, 0.0 },
{ 0.5, 0.2, 1.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 }, { 2.0, 5.0, 3.0 },
{ 0.0, -2.5, 6.5 }, { 0.0, -2.5, 6.5 },
{ 0.0, 0.0, 0.2 } { 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, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 },
{ 1.0, 0.0, 0.0 } { 1.0, 0.0, 0.0 }
@ -257,18 +257,18 @@ public class LUDecompositionImplTest extends TestCase {
/** test matrices values */ /** test matrices values */
public void testMatricesValues2() { public void testMatricesValues2() {
LUDecomposition lu = LUDecomposition lu =
new LUDecompositionImpl(new RealMatrixImpl(luData, false)); new LUDecompositionImpl(MatrixUtils.createRealMatrix(luData));
RealMatrix lRef = new RealMatrixImpl(new double[][] { RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0 },
{ 1.0 / 3.0, 0.0, 1.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 }, { 6.0, 9.0, 8.0 },
{ 0.0, 5.0, 7.0 }, { 0.0, 5.0, 7.0 },
{ 0.0, 0.0, 1.0 / 3.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, 0.0, 1.0 },
{ 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0 },
{ 1.0, 0.0, 0.0 } { 1.0, 0.0, 0.0 }

View File

@ -57,11 +57,11 @@ public class LUSolverTest extends TestCase {
/** test threshold impact */ /** test threshold impact */
public void testThreshold() { public void testThreshold() {
final RealMatrix matrix = new RealMatrixImpl(new double[][] { final RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 2.0, 3.0}, { 1.0, 2.0, 3.0},
{ 2.0, 5.0, 3.0}, { 2.0, 5.0, 3.0},
{ 4.000001, 9.0, 9.0} { 4.000001, 9.0, 9.0}
}, false); });
assertFalse(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-5)).isNonSingular()); assertFalse(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-5)).isNonSingular());
assertTrue(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-10)).isNonSingular()); assertTrue(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-10)).isNonSingular());
} }
@ -69,19 +69,19 @@ public class LUSolverTest extends TestCase {
/** test singular */ /** test singular */
public void testSingular() { public void testSingular() {
LUSolver lu = LUSolver lu =
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false))); new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
assertTrue(lu.isNonSingular()); assertTrue(lu.isNonSingular());
lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false))); lu = new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)));
assertFalse(lu.isNonSingular()); assertFalse(lu.isNonSingular());
lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false))); lu = new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular)));
assertFalse(lu.isNonSingular()); assertFalse(lu.isNonSingular());
} }
/** test solve dimension errors */ /** test solve dimension errors */
public void testSolveDimensionErrors() { public void testSolveDimensionErrors() {
LUSolver solver = LUSolver solver =
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false))); new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
RealMatrix b = new RealMatrixImpl(new double[2][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -111,8 +111,8 @@ public class LUSolverTest extends TestCase {
/** test solve singularity errors */ /** test solve singularity errors */
public void testSolveSingularityErrors() { public void testSolveSingularityErrors() {
LUSolver solver = LUSolver solver =
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false))); new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)));
RealMatrix b = new RealMatrixImpl(new double[2][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -150,11 +150,11 @@ public class LUSolverTest extends TestCase {
/** test solve */ /** test solve */
public void testSolve() { public void testSolve() {
LUSolver solver = LUSolver solver =
new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false))); new LUSolver(new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)));
RealMatrix b = new RealMatrixImpl(new double[][] { RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
{ 1, 0 }, { 2, -5 }, { 3, 1 } { 1, 0 }, { 2, -5 }, { 3, 1 }
}); });
RealMatrix xRef = new RealMatrixImpl(new double[][] { RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
{ 19, -71 }, { -6, 22 }, { -2, 9 } { 19, -71 }, { -6, 22 }, { -2, 9 }
}); });
@ -188,10 +188,10 @@ public class LUSolverTest extends TestCase {
/** test determinant */ /** test determinant */
public void testDeterminant() { public void testDeterminant() {
assertEquals( -1, getDeterminant(new RealMatrixImpl(testData, false)), 1.0e-15); assertEquals( -1, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
assertEquals(-10, getDeterminant(new RealMatrixImpl(luData, false)), 1.0e-14); assertEquals(-10, getDeterminant(MatrixUtils.createRealMatrix(luData)), 1.0e-14);
assertEquals( 0, getDeterminant(new RealMatrixImpl(singular, false)), 1.0e-17); assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(singular)), 1.0e-17);
assertEquals( 0, getDeterminant(new RealMatrixImpl(bigSingular, false)), 1.0e-10); assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(bigSingular)), 1.0e-10);
} }
private double getDeterminant(RealMatrix m) { private double getDeterminant(RealMatrix m) {

View File

@ -59,21 +59,21 @@ public class QRDecompositionImplTest extends TestCase {
/** test dimensions */ /** test dimensions */
public void testDimensions() { public void testDimensions() {
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
QRDecomposition qr = new QRDecompositionImpl(matrix); QRDecomposition qr = new QRDecompositionImpl(matrix);
assertEquals("3x3 Q size", qr.getQ().getRowDimension(), 3); assertEquals("3x3 Q size", qr.getQ().getRowDimension(), 3);
assertEquals("3x3 Q size", qr.getQ().getColumnDimension(), 3); assertEquals("3x3 Q size", qr.getQ().getColumnDimension(), 3);
assertEquals("3x3 R size", qr.getR().getRowDimension(), 3); assertEquals("3x3 R size", qr.getR().getRowDimension(), 3);
assertEquals("3x3 R size", qr.getR().getColumnDimension(), 3); assertEquals("3x3 R size", qr.getR().getColumnDimension(), 3);
matrix = new RealMatrixImpl(testData4x3, false); matrix = MatrixUtils.createRealMatrix(testData4x3);
qr = new QRDecompositionImpl(matrix); qr = new QRDecompositionImpl(matrix);
assertEquals("4x3 Q size", qr.getQ().getRowDimension(), 4); assertEquals("4x3 Q size", qr.getQ().getRowDimension(), 4);
assertEquals("4x3 Q size", qr.getQ().getColumnDimension(), 4); assertEquals("4x3 Q size", qr.getQ().getColumnDimension(), 4);
assertEquals("4x3 R size", qr.getR().getRowDimension(), 4); assertEquals("4x3 R size", qr.getR().getRowDimension(), 4);
assertEquals("4x3 R size", qr.getR().getColumnDimension(), 3); assertEquals("4x3 R size", qr.getR().getColumnDimension(), 3);
matrix = new RealMatrixImpl(testData3x4, false); matrix = MatrixUtils.createRealMatrix(testData3x4);
qr = new QRDecompositionImpl(matrix); qr = new QRDecompositionImpl(matrix);
assertEquals("3x4 Q size", qr.getQ().getRowDimension(), 3); assertEquals("3x4 Q size", qr.getQ().getRowDimension(), 3);
assertEquals("3x4 Q size", qr.getQ().getColumnDimension(), 3); assertEquals("3x4 Q size", qr.getQ().getColumnDimension(), 3);
@ -83,24 +83,24 @@ public class QRDecompositionImplTest extends TestCase {
/** test A = QR */ /** test A = QR */
public void testAEqualQR() { public void testAEqualQR() {
RealMatrix A = new RealMatrixImpl(testData3x3NonSingular, false); RealMatrix A = MatrixUtils.createRealMatrix(testData3x3NonSingular);
QRDecomposition qr = new QRDecompositionImpl(A); QRDecomposition qr = new QRDecompositionImpl(A);
RealMatrix Q = qr.getQ(); RealMatrix Q = qr.getQ();
RealMatrix R = qr.getR(); RealMatrix R = qr.getR();
double norm = Q.multiply(R).subtract(A).getNorm(); double norm = Q.multiply(R).subtract(A).getNorm();
assertEquals("3x3 nonsingular A = QR", 0, norm, normTolerance); assertEquals("3x3 nonsingular A = QR", 0, norm, normTolerance);
RealMatrix matrix = new RealMatrixImpl(testData3x3Singular, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
qr = new QRDecompositionImpl(matrix); qr = new QRDecompositionImpl(matrix);
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm(); norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
assertEquals("3x3 singular A = QR", 0, norm, normTolerance); assertEquals("3x3 singular A = QR", 0, norm, normTolerance);
matrix = new RealMatrixImpl(testData3x4, false); matrix = MatrixUtils.createRealMatrix(testData3x4);
qr = new QRDecompositionImpl(matrix); qr = new QRDecompositionImpl(matrix);
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm(); norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
assertEquals("3x4 A = QR", 0, norm, normTolerance); assertEquals("3x4 A = QR", 0, norm, normTolerance);
matrix = new RealMatrixImpl(testData4x3, false); matrix = MatrixUtils.createRealMatrix(testData4x3);
qr = new QRDecompositionImpl(matrix); qr = new QRDecompositionImpl(matrix);
norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm(); norm = qr.getQ().multiply(qr.getR()).subtract(matrix).getNorm();
assertEquals("4x3 A = QR", 0, norm, normTolerance); assertEquals("4x3 A = QR", 0, norm, normTolerance);
@ -108,28 +108,28 @@ public class QRDecompositionImplTest extends TestCase {
/** test the orthogonality of Q */ /** test the orthogonality of Q */
public void testQOrthogonal() { public void testQOrthogonal() {
RealMatrix matrix = new RealMatrixImpl(testData3x3NonSingular, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
RealMatrix q = new QRDecompositionImpl(matrix).getQ(); RealMatrix q = new QRDecompositionImpl(matrix).getQ();
RealMatrix qT = new QRDecompositionImpl(matrix).getQT(); RealMatrix qT = new QRDecompositionImpl(matrix).getQT();
RealMatrix eye = MatrixUtils.createRealIdentityMatrix(3); RealMatrix eye = MatrixUtils.createRealIdentityMatrix(3);
double norm = qT.multiply(q).subtract(eye).getNorm(); double norm = qT.multiply(q).subtract(eye).getNorm();
assertEquals("3x3 nonsingular Q'Q = I", 0, norm, normTolerance); assertEquals("3x3 nonsingular Q'Q = I", 0, norm, normTolerance);
matrix = new RealMatrixImpl(testData3x3Singular, false); matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
q = new QRDecompositionImpl(matrix).getQ(); q = new QRDecompositionImpl(matrix).getQ();
qT = new QRDecompositionImpl(matrix).getQT(); qT = new QRDecompositionImpl(matrix).getQT();
eye = MatrixUtils.createRealIdentityMatrix(3); eye = MatrixUtils.createRealIdentityMatrix(3);
norm = qT.multiply(q).subtract(eye).getNorm(); norm = qT.multiply(q).subtract(eye).getNorm();
assertEquals("3x3 singular Q'Q = I", 0, norm, normTolerance); assertEquals("3x3 singular Q'Q = I", 0, norm, normTolerance);
matrix = new RealMatrixImpl(testData3x4, false); matrix = MatrixUtils.createRealMatrix(testData3x4);
q = new QRDecompositionImpl(matrix).getQ(); q = new QRDecompositionImpl(matrix).getQ();
qT = new QRDecompositionImpl(matrix).getQT(); qT = new QRDecompositionImpl(matrix).getQT();
eye = MatrixUtils.createRealIdentityMatrix(3); eye = MatrixUtils.createRealIdentityMatrix(3);
norm = qT.multiply(q).subtract(eye).getNorm(); norm = qT.multiply(q).subtract(eye).getNorm();
assertEquals("3x4 Q'Q = I", 0, norm, normTolerance); assertEquals("3x4 Q'Q = I", 0, norm, normTolerance);
matrix = new RealMatrixImpl(testData4x3, false); matrix = MatrixUtils.createRealMatrix(testData4x3);
q = new QRDecompositionImpl(matrix).getQ(); q = new QRDecompositionImpl(matrix).getQ();
qT = new QRDecompositionImpl(matrix).getQT(); qT = new QRDecompositionImpl(matrix).getQT();
eye = MatrixUtils.createRealIdentityMatrix(4); eye = MatrixUtils.createRealIdentityMatrix(4);
@ -139,28 +139,28 @@ public class QRDecompositionImplTest extends TestCase {
/** test that R is upper triangular */ /** test that R is upper triangular */
public void testRUpperTriangular() { public void testRUpperTriangular() {
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
RealMatrix R = new QRDecompositionImpl(matrix).getR(); RealMatrix R = new QRDecompositionImpl(matrix).getR();
for (int i = 0; i < R.getRowDimension(); i++) for (int i = 0; i < R.getRowDimension(); i++)
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
assertEquals("R lower triangle", R.getEntry(i, j), 0, assertEquals("R lower triangle", R.getEntry(i, j), 0,
entryTolerance); entryTolerance);
matrix = new RealMatrixImpl(testData3x3Singular, false); matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
R = new QRDecompositionImpl(matrix).getR(); R = new QRDecompositionImpl(matrix).getR();
for (int i = 0; i < R.getRowDimension(); i++) for (int i = 0; i < R.getRowDimension(); i++)
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
assertEquals("R lower triangle", R.getEntry(i, j), 0, assertEquals("R lower triangle", R.getEntry(i, j), 0,
entryTolerance); entryTolerance);
matrix = new RealMatrixImpl(testData3x4, false); matrix = MatrixUtils.createRealMatrix(testData3x4);
R = new QRDecompositionImpl(matrix).getR(); R = new QRDecompositionImpl(matrix).getR();
for (int i = 0; i < R.getRowDimension(); i++) for (int i = 0; i < R.getRowDimension(); i++)
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
assertEquals("R lower triangle", R.getEntry(i, j), 0, assertEquals("R lower triangle", R.getEntry(i, j), 0,
entryTolerance); entryTolerance);
matrix = new RealMatrixImpl(testData4x3, false); matrix = MatrixUtils.createRealMatrix(testData4x3);
R = new QRDecompositionImpl(matrix).getR(); R = new QRDecompositionImpl(matrix).getR();
for (int i = 0; i < R.getRowDimension(); i++) for (int i = 0; i < R.getRowDimension(); i++)
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
@ -170,25 +170,25 @@ public class QRDecompositionImplTest extends TestCase {
/** test that H is trapezoidal */ /** test that H is trapezoidal */
public void testHTrapezoidal() { public void testHTrapezoidal() {
RealMatrixImpl matrix = new RealMatrixImpl(testData3x3NonSingular, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular);
RealMatrix H = new QRDecompositionImpl(matrix).getH(); RealMatrix H = new QRDecompositionImpl(matrix).getH();
for (int i = 0; i < H.getRowDimension(); i++) for (int i = 0; i < H.getRowDimension(); i++)
for (int j = i + 1; j < H.getColumnDimension(); j++) for (int j = i + 1; j < H.getColumnDimension(); j++)
assertEquals(H.getEntry(i, j), 0, entryTolerance); assertEquals(H.getEntry(i, j), 0, entryTolerance);
matrix = new RealMatrixImpl(testData3x3Singular, false); matrix = MatrixUtils.createRealMatrix(testData3x3Singular);
H = new QRDecompositionImpl(matrix).getH(); H = new QRDecompositionImpl(matrix).getH();
for (int i = 0; i < H.getRowDimension(); i++) for (int i = 0; i < H.getRowDimension(); i++)
for (int j = i + 1; j < H.getColumnDimension(); j++) for (int j = i + 1; j < H.getColumnDimension(); j++)
assertEquals(H.getEntry(i, j), 0, entryTolerance); assertEquals(H.getEntry(i, j), 0, entryTolerance);
matrix = new RealMatrixImpl(testData3x4, false); matrix = MatrixUtils.createRealMatrix(testData3x4);
H = new QRDecompositionImpl(matrix).getH(); H = new QRDecompositionImpl(matrix).getH();
for (int i = 0; i < H.getRowDimension(); i++) for (int i = 0; i < H.getRowDimension(); i++)
for (int j = i + 1; j < H.getColumnDimension(); j++) for (int j = i + 1; j < H.getColumnDimension(); j++)
assertEquals(H.getEntry(i, j), 0, entryTolerance); assertEquals(H.getEntry(i, j), 0, entryTolerance);
matrix = new RealMatrixImpl(testData4x3, false); matrix = MatrixUtils.createRealMatrix(testData4x3);
H = new QRDecompositionImpl(matrix).getH(); H = new QRDecompositionImpl(matrix).getH();
for (int i = 0; i < H.getRowDimension(); i++) for (int i = 0; i < H.getRowDimension(); i++)
for (int j = i + 1; j < H.getColumnDimension(); j++) for (int j = i + 1; j < H.getColumnDimension(); j++)
@ -199,18 +199,18 @@ public class QRDecompositionImplTest extends TestCase {
/** test matrices values */ /** test matrices values */
public void testMatricesValues() { public void testMatricesValues() {
QRDecomposition qr = QRDecomposition qr =
new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)); new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular));
RealMatrix qRef = new RealMatrixImpl(new double[][] { RealMatrix qRef = MatrixUtils.createRealMatrix(new double[][] {
{ -12.0 / 14.0, 69.0 / 175.0, -58.0 / 175.0 }, { -12.0 / 14.0, 69.0 / 175.0, -58.0 / 175.0 },
{ -6.0 / 14.0, -158.0 / 175.0, 6.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 } { 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 }, { -14.0, -21.0, 14.0 },
{ 0.0, -175.0, 70.0 }, { 0.0, -175.0, 70.0 },
{ 0.0, 0.0, 35.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 }, { 26.0 / 14.0, 0.0, 0.0 },
{ 6.0 / 14.0, 648.0 / 325.0, 0.0 }, { 6.0 / 14.0, 648.0 / 325.0, 0.0 },
{ -4.0 / 14.0, 36.0 / 325.0, 2.0 } { -4.0 / 14.0, 36.0 / 325.0, 2.0 }

View File

@ -56,16 +56,16 @@ public class QRSolverTest extends TestCase {
/** test rank */ /** test rank */
public void testRank() { public void testRank() {
QRSolver solver = QRSolver solver =
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false))); new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
assertTrue(solver.isNonSingular()); assertTrue(solver.isNonSingular());
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false))); solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)));
assertFalse(solver.isNonSingular()); assertFalse(solver.isNonSingular());
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x4, false))); solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x4)));
assertTrue(solver.isNonSingular()); assertTrue(solver.isNonSingular());
solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData4x3, false))); solver = new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData4x3)));
assertTrue(solver.isNonSingular()); assertTrue(solver.isNonSingular());
} }
@ -73,8 +73,8 @@ public class QRSolverTest extends TestCase {
/** test solve dimension errors */ /** test solve dimension errors */
public void testSolveDimensionErrors() { public void testSolveDimensionErrors() {
QRSolver solver = QRSolver solver =
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false))); new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
RealMatrix b = new RealMatrixImpl(new double[2][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -104,8 +104,8 @@ public class QRSolverTest extends TestCase {
/** test solve rank errors */ /** test solve rank errors */
public void testSolveRankErrors() { public void testSolveRankErrors() {
QRSolver solver = QRSolver solver =
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false))); new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)));
RealMatrix b = new RealMatrixImpl(new double[3][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -135,11 +135,11 @@ public class QRSolverTest extends TestCase {
/** test solve */ /** test solve */
public void testSolve() { public void testSolve() {
QRSolver solver = QRSolver solver =
new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false))); new QRSolver(new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)));
RealMatrix b = new RealMatrixImpl(new double[][] { RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
{ -102, 12250 }, { 544, 24500 }, { 167, -36750 } { -102, 12250 }, { 544, 24500 }, { 167, -36750 }
}); });
RealMatrix xRef = new RealMatrixImpl(new double[][] { RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
{ 1, 2515 }, { 2, 422 }, { -3, 898 } { 1, 2515 }, { 2, 422 }, { -3, 898 }
}); });

View File

@ -1060,7 +1060,7 @@ public class RealVectorImplTest extends TestCase {
double dot_2 = v1.dotProduct(v2_t); double dot_2 = v1.dotProduct(v2_t);
assertEquals("compare val ",32d, dot_2); 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)); assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t); RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);

View File

@ -79,7 +79,7 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test dimensions */ /** test dimensions */
public void testDimensions() { public void testDimensions() {
RealMatrixImpl matrix = new RealMatrixImpl(testSquare, false); RealMatrix matrix = MatrixUtils.createRealMatrix(testSquare);
final int m = matrix.getRowDimension(); final int m = matrix.getRowDimension();
final int n = matrix.getColumnDimension(); final int n = matrix.getColumnDimension();
SingularValueDecomposition svd = new SingularValueDecompositionImpl(matrix); SingularValueDecomposition svd = new SingularValueDecompositionImpl(matrix);
@ -94,9 +94,9 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test A = USVt */ /** test A = USVt */
public void testAEqualUSVt() { public void testAEqualUSVt() {
checkAEqualUSVt(new RealMatrixImpl(testSquare, false)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare));
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
checkAEqualUSVt(new RealMatrixImpl(testNonSquare, false).transpose()); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
} }
public void checkAEqualUSVt(final RealMatrix matrix) { public void checkAEqualUSVt(final RealMatrix matrix) {
@ -111,16 +111,16 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test that U is orthogonal */ /** test that U is orthogonal */
public void testUOrthogonal() { public void testUOrthogonal() {
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)).getU()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getU());
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false)).getU()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare)).getU());
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false).transpose()).getU()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU());
} }
/** test that V is orthogonal */ /** test that V is orthogonal */
public void testVOrthogonal() { public void testVOrthogonal() {
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)).getV()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getV());
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false)).getV()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare)).getV());
checkOrthogonal(new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false).transpose()).getV()); checkOrthogonal(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV());
} }
public void checkOrthogonal(final RealMatrix m) { public void checkOrthogonal(final RealMatrix m) {
@ -132,16 +132,16 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test matrices values */ /** test matrices values */
public void testMatricesValues1() { public void testMatricesValues1() {
SingularValueDecomposition svd = SingularValueDecomposition svd =
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)); new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
RealMatrix uRef = new RealMatrixImpl(new double[][] { RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
{ 3.0 / 5.0, -4.0 / 5.0 }, { 3.0 / 5.0, -4.0 / 5.0 },
{ 4.0 / 5.0, 3.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 }, { 3.0, 0.0 },
{ 0.0, 1.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 }, { 4.0 / 5.0, 3.0 / 5.0 },
{ 3.0 / 5.0, -4.0 / 5.0 } { 3.0 / 5.0, -4.0 / 5.0 }
}); });
@ -164,18 +164,18 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test matrices values */ /** test matrices values */
public void testMatricesValues2() { 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 }, { 0.0 / 5.0, 3.0 / 5.0, 0.0 / 5.0 },
{ -4.0 / 5.0, 0.0 / 5.0, -3.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 }, { 0.0 / 5.0, 4.0 / 5.0, 0.0 / 5.0 },
{ -3.0 / 5.0, 0.0 / 5.0, 4.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 }, { 4.0, 0.0, 0.0 },
{ 0.0, 3.0, 0.0 }, { 0.0, 3.0, 0.0 },
{ 0.0, 0.0, 2.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 }, { 80.0 / 125.0, -60.0 / 125.0, 75.0 / 125.0 },
{ 24.0 / 125.0, 107.0 / 125.0, 60.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 } { -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 // check values against known references
SingularValueDecomposition svd = SingularValueDecomposition svd =
new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false)); new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare));
RealMatrix u = svd.getU(); RealMatrix u = svd.getU();
assertEquals(0, u.subtract(uRef).getNorm(), normTolerance); assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
RealMatrix s = svd.getS(); RealMatrix s = svd.getS();
@ -201,7 +201,7 @@ public class SingularValueDecompositionImplTest extends TestCase {
/** test condition number */ /** test condition number */
public void testConditionNumber() { public void testConditionNumber() {
SingularValueDecompositionImpl svd = SingularValueDecompositionImpl svd =
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)); new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
assertEquals(3.0, svd.getConditionNumber(), 1.0e-15); assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
} }

View File

@ -43,8 +43,8 @@ public class SingularValueSolverTest extends TestCase {
/** test solve dimension errors */ /** test solve dimension errors */
public void testSolveDimensionErrors() { public void testSolveDimensionErrors() {
SingularValueSolver solver = SingularValueSolver solver =
new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false))); new SingularValueSolver(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)));
RealMatrix b = new RealMatrixImpl(new double[3][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -74,12 +74,12 @@ public class SingularValueSolverTest extends TestCase {
/** test solve singularity errors */ /** test solve singularity errors */
public void testSolveSingularityErrors() { public void testSolveSingularityErrors() {
RealMatrix m = RealMatrix m =
new RealMatrixImpl(new double[][] { MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 0.0 }, { 1.0, 0.0 },
{ 0.0, 0.0 } { 0.0, 0.0 }
}, false); });
SingularValueSolver solver = new SingularValueSolver(new SingularValueDecompositionImpl(m)); SingularValueSolver solver = new SingularValueSolver(new SingularValueDecompositionImpl(m));
RealMatrix b = new RealMatrixImpl(new double[2][2]); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try { try {
solver.solve(b); solver.solve(b);
fail("an exception should have been thrown"); fail("an exception should have been thrown");
@ -117,11 +117,11 @@ public class SingularValueSolverTest extends TestCase {
/** test solve */ /** test solve */
public void testSolve() { public void testSolve() {
SingularValueSolver solver = SingularValueSolver solver =
new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false))); new SingularValueSolver(new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)));
RealMatrix b = new RealMatrixImpl(new double[][] { RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
{ 1, 2, 3 }, { 0, -5, 1 } { 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 }, { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
{ 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.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 */ /** test condition number */
public void testConditionNumber() { public void testConditionNumber() {
SingularValueDecompositionImpl svd = SingularValueDecompositionImpl svd =
new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)); new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
assertEquals(3.0, svd.getConditionNumber(), 1.0e-15); assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
} }

View File

@ -45,7 +45,7 @@ public class TriDiagonalTransformerTest extends TestCase {
public void testNonSquare() { public void testNonSquare() {
try { 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"); fail("an exception should have been thrown");
} catch (InvalidMatrixException ime) { } catch (InvalidMatrixException ime) {
// expected behavior // expected behavior
@ -55,8 +55,8 @@ public class TriDiagonalTransformerTest extends TestCase {
} }
public void testAEqualQTQt() { public void testAEqualQTQt() {
checkAEqualQTQt(new RealMatrixImpl(testSquare5, false)); checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare5));
checkAEqualQTQt(new RealMatrixImpl(testSquare3, false)); checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare3));
} }
private void checkAEqualQTQt(RealMatrix matrix) { private void checkAEqualQTQt(RealMatrix matrix) {
@ -79,23 +79,23 @@ public class TriDiagonalTransformerTest extends TestCase {
modifiedData[i] = data[i].clone(); modifiedData[i] = data[i].clone();
Arrays.fill(modifiedData[i], 0, i, Double.NaN); Arrays.fill(modifiedData[i], 0, i, Double.NaN);
} }
RealMatrix matrix = new RealMatrixImpl(modifiedData, false); RealMatrix matrix = MatrixUtils.createRealMatrix(modifiedData);
TriDiagonalTransformer transformer = new TriDiagonalTransformer(matrix); TriDiagonalTransformer transformer = new TriDiagonalTransformer(matrix);
RealMatrix q = transformer.getQ(); RealMatrix q = transformer.getQ();
RealMatrix qT = transformer.getQT(); RealMatrix qT = transformer.getQT();
RealMatrix t = transformer.getT(); 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); assertEquals(0, norm, 4.0e-15);
} }
public void testQOrthogonal() { public void testQOrthogonal() {
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getQ()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQ());
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getQ()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQ());
} }
public void testQTOrthogonal() { public void testQTOrthogonal() {
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getQT()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQT());
checkOrthogonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getQT()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQT());
} }
private void checkOrthogonal(RealMatrix m) { private void checkOrthogonal(RealMatrix m) {
@ -105,8 +105,8 @@ public class TriDiagonalTransformerTest extends TestCase {
} }
public void testTTriDiagonal() { public void testTTriDiagonal() {
checkTriDiagonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare5, false)).getT()); checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT());
checkTriDiagonal(new TriDiagonalTransformer(new RealMatrixImpl(testSquare3, false)).getT()); checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT());
} }
private void checkTriDiagonal(RealMatrix m) { private void checkTriDiagonal(RealMatrix m) {
@ -149,11 +149,11 @@ public class TriDiagonalTransformerTest extends TestCase {
double[] mainDiagnonal, double[] mainDiagnonal,
double[] secondaryDiagonal) { double[] secondaryDiagonal) {
TriDiagonalTransformer transformer = TriDiagonalTransformer transformer =
new TriDiagonalTransformer(new RealMatrixImpl(matrix, false)); new TriDiagonalTransformer(MatrixUtils.createRealMatrix(matrix));
// check values against known references // check values against known references
RealMatrix q = transformer.getQ(); 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(); RealMatrix t = transformer.getT();
double[][] tData = new double[mainDiagnonal.length][mainDiagnonal.length]; double[][] tData = new double[mainDiagnonal.length][mainDiagnonal.length];
@ -166,7 +166,7 @@ public class TriDiagonalTransformerTest extends TestCase {
tData[i][i + 1] = secondaryDiagonal[i]; 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 // check the same cached instance is returned the second time
assertTrue(q == transformer.getQ()); assertTrue(q == transformer.getQ());

View File

@ -98,6 +98,7 @@ public class NelderMeadTest
} catch (ConvergenceException ce) { } catch (ConvergenceException ce) {
// expected behavior // expected behavior
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(System.err);
fail("wrong exception caught: " + e.getMessage()); fail("wrong exception caught: " + e.getMessage());
} }

View File

@ -17,14 +17,16 @@
package org.apache.commons.math.random; 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.DimensionMismatchException;
import org.apache.commons.math.linear.MatrixUtils;
import org.apache.commons.math.linear.RealMatrix; 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.VectorialCovariance;
import org.apache.commons.math.stat.descriptive.moment.VectorialMean; import org.apache.commons.math.stat.descriptive.moment.VectorialMean;
import junit.framework.*;
public class CorrelatedRandomVectorGeneratorTest public class CorrelatedRandomVectorGeneratorTest
extends TestCase { extends TestCase {
@ -48,7 +50,7 @@ extends TestCase {
{ 2, 16, 38, -1 }, { 2, 16, 38, -1 },
{ 6, 2, -1, 197 } { 6, 2, -1, 197 }
}; };
RealMatrix covRM = new RealMatrixImpl(cov, false); RealMatrix covRM = MatrixUtils.createRealMatrix(cov);
JDKRandomGenerator jg = new JDKRandomGenerator(); JDKRandomGenerator jg = new JDKRandomGenerator();
jg.setSeed(5322145245211l); jg.setSeed(5322145245211l);
NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg); NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg);
@ -99,24 +101,21 @@ extends TestCase {
try { try {
mean = new double[] { 0.0, 1.0, -3.0, 2.3}; mean = new double[] { 0.0, 1.0, -3.0, 2.3};
RealMatrixImpl b = new RealMatrixImpl(4, 3); RealMatrix b = MatrixUtils.createRealMatrix(4, 3);
double[][] bData = b.getDataRef();
int counter = 0; int counter = 0;
for (int i = 0; i < bData.length; ++i) { for (int i = 0; i < b.getRowDimension(); ++i) {
double[] bi = bData[i];
for (int j = 0; j < b.getColumnDimension(); ++j) { 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()); RealMatrix bbt = b.multiply(b.transpose());
covariance = new RealMatrixImpl(mean.length, mean.length); covariance = MatrixUtils.createRealMatrix(mean.length, mean.length);
double[][] covData = covariance.getDataRef();
for (int i = 0; i < covariance.getRowDimension(); ++i) { 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) { for (int j = 0; j < covariance.getColumnDimension(); ++j) {
double s = bbt.getEntry(i, j); double s = bbt.getEntry(i, j);
covData[i][j] = s; covariance.setEntry(i, j, s);
covData[j][i] = s; covariance.setEntry(j, i, s);
} }
} }
@ -145,7 +144,7 @@ extends TestCase {
} }
private double[] mean; private double[] mean;
private RealMatrixImpl covariance; private RealMatrix covariance;
private CorrelatedRandomVectorGenerator generator; private CorrelatedRandomVectorGenerator generator;
} }

View File

@ -63,7 +63,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
createRegression().newSampleData(y, x, null); createRegression().newSampleData(y, x, null);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=ArrayIndexOutOfBoundsException.class)
public void cannotAddNullCovarianceData() { public void cannotAddNullCovarianceData() {
createRegression().newSampleData(new double[]{}, new double[][]{}, null); createRegression().newSampleData(new double[]{}, new double[][]{}, null);
} }