mirror of
https://github.com/apache/commons-math.git
synced 2025-02-09 03:25:31 +00:00
MATH-795: moved minimal implementation of RealVector, ArrayRealVectorTest.RealVectorTestImpl to RealVectorAbstractTest.RealVectorTestImpl.
This minimal implementation is now used by RealVectorTest and ArrayRealVectorTest. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1358334 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
092c0972ba
commit
53fb7e8704
@ -30,105 +30,6 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class ArrayRealVectorTest extends RealVectorAbstractTest {
|
public class ArrayRealVectorTest extends RealVectorAbstractTest {
|
||||||
|
|
||||||
/**
|
|
||||||
* Minimal implementation of the {@link RealVector} abstract class, for
|
|
||||||
* mixed types unit tests.
|
|
||||||
*/
|
|
||||||
public static class RealVectorTestImpl extends RealVector
|
|
||||||
implements Serializable {
|
|
||||||
|
|
||||||
/** Serializable version identifier. */
|
|
||||||
private static final long serialVersionUID = 20120706L;
|
|
||||||
|
|
||||||
/** Entries of the vector. */
|
|
||||||
protected double data[];
|
|
||||||
|
|
||||||
public RealVectorTestImpl(double[] d) {
|
|
||||||
data = d.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
private UnsupportedOperationException unsupported() {
|
|
||||||
return new UnsupportedOperationException("Not supported, unneeded for test purposes");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector mapToSelf(UnivariateFunction function) {
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
data[i] = function.value(data[i]);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector copy() {
|
|
||||||
return new RealVectorTestImpl(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector ebeMultiply(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector ebeDivide(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getEntry(int index) {
|
|
||||||
return data[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getDimension() {
|
|
||||||
return data.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector append(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector append(double d) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector getSubVector(int index, int n) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setEntry(int index, double value) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setSubVector(int index, RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(double value) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double[] toArray() {
|
|
||||||
return data.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isNaN() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isInfinite() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RealVector create(final double[] data) {
|
public RealVector create(final double[] data) {
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.commons.math3.linear;
|
package org.apache.commons.math3.linear;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
@ -1903,4 +1904,101 @@ public abstract class RealVectorAbstractTest {
|
|||||||
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimal implementation of the {@link RealVector} abstract class, for
|
||||||
|
* mixed types unit tests.
|
||||||
|
*/
|
||||||
|
public static class RealVectorTestImpl extends RealVector
|
||||||
|
implements Serializable {
|
||||||
|
|
||||||
|
/** Serializable version identifier. */
|
||||||
|
private static final long serialVersionUID = 20120706L;
|
||||||
|
|
||||||
|
/** Entries of the vector. */
|
||||||
|
protected double data[];
|
||||||
|
|
||||||
|
public RealVectorTestImpl(double[] d) {
|
||||||
|
data = d.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
private UnsupportedOperationException unsupported() {
|
||||||
|
return new UnsupportedOperationException("Not supported, unneeded for test purposes");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector mapToSelf(UnivariateFunction function) {
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
data[i] = function.value(data[i]);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector copy() {
|
||||||
|
return new RealVectorTestImpl(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector ebeMultiply(RealVector v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector ebeDivide(RealVector v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getEntry(int index) {
|
||||||
|
checkIndex(index);
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getDimension() {
|
||||||
|
return data.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector append(RealVector v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector append(double d) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RealVector getSubVector(int index, int n) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEntry(int index, double value) {
|
||||||
|
checkIndex(index);
|
||||||
|
data[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSubVector(int index, RealVector v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double[] toArray() {
|
||||||
|
return data.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNaN() {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInfinite() {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,19 +17,15 @@
|
|||||||
|
|
||||||
package org.apache.commons.math3.linear;
|
package org.apache.commons.math3.linear;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.apache.commons.math3.analysis.UnivariateFunction;
|
|
||||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
|
||||||
import org.apache.commons.math3.exception.OutOfRangeException;
|
|
||||||
import org.apache.commons.math3.exception.util.LocalizedFormats;
|
|
||||||
import org.apache.commons.math3.linear.RealVector.Entry;
|
|
||||||
import org.apache.commons.math3.util.MathArrays;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||||
|
import org.apache.commons.math3.linear.RealVector.Entry;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link RealVector}.
|
* Tests for {@link RealVector}.
|
||||||
*/
|
*/
|
||||||
@ -37,148 +33,14 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
private double[] vec1 = { 1d, 2d, 3d, 4d, 5d };
|
private double[] vec1 = { 1d, 2d, 3d, 4d, 5d };
|
||||||
private double[] vec2 = { -3d, 0d, 0d, 2d, 1d };
|
private double[] vec2 = { -3d, 0d, 0d, 2d, 1d };
|
||||||
|
|
||||||
private static class TestVectorImpl extends RealVector {
|
|
||||||
private double[] values;
|
|
||||||
|
|
||||||
TestVectorImpl(double[] values) {
|
|
||||||
this.values = MathArrays.copyOf(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double[] toArray() { return values; }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector copy() {
|
|
||||||
return new TestVectorImpl(values.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
UnsupportedOperationException unsupported() {
|
|
||||||
return new UnsupportedOperationException("Test implementation only supports methods necessary for testing");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector mapAddToSelf(double d) {
|
|
||||||
for(int i=0; i<values.length; i++) {
|
|
||||||
values[i] += d;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector mapSubtractToSelf(double d) {
|
|
||||||
for(int i=0; i<values.length; i++) {
|
|
||||||
values[i] -= d;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector mapMultiplyToSelf(double d) {
|
|
||||||
for(int i=0; i<values.length; i++) {
|
|
||||||
values[i] *= d;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector mapDivideToSelf(double d) {
|
|
||||||
for(int i=0; i<values.length; i++) {
|
|
||||||
values[i] /= d;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector ebeMultiply(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector ebeDivide(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getL1Norm() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getLInfNorm() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getEntry(int index) {
|
|
||||||
try {
|
|
||||||
return values[index];
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0,
|
|
||||||
getDimension() - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setEntry(int index, double value) {
|
|
||||||
try {
|
|
||||||
values[index] = value;
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0,
|
|
||||||
getDimension() - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getDimension() {
|
|
||||||
return values.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector append(RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector append(double d) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
public RealVector append(double[] a) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RealVector getSubVector(int index, int n) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubVector(int index, double[] v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void setSubVector(int index, RealVector v) {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isNaN() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isInfinite() {
|
|
||||||
throw unsupported();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RealVector create(final double[] data) {
|
public RealVector create(final double[] data) {
|
||||||
return new TestVectorImpl(data);
|
return new RealVectorTestImpl(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RealVector createAlien(double[] data) {
|
public RealVector createAlien(double[] data) {
|
||||||
return new TestVectorImpl(data);
|
return new RealVectorTestImpl(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -337,7 +199,7 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSparseIterator() throws Exception {
|
public void testSparseIterator() throws Exception {
|
||||||
RealVector v = new TestVectorImpl(vec2.clone());
|
RealVector v = new RealVectorTestImpl(vec2.clone());
|
||||||
Entry e;
|
Entry e;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
double[] nonDefaultV2 = { -3d, 2d, 1d };
|
double[] nonDefaultV2 = { -3d, 2d, 1d };
|
||||||
@ -345,7 +207,7 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
Assert.assertEquals(nonDefaultV2[i], e.getValue(), 0);
|
Assert.assertEquals(nonDefaultV2[i], e.getValue(), 0);
|
||||||
}
|
}
|
||||||
double [] onlyOne = {0d, 1.0, 0d};
|
double [] onlyOne = {0d, 1.0, 0d};
|
||||||
v = new TestVectorImpl(onlyOne);
|
v = new RealVectorTestImpl(onlyOne);
|
||||||
for(Iterator<Entry> it = v.sparseIterator(); it.hasNext() && (e = it.next()) != null; ) {
|
for(Iterator<Entry> it = v.sparseIterator(); it.hasNext() && (e = it.next()) != null; ) {
|
||||||
Assert.assertEquals(onlyOne[1], e.getValue(), 0);
|
Assert.assertEquals(onlyOne[1], e.getValue(), 0);
|
||||||
}
|
}
|
||||||
@ -370,9 +232,9 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
final double a = 1d;
|
final double a = 1d;
|
||||||
final double b = 2d;
|
final double b = 2d;
|
||||||
double[] aux = new double[] { 3d, 4d, 5d };
|
double[] aux = new double[] { 3d, 4d, 5d };
|
||||||
final TestVectorImpl x = new TestVectorImpl(aux);
|
final RealVector x = new RealVectorTestImpl(aux);
|
||||||
aux = new double[] { 6d, 7d };
|
aux = new double[] { 6d, 7d };
|
||||||
final TestVectorImpl y = new TestVectorImpl(aux);
|
final RealVector y = new RealVectorTestImpl(aux);
|
||||||
x.combineToSelf(a, b, y);
|
x.combineToSelf(a, b, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,8 +244,8 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
final int dim = 10;
|
final int dim = 10;
|
||||||
final double a = (2 * random.nextDouble() - 1);
|
final double a = (2 * random.nextDouble() - 1);
|
||||||
final double b = (2 * random.nextDouble() - 1);
|
final double b = (2 * random.nextDouble() - 1);
|
||||||
final RealVector x = new TestVectorImpl(new double[dim]);
|
final RealVector x = new RealVectorTestImpl(new double[dim]);
|
||||||
final RealVector y = new TestVectorImpl(new double[dim]);
|
final RealVector y = new RealVectorTestImpl(new double[dim]);
|
||||||
final double[] expected = new double[dim];
|
final double[] expected = new double[dim];
|
||||||
for (int i = 0; i < dim; i++) {
|
for (int i = 0; i < dim; i++) {
|
||||||
final double xi = 2 * random.nextDouble() - 1;
|
final double xi = 2 * random.nextDouble() - 1;
|
||||||
@ -410,7 +272,7 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
|||||||
@Test
|
@Test
|
||||||
public void testAddToEntry() {
|
public void testAddToEntry() {
|
||||||
final double[] v = new double[] { 1, 2, 3 };
|
final double[] v = new double[] { 1, 2, 3 };
|
||||||
final RealVector x = new TestVectorImpl(v.clone());
|
final RealVector x = new RealVectorTestImpl(v.clone());
|
||||||
final double inc = 7;
|
final double inc = 7;
|
||||||
for (int i = 0; i < x.getDimension(); i++) {
|
for (int i = 0; i < x.getDimension(); i++) {
|
||||||
x.addToEntry(i, inc);
|
x.addToEntry(i, inc);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user