Sleipnir
src/mathb.h
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 #ifndef MATH_H
00023 #define MATH_H
00024 
00025 #include <math.h>
00026 
00027 #include "mathbi.h"
00028 
00029 namespace Sleipnir {
00030 
00035 class CMath : CMathImpl {
00036 public:
00037     static double LogFact( size_t iN );
00038 
00061     static double Sigmoid( double dHeight, double dShift, double dSlope, double dVertical, double dX ) {
00062 
00063         return ( dVertical + ( dHeight / ( 1 + exp( -dSlope * ( dX - dShift ) ) ) ) ); }
00064 
00081     static size_t GCD( size_t iA, size_t iB ) {
00082         size_t  i;
00083 
00084         while( iB ) {
00085             i = iA;
00086             iA = iB;
00087             iB = i % iB; }
00088 
00089         return iA; }
00090 
00105     static size_t Round( double d ) {
00106 
00107         return (size_t)floor( d + 0.5 ); }
00108 
00109     template<class tType, class tIter>
00110     static bool LeastSquares( const tIter BeginY, const tIter EndY, const tIter BeginX, const tIter EndX,
00111         tType& Alpha, tType& Beta, bool fAlpha = true ) {
00112         tIter   CurX, CurY;
00113         tType   AveX, AveY, AveXY, AveX2, Tmp;
00114         size_t  iN;
00115 
00116         AveX = AveY = AveXY = AveX2 = 0;
00117         for( iN = 0,CurX = BeginX,CurY = BeginY; ( CurX != EndX ) && ( CurY != EndY ); ++CurX,++CurY,++iN ) {
00118             AveX += *CurX;
00119             AveY += *CurY;
00120             AveXY += *CurX * *CurY;
00121             AveX2 += *CurX * *CurX; }
00122         if( !iN )
00123             return false;
00124         if( !fAlpha ) {
00125             Beta = AveXY / AveX2;
00126             return ( AveX2 != 0 ); }
00127         AveX /= iN;
00128         AveX2 /= iN;
00129         Tmp = AveX * AveX;
00130         if( Tmp == AveX2 )
00131             return false;
00132         AveY /= iN;
00133         AveXY /= iN;
00134 
00135         Beta = ( AveXY - ( AveX * AveY ) ) / ( AveX2 - Tmp );
00136         Alpha = AveY - ( Beta * AveX );
00137         return true; }
00138 };
00139 
00140 }
00141 
00142 #endif // MATH_H