binom.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _BINOM_H
00034 #define _BINOM_H
00035 
00036 #include <stdexcept>
00037 #include <vector>
00038 
00039 namespace Go {
00042 
00043 
00044 
00046 inline double binom(int n, int i)
00047 {
00048     if (i < 0 || i > n)
00049         return 0.0;
00050 
00051     static std::vector<std::vector<double> > pascals_triangle(10);
00052     static bool first_time = true;
00053     if (first_time) {
00054         first_time = false;
00055         pascals_triangle.resize(1);
00056         pascals_triangle[0].resize(1);
00057         pascals_triangle[0][0] = 1;
00058     }
00059     int old_size = pascals_triangle.size();
00060     if (old_size < n+1) {
00061         // We must expand the triangle
00062         pascals_triangle.resize(n+1);
00063         // Compute the terms of the new rows
00064         for (int nr = old_size; nr < n+1; ++nr) {
00065             pascals_triangle[nr].resize(nr+1);
00066             pascals_triangle[nr][0] = 1.0;
00067             for (int j = 1; j < nr; ++j) {
00068                 pascals_triangle[nr][j]
00069                     = pascals_triangle[nr-1][j-1] + pascals_triangle[nr-1][j];
00070             }
00071             pascals_triangle[nr][nr] = 1.0;
00072         }
00073     }
00074     return pascals_triangle[n][i];
00075 }
00076 
00078 inline double factorial(int n)
00079 {
00080     double res = 1;
00081     for (int i = 2; i <= n; ++i) {
00082         res *= i;
00083     }
00084     return res;
00085 }
00086 
00088 inline double trinomial(int n, int i, int j)
00089 {
00090     if (i < 0 || i > n || j < 0 || j > n)
00091         return 0;
00092 
00093     return binom(n, i) * binom(n-i, j);
00094 }
00095 
00096 
00098 inline double quadrinomial(int n, int i, int j, int k)
00099 {
00100     if (i < 0 || i > n || j < 0 || j > n || k < 0 || k > n)
00101         return 0;
00102 
00103     return binom(n, i) * binom(n-i, j) * binom(n-i-j, k);
00104 }
00105 
00107 }; // end Go
00108 
00109 #endif // _BINOM_H
00110 
00111 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1