View Javadoc

1   /*
2    * @(#)ProbMath.java Apr 3, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.jstats.math;
11  
12  /***
13   * The PropMath class defines some very basic probabilistic mathematical
14   * functions.
15   * <p>
16   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
17   * University of Technology </a>, the Netherlands. <br>
18   * See for project information <a
19   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
20   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
21   * License (GPL) </a>, no warranty <br>
22   * 
23   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
24   *         Jacobs </a>
25   * @version 1.6, 2004-03-20
26   * @since 1.0
27   */
28  public final class ProbMath
29  {
30  	/***
31  	 * constructs a new ProbMath
32  	 */
33  	private ProbMath()
34  	{
35  		super();
36  		//unreachable code for the utility class
37  	}
38  
39  	/***
40  	 * computes the faculty of n.
41  	 * 
42  	 * @param n is the input
43  	 * @return faculty of n
44  	 */
45  	public static double faculty(final int n)
46  	{
47  		if (n < 0)
48  		{
49  			throw new IllegalArgumentException("n! with n<0 is invalid");
50  		}
51  		if (n > 170)
52  		{
53  			throw new IllegalArgumentException("n! with n>170 is infinitely");
54  		}
55  		double result = 1.0;
56  		for (int i = 1; i <= n; i++)
57  		{
58  			result = result * i;
59  		}
60  		return result;
61  	}
62  
63  	/***
64  	 * computes the permutations of n over m.
65  	 * 
66  	 * @param n the first parameter
67  	 * @param m the second parameter
68  	 * @return the permutations of n over m
69  	 */
70  	public static double permutations(final int n, final int m)
71  	{
72  		if (m > n)
73  		{
74  			throw new IllegalArgumentException(
75  					"permutations of (n,m) with m>n ?...");
76  		}
77  		return faculty(n) / (faculty(m) * faculty(n - m));
78  	}
79  }