EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.probfunction.math.util]

COVERAGE SUMMARY FOR SOURCE FILE [MathTools.java]

nameclass, %method, %block, %line, %
MathTools.java100% (3/3)50%  (15/30)49%  (335/684)45%  (56,8/125)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MathTools100% (1/1)42%  (11/26)47%  (314/663)44%  (53,8/122)
MathTools (): void 0%   (0/1)0%   (0/3)0%   (0/1)
asString (double): String 0%   (0/1)0%   (0/11)0%   (0/2)
computeJointProbability (BigDecimal [], int []): BigDecimal 0%   (0/1)0%   (0/33)0%   (0/5)
convertToDouble (Object): double 0%   (0/1)0%   (0/39)0%   (0/10)
factorial (long): BigDecimal 0%   (0/1)0%   (0/34)0%   (0/8)
isNumeric (Object): boolean 0%   (0/1)0%   (0/16)0%   (0/4)
less (double, double): boolean 0%   (0/1)0%   (0/12)0%   (0/1)
lessOrEqual (double, double): boolean 0%   (0/1)0%   (0/10)0%   (0/1)
over (int, int []): BigDecimal 0%   (0/1)0%   (0/32)0%   (0/5)
over (int, int): BigDecimal 0%   (0/1)0%   (0/14)0%   (0/1)
round (double, double): double 0%   (0/1)0%   (0/19)0%   (0/4)
sumOfCountinuousSamples (List): double 0%   (0/1)0%   (0/21)0%   (0/4)
sumOfDoubles (List): double 0%   (0/1)0%   (0/20)0%   (0/4)
sumOfSamples (List): double 0%   (0/1)0%   (0/23)0%   (0/5)
transformSampleToComplex (List): List 0%   (0/1)0%   (0/28)0%   (0/5)
equalsDouble (double, double): boolean 100% (1/1)73%  (19/26)72%  (3,6/5)
gcd (List): double 100% (1/1)78%  (38/49)67%  (6/9)
equalsComplex (Complex, Complex): boolean 100% (1/1)79%  (23/29)77%  (4,6/6)
<static initializer> 100% (1/1)88%  (7/8)87%  (0,9/1)
computeCumulativeProbabilities (List): List 100% (1/1)88%  (36/41)88%  (7/8)
gcd (double, double): double 100% (1/1)92%  (48/52)82%  (9/11)
computeLines (List, List): HashMap 100% (1/1)100% (83/83)100% (11/11)
getContinuousSampleComparator (): Comparator 100% (1/1)100% (6/6)100% (2/2)
getSampleComparator (): Comparator 100% (1/1)100% (6/6)100% (2/2)
transformComplexToDouble (List): List 100% (1/1)100% (23/23)100% (4/4)
transformDoubleToComplex (List): List 100% (1/1)100% (25/25)100% (4/4)
     
class MathTools$1100% (1/1)100% (2/2)100% (11/11)100% (3/3)
MathTools$1 (): void 100% (1/1)100% (3/3)100% (2/2)
compare (IContinuousSample, IContinuousSample): int 100% (1/1)100% (8/8)100% (1/1)
     
class MathTools$2100% (1/1)100% (2/2)100% (10/10)100% (3/3)
MathTools$2 (): void 100% (1/1)100% (3/3)100% (2/2)
compare (ISample, ISample): int 100% (1/1)100% (7/7)100% (1/1)

