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