realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/bernstein_binomials.hpp
Go to the documentation of this file.
00001 /********************************************************************
00002  *   This file is part of the source code of the realroot library.
00003  *   Author(s): J.P. Pavone, GALAAD, INRIA
00004  *   $Id: binomials.hpp,v 1.1 2005/07/11 10:03:55 jppavone Exp $
00005  ********************************************************************/
00006 #ifndef realroot_SOLVE_SBDSLV_BERNSTEIN_BINOMIALS_H
00007 #define realroot_SOLVE_SBDSLV_BERNSTEIN_BINOMIALS_H
00008 //--------------------------------------------------------------------
00009 #include <vector>
00010 #include <assert.h>
00011 //====================================================================
00012 namespace mmx {
00013 //--------------------------------------------------------------------
00014 namespace bernstein
00015 {
00016   template<typename T>
00017   struct binomials
00018   {
00019     static binomials _default_;
00020     std::vector< T * > m_data;
00021     binomials( const binomials& );
00022     static void pwsum( T * dst, T * src, int sz ) { for(int i=0;i<sz-1;dst[i]=src[i]+src[i+1],i++) ; };
00023     binomials( int dmax = 100 ) 
00024     {
00025       m_data.push_back( new T[1] );
00026       m_data[0][0] = 1;
00027     };
00028     ~binomials() { 
00029       for ( unsigned i = 0; i < m_data.size(); i ++ ) 
00030         delete[] m_data[i]; 
00031 
00032     };
00033     inline T * get( unsigned i ) 
00034     {
00035       if ( m_data.size() > i ) return m_data[i];
00036       for ( unsigned j = m_data.size(); j <= i; j ++ )
00037         {
00038           m_data.push_back( new T[j+1] );
00039           pwsum(m_data[j]+1,m_data[j-1],j);
00040           m_data[j][0] = 1;
00041           m_data[j][j] = 1;
00042         };
00043       return m_data[i];
00044     };
00045     const T& binomial( unsigned n, unsigned i )
00046     { return get(n)[i]; };
00047   };
00048   template<typename T>
00049   binomials<T> binomials<T>::_default_;
00050   template<typename T>
00051   T binomial( int n, int i ) { return binomials<T>::_default_.binomial(n,i); };
00052 };
00053 //--------------------------------------------------------------------
00054 } //namespace mmx
00055 /********************************************************************/
00056 #endif //