[MATH-666] make FieldVector#getData() method deprecated in favor of toArray(), minor formatting, move implementation of SparseFieldVector#getData() to toArray().

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1385316 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-09-16 16:50:32 +00:00
parent 1f914f9917
commit 79f99111d5
2 changed files with 60 additions and 59 deletions

View File

@ -66,8 +66,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* Compute the sum of {@code this} and {@code v}. * Compute the sum of {@code this} and {@code v}.
* @param v vector to be added * @param v vector to be added
* @return {@code this + v} * @return {@code this + v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
*/ */
FieldVector<T> add(FieldVector<T> v) throws DimensionMismatchException; FieldVector<T> add(FieldVector<T> v) throws DimensionMismatchException;
@ -75,8 +74,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* Compute {@code this} minus {@code v}. * Compute {@code this} minus {@code v}.
* @param v vector to be subtracted * @param v vector to be subtracted
* @return {@code this - v} * @return {@code this - v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
*/ */
FieldVector<T> subtract(FieldVector<T> v) throws DimensionMismatchException; FieldVector<T> subtract(FieldVector<T> v) throws DimensionMismatchException;
@ -154,8 +152,7 @@ public interface FieldVector<T extends FieldElement<T>> {
/** /**
* Map the 1/x function to each entry. * Map the 1/x function to each entry.
* @return a vector containing the result of applying the function to each * @return a vector containing the result of applying the function to each entry.
* entry.
* @throws MathArithmeticException if one of the entries is zero. * @throws MathArithmeticException if one of the entries is zero.
*/ */
FieldVector<T> mapInv() throws MathArithmeticException; FieldVector<T> mapInv() throws MathArithmeticException;
@ -172,8 +169,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* Element-by-element multiplication. * Element-by-element multiplication.
* @param v vector by which instance elements must be multiplied * @param v vector by which instance elements must be multiplied
* @return a vector containing {@code this[i] * v[i]} for all {@code i} * @return a vector containing {@code this[i] * v[i]} for all {@code i}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
*/ */
FieldVector<T> ebeMultiply(FieldVector<T> v) FieldVector<T> ebeMultiply(FieldVector<T> v)
throws DimensionMismatchException; throws DimensionMismatchException;
@ -182,8 +178,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* Element-by-element division. * Element-by-element division.
* @param v vector by which instance elements must be divided * @param v vector by which instance elements must be divided
* @return a vector containing {@code this[i] / v[i]} for all {@code i} * @return a vector containing {@code this[i] / v[i]} for all {@code i}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
* @throws MathArithmeticException if one entry of {@code v} is zero. * @throws MathArithmeticException if one entry of {@code v} is zero.
*/ */
FieldVector<T> ebeDivide(FieldVector<T> v) FieldVector<T> ebeDivide(FieldVector<T> v)
@ -192,23 +187,24 @@ public interface FieldVector<T extends FieldElement<T>> {
/** /**
* Returns vector entries as a T array. * Returns vector entries as a T array.
* @return T array of entries * @return T array of entries
* @deprecated as of 3.1, to be removed in 4.0. Please use the {@link #toArray()} method instead.
*/ */
T[] getData(); @Deprecated
T[] getData();
/** /**
* Compute the dot product. * Compute the dot product.
* @param v vector with which dot product should be computed * @param v vector with which dot product should be computed
* @return the scalar dot product of {@code this} and {@code v} * @return the scalar dot product of {@code this} and {@code v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
*/ */
T dotProduct(FieldVector<T> v) throws DimensionMismatchException; T dotProduct(FieldVector<T> v) throws DimensionMismatchException;
/** Find the orthogonal projection of this vector onto another vector. /**
* Find the orthogonal projection of this vector onto another vector.
* @param v vector onto which {@code this} must be projected * @param v vector onto which {@code this} must be projected
* @return projection of {@code this} onto {@code v} * @return projection of {@code this} onto {@code v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as {@code this}
* {@code this}
* @throws MathArithmeticException if {@code v} is the null vector. * @throws MathArithmeticException if {@code v} is the null vector.
*/ */
FieldVector<T> projection(FieldVector<T> v) FieldVector<T> projection(FieldVector<T> v)

View File

@ -202,7 +202,7 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> copy() { public FieldVector<T> copy() {
return new SparseFieldVector<T>(this); return new SparseFieldVector<T>(this);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public T dotProduct(FieldVector<T> v) throws DimensionMismatchException { public T dotProduct(FieldVector<T> v) throws DimensionMismatchException {
@ -242,34 +242,33 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
return res; return res;
} }
/** {@inheritDoc} */ /**
public T[] getData() { * {@inheritDoc}
T[] res = buildArray(virtualSize); *
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); * @deprecated as of 3.1, to be removed in 4.0. Please use the {@link #toArray()} method instead.
while (iter.hasNext()) { */
iter.advance(); @Deprecated
res[iter.key()] = iter.value(); public T[] getData() {
} return toArray();
return res; }
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public int getDimension() { public int getDimension() {
return virtualSize; return virtualSize;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public T getEntry(int index) throws OutOfRangeException { public T getEntry(int index) throws OutOfRangeException {
checkIndex(index); checkIndex(index);
return entries.get(index); return entries.get(index);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public Field<T> getField() { public Field<T> getField() {
return field; return field;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> getSubVector(int index, int n) public FieldVector<T> getSubVector(int index, int n)
throws OutOfRangeException, NotPositiveException { throws OutOfRangeException, NotPositiveException {
if (n < 0) { if (n < 0) {
@ -290,26 +289,26 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
return res; return res;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapAdd(T d) throws NullArgumentException { public FieldVector<T> mapAdd(T d) throws NullArgumentException {
return copy().mapAddToSelf(d); return copy().mapAddToSelf(d);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapAddToSelf(T d) throws NullArgumentException { public FieldVector<T> mapAddToSelf(T d) throws NullArgumentException {
for (int i = 0; i < virtualSize; i++) { for (int i = 0; i < virtualSize; i++) {
setEntry(i, getEntry(i).add(d)); setEntry(i, getEntry(i).add(d));
} }
return this; return this;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapDivide(T d) public FieldVector<T> mapDivide(T d)
throws NullArgumentException, MathArithmeticException { throws NullArgumentException, MathArithmeticException {
return copy().mapDivideToSelf(d); return copy().mapDivideToSelf(d);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapDivideToSelf(T d) public FieldVector<T> mapDivideToSelf(T d)
throws NullArgumentException, MathArithmeticException { throws NullArgumentException, MathArithmeticException {
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
@ -318,43 +317,43 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
entries.put(iter.key(), iter.value().divide(d)); entries.put(iter.key(), iter.value().divide(d));
} }
return this; return this;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapInv() throws MathArithmeticException { public FieldVector<T> mapInv() throws MathArithmeticException {
return copy().mapInvToSelf(); return copy().mapInvToSelf();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapInvToSelf() throws MathArithmeticException { public FieldVector<T> mapInvToSelf() throws MathArithmeticException {
for (int i = 0; i < virtualSize; i++) { for (int i = 0; i < virtualSize; i++) {
setEntry(i, field.getOne().divide(getEntry(i))); setEntry(i, field.getOne().divide(getEntry(i)));
} }
return this; return this;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapMultiply(T d) throws NullArgumentException { public FieldVector<T> mapMultiply(T d) throws NullArgumentException {
return copy().mapMultiplyToSelf(d); return copy().mapMultiplyToSelf(d);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapMultiplyToSelf(T d) throws NullArgumentException { public FieldVector<T> mapMultiplyToSelf(T d) throws NullArgumentException {
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
iter.advance(); iter.advance();
entries.put(iter.key(), iter.value().multiply(d)); entries.put(iter.key(), iter.value().multiply(d));
} }
return this; return this;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapSubtract(T d) throws NullArgumentException { public FieldVector<T> mapSubtract(T d) throws NullArgumentException {
return copy().mapSubtractToSelf(d); return copy().mapSubtractToSelf(d);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapSubtractToSelf(T d) throws NullArgumentException { public FieldVector<T> mapSubtractToSelf(T d) throws NullArgumentException {
return mapAddToSelf(field.getZero().subtract(d)); return mapAddToSelf(field.getZero().subtract(d));
} }
@ -416,7 +415,7 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
public void setEntry(int index, T value) throws OutOfRangeException { public void setEntry(int index, T value) throws OutOfRangeException {
checkIndex(index); checkIndex(index);
entries.put(index, value); entries.put(index, value);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public void setSubVector(int index, FieldVector<T> v) public void setSubVector(int index, FieldVector<T> v)
@ -475,7 +474,13 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
/** {@inheritDoc} */ /** {@inheritDoc} */
public T[] toArray() { public T[] toArray() {
return getData(); T[] res = buildArray(virtualSize);
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res[iter.key()] = iter.value();
}
return res;
} }
/** /**