From 94fcce51cd6c448240f79d62a13a17fe9c45dea3 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Wed, 4 Feb 2009 13:59:50 +0000 Subject: [PATCH] improved consistency between RealVector and RealMatrix API JIRA: MATH-245 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@740744 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/linear/RealVector.java | 30 ++++++----- .../commons/math/linear/RealVectorImpl.java | 10 ++-- .../SingularValueDecompositionImpl.java | 2 +- .../commons/math/linear/SparseRealVector.java | 50 +++++++++---------- .../math/linear/RealVectorImplTest.java | 30 +++++------ .../math/linear/SparseRealVectorTest.java | 32 ++++++------ 6 files changed, 79 insertions(+), 75 deletions(-) diff --git a/src/java/org/apache/commons/math/linear/RealVector.java b/src/java/org/apache/commons/math/linear/RealVector.java index 9c449e326..6cfdd6b0a 100644 --- a/src/java/org/apache/commons/math/linear/RealVector.java +++ b/src/java/org/apache/commons/math/linear/RealVector.java @@ -696,10 +696,22 @@ public interface RealVector { * @param index index location of entry to be fetched * @return vector entry at index * @throws MatrixIndexException if the index is not valid + * @see #setEntry(int, double) */ double getEntry(int index) throws MatrixIndexException; + /** + * Set a single element. + * @param index element index. + * @param value new value for the element. + * @exception MatrixIndexException if the index is + * inconsistent with vector size + * @see #getEntry(int) + */ + void setEntry(int index, double value) + throws MatrixIndexException; + /** * Returns the size of the vector. * @return size @@ -735,17 +747,7 @@ public interface RealVector { * @exception MatrixIndexException if the index is * inconsistent with vector size */ - RealVector get(int index, int n) - throws MatrixIndexException; - - /** - * Set a single element. - * @param index element index. - * @param value new value for the element. - * @exception MatrixIndexException if the index is - * inconsistent with vector size - */ - void set(int index, double value) + RealVector getSubVector(int index, int n) throws MatrixIndexException; /** @@ -754,8 +756,9 @@ public interface RealVector { * @param v vector containing the values to set. * @exception MatrixIndexException if the index is * inconsistent with vector size + * @see #setSubVector(int, double[]) */ - void set(int index, RealVector v) + void setSubVector(int index, RealVector v) throws MatrixIndexException; /** @@ -764,8 +767,9 @@ public interface RealVector { * @param v vector containing the values to set. * @exception MatrixIndexException if the index is * inconsistent with vector size + * @see #setSubVector(int, RealVector) */ - void set(int index, double[] v) + void setSubVector(int index, double[] v) throws MatrixIndexException; /** diff --git a/src/java/org/apache/commons/math/linear/RealVectorImpl.java b/src/java/org/apache/commons/math/linear/RealVectorImpl.java index 640be4549..725f12d29 100644 --- a/src/java/org/apache/commons/math/linear/RealVectorImpl.java +++ b/src/java/org/apache/commons/math/linear/RealVectorImpl.java @@ -1190,7 +1190,7 @@ public class RealVectorImpl implements RealVector, Serializable { } /** {@inheritDoc} */ - public RealVector get(int index, int n) { + public RealVector getSubVector(int index, int n) { RealVectorImpl out = new RealVectorImpl(n); try { System.arraycopy(data, index, out.data, 0, n); @@ -1202,7 +1202,7 @@ public class RealVectorImpl implements RealVector, Serializable { } /** {@inheritDoc} */ - public void set(int index, double value) { + public void setEntry(int index, double value) { try { data[index] = value; } catch (IndexOutOfBoundsException e) { @@ -1211,7 +1211,7 @@ public class RealVectorImpl implements RealVector, Serializable { } /** {@inheritDoc} */ - public void set(int index, RealVector v) { + public void setSubVector(int index, RealVector v) { try { try { set(index, (RealVectorImpl) v); @@ -1227,7 +1227,7 @@ public class RealVectorImpl implements RealVector, Serializable { } /** {@inheritDoc} */ - public void set(int index, double[] v) { + public void setSubVector(int index, double[] v) { try { System.arraycopy(v, 0, data, index, v.length); } catch (IndexOutOfBoundsException e) { @@ -1246,7 +1246,7 @@ public class RealVectorImpl implements RealVector, Serializable { */ public void set(int index, RealVectorImpl v) throws MatrixIndexException { - set(index, v.data); + setSubVector(index, v.data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java b/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java index 2c361b5e0..d3fc7da43 100644 --- a/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java +++ b/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java @@ -372,7 +372,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio if (si == 0) { throw new SingularMatrixException(); } - w.set(i, w.getEntry(i) / si); + w.setEntry(i, w.getEntry(i) / si); } return v.operate(w); diff --git a/src/java/org/apache/commons/math/linear/SparseRealVector.java b/src/java/org/apache/commons/math/linear/SparseRealVector.java index 865b589a9..a2b2d7977 100644 --- a/src/java/org/apache/commons/math/linear/SparseRealVector.java +++ b/src/java/org/apache/commons/math/linear/SparseRealVector.java @@ -238,7 +238,7 @@ public class SparseRealVector implements RealVector { iter.advance(); int key = iter.key(); if (v.getEntries().containsKey(key)) { - res.set(key, iter.value() + v.getEntry(key)); + res.setEntry(key, iter.value() + v.getEntry(key)); } } iter = v.getEntries().iterator(); @@ -246,7 +246,7 @@ public class SparseRealVector implements RealVector { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { - res.set(key, iter.value()); + res.setEntry(key, iter.value()); } } return res; @@ -257,7 +257,7 @@ public class SparseRealVector implements RealVector { checkVectorDimensions(v.length); SparseRealVector res = new SparseRealVector(getDimension()); for (int i = 0; i < v.length; i++) { - res.set(i, v[i] + getEntry(i)); + res.setEntry(i, v[i] + getEntry(i)); } return res; } @@ -272,7 +272,7 @@ public class SparseRealVector implements RealVector { Iterator iter = v.entries.iterator(); while (iter.hasNext()) { iter.advance(); - res.set(iter.key() + virtualSize, iter.value()); + res.setEntry(iter.key() + virtualSize, iter.value()); } return res; } @@ -288,7 +288,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector append(double d) { RealVector res = new SparseRealVector(this, 1); - res.set(virtualSize, d); + res.setEntry(virtualSize, d); return res; } @@ -296,7 +296,7 @@ public class SparseRealVector implements RealVector { public RealVector append(double[] a) { RealVector res = new SparseRealVector(this, a.length); for (int i = 0; i < a.length; i++) { - res.set(i + virtualSize, a[i]); + res.setEntry(i + virtualSize, a[i]); } return res; } @@ -340,7 +340,7 @@ public class SparseRealVector implements RealVector { Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); - res.set(iter.key(), iter.value() / v.getEntry(iter.key())); + res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key())); } return res; } @@ -352,7 +352,7 @@ public class SparseRealVector implements RealVector { Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); - res.set(iter.key(), iter.value() / v[iter.key()]); + res.setEntry(iter.key(), iter.value() / v[iter.key()]); } return null; } @@ -364,7 +364,7 @@ public class SparseRealVector implements RealVector { Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); - res.set(iter.key(), iter.value() * v.getEntry(iter.key())); + res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key())); } return res; } @@ -376,13 +376,13 @@ public class SparseRealVector implements RealVector { Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); - res.set(iter.key(), iter.value() * v[iter.key()]); + res.setEntry(iter.key(), iter.value() * v[iter.key()]); } return res; } /** {@inheritDoc} */ - public RealVector get(int index, int n) throws MatrixIndexException { + public RealVector getSubVector(int index, int n) throws MatrixIndexException { checkIndex(index); checkIndex(index+n-1); SparseRealVector res = new SparseRealVector(n); @@ -392,7 +392,7 @@ public class SparseRealVector implements RealVector { iter.advance(); int key = iter.key(); if (key >= index && key < end) { - res.set(key - index, iter.value()); + res.setEntry(key - index, iter.value()); } } return res; @@ -632,7 +632,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector mapAcosToSelf() { for(int i=0; i < virtualSize; i++){ - set(i, Math.acos(getEntry(i))); + setEntry(i, Math.acos(getEntry(i))); } return this; } @@ -645,7 +645,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector mapAddToSelf(double d) { for (int i = 0; i < virtualSize; i++) { - set(i, getEntry(i) + d); + setEntry(i, getEntry(i) + d); } return this; } @@ -806,7 +806,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector mapInvToSelf() { for(int i=0; i < virtualSize; i++){ - set(i, 1.0/getEntry(i)); + setEntry(i, 1.0/getEntry(i)); } return this; } @@ -824,7 +824,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector mapLog10ToSelf() { for(int i=0; i < virtualSize; i++){ - set(i, Math.log10(getEntry(i))); + setEntry(i, Math.log10(getEntry(i))); } return this; } @@ -847,7 +847,7 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public RealVector mapLogToSelf() { for(int i=0; i < virtualSize; i++){ - set(i, Math.log(getEntry(i))); + setEntry(i, Math.log(getEntry(i))); } return this; } @@ -1080,7 +1080,7 @@ public class SparseRealVector implements RealVector { } /** {@inheritDoc} */ - public void set(int index, double value) throws MatrixIndexException { + public void setEntry(int index, double value) throws MatrixIndexException { checkIndex(index); if (!isZero(value)) { entries.put(index, value); @@ -1090,25 +1090,25 @@ public class SparseRealVector implements RealVector { } /** {@inheritDoc} */ - public void set(int index, RealVector v) throws MatrixIndexException { + public void setSubVector(int index, RealVector v) throws MatrixIndexException { checkIndex(index); checkIndex(index + v.getDimension() - 1); - set(index, v.getData()); + setSubVector(index, v.getData()); } /** {@inheritDoc} */ - public void set(int index, double[] v) throws MatrixIndexException { + public void setSubVector(int index, double[] v) throws MatrixIndexException { checkIndex(index); checkIndex(index + v.length - 1); for (int i = 0; i < v.length; i++) { - set(i + index, v[i]); + setEntry(i + index, v[i]); } } /** {@inheritDoc} */ public void set(double value) { for(int i=0; i < virtualSize; i++){ - set(i, value); + setEntry(i, value); } } @@ -1145,9 +1145,9 @@ public class SparseRealVector implements RealVector { SparseRealVector res = new SparseRealVector(this); for (int i = 0; i < v.length; i++) { if (entries.containsKey(i)) { - res.set(i, entries.get(i) - v[i]); + res.setEntry(i, entries.get(i) - v[i]); } else { - res.set(i, -v[i]); + res.setEntry(i, -v[i]); } } return res; diff --git a/src/test/org/apache/commons/math/linear/RealVectorImplTest.java b/src/test/org/apache/commons/math/linear/RealVectorImplTest.java index 27508dc20..c72b2a64a 100644 --- a/src/test/org/apache/commons/math/linear/RealVectorImplTest.java +++ b/src/test/org/apache/commons/math/linear/RealVectorImplTest.java @@ -424,19 +424,19 @@ public class RealVectorImplTest extends TestCase { throw unsupported(); } - public RealVector get(int index, int n) throws MatrixIndexException { + public RealVector getSubVector(int index, int n) throws MatrixIndexException { throw unsupported(); } - public void set(int index, double value) throws MatrixIndexException { + public void setEntry(int index, double value) throws MatrixIndexException { throw unsupported(); } - public void set(int index, RealVector v) throws MatrixIndexException { + public void setSubVector(int index, RealVector v) throws MatrixIndexException { throw unsupported(); } - public void set(int index, double[] v) throws MatrixIndexException { + public void setSubVector(int index, double[] v) throws MatrixIndexException { throw unsupported(); } @@ -576,11 +576,11 @@ public class RealVectorImplTest extends TestCase { // assertEquals("testData not same object ", v1.data, vout4.data); - RealVector vout5 = v4.get(3, 3); + RealVector vout5 = v4.getSubVector(3, 3); assertEquals("testData len", 3, vout5.getDimension()); assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1)); try { - v4.get(3, 7); + v4.getSubVector(3, 7); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -589,10 +589,10 @@ public class RealVectorImplTest extends TestCase { } RealVectorImpl v_set1 = (RealVectorImpl) v1.copy(); - v_set1.set(1, 11.0); + v_set1.setEntry(1, 11.0); assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1)); try { - v_set1.set(3, 11.0); + v_set1.setEntry(3, 11.0); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -627,11 +627,11 @@ public class RealVectorImplTest extends TestCase { } RealVectorImpl v_set4 = (RealVectorImpl) v4.copy(); - v_set4.set(3, v2_t); + v_set4.setSubVector(3, v2_t); assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3)); assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6)); try { - v_set4.set(7, v2_t); + v_set4.setSubVector(7, v2_t); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -643,7 +643,7 @@ public class RealVectorImplTest extends TestCase { RealVectorImpl vout10 = (RealVectorImpl) v1.copy(); RealVectorImpl vout10_2 = (RealVectorImpl) v1.copy(); assertEquals(vout10, vout10_2); - vout10_2.set(0, 1.1); + vout10_2.setEntry(0, 1.1); assertNotSame(vout10, vout10_2); } @@ -1147,16 +1147,16 @@ public class RealVectorImplTest extends TestCase { RealVectorImpl v = new RealVectorImpl(new double[] { 0, 1, 2 }); assertFalse(v.isNaN()); - v.set(1, Double.NaN); + v.setEntry(1, Double.NaN); assertTrue(v.isNaN()); assertFalse(v.isInfinite()); - v.set(0, Double.POSITIVE_INFINITY); + v.setEntry(0, Double.POSITIVE_INFINITY); assertFalse(v.isInfinite()); - v.set(1, 1); + v.setEntry(1, 1); assertTrue(v.isInfinite()); - v.set(0, 0); + v.setEntry(0, 0); assertEquals(v, new RealVectorImpl(new double[] { 0, 1, 2 })); assertNotSame(v, new RealVectorImpl(new double[] { 0, 1, 2 + Math.ulp(2)})); assertNotSame(v, new RealVectorImpl(new double[] { 0, 1, 2, 3 })); diff --git a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java index 2ab1227d0..5be3d5b55 100644 --- a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java +++ b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java @@ -424,19 +424,19 @@ public class SparseRealVectorTest extends TestCase { throw unsupported(); } - public RealVector get(int index, int n) throws MatrixIndexException { + public RealVector getSubVector(int index, int n) throws MatrixIndexException { throw unsupported(); } - public void set(int index, double value) throws MatrixIndexException { + public void setEntry(int index, double value) throws MatrixIndexException { throw unsupported(); } - public void set(int index, RealVector v) throws MatrixIndexException { + public void setSubVector(int index, RealVector v) throws MatrixIndexException { throw unsupported(); } - public void set(int index, double[] v) throws MatrixIndexException { + public void setSubVector(int index, double[] v) throws MatrixIndexException { throw unsupported(); } @@ -580,11 +580,11 @@ public class SparseRealVectorTest extends TestCase { // assertEquals("testData not same object ", v1.data, vout4.data); - RealVector vout5 = v4.get(3, 3); + RealVector vout5 = v4.getSubVector(3, 3); assertEquals("testData len", 3, vout5.getDimension()); assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1)); try { - v4.get(3, 7); + v4.getSubVector(3, 7); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -593,10 +593,10 @@ public class SparseRealVectorTest extends TestCase { } SparseRealVector v_set1 = (SparseRealVector) v1.copy(); - v_set1.set(1, 11.0); + v_set1.setEntry(1, 11.0); assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1)); try { - v_set1.set(3, 11.0); + v_set1.setEntry(3, 11.0); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -605,11 +605,11 @@ public class SparseRealVectorTest extends TestCase { } SparseRealVector v_set2 = (SparseRealVector) v4.copy(); - v_set2.set(3, v1); + v_set2.setSubVector(3, v1); assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3)); assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6)); try { - v_set2.set(7, v1); + v_set2.setSubVector(7, v1); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -631,11 +631,11 @@ public class SparseRealVectorTest extends TestCase { } SparseRealVector v_set4 = (SparseRealVector) v4.copy(); - v_set4.set(3, v2_t); + v_set4.setSubVector(3, v2_t); assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3)); assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6)); try { - v_set4.set(7, v2_t); + v_set4.setSubVector(7, v2_t); fail("MatrixIndexException expected"); } catch (MatrixIndexException ex) { // expected behavior @@ -1154,17 +1154,17 @@ public class SparseRealVectorTest extends TestCase { SparseRealVector v = new SparseRealVector(new double[] { 0, 1, 2 }); assertFalse(v.isNaN()); - v.set(1, Double.NaN); + v.setEntry(1, Double.NaN); assertTrue(v.isNaN()); assertFalse(v.isInfinite()); - v.set(0, Double.POSITIVE_INFINITY); + v.setEntry(0, Double.POSITIVE_INFINITY); // TODO: fixme //assertFalse(v.isInfinite()); - v.set(1, 1); + v.setEntry(1, 1); assertTrue(v.isInfinite()); - v.set(0, 0); + v.setEntry(0, 0); // TODO: backing store doesn't yet implement equals //assertEquals(v, new SparseRealVector(new double[] { 0, 1, 2 })); //assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));