Author: billbarker Date: Sun Dec 13 04:27:34 2009 New Revision: 890023 URL: http://svn.apache.org/viewvc?rev=890023&view=rev Log: Removing the mapTo* metheds from OpenMapRealVector, and use the base class methods instead
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/ComposableFunction.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/ComposableFunction.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/ComposableFunction.java?rev=890023&r1=890022&r2=890023&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/ComposableFunction.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/ComposableFunction.java Sun Dec 13 04:27:34 2009 @@ -67,6 +67,14 @@ } }; + /** 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}. */ public static final ComposableFunction SIN = new ComposableFunction() { /** {...@inheritdoc} */ @@ -178,6 +186,13 @@ 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}. */ public static final ComposableFunction COS = new ComposableFunction() { Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java?rev=890023&r1=890022&r2=890023&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java Sun Dec 13 04:27:34 2009 @@ -21,6 +21,7 @@ import org.apache.commons.math.FunctionEvaluationException; 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.ComposableFunction; @@ -152,10 +153,10 @@ /** {...@inheritdoc} */ public RealVector mapAddToSelf(double d) { if (d != 0) { - Iterator<Entry> it = iterator(); - Entry e; - while (it.hasNext() && (e = it.next()) != null) { - e.setValue(e.getValue() + d); + try { + return mapToSelf(BinaryFunction.ADD.fix1stArgument(d)); + } catch (FunctionEvaluationException e) { + throw new IllegalArgumentException(e); } } return this; @@ -383,6 +384,15 @@ } /** {...@inheritdoc} */ + public RealVector mapDivideToSelf(double d){ + try { + return mapToSelf(BinaryFunction.DIVIDE.fix2ndArgument(d)); + } catch (FunctionEvaluationException e) { + throw new IllegalArgumentException(e); + } + } + + /** {...@inheritdoc} */ public RealVector mapExp() { return copy().mapExpToSelf(); } @@ -395,12 +405,12 @@ throw new IllegalArgumentException(e); } } - + /** {...@inheritdoc} */ public RealVector mapExpm1() { return copy().mapExpm1ToSelf(); } - + /** {...@inheritdoc} */ public RealVector mapExpm1ToSelf() { try { @@ -409,12 +419,12 @@ throw new IllegalArgumentException(e); } } - + /** {...@inheritdoc} */ public RealVector mapFloor() { return copy().mapFloorToSelf(); } - + /** {...@inheritdoc} */ public RealVector mapFloorToSelf() { try { @@ -423,11 +433,20 @@ throw new IllegalArgumentException(e); } } - + /** {...@inheritdoc} */ public RealVector mapInv() { return copy().mapInvToSelf(); } + + /** {...@inheritdoc} */ + public RealVector mapInvToSelf() { + try { + return mapToSelf(ComposableFunction.INVERT); + } catch (FunctionEvaluationException e) { + throw new IllegalArgumentException(e); + } + } /** {...@inheritdoc} */ public RealVector mapLog() { @@ -465,7 +484,7 @@ /** {...@inheritdoc} */ public RealVector mapLog1pToSelf() { try { - return mapToSelf(ComposableFunction.ASIN); + return mapToSelf(ComposableFunction.LOG1P); } catch (FunctionEvaluationException e) { throw new IllegalArgumentException(e); } @@ -475,11 +494,29 @@ public RealVector mapMultiply(double 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} */ public RealVector mapPow(double 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} */ public RealVector mapRint() { @@ -555,6 +592,11 @@ public RealVector mapSubtract(double d) { return copy().mapSubtractToSelf(d); } + + /** {...@inheritdoc} */ + public RealVector mapSubtractToSelf(double d){ + return mapAddToSelf(-d); + } /** {...@inheritdoc} */ public RealVector mapTan() { Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java?rev=890023&r1=890022&r2=890023&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java Sun Dec 13 04:27:34 2009 @@ -599,34 +599,7 @@ 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} */ public OpenMapRealVector mapAdd(double d) { return copy().mapAddToSelf(d); @@ -640,405 +613,7 @@ return this; } - /** {...@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} */ + /** {...@inheritdoc} */ public RealMatrix outerProduct(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java?rev=890023&r1=890022&r2=890023&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java Sun Dec 13 04:27:34 2009 @@ -50,11 +50,9 @@ // Testclass to test the RealVector interface // only with enough content to support the test - public static class SparseRealVectorTestImpl implements RealVector, Serializable { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 4715341047369582908L; + public static class SparseRealVectorTestImpl extends AbstractRealVector implements Serializable { + private static final long serialVersionUID = -6251371752518113791L; /** Entries of the vector. */ protected double data[]; @@ -78,11 +76,7 @@ throw unsupported(); } - public Iterator<Entry> sparseIterator() { - throw unsupported(); - } - - public RealVector copy() { + public AbstractRealVector copy() { return new SparseRealVectorTestImpl(data); }