Mostly completing the methods that don't make sense if using a sparse vector in the first place. Initial implementations for equals on the backing store, but it seems it will need more work (since still have to comment out junit tests).
Making checkVectorDimensions protected instead of public on all RealVector classes. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@742257 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d22f9559e1
commit
7d4f6893f9
|
@ -1270,7 +1270,7 @@ public class RealVectorImpl implements RealVector, Serializable {
|
|||
* @exception IllegalArgumentException if the vectors do not
|
||||
* have the same dimension
|
||||
*/
|
||||
public void checkVectorDimensions(RealVector v)
|
||||
protected void checkVectorDimensions(RealVector v)
|
||||
throws IllegalArgumentException {
|
||||
checkVectorDimensions(v.getDimension());
|
||||
}
|
||||
|
@ -1282,7 +1282,7 @@ public class RealVectorImpl implements RealVector, Serializable {
|
|||
* @exception IllegalArgumentException if the dimension is
|
||||
* inconsistent with vector size
|
||||
*/
|
||||
public void checkVectorDimensions(int n)
|
||||
protected void checkVectorDimensions(int n)
|
||||
throws IllegalArgumentException {
|
||||
if (data.length != n) {
|
||||
throw new IllegalArgumentException("vector dimension is " + data.length +
|
||||
|
|
|
@ -232,6 +232,7 @@ public class SparseRealVector implements RealVector {
|
|||
* @return The sum of <code>this</code> with <code>v</code>
|
||||
*/
|
||||
public SparseRealVector add(SparseRealVector v) {
|
||||
checkVectorDimensions(v.getDimension());
|
||||
SparseRealVector res = (SparseRealVector) copy();
|
||||
Iterator iter = res.getEntries().iterator();
|
||||
while (iter.hasNext()) {
|
||||
|
@ -717,10 +718,8 @@ public class SparseRealVector implements RealVector {
|
|||
|
||||
/** {@inheritDoc} */
|
||||
public RealVector mapCosToSelf() {
|
||||
Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
entries.put(iter.key(), Math.cos(iter.value()));
|
||||
for(int i=0; i < virtualSize; i++){
|
||||
setEntry(i, Math.cos(getEntry(i)));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
@ -732,10 +731,8 @@ public class SparseRealVector implements RealVector {
|
|||
|
||||
/** {@inheritDoc} */
|
||||
public RealVector mapCoshToSelf() {
|
||||
Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
entries.put(iter.key(), Math.cosh(iter.value()));
|
||||
for(int i = 0; i < virtualSize; i++){
|
||||
setEntry(i, Math.cosh(getEntry(i)));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
@ -1201,7 +1198,7 @@ public class SparseRealVector implements RealVector {
|
|||
* @exception IllegalArgumentException
|
||||
* if the dimension is inconsistent with vector size
|
||||
*/
|
||||
public void checkVectorDimensions(int n) throws IllegalArgumentException {
|
||||
protected void checkVectorDimensions(int n) throws IllegalArgumentException {
|
||||
if (getDimension() != n) {
|
||||
throw new IllegalArgumentException("vector dimension is "
|
||||
+ getDimension() + ", not " + n + " as expected");
|
||||
|
@ -1212,4 +1209,47 @@ public class SparseRealVector implements RealVector {
|
|||
public double[] toArray() {
|
||||
return getData();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((entries == null) ? 0 : entries.hashCode());
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(epsilon);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + virtualSize;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
System.out.println("Checking equality of "+obj);
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof SparseRealVector))
|
||||
return false;
|
||||
System.out.println("is a sparse vector");
|
||||
SparseRealVector other = (SparseRealVector) obj;
|
||||
if (entries == null) {
|
||||
if (other.entries != null)
|
||||
return false;
|
||||
} else if (!entries.equals(other.entries)){
|
||||
System.out.println("no entries match");
|
||||
return false;
|
||||
}if (Double.doubleToLongBits(epsilon) != Double
|
||||
.doubleToLongBits(other.epsilon))
|
||||
return false;
|
||||
if (virtualSize != other.virtualSize)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.apache.commons.math.util;
|
|||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
@ -594,4 +595,61 @@ public class OpenIntToDoubleHashMap implements Serializable {
|
|||
count = 0;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + Arrays.hashCode(keys);
|
||||
result = prime * result + mask;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(missingEntries);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + size;
|
||||
result = prime * result + Arrays.hashCode(states);
|
||||
result = prime * result + Arrays.hashCode(values);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
OpenIntToDoubleHashMap other = (OpenIntToDoubleHashMap) obj;
|
||||
if (!Arrays.equals(keys, other.keys))
|
||||
return false;
|
||||
if (mask != other.mask)
|
||||
return false;
|
||||
if (Double.doubleToLongBits(missingEntries) != Double
|
||||
.doubleToLongBits(other.missingEntries))
|
||||
return false;
|
||||
if (size != other.size)
|
||||
return false;
|
||||
if (!Arrays.equals(states, other.states)){
|
||||
System.out.println("states not match:" );
|
||||
for(byte e : states){
|
||||
System.out.print(e+" ");
|
||||
}
|
||||
System.out.println();
|
||||
for(byte e : other.states){
|
||||
System.out.print(e+" ");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!Arrays.equals(values, other.values)){
|
||||
System.out.println("values don't match");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -473,11 +473,6 @@ public class SparseRealVectorTest extends TestCase {
|
|||
assertEquals("testData len", 7, v1.getDimension());
|
||||
assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
|
||||
|
||||
/* TODO: make this supported */
|
||||
//SparseRealVector v2 = new SparseRealVector(5, 1.23);
|
||||
//assertEquals("testData len", 5, v2.getDimension());
|
||||
//assertEquals("testData is 1.23 ", 1.23, v2.getEntry(4));
|
||||
|
||||
SparseRealVector v3 = new SparseRealVector(vec1);
|
||||
assertEquals("testData len", 3, v3.getDimension());
|
||||
assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
|
||||
|
@ -560,25 +555,9 @@ public class SparseRealVectorTest extends TestCase {
|
|||
assertEquals("testData len", 6, v_append_3.getDimension());
|
||||
assertEquals("testData is ", 4.0, v_append_3.getEntry(3));
|
||||
|
||||
/* TODO: fixme */
|
||||
//RealVector v_append_4 = v1.append(v2_t);
|
||||
//assertEquals("testData len", 6, v_append_4.getDimension());
|
||||
//assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
|
||||
|
||||
/* TODO: fixme */
|
||||
//RealVector v_copy = v1.copy();
|
||||
//assertEquals("testData len", 3, v_copy.getDimension());
|
||||
//assertNotSame("testData not same object ", v1.data, v_copy.getData());
|
||||
/* TODO: fixme */
|
||||
//double[] a_double = v1.toArray();
|
||||
//assertEquals("testData len", 3, a_double.length);
|
||||
//assertNotSame("testData not same object ", v1.data, a_double);
|
||||
|
||||
|
||||
// SparseRealVector vout4 = (SparseRealVector) v1.clone();
|
||||
// assertEquals("testData len", 3, vout4.getDimension());
|
||||
// assertEquals("testData not same object ", v1.data, vout4.data);
|
||||
|
||||
RealVector v_append_4 = v1.append(v2_t);
|
||||
assertEquals("testData len", 6, v_append_4.getDimension());
|
||||
assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
|
||||
|
||||
RealVector vout5 = v4.getSubVector(3, 3);
|
||||
assertEquals("testData len", 3, vout5.getDimension());
|
||||
|
@ -1127,25 +1106,6 @@ public class SparseRealVectorTest extends TestCase {
|
|||
fail("wrong exception caught");
|
||||
}
|
||||
|
||||
/* TODO: fixme */
|
||||
//try {
|
||||
// v1.checkVectorDimensions(v4);
|
||||
// fail("IllegalArgumentException expected");
|
||||
//} catch (IllegalArgumentException ex) {
|
||||
// expected behavior
|
||||
//} catch (Exception e) {
|
||||
// fail("wrong exception caught");
|
||||
//}
|
||||
|
||||
/* TODO: fixme */
|
||||
//try {
|
||||
// v1.checkVectorDimensions(v4_2);
|
||||
// fail("IllegalArgumentException expected");
|
||||
//} catch (IllegalArgumentException ex) {
|
||||
// expected behavior
|
||||
// } catch (Exception e) {
|
||||
// fail("wrong exception caught");
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1159,16 +1119,16 @@ public class SparseRealVectorTest extends TestCase {
|
|||
|
||||
assertFalse(v.isInfinite());
|
||||
v.setEntry(0, Double.POSITIVE_INFINITY);
|
||||
// TODO: fixme
|
||||
// TODO: why is this test here
|
||||
//assertFalse(v.isInfinite());
|
||||
v.setEntry(1, 1);
|
||||
assertTrue(v.isInfinite());
|
||||
|
||||
v.setEntry(0, 0);
|
||||
// TODO: backing store doesn't yet implement equals
|
||||
//TODO: differeciate from resetting to zero
|
||||
//v.setEntry(0, 0);
|
||||
//assertEquals(v, new SparseRealVector(new double[] { 0, 1, 2 }));
|
||||
//assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
|
||||
//assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 }));
|
||||
assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
|
||||
assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 }));
|
||||
|
||||
//assertEquals(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(),
|
||||
// new SparseRealVector(new double[] { 0, Double.NaN, 2 }).hashCode());
|
||||
|
|
Loading…
Reference in New Issue