001 /*--------------------------------------------------------------------------+ 002 $Id: MathUtils.java 29788 2010-08-19 08:46:02Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.math; 019 020 import java.util.Collection; 021 022 /** 023 * Collection of math utility methods. 024 * 025 * @author deissenb 026 * @author $Author: juergens $ 027 * @version $Rev: 29788 $ 028 * @levd.rating GREEN Hash: AC3B3CC253965F47E5F474B132F7F59B 029 */ 030 public class MathUtils { 031 032 /** 033 * Sum values. 034 * 035 * @see SumAggregator 036 * @see EAggregationStrategy#SUM 037 */ 038 public static double sum(Collection<? extends Number> collection) { 039 return aggregate(collection, EAggregationStrategy.SUM); 040 } 041 042 /** 043 * Find maximum. 044 * 045 * @see MaxAggregator 046 * @see EAggregationStrategy#MAX 047 */ 048 public static double max(Collection<? extends Number> collection) { 049 return aggregate(collection, EAggregationStrategy.MAX); 050 } 051 052 /** 053 * Find minimum. 054 * 055 * @see MinAggregator 056 * @see EAggregationStrategy#MIN 057 */ 058 public static double min(Collection<? extends Number> collection) { 059 return aggregate(collection, EAggregationStrategy.MIN); 060 } 061 062 /** 063 * Find mean. 064 * 065 * @return {@link Double#NaN} for empty input collection 066 * 067 * @see MeanAggregator 068 * @see EAggregationStrategy#MEAN 069 */ 070 public static double mean(Collection<? extends Number> collection) { 071 return aggregate(collection, EAggregationStrategy.MEAN); 072 } 073 074 /** 075 * Find median. 076 * 077 * @return {@link Double#NaN} for empty input collection 078 * 079 * @see MedianAggregator 080 * @see EAggregationStrategy#MEDIAN 081 */ 082 public static double median(Collection<? extends Number> collection) { 083 return aggregate(collection, EAggregationStrategy.MEDIAN); 084 } 085 086 /** 087 * Aggregate collections of values with a given aggregation strategy. 088 * 089 * @return certain aggregation strategies may return {@link Double#NaN} for 090 * empty input collections 091 */ 092 public static double aggregate(Collection<? extends Number> values, 093 EAggregationStrategy aggregation) { 094 return aggregation.getAggregator().aggregate(values); 095 } 096 097 /** 098 * Computes the factorial of n. Errors are not handled. If n is negative, 1 099 * will be returned. If n to too large, wrong results will be produced due 100 * to numerical overflow. 101 */ 102 public static long factorial(int n) { 103 long result = 1; 104 for (int i = 2; i <= n; ++i) { 105 result *= i; 106 } 107 return result; 108 } 109 110 /** Checks if the provided number is neither infinite nor NaN. */ 111 public static boolean isNormal(double number) { 112 return !Double.isInfinite(number) && !Double.isNaN(number); 113 } 114 }