mirror of
https://github.com/apache/commons-math.git
synced 2025-02-11 12:36:05 +00:00
Removed methods treating T[] as FieldVector. Modified SparseFieldVectorTest to allow for testing of operations on mixed types (Sparse/non-sparse).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1172044 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ff7b63131c
commit
237c80ba7b
@ -410,16 +410,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> add(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].add(v[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the sum of this and v.
|
||||
* @param v vector to be added
|
||||
@ -427,7 +417,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public ArrayFieldVector<T> add(ArrayFieldVector<T> v) {
|
||||
return (ArrayFieldVector<T>) add(v.data);
|
||||
checkVectorDimensions(v.data.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].add(v.data[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -444,16 +439,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> subtract(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].subtract(v[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute this minus v.
|
||||
* @param v vector to be subtracted
|
||||
@ -461,7 +446,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v) {
|
||||
return (ArrayFieldVector<T>) subtract(v.data);
|
||||
checkVectorDimensions(v.data.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].subtract(v.data[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -565,16 +555,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeMultiply(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].multiply(v[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Element-by-element multiplication.
|
||||
* @param v vector by which instance elements must be multiplied
|
||||
@ -582,7 +562,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @exception IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public ArrayFieldVector<T> ebeMultiply(ArrayFieldVector<T> v) {
|
||||
return (ArrayFieldVector<T>) ebeMultiply(v.data);
|
||||
checkVectorDimensions(v.data.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].multiply(v.data[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -599,16 +584,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeDivide(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].divide(v[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Element-by-element division.
|
||||
* @param v vector by which instance elements must be divided
|
||||
@ -616,7 +591,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v) {
|
||||
return (ArrayFieldVector<T>) ebeDivide(v.data);
|
||||
checkVectorDimensions(v.data.length);
|
||||
T[] out = buildArray(data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = data[i].divide(v.data[i]);
|
||||
}
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -647,16 +627,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public T dotProduct(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T dot = field.getZero();
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
dot = dot.add(data[i].multiply(v[i]));
|
||||
}
|
||||
return dot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the dot product.
|
||||
* @param v vector with which dot product should be computed
|
||||
@ -664,7 +634,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @exception IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public T dotProduct(ArrayFieldVector<T> v) {
|
||||
return dotProduct(v.data);
|
||||
checkVectorDimensions(v.data.length);
|
||||
T dot = field.getZero();
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
dot = dot.add(data[i].multiply(v.data[i]));
|
||||
}
|
||||
return dot;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -672,11 +647,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> projection(T[] v) {
|
||||
return projection(new ArrayFieldVector<T>(getField(), v, false));
|
||||
}
|
||||
|
||||
/** Find the orthogonal projection of this vector onto another vector.
|
||||
* @param v vector onto which instance must be projected
|
||||
* @return projection of the instance onto v
|
||||
@ -710,17 +680,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* @exception IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
public FieldMatrix<T> outerProduct(ArrayFieldVector<T> v) {
|
||||
return outerProduct(v.data);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldMatrix<T> outerProduct(T[] v) {
|
||||
final int m = data.length;
|
||||
final int n = v.length;
|
||||
final int n = v.data.length;
|
||||
final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, n);
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
out.setEntry(i, j, data[i].multiply(v[j]));
|
||||
out.setEntry(i, j, data[i].multiply(v.data[j]));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
@ -762,11 +727,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
return new ArrayFieldVector<T>(field, out, false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> append(T[] in) {
|
||||
return new ArrayFieldVector<T>(this, in);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> getSubVector(int index, int n) {
|
||||
ArrayFieldVector<T> out = new ArrayFieldVector<T>(field, n);
|
||||
@ -804,16 +764,6 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSubVector(int index, T[] v) {
|
||||
try {
|
||||
System.arraycopy(v, 0, data, index, v.length);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
checkIndex(index);
|
||||
checkIndex(index + v.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a set of consecutive elements.
|
||||
*
|
||||
@ -823,7 +773,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
|
||||
* inconsistent with vector size
|
||||
*/
|
||||
public void set(int index, ArrayFieldVector<T> v) {
|
||||
setSubVector(index, v.data);
|
||||
try {
|
||||
System.arraycopy(v.data, 0, data, index, v.data.length);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
checkIndex(index);
|
||||
checkIndex(index + v.data.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
|
@ -66,15 +66,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
FieldVector<T> add(FieldVector<T> v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Compute the sum of this and v.
|
||||
* @param v vector to be added
|
||||
* @return this + v
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
FieldVector<T> add(T[] v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Compute this minus v.
|
||||
* @param v vector to be subtracted
|
||||
@ -84,15 +75,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
FieldVector<T> subtract(FieldVector<T> v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Compute this minus v.
|
||||
* @param v vector to be subtracted
|
||||
* @return this + v
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
FieldVector<T> subtract(T[] v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Map an addition operation to each entry.
|
||||
* @param d value to be added to each entry
|
||||
@ -174,14 +156,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
*/
|
||||
FieldVector<T> ebeMultiply(FieldVector<T> v) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Element-by-element multiplication.
|
||||
* @param v vector by which instance elements must be multiplied
|
||||
* @return a vector containing this[i] * v[i] for all i
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
FieldVector<T> ebeMultiply(T[] v) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Element-by-element division.
|
||||
* @param v vector by which instance elements must be divided
|
||||
@ -190,14 +164,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
*/
|
||||
FieldVector<T> ebeDivide(FieldVector<T> v) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Element-by-element division.
|
||||
* @param v vector by which instance elements must be divided
|
||||
* @return a vector containing this[i] / v[i] for all i
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
FieldVector<T> ebeDivide(T[] v) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Returns vector entries as a T array.
|
||||
* @return T array of entries
|
||||
@ -213,15 +179,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
T dotProduct(FieldVector<T> v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Compute the dot product.
|
||||
* @param v vector with which dot product should be computed
|
||||
* @return the scalar dot product between instance and v
|
||||
* @exception IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
T dotProduct(T[] v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/** Find the orthogonal projection of this vector onto another vector.
|
||||
* @param v vector onto which instance must be projected
|
||||
* @return projection of the instance onto v
|
||||
@ -230,14 +187,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
FieldVector<T> projection(FieldVector<T> v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/** Find the orthogonal projection of this vector onto another vector.
|
||||
* @param v vector onto which instance must be projected
|
||||
* @return projection of the instance onto v
|
||||
* @throws IllegalArgumentException if v is not the same size as this
|
||||
*/
|
||||
FieldVector<T> projection(T[] v)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Compute the outer product.
|
||||
* @param v vector with which outer product should be computed
|
||||
@ -245,13 +194,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
*/
|
||||
FieldMatrix<T> outerProduct(FieldVector<T> v);
|
||||
|
||||
/**
|
||||
* Compute the outer product.
|
||||
* @param v vector with which outer product should be computed
|
||||
* @return the matrix outer product between instance and v
|
||||
*/
|
||||
FieldMatrix<T> outerProduct(T[] v);
|
||||
|
||||
/**
|
||||
* Returns the entry in the specified index.
|
||||
*
|
||||
@ -293,13 +235,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
*/
|
||||
FieldVector<T> append(T d);
|
||||
|
||||
/**
|
||||
* Construct a vector by appending a T array to this vector.
|
||||
* @param a T array to append.
|
||||
* @return a new vector
|
||||
*/
|
||||
FieldVector<T> append(T[] a);
|
||||
|
||||
/**
|
||||
* Get a subvector from consecutive elements.
|
||||
* @param index index of first element.
|
||||
@ -320,16 +255,6 @@ public interface FieldVector<T extends FieldElement<T>> {
|
||||
*/
|
||||
void setSubVector(int index, FieldVector<T> v);
|
||||
|
||||
/**
|
||||
* Set a set of consecutive elements.
|
||||
* @param index index of first element to be set.
|
||||
* @param v vector containing the values to set.
|
||||
* @throws org.apache.commons.math.exception.OutOfRangeException
|
||||
* if the index is inconsistent with vector size.
|
||||
* @see #setSubVector(int, FieldVector)
|
||||
*/
|
||||
void setSubVector(int index, T[] v);
|
||||
|
||||
/**
|
||||
* Set all elements to a single value.
|
||||
* @param value single value to set for all elements
|
||||
|
@ -158,17 +158,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> add(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(field,getDimension());
|
||||
for (int i = 0; i < v.length; i++) {
|
||||
res.setEntry(i, v[i].add(getEntry(i)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a vector by appending a vector to this vector.
|
||||
*
|
||||
@ -190,7 +179,12 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
if (v instanceof SparseFieldVector<?>) {
|
||||
return append((SparseFieldVector<T>) v);
|
||||
} else {
|
||||
return append(v.toArray());
|
||||
final int n = v.getDimension();
|
||||
FieldVector<T> res = new SparseFieldVector<T>(this, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
res.setEntry(i + virtualSize, v.getEntry(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,15 +195,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> append(T[] a) {
|
||||
FieldVector<T> res = new SparseFieldVector<T>(this, a.length);
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
res.setEntry(i + virtualSize, a[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> copy() {
|
||||
return new SparseFieldVector<T>(this);
|
||||
@ -227,22 +212,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public T dotProduct(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
T res = field.getZero();
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
int idx = iter.key();
|
||||
T value = field.getZero();
|
||||
if (idx < v.length) {
|
||||
value = v[idx];
|
||||
}
|
||||
res = res.add(value.multiply(iter.value()));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeDivide(FieldVector<T> v) {
|
||||
checkVectorDimensions(v.getDimension());
|
||||
@ -255,18 +224,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeDivide(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(this);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = res.entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
res.setEntry(iter.key(), iter.value().divide(v[iter.key()]));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeMultiply(FieldVector<T> v) {
|
||||
checkVectorDimensions(v.getDimension());
|
||||
@ -279,18 +236,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> ebeMultiply(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(this);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = res.entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
res.setEntry(iter.key(), iter.value().multiply(v[iter.key()]));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public T[] getData() {
|
||||
T[] res = buildArray(virtualSize);
|
||||
@ -423,28 +368,23 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldMatrix<T> outerProduct(T[] v) {
|
||||
final int n = v.length;
|
||||
FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
int row = iter.key();
|
||||
FieldElement<T>value = iter.value();
|
||||
for (int col = 0; col < n; col++) {
|
||||
res.setEntry(row, col, value.multiply(v[col]));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldMatrix<T> outerProduct(FieldVector<T> v) {
|
||||
if (v instanceof SparseFieldVector<?>) {
|
||||
return outerProduct((SparseFieldVector<T>)v);
|
||||
} else {
|
||||
return outerProduct(v.toArray());
|
||||
final int n = v.getDimension();
|
||||
FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
int row = iter.key();
|
||||
FieldElement<T>value = iter.value();
|
||||
for (int col = 0; col < n; col++) {
|
||||
res.setEntry(row, col, value.multiply(v.getEntry(col)));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -454,12 +394,6 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> projection(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
return projection(new SparseFieldVector<T>(field,v));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void set(T value) {
|
||||
for (int i = 0; i < virtualSize; i++) {
|
||||
@ -477,17 +411,10 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
public void setSubVector(int index, FieldVector<T> v) {
|
||||
checkIndex(index);
|
||||
checkIndex(index + v.getDimension() - 1);
|
||||
setSubVector(index, v.getData());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSubVector(int index, T[] v) {
|
||||
checkIndex(index);
|
||||
checkIndex(index + v.length - 1);
|
||||
for (int i = 0; i < v.length; i++) {
|
||||
setEntry(i + index, v[i]);
|
||||
final int n = v.getDimension();
|
||||
for (int i = 0; i < n; i++) {
|
||||
setEntry(i + index, v.getEntry(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -519,22 +446,18 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
if (v instanceof SparseFieldVector<?>) {
|
||||
return subtract((SparseFieldVector<T>)v);
|
||||
} else {
|
||||
return subtract(v.toArray());
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> subtract(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(this);
|
||||
for (int i = 0; i < v.length; i++) {
|
||||
if (entries.containsKey(i)) {
|
||||
res.setEntry(i, entries.get(i).subtract(v[i]));
|
||||
} else {
|
||||
res.setEntry(i, field.getZero().subtract(v[i]));
|
||||
final int n = v.getDimension();
|
||||
checkVectorDimensions(n);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(this);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (entries.containsKey(i)) {
|
||||
res.setEntry(i, entries.get(i).subtract(v.getEntry(i)));
|
||||
} else {
|
||||
res.setEntry(i, field.getZero().subtract(v.getEntry(i)));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@ -569,9 +492,16 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
||||
/** {@inheritDoc} */
|
||||
public FieldVector<T> add(FieldVector<T> v) {
|
||||
if (v instanceof SparseFieldVector<?>) {
|
||||
return add((SparseFieldVector<T>)v);
|
||||
return add((SparseFieldVector<T>) v);
|
||||
} else {
|
||||
return add(v.toArray());
|
||||
final int n = v.getDimension();
|
||||
checkVectorDimensions(n);
|
||||
SparseFieldVector<T> res = new SparseFieldVector<T>(field,
|
||||
getDimension());
|
||||
for (int i = 0; i < n; i++) {
|
||||
res.setEntry(i, v.getEntry(i).add(getEntry(i)));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,10 +342,6 @@ public class ArrayFieldVectorTest {
|
||||
Assert.assertEquals(4, v_append_2.getDimension());
|
||||
Assert.assertEquals(new Fraction(2), v_append_2.getEntry(3));
|
||||
|
||||
FieldVector<Fraction> v_append_3 = v1.append(vec2);
|
||||
Assert.assertEquals(6, v_append_3.getDimension());
|
||||
Assert.assertEquals(new Fraction(4), v_append_3.getEntry(3));
|
||||
|
||||
FieldVector<Fraction> v_append_4 = v1.append(v2_t);
|
||||
Assert.assertEquals(6, v_append_4.getDimension());
|
||||
Assert.assertEquals(new Fraction(4), v_append_4.getEntry(3));
|
||||
|
@ -115,14 +115,14 @@ public class SparseFieldVectorTest {
|
||||
SparseFieldVector<Fraction> v1 = new SparseFieldVector<Fraction>(field,vec1);
|
||||
SparseFieldVector<Fraction> v2 = new SparseFieldVector<Fraction>(field,vec2);
|
||||
|
||||
SparseFieldVector<Fraction> v2_t = new SparseFieldVector<Fraction>(field,vec2);
|
||||
FieldVector<Fraction> v2_t = new ArrayFieldVectorTest.FieldVectorTestImpl<Fraction>(vec2);
|
||||
|
||||
//octave = v1 + v2
|
||||
FieldVector<Fraction> v_add = v1.add(v2);
|
||||
Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
|
||||
Assert.assertArrayEquals("compare vect" ,v_add.getData(),result_add);
|
||||
|
||||
SparseFieldVector<Fraction> vt2 = new SparseFieldVector<Fraction>(field,vec2);
|
||||
FieldVector<Fraction> vt2 = new ArrayFieldVectorTest.FieldVectorTestImpl<Fraction>(vec2);
|
||||
FieldVector<Fraction> v_add_i = v1.add(vt2);
|
||||
Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
|
||||
Assert.assertArrayEquals("compare vect" ,v_add_i.getData(),result_add_i);
|
||||
|
Loading…
x
Reference in New Issue
Block a user