You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bi...@apache.org on 2009/12/13 05:27:35 UTC

svn commit: r890023 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/ main/java/org/apache/commons/math/linear/ test/java/org/apache/commons/math/linear/

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);
         }