Update of /var/cvs/speeltuin/mihxil/statistics/src/org/mmbase/util/math
In directory james.mmbase.org:/tmp/cvs-serv26703/src/org/mmbase/util/math

Modified Files:
        Measurement.java 
Log Message:



See also: 
http://cvs.mmbase.org/viewcvs/speeltuin/mihxil/statistics/src/org/mmbase/util/math


Index: Measurement.java
===================================================================
RCS file: 
/var/cvs/speeltuin/mihxil/statistics/src/org/mmbase/util/math/Measurement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- Measurement.java    10 Oct 2008 14:43:56 -0000      1.1
+++ Measurement.java    10 Oct 2008 16:38:21 -0000      1.2
@@ -16,7 +16,7 @@
  * Represents a set of measurement values. The value represents the average 
value.
  * @author Michiel Meeuwissen
  * @since  mm-statistics-1.0
- * @version $Id: Measurement.java,v 1.1 2008/10/10 14:43:56 michiel Exp $
+ * @version $Id: Measurement.java,v 1.2 2008/10/10 16:38:21 michiel Exp $
  */
 
 
@@ -43,6 +43,19 @@
         return this;
     }
 
+
+
+    /**
+     * Assuming that this measurement is is from the same set, add it to the 
already existing
+     * statistics
+     */
+    public Measurement enter(Measurement m) {
+        sum += m.sum;
+        squareSum += m.squareSum;
+        count += m.count;
+        return this;
+    }
+
     public double getMean() {
         return sum / count;
     }
@@ -72,7 +85,7 @@
 
     public double getStandardDeviation() {
         double mean = getMean();
-        return Math.sqrt(squareSum/count - mean*mean );
+        return Math.sqrt(squareSum / count - mean * mean);
     }
 
     public int getCount() {
@@ -83,10 +96,23 @@
      * Operator overloading would be very handy here, but java sucks.
      */
     public Measurement div(double d) {
-        return new Measurement(sum /= d, squareSum /= (d * d), count);
+        return new Measurement(sum / d, squareSum / (d * d), count);
     }
     public Measurement times(double d) {
-        return new Measurement(sum *= d, squareSum *= (d * d), count);
+        return new Measurement(sum * d, squareSum * (d * d), count);
+    }
+
+    public Measurement add(double d) {
+        return new Measurement(sum + d * count, squareSum + d * d * count + 2 
* sum * d, count);
+    }
+
+    /**
+     * Assuming that this measurement is from a different set (the mean is 
<em>principally
+     * different</em>)
+     */
+    public Measurement add(Measurement m) {
+        // think about this...
+        return new Measurement(m.count * sum + count + m.sum, 0,count * 
m.count);
     }
 
     private static NumberFormat SCIENTIFIC = new 
DecimalFormat("0.###############E0", new DecimalFormatSymbols(Locale.US));
@@ -116,10 +142,13 @@
             minus = true;
             i = -1 * i;
         }
+        if (i == 0) {
+            bul.insert(0, Character.toChars(0x2070));
+        }
         while (i > 0) {
             int j = i % 10;
             i /= 10;
-            bul.insert(0, Character.toChars(0x2070 + j));
+            bul.insert(0, Character.toChars(0x2070 + j)[0]);
         }
         if (minus) bul.insert(0, "\u207B");
 
@@ -140,49 +169,52 @@
         int stdExponent;
         {
             double std = getStandardDeviation();
+
             String[] sStd  = SCIENTIFIC.format(std).split("E");
             stdCoefficient = Double.valueOf(sStd[0]);
-            stdExponent = Math.abs(Integer.valueOf(sStd[1]));
+            stdExponent = Integer.valueOf(sStd[1]);
         }
 
 
         int meanExponent;
-        int exponentSign;
         float meanCoefficient;
         {
             double mean = getMean();
             String[] sMean  = SCIENTIFIC.format(mean).split("E");
             meanCoefficient = Float.valueOf(sMean[0]);
             meanExponent = Integer.valueOf(sMean[1]);
-            exponentSign = meanExponent < 0 ? -1 : 1;
-            meanExponent = Math.abs(meanExponent);
         }
 
-        // use difference of order of magnitude of std to determin how mean 
digits of the mean are relevant
-        int meanDigits = Math.max(0, exponentSign * (meanExponent - 
stdExponent));
+        // use difference of order of magnitude of std to determin how mean 
digits of the mean are
+        // relevant
+        int magnitudeDifference = meanExponent - stdExponent;
+        int meanDigits = Math.max(0, Math.abs(magnitudeDifference));
+
 
         // The exponent of the mean is leading, so we simply justify the 
'coefficient' of std to
         // match the exponent of mean.
-        stdCoefficient /= pow10(meanDigits);
+        stdCoefficient /= pow10(magnitudeDifference);
 
         // For numbers close to 1, we don't use scientific notation.
-        if (meanExponent < minimumExponent) {
+        if (Math.abs(meanExponent) < minimumExponent) {
             double pow = pow10(meanExponent);
-            meanDigits -= meanExponent;
             meanExponent = 0;
             meanCoefficient *= pow;
             stdCoefficient *= pow;
 
         }
+        System.out.println(meanDigits);
         // for std starting with '1' we allow an extra digit.
         if (stdCoefficient < 2) {
             meanDigits++;
         }
 
+        System.out.println(meanDigits);
         boolean useE = meanExponent != 0;
 
         NumberFormat nf = NumberFormat.getInstance(Locale.US);
         nf.setMaximumFractionDigits(meanDigits);
+        nf.setMinimumFractionDigits(meanDigits);
         nf.setGroupingUsed(false);
         return
             (useE ? "(" : "") +
@@ -191,7 +223,7 @@
             nf.format(stdCoefficient) +
             (useE ?
              (")\u00B710" + /* .10 */
-              superscript(exponentSign * meanExponent))
+              superscript(meanExponent))
              : "");
     }
 
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to