1package de.uka.ipd.sdq.probfunction.math.util;
2 
3import java.math.BigDecimal;
4import java.math.BigInteger;
5import java.util.ArrayList;
6import java.util.Comparator;
7import java.util.HashMap;
8import java.util.List;
9 
10import de.uka.ipd.sdq.probfunction.BoxedPDF;
11import de.uka.ipd.sdq.probfunction.ContinuousSample;
12import de.uka.ipd.sdq.probfunction.math.IBoxedPDF;
13import de.uka.ipd.sdq.probfunction.math.IContinuousSample;
14import de.uka.ipd.sdq.probfunction.math.ISample;
15import flanagan.complex.Complex;
16 
17/**
18 * MathTools contains a set of commonly used mathematical functions, that are
19 * not provided by the Java libraries.
20 * 
21 * @author ihssane, jens
22 * 
23 */
24public class MathTools {
25 
26        /**
27         * Difference up to which two values are considered as equal.
28         */
29        public static final double EPSILON_ERROR = 1e-5;
30 
31        /**
32         * Computes the greatest common divisor (GDC) of a set of numbers.
33         * 
34         * @param numbers
35         *            List of numbers for which the GDC shall be computed.
36         * @return Returns the greatest common divisor of all numbers
37         */
38        public static double gcd(List<Double> numbers) {
39                if (numbers.size() < 1)
40                        throw new IllegalArgumentException(
41                                        "number of digit must be greater than 0");
42                if(numbers.size() < 2) 
43                        return numbers.get(0);
44 
45                double gcd = gcd(numbers.get(0), numbers.get(1));
46                for (int i = 2; i < numbers.size(); i++)
47                        gcd = gcd(gcd, numbers.get(i));
48                return gcd;
49        }
50 
51        /**
52         * Computes something similar to the greatest common 
53         * divisor (GCD) of two numbers.
54         * Note that the GCD for two doubles is calculates, which is 
55         * different to the standard definition of GCD.  
56         * 
57         * @param x
58         *            first number
59         * @param y
60         *            second number
61         * @return Returns the GDC of y and x.
62         */
63        public static double gcd(double x, double y) {
64 
65                if (x == 0.0)
66                        return y;
67                if (y == 0.0)
68                        return x;
69                
70                //if one already divides the other almost without remainder, return the smaller one
71                if (Math.abs(x%y) < EPSILON_ERROR) return y;
72                if (Math.abs(y%x) < EPSILON_ERROR) return x;
73                
74                while (Math.abs(x - y) > EPSILON_ERROR) {
75                        if (x > y) {
76                                x -= y;
77                        } else {
78                                y -= x;
79                        }
80                }
81                return x;
82        }
83 
84        /**
85         * Transforms a list of complex values to a list of double values by
86         * throwing away the imaginary part.
87         * 
88         * @param values
89         *            List of complex values to transform.
90         * @return The real part of the value list as doubles.
91         */
92        public static List<Double> transformComplexToDouble(List<Complex> values) {
93                List<Double> resultList = new ArrayList<Double>();
94                for (Complex complex : values) {
95                        resultList.add(complex.getReal());
96                }
97                return resultList;
98        }
99 
100        /**
101         * Transforms a list of double values to a list of complex values. The real
102         * parts are set to the values in the list, the imaginary part is set to
103         * zero.
104         * 
105         * @param values
106         *            List of double values to transform.
107         * @return A list of complex values equivalent to the doubles.
108         */
109        public static List<Complex> transformDoubleToComplex(List<Double> values) {
110                List<Complex> resultList = new ArrayList<Complex>();
111                for (Double d : values) {
112                        resultList.add(new Complex(d));
113                }
114                return resultList;
115        }
116 
117        /**
118         * Compares two doubles.
119         * 
120         * @param d1
121         * @param d2
122         * @return True, if the difference between both values is lower than
123         *         EPSILON_ERROR; false otherwise.
124         */
125        public static boolean equalsDouble(double d1, double d2) {
126                boolean result = false;
127                if (d1 == Double.NaN && d2 == Double.NaN) {
128                        result = true;
129                } else {
130                        result = (Math.abs(d1 - d2) < EPSILON_ERROR);
131                }
132                return result;
133        }
134 
135        public static boolean equalsComplex(Complex z1, Complex z2) {
136                boolean result = false;
137                if (z1.isNaN() && z2.isNaN()) {
138                        result = true;
139                } else {
140                        result = equalsDouble(z1.getReal(), z2.getReal())
141                                        && equalsDouble(z1.getImag(), z2.getImag());
142                }
143                return result;
144 
145        }
146 
147        /**
148         * Compute the sum of probabilities associated with a set of
149         * IContinuousSamples.
150         * 
151         * @param list
152         * @return the computed value.
153         */
154        public static double sumOfCountinuousSamples(List<IContinuousSample> list) {
155                double sum = 0.0;
156                for (IContinuousSample s : list)
157                        sum += s.getProbability();
158                return sum;
159        }
160        
161        /**
162         * Compute the sum of probabilities associated with a set of ISamples.
163         * 
164         * @param list
165         * @return the computed value.
166         */
167        public static double sumOfSamples(List<ISample> list) {
168                double sum = 0.0;
169                for (ISample s : list) {
170                        sum += s.getProbability();
171                        System.out.println(sum);
172                }
173                return sum;
174        }
175 
176        /**
177         * Compute the sum of a set Doubles.
178         * 
179         * @param list
180         * @return the computed value.
181         */
182        public static double sumOfDoubles(List<Double> list) {
183                double sum = 0.0;
184                for (Double d : list)
185                        sum += d;
186                return sum;
187        }
188 
189        /**
190         * Returns the cumulative probabilities of the list of input probabilities.
191         * The size of the result list might be smaller than the size of the input list, 
192         * since the function terminates when it reaches 1.0.
193         * 
194         * @param probabilityList
195         * @return
196         */
197        public static List<Double> computeCumulativeProbabilities(List<Double> probabilityList) {
198                List<Double> resultList = new ArrayList<Double>(probabilityList.size());
199                if (probabilityList == null || probabilityList.size() == 0)
200                        throw new IllegalArgumentException("ProbabilityList is empty or null!");
201                double prob = 0;
202                for(Double d : probabilityList){
203                        prob += d;
204                        resultList.add(prob);
205                }
206                return resultList;
207        }
208 
209        /**
210         * @param samples
211         * @param prob
212         * @return
213         */
214        public static HashMap<Double, Line> computeLines(
215                        List<IContinuousSample> samples, List<Double> intervals) {
216                HashMap<Double, Line> lines = new HashMap<Double, Line>();
217                lines.put(intervals.get(0), new Line(0, 0, samples.get(0).getValue(),
218                                samples.get(0).getProbability()));
219 
220                for (int i = 1; i < intervals.size(); i++) {
221                        double x1 = samples.get(i - 1).getValue();
222                        double y1 = intervals.get(i - 1);
223                        double x2 = samples.get(i).getValue();
224                        double y2 = intervals.get(i);
225                        if (y1 != y2)
226                                lines.put(intervals.get(i), new Line(x1, y1, x2, y2));
227                }
228 
229                return lines;
230        }
231 
232        public static Comparator<IContinuousSample> getContinuousSampleComparator() {
233                Comparator<IContinuousSample> comp = new Comparator<IContinuousSample>() {
234                        @SuppressWarnings("unchecked")
235                        public int compare(IContinuousSample o1, IContinuousSample o2) {
236                                return ((Comparable) o1.getValue()).compareTo(o2.getValue());
237                        }
238 
239                };
240                return comp;
241        }
242 
243        public static Comparator<ISample> getSampleComparator() {
244                Comparator<ISample> sComparator = new Comparator<ISample>() {
245 
246                        @SuppressWarnings("unchecked")
247                        public int compare(ISample o1, ISample o2) {
248                                return ((Comparable) o1.getValue()).compareTo(o2.getValue());
249                        }
250                };
251                return sComparator;
252        }
253 
254        public static String asString(double val) {
255                double rVal = ((double) Math.round(val * 10000.0)) / 10000.0;
256                return Double.toString(rVal);
257        }
258 
259        public static BigDecimal over(int n, int k) {
260                return factorial(n).divide(factorial(k).multiply(factorial(n - k)));
261        }
262        
263        public static BigDecimal over(int n, int[] nList){
264                BigDecimal numerator = factorial(n);
265                BigDecimal denominator = BigDecimal.ONE;
266                for (int ni : nList) {
267                        denominator = denominator.multiply(factorial(ni));
268                }
269                return numerator.divide(denominator);
270        }
271 
272        
273        public static BigDecimal computeJointProbability(BigDecimal[] probList, int[] nList){
274                assert(nList.length == probList.length);
275                BigDecimal result = BigDecimal.ONE;
276                for (int i = 0; i < nList.length; i++) {
277                        result = result.multiply(probList[i].pow(nList[i]));
278                }
279                return result;
280        }
281        
282 
283        public static BigDecimal factorial(long n) {
284                if (n < 0)
285                        return null;
286                if (n == 0)
287                        return BigDecimal.ONE;
288                BigDecimal fac = BigDecimal.ONE;
289                for (long i = 1; i <= n; i++) {
290                        fac = fac.multiply(new BigDecimal(i));
291                }
292                return fac;
293        }
294 
295        public static boolean isNumeric(Object value) {
296                if ((value instanceof Double) || (value instanceof Integer)
297                                || (value instanceof Long) || (value instanceof Float))
298                        return true;
299                return false;
300        }
301 
302        public static List<Complex> transformSampleToComplex(List<ISample> samples) {
303                List<Complex> resultList = new ArrayList<Complex>();
304                for (ISample s : samples) {
305                        resultList.add(new Complex(s.getProbability(), convertToDouble(s
306                                        .getValue())));
307                }
308                return resultList;
309        }
310 
311        public static double convertToDouble(Object value) {
312                double r = 0.0;
313                if (value instanceof Double) {
314                        r = (Double) value;
315                } else if (value instanceof Integer) {
316                        r = ((Integer) value).doubleValue();
317                } else if (value instanceof Boolean) {
318                        r = ((Boolean) value).booleanValue() ? 1.0 : 0.0;
319                } else if (value instanceof Float)
320                        r = ((Float) value).doubleValue();
321                return r;
322        }
323        
324        public static double round(double value,double precision){
325                long factor = (long)(1 / precision);
326                value *= factor;
327                long temp = Math.round(value);
328                return (double)temp / (double)factor;
329        }
330 
331        public static boolean lessOrEqual(double d1, double d2) {
332                return d1 <= d2 + EPSILON_ERROR;
333        }
334 
335        public static boolean less(double d1, double d2) {
336                return d1 < d2 && !equalsDouble(d1, d2);
337        }
338}

[all classes][de.uka.ipd.sdq.probfunction.math.util]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov