Sleipnir
src/mathb.cpp
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #include "stdafx.h"
00023 #include "mathb.h"
00024 
00025 namespace Sleipnir {
00026 
00027 vector<double>  CMathImpl::s_vecdLogFact;
00028 
00042 double CMath::LogFact( size_t iN ) {
00043     size_t  i;
00044 
00045     if( s_vecdLogFact.empty( ) ) {
00046         s_vecdLogFact.resize( c_iFactCutoff );
00047         s_vecdLogFact[ 0 ] = s_vecdLogFact[ 1 ] = 0;
00048         for( i = 2; i < s_vecdLogFact.size( ); ++i )
00049             s_vecdLogFact[ i ] = s_vecdLogFact[ i - 1 ] + log( (double)i ); }
00050 
00051     return ( ( iN >= c_iFactCutoff ) ? LogFactStirling( iN ) : s_vecdLogFact[ iN ] ); }
00052 
00053 double CMathImpl::LogFactStirling( size_t iN ) {
00054 
00055     if( iN < 3 )
00056         return ( ( iN == 2 ) ? log( 2.0 ) : 0 );
00057 
00058     return ( log( 2.5066282746271 ) + ( ( iN + 0.5 ) * log( (double)iN ) ) - iN ); }
00059 
00060 double CMathImpl::LogFactRec( size_t iN ) {
00061 
00062     if( iN < 3 )
00063         return ( ( iN == 2 ) ? log( 2.0 ) : 0 );
00064 
00065     return ( log( (double)iN ) + LogFactRec( iN - 1 ) ); }
00066 
00067 }