Removing the mapTo* metheds from OpenMapRealVector, and use the base class methods instead

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@890023 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
William Barker 2009-12-13 04:27:34 +00:00
parent 6a5848130f
commit b26de3b0aa
4 changed files with 72 additions and 446 deletions

View File

@ -67,6 +67,14 @@ public abstract class ComposableFunction implements UnivariateRealFunction {
} }
}; };
/** The invert operator wrapped as a {@link ComposableFunction}. */
public static final ComposableFunction INVERT = new ComposableFunction () {
/** {@inheritDoc} */
public double value(double d){
return 1/d;
}
};
/** The {@code Math.sin} method wrapped as a {@link ComposableFunction}. */ /** The {@code Math.sin} method wrapped as a {@link ComposableFunction}. */
public static final ComposableFunction SIN = new ComposableFunction() { public static final ComposableFunction SIN = new ComposableFunction() {
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -178,6 +186,13 @@ public abstract class ComposableFunction implements UnivariateRealFunction {
return Math.log10(d); return Math.log10(d);
} }
}; };
/** The {@code Math.log1p} method wrapped as a {@link ComposableFunction}. */
public static final ComposableFunction LOG1P = new ComposableFunction () {
public double value(double d){
return Math.log1p(d);
}
};
/** The {@code Math.cos} method wrapped as a {@link ComposableFunction}. */ /** The {@code Math.cos} method wrapped as a {@link ComposableFunction}. */
public static final ComposableFunction COS = new ComposableFunction() { public static final ComposableFunction COS = new ComposableFunction() {

View File

@ -21,6 +21,7 @@ import java.util.Iterator;
import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.BinaryFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.ComposableFunction; import org.apache.commons.math.analysis.ComposableFunction;
@ -152,10 +153,10 @@ public abstract class AbstractRealVector implements RealVector {
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapAddToSelf(double d) { public RealVector mapAddToSelf(double d) {
if (d != 0) { if (d != 0) {
Iterator<Entry> it = iterator(); try {
Entry e; return mapToSelf(BinaryFunction.ADD.fix1stArgument(d));
while (it.hasNext() && (e = it.next()) != null) { } catch (FunctionEvaluationException e) {
e.setValue(e.getValue() + d); throw new IllegalArgumentException(e);
} }
} }
return this; return this;
@ -382,6 +383,15 @@ public abstract class AbstractRealVector implements RealVector {
return copy().mapDivideToSelf(d); return copy().mapDivideToSelf(d);
} }
/** {@inheritDoc} */
public RealVector mapDivideToSelf(double d){
try {
return mapToSelf(BinaryFunction.DIVIDE.fix2ndArgument(d));
} catch (FunctionEvaluationException e) {
throw new IllegalArgumentException(e);
}
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapExp() { public RealVector mapExp() {
return copy().mapExpToSelf(); return copy().mapExpToSelf();
@ -395,12 +405,12 @@ public abstract class AbstractRealVector implements RealVector {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapExpm1() { public RealVector mapExpm1() {
return copy().mapExpm1ToSelf(); return copy().mapExpm1ToSelf();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapExpm1ToSelf() { public RealVector mapExpm1ToSelf() {
try { try {
@ -409,12 +419,12 @@ public abstract class AbstractRealVector implements RealVector {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapFloor() { public RealVector mapFloor() {
return copy().mapFloorToSelf(); return copy().mapFloorToSelf();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapFloorToSelf() { public RealVector mapFloorToSelf() {
try { try {
@ -423,11 +433,20 @@ public abstract class AbstractRealVector implements RealVector {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapInv() { public RealVector mapInv() {
return copy().mapInvToSelf(); return copy().mapInvToSelf();
} }
/** {@inheritDoc} */
public RealVector mapInvToSelf() {
try {
return mapToSelf(ComposableFunction.INVERT);
} catch (FunctionEvaluationException e) {
throw new IllegalArgumentException(e);
}
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapLog() { public RealVector mapLog() {
@ -465,7 +484,7 @@ public abstract class AbstractRealVector implements RealVector {
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapLog1pToSelf() { public RealVector mapLog1pToSelf() {
try { try {
return mapToSelf(ComposableFunction.ASIN); return mapToSelf(ComposableFunction.LOG1P);
} catch (FunctionEvaluationException e) { } catch (FunctionEvaluationException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
@ -475,11 +494,29 @@ public abstract class AbstractRealVector implements RealVector {
public RealVector mapMultiply(double d) { public RealVector mapMultiply(double d) {
return copy().mapMultiplyToSelf(d); return copy().mapMultiplyToSelf(d);
} }
/** {@inheritDoc} */
public RealVector mapMultiplyToSelf(double d){
try {
return mapToSelf(BinaryFunction.MULTIPLY.fix1stArgument(d));
} catch (FunctionEvaluationException e) {
throw new IllegalArgumentException(e);
}
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapPow(double d) { public RealVector mapPow(double d) {
return copy().mapPowToSelf(d); return copy().mapPowToSelf(d);
} }
/** {@inheritDoc} */
public RealVector mapPowToSelf(double d){
try {
return mapToSelf(BinaryFunction.POW.fix2ndArgument(d));
} catch (FunctionEvaluationException e) {
throw new IllegalArgumentException(e);
}
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapRint() { public RealVector mapRint() {
@ -555,6 +592,11 @@ public abstract class AbstractRealVector implements RealVector {
public RealVector mapSubtract(double d) { public RealVector mapSubtract(double d) {
return copy().mapSubtractToSelf(d); return copy().mapSubtractToSelf(d);
} }
/** {@inheritDoc} */
public RealVector mapSubtractToSelf(double d){
return mapAddToSelf(-d);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealVector mapTan() { public RealVector mapTan() {

View File

@ -599,34 +599,7 @@ public class OpenMapRealVector extends AbstractRealVector implements SparseRealV
return false; return false;
} }
/** {@inheritDoc} */
public OpenMapRealVector mapAbs() {
return copy().mapAbsToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapAbsToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.abs(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapAcos() {
return copy().mapAcosToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapAcosToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.acos(getEntry(i)));
}
return this;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public OpenMapRealVector mapAdd(double d) { public OpenMapRealVector mapAdd(double d) {
return copy().mapAddToSelf(d); return copy().mapAddToSelf(d);
@ -640,405 +613,7 @@ public class OpenMapRealVector extends AbstractRealVector implements SparseRealV
return this; return this;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public OpenMapRealVector mapAsin() {
return copy().mapAsinToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapAsinToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.asin(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapAtan() {
return copy().mapAtanToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapAtanToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.atan(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapCbrt() {
return copy().mapCbrtToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapCbrtToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.cbrt(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapCeil() {
return copy().mapCeilToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapCeilToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.ceil(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapCos() {
return copy().mapCosToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapCosToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.cos(getEntry(i)));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapCosh() {
return copy().mapCoshToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapCoshToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.cosh(getEntry(i)));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapDivide(double d) {
return copy().mapDivideToSelf(d);
}
/** {@inheritDoc} */
public OpenMapRealVector mapDivideToSelf(double d) {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), iter.value() / d);
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapExp() {
return copy().mapExpToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapExpToSelf() {
for (int i = 0; i < virtualSize; i++) {
entries.put(i, Math.exp(entries.get(i)));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapExpm1() {
return copy().mapExpm1ToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapExpm1ToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.expm1(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapFloor() {
return copy().mapFloorToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapFloorToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.floor(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapInv() {
return copy().mapInvToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapInvToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, 1.0/getEntry(i));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapLog() {
return copy().mapLogToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapLog10() {
return copy().mapLog10ToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapLog10ToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.log10(getEntry(i)));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapLog1p() {
return copy().mapLog1pToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapLog1pToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.log1p(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapLogToSelf() {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, Math.log(getEntry(i)));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapMultiply(double d) {
return copy().mapMultiplyToSelf(d);
}
/** {@inheritDoc} */
public OpenMapRealVector mapMultiplyToSelf(double d) {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), iter.value() * d);
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapPow(double d) {
return copy().mapPowToSelf(d);
}
/** {@inheritDoc} */
public OpenMapRealVector mapPowToSelf(double d) {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.pow(iter.value(), d));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapRint() {
return copy().mapRintToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapRintToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.rint(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapSignum() {
return copy().mapSignumToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapSignumToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.signum(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapSin() {
return copy().mapSinToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapSinToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.sin(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapSinh() {
return copy().mapSinhToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapSinhToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.sinh(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapSqrt() {
return copy().mapSqrtToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapSqrtToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.sqrt(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapSubtract(double d) {
return copy().mapSubtractToSelf(d);
}
/** {@inheritDoc} */
public OpenMapRealVector mapSubtractToSelf(double d) {
return mapAddToSelf(-d);
}
/** {@inheritDoc} */
public OpenMapRealVector mapTan() {
return copy().mapTanToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapTanToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.tan(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapTanh() {
return copy().mapTanhToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapTanhToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.tanh(iter.value()));
}
return this;
}
/** {@inheritDoc} */
public OpenMapRealVector mapUlp() {
return copy().mapUlpToSelf();
}
/** {@inheritDoc} */
public OpenMapRealVector mapUlpToSelf() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), Math.ulp(iter.value()));
}
return this;
}
/**
* Optimized method to compute the outer product.
* @param v The vector to comput the outer product on
* @return The outer product of <code>this</code> and <code>v</code>
* @throws IllegalArgumentException If the dimensions don't match
*/
public OpenMapRealMatrix outerproduct(OpenMapRealVector v) throws IllegalArgumentException{
checkVectorDimensions(v.getDimension());
OpenMapRealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
Iterator iter2 = v.getEntries().iterator();
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 OpenMapRealVector) {
return outerproduct((OpenMapRealVector)v);
}
RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
int row = iter.key();
for (int col = 0; col < virtualSize; col++) {
res.setEntry(row, col, iter.value()*v.getEntry(col));
}
}
return res;
}
/** {@inheritDoc} */
public RealMatrix outerProduct(double[] v) throws IllegalArgumentException { public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
checkVectorDimensions(v.length); checkVectorDimensions(v.length);
RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);

View File

@ -50,11 +50,9 @@ public class SparseRealVectorTest extends TestCase {
// Testclass to test the RealVector interface // Testclass to test the RealVector interface
// only with enough content to support the test // only with enough content to support the test
public static class SparseRealVectorTestImpl implements RealVector, Serializable { public static class SparseRealVectorTestImpl extends AbstractRealVector implements Serializable {
/** Serializable version identifier. */
private static final long serialVersionUID = 4715341047369582908L;
private static final long serialVersionUID = -6251371752518113791L;
/** Entries of the vector. */ /** Entries of the vector. */
protected double data[]; protected double data[];
@ -78,11 +76,7 @@ public class SparseRealVectorTest extends TestCase {
throw unsupported(); throw unsupported();
} }
public Iterator<Entry> sparseIterator() { public AbstractRealVector copy() {
throw unsupported();
}
public RealVector copy() {
return new SparseRealVectorTestImpl(data); return new SparseRealVectorTestImpl(data);
} }