Adding constructors that allow specifing epsilon.

Remove the isZero(int) method, since it is inconsistant with the isZero(double) method, and this class is tightly bound to it's backing store.
Some javadoc fixes.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@739667 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
William Barker 2009-02-01 00:28:07 +00:00
parent d957f434e4
commit e8bb7ce937
2 changed files with 61 additions and 15 deletions

View File

@ -46,7 +46,7 @@ public class SparseRealVector implements RealVector {
}
/**
* Construct a (size)-length vector of zeros.
* Construct a (dimension)-length vector of zeros.
* @param dimension size of the vector
*/
public SparseRealVector(int dimension) {
@ -54,6 +54,17 @@ public class SparseRealVector implements RealVector {
entries = new OpenIntToDoubleHashMap(0.0);
}
/**
* Construct a (dimension)-length vector of zeros, specifying zero tolerance
* @param dimension Size of the vector
* @param epsilon The tolerance for having a value considered zero
*/
public SparseRealVector(int dimension, double epsilon){
virtualSize = dimension;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
}
/**
* Resize the vector, for use with append
* @param v The original vector
@ -67,16 +78,28 @@ public class SparseRealVector implements RealVector {
/**
* For advanced use, when you know the sparseness
* @param dimension The size of the vector
* @param expectedSize The excpected number of non-zer entries
* @param expectedSize The excpected number of non-zero entries
*/
public SparseRealVector(int dimension, int expectedSize) {
entries = new OpenIntToDoubleHashMap(expectedSize, 0.0);
virtualSize = dimension;
}
/**
* For advanced use, when you know the sparseness and want to specify zero tolerance
* @param dimension The size of the vector
* @param expectedSize The expected number of non-zero entries
* @param epsilon The tolerance for having a value considered zero
*/
public SparseRealVector(int dimension, int expectedSize, double epsilon){
virtualSize = dimension;
entries = new OpenIntToDoubleHashMap(expectedSize, 0.0);
this.epsilon = epsilon;
}
/**
* Create from a double array.
* only non-zero entries will be stored
* Only non-zero entries will be stored
* @param values The set of values to create from
*/
public SparseRealVector(double[] values) {
@ -84,6 +107,18 @@ public class SparseRealVector implements RealVector {
fromDoubleArray(values);
}
/**
* Create from a double array, specifying zero tolerance.
* Only non-zero entries will be stored
* @param values The set of values to create from
* @param epsilon The tolerance for having a value considered zero
*/
public SparseRealVector(double [] values, double epsilon){
virtualSize = values.length;
this.epsilon = epsilon;
fromDoubleArray(values);
}
/**
* Create from a Double array.
* Only non-zero entries will be stored
@ -93,7 +128,23 @@ public class SparseRealVector implements RealVector {
virtualSize = values.length;
double[] vals = new double[values.length];
for(int i=0; i < values.length; i++){
vals[i] = values[i];
vals[i] = values[i].doubleValue();
}
fromDoubleArray(vals);
}
/**
* Create from a Double array.
* Only non-zero entries will be stored
* @param values The set of values to create from
* @param epsilon The tolerance for having a value considered zero
*/
public SparseRealVector(Double [] values, double epsilon){
virtualSize = values.length;
this.epsilon = epsilon;
double[] vals = new double[values.length];
for(int i=0; i < values.length; i++){
vals[i] = values[i].doubleValue();
}
fromDoubleArray(vals);
}
@ -140,15 +191,7 @@ public class SparseRealVector implements RealVector {
return entries;
}
/**
* Determine if this index value is zero
* @param key The index to text
* @return <code>true</code> if this index is missing from the map, <code>false</code> otherwise
*/
protected boolean isZero(int key) {
return !entries.containsKey(key);
}
/**
* Determine if this value is zero
* @param value The value to test
@ -1047,7 +1090,7 @@ public class SparseRealVector implements RealVector {
checkIndex(index);
if (!isZero(value)) {
entries.put(index, value);
} else if (!isZero(index)) {
} else if (entries.containsKey(index)) {
entries.remove(index);
}
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -294,6 +294,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="brentworden" type="fix" issue="MATH-204" due-to="Mick">
Added root checks for the endpoints.
</action>
<action dev="billbarker" type="add">
Added a SparseRealVector class that implements a sparse vector for the RealVector interface.
</action>
</release>
<release version="1.2" date="2008-02-24"
description="This release combines bug fixes and new features. Most notable