Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

guar_bincoeff.cpp

Go to the documentation of this file.
00001 /* LIBGUL - Geometry Utility Library
00002  * Copyright (C) 1998-1999 Norbert Irmer
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include "stdafx.h"
00021 
00022 #include "gul_types.h"
00023 #include "guar_bincoeff.h"
00024 
00025 float **gul::rtr<float>::m_BinCoeff = 0;
00026 int gul::rtr<float>::m_BinCoeff_Pmax = -1;
00027 
00028 double **gul::rtr<double>::m_BinCoeff = 0;
00029 int gul::rtr<double>::m_BinCoeff_Pmax = -1;
00030 
00031 
00032 namespace gul {
00033 
00034 /* -------------------------------------------------------------------------
00035  Create table with binomial coefficients (using the triangle method of Pascal)
00036  B[0][0] = 1;
00037  B[p][k] = B[p-1][k-1] + B[p-1][k]
00038  These are getting large very fast, so 'p' shouldn't be to big
00039 --------------------------------------------------------------------------- */
00040 
00041 void rtr<float>::InitBinCoeff( const int Pmax ) 
00042 {
00043   float s1,s2;
00044   int p,k;
00045 
00046   if( Pmax <= m_BinCoeff_Pmax )
00047     return;
00048   
00049   m_BinCoeff = (float **)realloc( m_BinCoeff, sizeof(float *) * (Pmax+1) );
00050   if( m_BinCoeff == NULL ) { throw AllocError(); }
00051 
00052   for( p = m_BinCoeff_Pmax+1; p <= Pmax; p++ ) 
00053   {
00054     m_BinCoeff[p] = (float *)malloc( sizeof(float) * (p+1) );
00055     if( m_BinCoeff[p] == 0 ) { throw AllocError(); }
00056 
00057     if( p == 0 )
00058     {
00059       m_BinCoeff[0][0] = 1;
00060       continue;
00061     }
00062     for( k = 0; k <= p; k++ )
00063     {
00064       if( k == 0 )
00065         s1 = 0;
00066       else
00067         s1 = m_BinCoeff[p-1][k-1];
00068 
00069       if( k > p-1 )
00070         s2 = 0;
00071       else
00072         s2 = m_BinCoeff[p-1][k];
00073     
00074       m_BinCoeff[p][k] = s1 + s2;  
00075     }      
00076   }
00077   m_BinCoeff_Pmax = Pmax;
00078 }
00079 
00080 /* -------------------------------------------------------------------------
00081  destroy table with binomial coefficients 
00082 -------------------------------------------------------------------------- */
00083 
00084 void rtr<float>::ExitBinCoeff()
00085 {
00086   int i;
00087 
00088   if( m_BinCoeff == 0 )
00089     return;
00090 
00091   for( i = 0; i <= m_BinCoeff_Pmax; i++ )
00092     free( m_BinCoeff[i] ); 
00093 
00094   free( m_BinCoeff );
00095 
00096   m_BinCoeff = 0;
00097   m_BinCoeff_Pmax = -1;  
00098 }
00099 
00100 /************************************************************************
00101   Same again for "double", because i wasn't able to formulate this as
00102   a template with Visual C++ :(
00103 ************************************************************************/
00104 
00105 /* -------------------------------------------------------------------------
00106  destroy table with binomial coefficients 
00107 -------------------------------------------------------------------------- */
00108 
00109 void rtr<double>::ExitBinCoeff()
00110 {
00111   int i;
00112 
00113   if( m_BinCoeff == 0 )
00114     return;
00115 
00116   for( i = 0; i <= m_BinCoeff_Pmax; i++ )
00117     free( m_BinCoeff[i] ); 
00118 
00119   free( m_BinCoeff );
00120 
00121   m_BinCoeff = 0;
00122   m_BinCoeff_Pmax = -1;  
00123 }
00124 
00125 /* -------------------------------------------------------------------------
00126  Create table with binomial coefficients (using the triangle method of Pascal)
00127  B[0][0] = 1;
00128  B[p][k] = B[p-1][k-1] + B[p-1][k]
00129  These are getting large very fast, so 'p' shouldn't be to big
00130 --------------------------------------------------------------------------- */
00131 void rtr<double>::InitBinCoeff( const int Pmax ) 
00132 {
00133   double s1,s2;
00134   int p,k;
00135 
00136   if( Pmax <= m_BinCoeff_Pmax )
00137     return;
00138   
00139   m_BinCoeff = (double **)realloc( m_BinCoeff, sizeof(double *) * (Pmax+1) );
00140   if( m_BinCoeff == NULL ) { throw AllocError(); }
00141 
00142   for( p = m_BinCoeff_Pmax+1; p <= Pmax; p++ ) 
00143   {
00144     m_BinCoeff[p] = (double *)malloc( sizeof(double) * (p+1) );
00145     if( m_BinCoeff[p] == 0 ) { throw AllocError(); }
00146 
00147     if( p == 0 )
00148     {
00149       m_BinCoeff[0][0] = 1;
00150       continue;
00151     }
00152 
00153     for( k = 0; k <= p; k++ )
00154     {
00155       if( k == 0 )
00156         s1 = 0;
00157       else
00158         s1 = m_BinCoeff[p-1][k-1];
00159 
00160       if( k > p-1 )
00161         s2 = 0;
00162       else
00163         s2 = m_BinCoeff[p-1][k];
00164     
00165       m_BinCoeff[p][k] = s1 + s2;  
00166     }      
00167   }
00168   m_BinCoeff_Pmax = Pmax;
00169 }
00170 
00171 
00172 }
00173 

Generated on Mon Jan 21 04:17:17 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001