Initial refactoring of sparse matrix/vector
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@775768 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4d3b3886f9
commit
7a096e9712
|
@ -25,7 +25,7 @@ import org.apache.commons.math.util.OpenIntToDoubleHashMap;
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class SparseRealMatrix extends AbstractRealMatrix {
|
public class OpenMapRealMatrix extends AbstractRealMatrix {
|
||||||
|
|
||||||
/** Serializable version identifier. */
|
/** Serializable version identifier. */
|
||||||
private static final long serialVersionUID = -5962461716457143437L;
|
private static final long serialVersionUID = -5962461716457143437L;
|
||||||
|
@ -44,7 +44,7 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
* @param rowDimension number of rows of the matrix
|
* @param rowDimension number of rows of the matrix
|
||||||
* @param columnDimension number of columns of the matrix
|
* @param columnDimension number of columns of the matrix
|
||||||
*/
|
*/
|
||||||
public SparseRealMatrix(int rowDimension, int columnDimension) {
|
public OpenMapRealMatrix(int rowDimension, int columnDimension) {
|
||||||
super(rowDimension, columnDimension);
|
super(rowDimension, columnDimension);
|
||||||
this.rowDimension = rowDimension;
|
this.rowDimension = rowDimension;
|
||||||
this.columnDimension = columnDimension;
|
this.columnDimension = columnDimension;
|
||||||
|
@ -55,7 +55,7 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
* Build a matrix by copying another one.
|
* Build a matrix by copying another one.
|
||||||
* @param matrix matrix to copy
|
* @param matrix matrix to copy
|
||||||
*/
|
*/
|
||||||
public SparseRealMatrix(SparseRealMatrix matrix) {
|
public OpenMapRealMatrix(OpenMapRealMatrix matrix) {
|
||||||
this.rowDimension = matrix.rowDimension;
|
this.rowDimension = matrix.rowDimension;
|
||||||
this.columnDimension = matrix.columnDimension;
|
this.columnDimension = matrix.columnDimension;
|
||||||
this.entries = new OpenIntToDoubleHashMap(matrix.entries);
|
this.entries = new OpenIntToDoubleHashMap(matrix.entries);
|
||||||
|
@ -64,14 +64,14 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public RealMatrix copy() {
|
public RealMatrix copy() {
|
||||||
return new SparseRealMatrix(this);
|
return new OpenMapRealMatrix(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public RealMatrix createMatrix(int rowDimension, int columnDimension)
|
public RealMatrix createMatrix(int rowDimension, int columnDimension)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
return new SparseRealMatrix(rowDimension, columnDimension);
|
return new OpenMapRealMatrix(rowDimension, columnDimension);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
|
@ -85,7 +85,7 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
public RealMatrix add(final RealMatrix m)
|
public RealMatrix add(final RealMatrix m)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
try {
|
try {
|
||||||
return add((SparseRealMatrix) m);
|
return add((OpenMapRealMatrix) m);
|
||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
return super.add(m);
|
return super.add(m);
|
||||||
}
|
}
|
||||||
|
@ -98,12 +98,12 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
* @return this + m
|
* @return this + m
|
||||||
* @throws IllegalArgumentException if m is not the same size as this
|
* @throws IllegalArgumentException if m is not the same size as this
|
||||||
*/
|
*/
|
||||||
public RealMatrix add(SparseRealMatrix m) throws IllegalArgumentException {
|
public RealMatrix add(OpenMapRealMatrix m) throws IllegalArgumentException {
|
||||||
|
|
||||||
// safety check
|
// safety check
|
||||||
MatrixUtils.checkAdditionCompatible(this, m);
|
MatrixUtils.checkAdditionCompatible(this, m);
|
||||||
|
|
||||||
final RealMatrix out = new SparseRealMatrix(this);
|
final RealMatrix out = new OpenMapRealMatrix(this);
|
||||||
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
|
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
|
||||||
iterator.advance();
|
iterator.advance();
|
||||||
final int row = iterator.key() / columnDimension;
|
final int row = iterator.key() / columnDimension;
|
||||||
|
@ -120,7 +120,7 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
public RealMatrix subtract(final RealMatrix m)
|
public RealMatrix subtract(final RealMatrix m)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
try {
|
try {
|
||||||
return subtract((SparseRealMatrix) m);
|
return subtract((OpenMapRealMatrix) m);
|
||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
return super.add(m);
|
return super.add(m);
|
||||||
}
|
}
|
||||||
|
@ -133,12 +133,12 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
* @return this - m
|
* @return this - m
|
||||||
* @throws IllegalArgumentException if m is not the same size as this
|
* @throws IllegalArgumentException if m is not the same size as this
|
||||||
*/
|
*/
|
||||||
public RealMatrix subtract(SparseRealMatrix m) throws IllegalArgumentException {
|
public RealMatrix subtract(OpenMapRealMatrix m) throws IllegalArgumentException {
|
||||||
|
|
||||||
// safety check
|
// safety check
|
||||||
MatrixUtils.checkAdditionCompatible(this, m);
|
MatrixUtils.checkAdditionCompatible(this, m);
|
||||||
|
|
||||||
final RealMatrix out = new SparseRealMatrix(this);
|
final RealMatrix out = new OpenMapRealMatrix(this);
|
||||||
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
|
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
|
||||||
iterator.advance();
|
iterator.advance();
|
||||||
final int row = iterator.key() / columnDimension;
|
final int row = iterator.key() / columnDimension;
|
||||||
|
@ -155,7 +155,7 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
public RealMatrix multiply(final RealMatrix m)
|
public RealMatrix multiply(final RealMatrix m)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
try {
|
try {
|
||||||
return multiply((SparseRealMatrix) m);
|
return multiply((OpenMapRealMatrix) m);
|
||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
|
|
||||||
// safety check
|
// safety check
|
||||||
|
@ -187,13 +187,13 @@ public class SparseRealMatrix extends AbstractRealMatrix {
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
* if columnDimension(this) != rowDimension(m)
|
* if columnDimension(this) != rowDimension(m)
|
||||||
*/
|
*/
|
||||||
public SparseRealMatrix multiply(SparseRealMatrix m) throws IllegalArgumentException {
|
public OpenMapRealMatrix multiply(OpenMapRealMatrix m) throws IllegalArgumentException {
|
||||||
|
|
||||||
// safety check
|
// safety check
|
||||||
MatrixUtils.checkMultiplicationCompatible(this, m);
|
MatrixUtils.checkMultiplicationCompatible(this, m);
|
||||||
|
|
||||||
final int outCols = m.getColumnDimension();
|
final int outCols = m.getColumnDimension();
|
||||||
SparseRealMatrix out = new SparseRealMatrix(rowDimension, outCols);
|
OpenMapRealMatrix out = new OpenMapRealMatrix(rowDimension, outCols);
|
||||||
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
|
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
|
||||||
iterator.advance();
|
iterator.advance();
|
||||||
final double value = iterator.value();
|
final double value = iterator.value();
|
|
@ -25,7 +25,7 @@ import org.apache.commons.math.util.OpenIntToDoubleHashMap.Iterator;
|
||||||
* @version $Revision: 728186 $ $Date$
|
* @version $Revision: 728186 $ $Date$
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class SparseRealVector implements RealVector {
|
public class OpenMapRealVector implements RealVector {
|
||||||
|
|
||||||
/** Serializable version identifier. */
|
/** Serializable version identifier. */
|
||||||
private static final long serialVersionUID = 8772222695580707260L;
|
private static final long serialVersionUID = 8772222695580707260L;
|
||||||
|
@ -46,12 +46,12 @@ public class SparseRealVector implements RealVector {
|
||||||
* Build a 0-length vector.
|
* Build a 0-length vector.
|
||||||
* <p>Zero-length vectors may be used to initialized construction of vectors
|
* <p>Zero-length vectors may be used to initialized construction of vectors
|
||||||
* by data gathering. We start with zero-length and use either the {@link
|
* by data gathering. We start with zero-length and use either the {@link
|
||||||
* #SparseRealVector(SparseRealVector, int)} constructor
|
* #SparseRealVector(OpenMapRealVector, int)} constructor
|
||||||
* or one of the <code>append</code> method ({@link #append(double)}, {@link
|
* or one of the <code>append</code> method ({@link #append(double)}, {@link
|
||||||
* #append(double[])}, {@link #append(RealVector)}) to gather data
|
* #append(double[])}, {@link #append(RealVector)}) to gather data
|
||||||
* into this vector.</p>
|
* into this vector.</p>
|
||||||
*/
|
*/
|
||||||
public SparseRealVector() {
|
public OpenMapRealVector() {
|
||||||
this(0, DEFAULT_ZERO_TOLERANCE);
|
this(0, DEFAULT_ZERO_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* Construct a (dimension)-length vector of zeros.
|
* Construct a (dimension)-length vector of zeros.
|
||||||
* @param dimension size of the vector
|
* @param dimension size of the vector
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(int dimension) {
|
public OpenMapRealVector(int dimension) {
|
||||||
this(dimension, DEFAULT_ZERO_TOLERANCE);
|
this(dimension, DEFAULT_ZERO_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param dimension Size of the vector
|
* @param dimension Size of the vector
|
||||||
* @param epsilon The tolerance for having a value considered zero
|
* @param epsilon The tolerance for having a value considered zero
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(int dimension, double epsilon) {
|
public OpenMapRealVector(int dimension, double epsilon) {
|
||||||
virtualSize = dimension;
|
virtualSize = dimension;
|
||||||
entries = new OpenIntToDoubleHashMap(0.0);
|
entries = new OpenIntToDoubleHashMap(0.0);
|
||||||
this.epsilon = epsilon;
|
this.epsilon = epsilon;
|
||||||
|
@ -79,7 +79,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param v The original vector
|
* @param v The original vector
|
||||||
* @param resize The amount to resize it
|
* @param resize The amount to resize it
|
||||||
*/
|
*/
|
||||||
protected SparseRealVector(SparseRealVector v, int resize) {
|
protected OpenMapRealVector(OpenMapRealVector v, int resize) {
|
||||||
virtualSize = v.getDimension() + resize;
|
virtualSize = v.getDimension() + resize;
|
||||||
entries = new OpenIntToDoubleHashMap(v.entries);
|
entries = new OpenIntToDoubleHashMap(v.entries);
|
||||||
epsilon = v.getEpsilon();
|
epsilon = v.getEpsilon();
|
||||||
|
@ -90,7 +90,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param dimension The size of the vector
|
* @param dimension The size of the vector
|
||||||
* @param expectedSize The expected number of non-zero entries
|
* @param expectedSize The expected number of non-zero entries
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(int dimension, int expectedSize) {
|
public OpenMapRealVector(int dimension, int expectedSize) {
|
||||||
this(dimension, expectedSize, DEFAULT_ZERO_TOLERANCE);
|
this(dimension, expectedSize, DEFAULT_ZERO_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param expectedSize The expected number of non-zero entries
|
* @param expectedSize The expected number of non-zero entries
|
||||||
* @param epsilon The tolerance for having a value considered zero
|
* @param epsilon The tolerance for having a value considered zero
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(int dimension, int expectedSize, double epsilon) {
|
public OpenMapRealVector(int dimension, int expectedSize, double epsilon) {
|
||||||
virtualSize = dimension;
|
virtualSize = dimension;
|
||||||
entries = new OpenIntToDoubleHashMap(expectedSize, 0.0);
|
entries = new OpenIntToDoubleHashMap(expectedSize, 0.0);
|
||||||
this.epsilon = epsilon;
|
this.epsilon = epsilon;
|
||||||
|
@ -111,7 +111,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* Only non-zero entries will be stored
|
* Only non-zero entries will be stored
|
||||||
* @param values The set of values to create from
|
* @param values The set of values to create from
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(double[] values) {
|
public OpenMapRealVector(double[] values) {
|
||||||
this(values, DEFAULT_ZERO_TOLERANCE);
|
this(values, DEFAULT_ZERO_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param values The set of values to create from
|
* @param values The set of values to create from
|
||||||
* @param epsilon The tolerance for having a value considered zero
|
* @param epsilon The tolerance for having a value considered zero
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(double[] values, double epsilon) {
|
public OpenMapRealVector(double[] values, double epsilon) {
|
||||||
virtualSize = values.length;
|
virtualSize = values.length;
|
||||||
entries = new OpenIntToDoubleHashMap(0.0);
|
entries = new OpenIntToDoubleHashMap(0.0);
|
||||||
this.epsilon = epsilon;
|
this.epsilon = epsilon;
|
||||||
|
@ -138,7 +138,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* Only non-zero entries will be stored
|
* Only non-zero entries will be stored
|
||||||
* @param values The set of values to create from
|
* @param values The set of values to create from
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(Double[] values) {
|
public OpenMapRealVector(Double[] values) {
|
||||||
this(values, DEFAULT_ZERO_TOLERANCE);
|
this(values, DEFAULT_ZERO_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param values The set of values to create from
|
* @param values The set of values to create from
|
||||||
* @param epsilon The tolerance for having a value considered zero
|
* @param epsilon The tolerance for having a value considered zero
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(Double[] values, double epsilon) {
|
public OpenMapRealVector(Double[] values, double epsilon) {
|
||||||
virtualSize = values.length;
|
virtualSize = values.length;
|
||||||
entries = new OpenIntToDoubleHashMap(0.0);
|
entries = new OpenIntToDoubleHashMap(0.0);
|
||||||
this.epsilon = epsilon;
|
this.epsilon = epsilon;
|
||||||
|
@ -164,7 +164,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* Copy constructor.
|
* Copy constructor.
|
||||||
* @param v The instance to copy from
|
* @param v The instance to copy from
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(SparseRealVector v) {
|
public OpenMapRealVector(OpenMapRealVector v) {
|
||||||
virtualSize = v.getDimension();
|
virtualSize = v.getDimension();
|
||||||
entries = new OpenIntToDoubleHashMap(v.getEntries());
|
entries = new OpenIntToDoubleHashMap(v.getEntries());
|
||||||
epsilon = v.getEpsilon();
|
epsilon = v.getEpsilon();
|
||||||
|
@ -174,7 +174,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* Generic copy constructor.
|
* Generic copy constructor.
|
||||||
* @param v The instance to copy from
|
* @param v The instance to copy from
|
||||||
*/
|
*/
|
||||||
public SparseRealVector(RealVector v) {
|
public OpenMapRealVector(RealVector v) {
|
||||||
virtualSize = v.getDimension();
|
virtualSize = v.getDimension();
|
||||||
entries = new OpenIntToDoubleHashMap(0.0);
|
entries = new OpenIntToDoubleHashMap(0.0);
|
||||||
epsilon = DEFAULT_ZERO_TOLERANCE;
|
epsilon = DEFAULT_ZERO_TOLERANCE;
|
||||||
|
@ -222,8 +222,8 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector add(RealVector v) throws IllegalArgumentException {
|
public RealVector add(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return add((SparseRealVector) v);
|
return add((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return add(v.getData());
|
return add(v.getData());
|
||||||
}
|
}
|
||||||
|
@ -234,9 +234,9 @@ public class SparseRealVector implements RealVector {
|
||||||
* @return The sum of <code>this</code> with <code>v</code>
|
* @return The sum of <code>this</code> with <code>v</code>
|
||||||
* @throws IllegalArgumentException If the dimensions don't match
|
* @throws IllegalArgumentException If the dimensions don't match
|
||||||
*/
|
*/
|
||||||
public SparseRealVector add(SparseRealVector v) throws IllegalArgumentException{
|
public OpenMapRealVector add(OpenMapRealVector v) throws IllegalArgumentException{
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
SparseRealVector res = (SparseRealVector)copy();
|
OpenMapRealVector res = (OpenMapRealVector)copy();
|
||||||
Iterator iter = v.getEntries().iterator();
|
Iterator iter = v.getEntries().iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -253,7 +253,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector add(double[] v) throws IllegalArgumentException {
|
public RealVector add(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
SparseRealVector res = new SparseRealVector(getDimension());
|
OpenMapRealVector res = new OpenMapRealVector(getDimension());
|
||||||
for (int i = 0; i < v.length; i++) {
|
for (int i = 0; i < v.length; i++) {
|
||||||
res.setEntry(i, v[i] + getEntry(i));
|
res.setEntry(i, v[i] + getEntry(i));
|
||||||
}
|
}
|
||||||
|
@ -265,8 +265,8 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param v vector to append
|
* @param v vector to append
|
||||||
* @return The result of appending <code>v</code> to self
|
* @return The result of appending <code>v</code> to self
|
||||||
*/
|
*/
|
||||||
public SparseRealVector append(SparseRealVector v) {
|
public OpenMapRealVector append(OpenMapRealVector v) {
|
||||||
SparseRealVector res = new SparseRealVector(this, v.getDimension());
|
OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension());
|
||||||
Iterator iter = v.entries.iterator();
|
Iterator iter = v.entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -277,22 +277,22 @@ public class SparseRealVector implements RealVector {
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector append(RealVector v) {
|
public RealVector append(RealVector v) {
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return append((SparseRealVector) v);
|
return append((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return append(v.getData());
|
return append(v.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector append(double d) {
|
public RealVector append(double d) {
|
||||||
RealVector res = new SparseRealVector(this, 1);
|
RealVector res = new OpenMapRealVector(this, 1);
|
||||||
res.setEntry(virtualSize, d);
|
res.setEntry(virtualSize, d);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector append(double[] a) {
|
public RealVector append(double[] a) {
|
||||||
RealVector res = new SparseRealVector(this, a.length);
|
RealVector res = new OpenMapRealVector(this, a.length);
|
||||||
for (int i = 0; i < a.length; i++) {
|
for (int i = 0; i < a.length; i++) {
|
||||||
res.setEntry(i + virtualSize, a[i]);
|
res.setEntry(i + virtualSize, a[i]);
|
||||||
}
|
}
|
||||||
|
@ -301,7 +301,7 @@ public class SparseRealVector implements RealVector {
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector copy() {
|
public RealVector copy() {
|
||||||
return new SparseRealVector(this);
|
return new OpenMapRealVector(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
|
@ -335,7 +335,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector ebeDivide(RealVector v) throws IllegalArgumentException {
|
public RealVector ebeDivide(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
SparseRealVector res = new SparseRealVector(this);
|
OpenMapRealVector res = new OpenMapRealVector(this);
|
||||||
Iterator iter = res.entries.iterator();
|
Iterator iter = res.entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -347,7 +347,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector ebeDivide(double[] v) throws IllegalArgumentException {
|
public RealVector ebeDivide(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
SparseRealVector res = new SparseRealVector(this);
|
OpenMapRealVector res = new OpenMapRealVector(this);
|
||||||
Iterator iter = res.entries.iterator();
|
Iterator iter = res.entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -359,7 +359,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException {
|
public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
SparseRealVector res = new SparseRealVector(this);
|
OpenMapRealVector res = new OpenMapRealVector(this);
|
||||||
Iterator iter = res.entries.iterator();
|
Iterator iter = res.entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -371,7 +371,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector ebeMultiply(double[] v) throws IllegalArgumentException {
|
public RealVector ebeMultiply(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
SparseRealVector res = new SparseRealVector(this);
|
OpenMapRealVector res = new OpenMapRealVector(this);
|
||||||
Iterator iter = res.entries.iterator();
|
Iterator iter = res.entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -384,7 +384,7 @@ public class SparseRealVector implements RealVector {
|
||||||
public RealVector getSubVector(int index, int n) throws MatrixIndexException {
|
public RealVector getSubVector(int index, int n) throws MatrixIndexException {
|
||||||
checkIndex(index);
|
checkIndex(index);
|
||||||
checkIndex(index + n - 1);
|
checkIndex(index + n - 1);
|
||||||
SparseRealVector res = new SparseRealVector(n);
|
OpenMapRealVector res = new OpenMapRealVector(n);
|
||||||
int end = index + n;
|
int end = index + n;
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
|
@ -419,7 +419,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @return The distance from <code>this</code> and <code>v</code>
|
* @return The distance from <code>this</code> and <code>v</code>
|
||||||
* @throws IllegalArgumentException If the dimensions don't match
|
* @throws IllegalArgumentException If the dimensions don't match
|
||||||
*/
|
*/
|
||||||
public double getDistance(SparseRealVector v) throws IllegalArgumentException {
|
public double getDistance(OpenMapRealVector v) throws IllegalArgumentException {
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
double res = 0;
|
double res = 0;
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
|
@ -444,8 +444,8 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public double getDistance(RealVector v) throws IllegalArgumentException {
|
public double getDistance(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return getDistance((SparseRealVector) v);
|
return getDistance((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return getDistance(v.getData());
|
return getDistance(v.getData());
|
||||||
}
|
}
|
||||||
|
@ -475,7 +475,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param v vector to which distance is requested
|
* @param v vector to which distance is requested
|
||||||
* @return distance between two vectors.
|
* @return distance between two vectors.
|
||||||
*/
|
*/
|
||||||
public double getL1Distance(SparseRealVector v) {
|
public double getL1Distance(OpenMapRealVector v) {
|
||||||
double max = 0;
|
double max = 0;
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
|
@ -498,8 +498,8 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public double getL1Distance(RealVector v) throws IllegalArgumentException {
|
public double getL1Distance(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return getL1Distance((SparseRealVector) v);
|
return getL1Distance((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return getL1Distance(v.getData());
|
return getL1Distance(v.getData());
|
||||||
}
|
}
|
||||||
|
@ -531,7 +531,7 @@ public class SparseRealVector implements RealVector {
|
||||||
* @param v The vector to compute from
|
* @param v The vector to compute from
|
||||||
* @return the LInfDistance
|
* @return the LInfDistance
|
||||||
*/
|
*/
|
||||||
private double getLInfDistance(SparseRealVector v) {
|
private double getLInfDistance(OpenMapRealVector v) {
|
||||||
double max = 0;
|
double max = 0;
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
|
@ -557,8 +557,8 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public double getLInfDistance(RealVector v) throws IllegalArgumentException {
|
public double getLInfDistance(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return getLInfDistance((SparseRealVector) v);
|
return getLInfDistance((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return getLInfDistance(v.getData());
|
return getLInfDistance(v.getData());
|
||||||
}
|
}
|
||||||
|
@ -1032,9 +1032,9 @@ public class SparseRealVector implements RealVector {
|
||||||
* @return The outer product of <code>this</code> and <code>v</code>
|
* @return The outer product of <code>this</code> and <code>v</code>
|
||||||
* @throws IllegalArgumentException If the dimensions don't match
|
* @throws IllegalArgumentException If the dimensions don't match
|
||||||
*/
|
*/
|
||||||
public SparseRealMatrix outerproduct(SparseRealVector v) throws IllegalArgumentException{
|
public OpenMapRealMatrix outerproduct(OpenMapRealVector v) throws IllegalArgumentException{
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
SparseRealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
|
OpenMapRealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -1051,10 +1051,10 @@ public class SparseRealVector implements RealVector {
|
||||||
public RealMatrix outerProduct(RealVector v)
|
public RealMatrix outerProduct(RealVector v)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return outerproduct((SparseRealVector)v);
|
return outerproduct((OpenMapRealVector)v);
|
||||||
}
|
}
|
||||||
RealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
|
RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -1069,7 +1069,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
|
public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
RealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
|
RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
|
||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -1091,7 +1091,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector projection(double[] v) throws IllegalArgumentException {
|
public RealVector projection(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
return projection(new SparseRealVector(v));
|
return projection(new OpenMapRealVector(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
|
@ -1133,9 +1133,9 @@ public class SparseRealVector implements RealVector {
|
||||||
* @return The difference of <code>this</code> and <code>v</code>
|
* @return The difference of <code>this</code> and <code>v</code>
|
||||||
* @throws IllegalArgumentException If the dimensions don't match
|
* @throws IllegalArgumentException If the dimensions don't match
|
||||||
*/
|
*/
|
||||||
public SparseRealVector subtract(SparseRealVector v) throws IllegalArgumentException{
|
public OpenMapRealVector subtract(OpenMapRealVector v) throws IllegalArgumentException{
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
SparseRealVector res = (SparseRealVector)copy();
|
OpenMapRealVector res = (OpenMapRealVector)copy();
|
||||||
Iterator iter = v.getEntries().iterator();
|
Iterator iter = v.getEntries().iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
|
@ -1152,8 +1152,8 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector subtract(RealVector v) throws IllegalArgumentException {
|
public RealVector subtract(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.getDimension());
|
checkVectorDimensions(v.getDimension());
|
||||||
if (v instanceof SparseRealVector) {
|
if (v instanceof OpenMapRealVector) {
|
||||||
return subtract((SparseRealVector) v);
|
return subtract((OpenMapRealVector) v);
|
||||||
}
|
}
|
||||||
return subtract(v.getData());
|
return subtract(v.getData());
|
||||||
}
|
}
|
||||||
|
@ -1161,7 +1161,7 @@ public class SparseRealVector implements RealVector {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealVector subtract(double[] v) throws IllegalArgumentException {
|
public RealVector subtract(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
checkVectorDimensions(v.length);
|
||||||
SparseRealVector res = new SparseRealVector(this);
|
OpenMapRealVector res = new OpenMapRealVector(this);
|
||||||
for (int i = 0; i < v.length; i++) {
|
for (int i = 0; i < v.length; i++) {
|
||||||
if (entries.containsKey(i)) {
|
if (entries.containsKey(i)) {
|
||||||
res.setEntry(i, entries.get(i) - v[i]);
|
res.setEntry(i, entries.get(i) - v[i]);
|
||||||
|
@ -1267,10 +1267,10 @@ public class SparseRealVector implements RealVector {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof SparseRealVector)) {
|
if (!(obj instanceof OpenMapRealVector)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
SparseRealVector other = (SparseRealVector) obj;
|
OpenMapRealVector other = (OpenMapRealVector) obj;
|
||||||
if (virtualSize != other.virtualSize) {
|
if (virtualSize != other.virtualSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
Loading…
Reference in New Issue