Binomial.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools
00003 //
00004 // GoTools module: Implicitization, version 1.0
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 _BINOMIAL_H
00034 #define _BINOMIAL_H
00035 
00036 
00037 #include <vector>
00038 
00039 
00040 namespace Go {
00043 
00044 
00045 
00050 class Binomial {
00051 public:
00053     typedef std::vector<double>::iterator iter;
00054 
00056     Binomial() : pascals_triangle(10)
00057     {
00058         for (int i = 0; i < 10; ++i)
00059             pascals_triangle[i].reserve(10);
00060         pascals_triangle.resize(1);
00061         pascals_triangle[0].resize(1);
00062         pascals_triangle[0][0] = 1.0;
00063     }
00067     Binomial(int n)
00068     {
00069         init(n);
00070     }
00071 
00078     double operator() (int n, int i)
00079     {
00080         if (i < 0 || i > n)
00081             return 0.0;
00082         if (int(pascals_triangle.size()) < n+1)
00083             expand(n);
00084 
00085         return pascals_triangle[n][i];
00086     }
00087 
00092     iter operator[] (int n)
00093     {
00094         if (int(pascals_triangle.size()) < n+1)
00095             expand(n);
00096 
00097         return pascals_triangle[n].begin();
00098     }
00099 
00106     double trinomial(int n, int i, int j)
00107     {
00108         if (i < 0 || i > n || j < 0 || j > n)
00109             return 0.0;
00110         if (int(pascals_triangle.size()) < n+1)
00111             expand(n);
00112 
00113         return pascals_triangle[n][i] * pascals_triangle[n-i][j];
00114     }
00115 
00123     double quadrinomial(int n, int i, int j, int k)
00124     {
00125         if (i < 0 || i > n || j < 0 || j > n || k < 0 || k > n)
00126             return 0;
00127         if (int(pascals_triangle.size()) < n+1)
00128             expand(n);
00129         
00130         return pascals_triangle[n][i]
00131             * pascals_triangle[n-i][j]
00132             * pascals_triangle[n-i-j][k];
00133     }
00134 
00135 private:
00136     void init(int n);
00137     void expand(int n);
00138 
00139     std::vector<std::vector<double> > pascals_triangle;
00140 
00141 };
00142 
00143 
00145 } // namespace Go
00146 
00147 
00148 #endif // _BINOMIAL_H
00149 
00150 

Generated on Mon Jun 11 15:13:16 2007 for GoTools Implicitization Library by  doxygen 1.5.1