~ubuntu-branches/ubuntu/quantal/psicode/quantal

« back to all changes in this revision

Viewing changes to src/lib/libqt/probabil.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file
 
3
  \brief Contains some probability functions
 
4
  \ingroup QT
 
5
*/
 
6
 
 
7
extern "C" {
 
8
        
 
9
/*!
 
10
** combinations() : Calculates the number of ways to choose k objects
 
11
**    from n objects, or "n choose k" 
 
12
**
 
13
** Parameters:
 
14
**   \param n   =  number of objects in total
 
15
**   \param k   =  number of objects taken at a time
 
16
**
 
17
** Returns:
 
18
**    number of combinations of n objects taken k at a time ("n choose k")
 
19
**    (returned as a double).
 
20
**
 
21
** \ingroup QT
 
22
*/
 
23
double combinations(int n, int k)
 
24
{
 
25
   double factorial(int) ;
 
26
   double comb ;
 
27
 
 
28
   if (n == k) return (1.0) ;
 
29
   else if (k > n) return(0.0) ;
 
30
   else if (k == 0) return(1.0) ; 
 
31
   comb = factorial(n) / (factorial(k) * factorial(n-k)) ;
 
32
 
 
33
   return(comb) ;
 
34
}
 
35
 
 
36
 
 
37
/*!
 
38
** factorial(): Returns n!
 
39
** 
 
40
** Parameters:
 
41
**    \param n  = number to take factorial of
 
42
**
 
43
** Returns:
 
44
**    n factorial, as a double word (since n! can get very large).
 
45
** \ingroup QT
 
46
*/
 
47
double factorial(int n)
 
48
{
 
49
 
 
50
   if (n == 0 || n == 1) return(1.0);
 
51
   if (n < 0) return(0.0) ;
 
52
   else {
 
53
      return ((double) n * factorial(n-1)) ;
 
54
      }
 
55
}
 
56
 
 
57
 
 
58
 
 
59
/*
 
60
** test combinations routines
 
61
**
 
62
#include <cstdio>
 
63
 
 
64
main()
 
65
{
 
66
   int i, j ;
 
67
   double factorial() ;
 
68
   double combinations() ;
 
69
 
 
70
   printf("Enter two numbers: ") ;
 
71
   scanf("%d %d", &i, &j) ;
 
72
   printf("i! = %.2f\n", factorial(i)) ;
 
73
   printf("j! = %.2f\n", factorial(j)) ;
 
74
   printf("i choose j = %.2f\n", combinations(i,j)) ;
 
75
}
 
76
**
 
77
*/
 
78
 
 
79
} /* extern "C" */