From 1fe117edf0a73e0c5476f41e7f74ecc8b20b5ea2 Mon Sep 17 00:00:00 2001
From: Luc Maisonobe
true
if this value is zero, false
otherwise
*/
@@ -207,7 +201,7 @@ public class SparseRealVector implements RealVector {
}
/**
- *
+ * Get the tolerance for having a value considered zero.
* @return The test range for testing if a value is zero
*/
public double getEpsilon() {
@@ -215,7 +209,7 @@ public class SparseRealVector implements RealVector {
}
/**
- *
+ * Set the tolerance for having a value considered zero.
* @param epsilon The test range for testing if a value is zero
*/
public void setEpsilon(double epsilon) {
@@ -225,14 +219,14 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector add(RealVector v) throws IllegalArgumentException {
checkVectorDimensions(v.getDimension());
- if (v instanceof SparseRealVector)
+ if (v instanceof SparseRealVector) {
return add((SparseRealVector) v);
+ }
return add(v.getData());
-
}
/**
- * Optimized method to add two SparseRealVectors
+ * Optimized method to add two SparseRealVectors.
* @param v Vector to add with
* @return The sum of this
with v
* @throws IllegalArgumentException If the dimensions don't match
@@ -264,7 +258,7 @@ public class SparseRealVector implements RealVector {
}
/**
- * Optimized method to append a SparseRealVector
+ * Optimized method to append a SparseRealVector.
* @param v vector to append
* @return The result of appending v
to self
*/
@@ -327,8 +321,9 @@ public class SparseRealVector implements RealVector {
while (iter.hasNext()) {
int idx = iter.key();
double value = 0;
- if (idx < v.length)
+ if (idx < v.length) {
value = v[idx];
+ }
res += value * iter.value();
}
return res;
@@ -385,7 +380,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector getSubVector(int index, int n) throws MatrixIndexException {
checkIndex(index);
- checkIndex(index+n-1);
+ checkIndex(index + n - 1);
SparseRealVector res = new SparseRealVector(n);
int end = index + n;
Iterator iter = entries.iterator();
@@ -416,7 +411,7 @@ public class SparseRealVector implements RealVector {
}
/**
- * Optimized method to compute distance
+ * Optimized method to compute distance.
* @param v The vector to compute distance to
* @return The distance from this
and v
* @throws IllegalArgumentException If the dimensions don't match
@@ -436,7 +431,8 @@ public class SparseRealVector implements RealVector {
iter.advance();
int key = iter.key();
if (!entries.containsKey(key)) {
- res += iter.value() * iter.value();
+ final double value = iter.value();
+ res += value * value;
}
}
return Math.sqrt(res);
@@ -521,7 +517,7 @@ public class SparseRealVector implements RealVector {
}
/**
- * Optimized method to compute LInfDistance
+ * Optimized method to compute LInfDistance.
* @param v The vector to compute from
* @return the LInfDistance
*/
@@ -531,16 +527,18 @@ public class SparseRealVector implements RealVector {
while (iter.hasNext()) {
iter.advance();
double delta = Math.abs(iter.value() - v.getEntry(iter.key()));
- if(delta > max)
+ if (delta > max) {
max = delta;
+ }
}
iter = v.getEntries().iterator();
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (!entries.containsKey(key)) {
- if(iter.value() > max)
+ if (iter.value() > max) {
max = iter.value();
+ }
}
}
return max;
@@ -561,8 +559,9 @@ public class SparseRealVector implements RealVector {
double max = 0;
for (int i = 0; i < v.length; i++) {
double delta = Math.abs(getEntry(i) - v[i]);
- if(delta > max)
+ if (delta > max) {
max = delta;
+ }
}
return max;
}
@@ -594,8 +593,9 @@ public class SparseRealVector implements RealVector {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
- if (Double.isInfinite(iter.value()))
+ if (Double.isInfinite(iter.value())) {
return true;
+ }
}
return false;
}
@@ -605,8 +605,9 @@ public class SparseRealVector implements RealVector {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
- if (Double.isNaN(iter.value()))
+ if (Double.isNaN(iter.value())) {
return true;
+ }
}
return false;
}
@@ -633,7 +634,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector mapAcosToSelf() {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.acos(getEntry(i)));
}
return this;
@@ -719,7 +720,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector mapCosToSelf() {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.cos(getEntry(i)));
}
return this;
@@ -732,7 +733,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector mapCoshToSelf() {
- for(int i = 0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.cosh(getEntry(i)));
}
return this;
@@ -803,7 +804,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector mapInvToSelf() {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, 1.0/getEntry(i));
}
return this;
@@ -821,7 +822,7 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public RealVector mapLog10ToSelf() {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.log10(getEntry(i)));
}
return this;
@@ -841,10 +842,10 @@ public class SparseRealVector implements RealVector {
}
return this;
}
-
+
/** {@inheritDoc} */
public RealVector mapLogToSelf() {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.log(getEntry(i)));
}
return this;
@@ -1011,7 +1012,7 @@ public class SparseRealVector implements RealVector {
}
/**
- * Optimized method to compute the outer product
+ * Optimized method to compute the outer product.
* @param v The vector to comput the outer product on
* @return The outer product of this
and v
* @throws IllegalArgumentException If the dimensions don't match
@@ -1020,30 +1021,30 @@ public class SparseRealVector implements RealVector {
checkVectorDimensions(v.getDimension());
SparseRealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
Iterator iter = entries.iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
iter.advance();
Iterator iter2 = v.getEntries().iterator();
- while(iter2.hasNext()){
+ while (iter2.hasNext()) {
iter2.advance();
res.setEntry(iter.key(), iter2.key(), iter.value()*iter2.value());
}
}
return res;
}
-
+
/** {@inheritDoc} */
public RealMatrix outerProduct(RealVector v)
throws IllegalArgumentException {
checkVectorDimensions(v.getDimension());
- if(v instanceof SparseRealVector){
+ if (v instanceof SparseRealVector) {
return outerproduct((SparseRealVector)v);
}
RealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
Iterator iter = entries.iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
iter.advance();
int row = iter.key();
- for(int col=0; col < virtualSize; col++){
+ for (int col = 0; col < virtualSize; col++) {
res.setEntry(row, col, iter.value()*v.getEntry(col));
}
}
@@ -1106,13 +1107,13 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public void set(double value) {
- for(int i=0; i < virtualSize; i++){
+ for (int i = 0; i < virtualSize; i++) {
setEntry(i, value);
}
}
/**
- * Optimized method to subtract SparseRealVectors
+ * Optimized method to subtract SparseRealVectors.
* @param v The vector to subtract from this
* @return The difference of this
and v
* @throws IllegalArgumentException If the dimensions don't match
@@ -1167,9 +1168,8 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public void unitize() {
double norm = getNorm();
- if(isZero(norm)){
+ if (isZero(norm)) {
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector");
-
}
Iterator iter = entries.iterator();
while (iter.hasNext()) {
@@ -1181,7 +1181,7 @@ public class SparseRealVector implements RealVector {
/**
* Check if an index is valid.
- *
+ *
* @param index
* index to check
* @exception MatrixIndexException
@@ -1197,7 +1197,7 @@ public class SparseRealVector implements RealVector {
/**
* Check if instance dimension is equal to some expected value.
- *
+ *
* @param n
* expected dimension.
* @exception IllegalArgumentException
@@ -1231,31 +1231,38 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
@Override
public boolean equals(Object obj) {
- if (this == obj)
+ if (this == obj) {
return true;
- if (obj == null)
+ }
+ if (obj == null) {
return false;
- if (!(obj instanceof SparseRealVector))
+ }
+ if (!(obj instanceof SparseRealVector)) {
return false;
+ }
SparseRealVector other = (SparseRealVector) obj;
- if (virtualSize != other.virtualSize)
+ if (virtualSize != other.virtualSize) {
return false;
- if (Double.doubleToLongBits(epsilon) != Double
- .doubleToLongBits(other.epsilon))
+ }
+ if (Double.doubleToLongBits(epsilon) !=
+ Double.doubleToLongBits(other.epsilon)) {
return false;
+ }
Iterator iter = entries.iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
iter.advance();
double test = iter.value() - other.getEntry(iter.key());
- if(Math.abs(test) > epsilon)
+ if (Math.abs(test) > epsilon) {
return false;
+ }
}
iter = other.getEntries().iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
iter.advance();
double test = iter.value() - getEntry(iter.key());
- if(!isZero(test))
+ if (!isZero(test)) {
return false;
+ }
}
return true;
}