00001 /* 00002 libit - Library for basic source and channel coding functions 00003 Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 /* 00021 Mathematical functions 00022 Copyright (C) 2005 Vivien Chappelier 00023 */ 00024 00025 #ifndef __it_math_h 00026 #define __it_math_h 00027 00028 #include <math.h> 00029 #include <it/types.h> 00030 #include <it/math.h> 00031 00032 /*---------------------------------------------------------------------------*/ 00033 /*! \defgroup math Mathematical functions */ 00034 /* @{ */ 00035 /*---------------------------------------------------------------------------*/ 00036 00037 #ifndef NAN 00038 #define NAN (sqrt(-1)) 00039 #endif 00040 00041 #ifndef M_PI 00042 #define M_PI 3.141592653589793238 00043 #endif 00044 00045 #ifndef M_E 00046 #define M_E 2.71828182845905 00047 #endif 00048 00049 /* logarithm in base 2 */ 00050 #define LOG2 0.69314718055994529 00051 #define INVLOG2 1.44269504088896339 00052 #define log2(x) (INVLOG2 * log(x)) 00053 00054 /* some small constant */ 00055 #define IT_EPSILON (1e-10) 00056 00057 /*----------------------------------------------------------------------------*/ 00058 /* Basic mathematical functions */ 00059 /*----------------------------------------------------------------------------*/ 00060 00061 double erfinv (double x); 00062 00063 int nchoosek (int n, int k); 00064 double lognchoosek (int n, int k); 00065 00066 /* Compute log(a+b) from log(a) and log(b) */ 00067 double log_sum(double log_a, double log_b); 00068 00069 /* Compute the logarithm of the Gamma function. */ 00070 double log_gamma (double x); 00071 00072 /* Sigmoid function: 1 / (1 + exp(-lambda * x)) and its inverse */ 00073 double sigmoid (double x, double lambda); 00074 double invsigmoid (double x, double lambda); 00075 00076 /*----------------------- some well-known functions --------------------------*/ 00077 00078 /* identity, returns x */ 00079 extern it_function_t itf_identity; 00080 00081 /* Gaussian distribution */ 00082 it_function_args (itf_gaussian) { 00083 double sigma; /* standard deviation */ 00084 }; 00085 extern it_function_t itf_gaussian; 00086 00087 /* Laplacian distribution */ 00088 it_function_args (itf_laplacian) { 00089 double lambda; /* lambda parameter. variance is 2*lambda^2 */ 00090 }; 00091 extern it_function_t itf_laplacian; 00092 00093 /* Generalized Gaussian distribution */ 00094 /* beta / (2 alpha Gamma(1/beta)) exp( - (| x | / alpha) ^ beta ) */ 00095 it_function_args (itf_generalized_gaussian) { 00096 double alpha; 00097 double beta; 00098 }; 00099 extern it_function_t itf_generalized_gaussian; 00100 00101 /*-------------------------------- Operators ----------------------------------*/ 00102 /* These are just particular functions with one or more function(s) 00103 (and its parameters) passed as parameters. */ 00104 00105 /* the differentiation operator */ 00106 it_function_args (itf_differentiate) { 00107 it_function_t function; /* function to differentiate */ 00108 it_args_t args; /* and its arguments */ 00109 }; 00110 extern it_function_t itf_differentiate; 00111 00112 /* the 2nd-order differentiation operator */ 00113 it_function_args (itf_diff2) { 00114 it_function_t function; /* function to differentiate twice */ 00115 it_args_t args; /* and its arguments */ 00116 }; 00117 extern it_function_t itf_diff2; 00118 00119 /* integration between [a, b] using the trapezoid method (N samples). 00120 the upper bound is the function argument whereas the lower bound 00121 and number of intervals are passed as parameters (along with the 00122 function to integrate and its arguments). This allows for a 00123 consistent definition of the integral that can be differentiated 00124 again to obtain [an approximation of] the original function. */ 00125 it_function_args (itf_integrate_trapezoid) { 00126 double a; /* lower bound */ 00127 int N; /* number of intervals */ 00128 it_function_t function; /* function to integrate */ 00129 it_args_t args; /* and its arguments */ 00130 }; 00131 extern it_function_t itf_integrate_trapezoid; 00132 00133 /* integration between [a, b] using Romberg convergence acceleration method. */ 00134 /* the error of this method is O(((b-a)/N)^(2p+2)) */ 00135 it_function_args (itf_integrate_romberg) { 00136 double a; /* lower bound */ 00137 int N; /* number of intervals */ 00138 int p; /* precision order */ 00139 it_function_t function; /* function to integrate */ 00140 it_args_t args; /* and its arguments */ 00141 }; 00142 extern it_function_t itf_integrate_romberg; 00143 00144 /* default integration between [a, b]. This uses the Romberg method with 00145 a fixed precision of O(2^-12). The constant depends on the value 00146 of the tenth and further derivatives of the function, which are usually 00147 quite small for smooth enough functions. 00148 */ 00149 it_function_args (itf_integrate) { 00150 double a; /* lower bound */ 00151 it_function_t function; /* function to integrate */ 00152 it_args_t args; /* and its arguments */ 00153 }; 00154 extern it_function_t itf_integrate; 00155 00156 /* compute the expectation (first moment of the function) */ 00157 it_function_args (itf_expectation) { 00158 double a; /* lower bound */ 00159 it_function_t function; /* function to integrate */ 00160 it_args_t args; /* and its arguments */ 00161 }; 00162 extern it_function_t itf_expectation; 00163 00164 00165 /*--------------------------------- Binary operators -------------------------*/ 00166 00167 /* function composition, returns f(g(x)) */ 00168 it_function_args (itf_compose) { 00169 it_function_t f; 00170 it_args_t f_args; 00171 it_function_t g; 00172 it_args_t g_args; 00173 }; 00174 extern it_function_t itf_compose; 00175 00176 /* function sum, returns f(x) + g(x) */ 00177 it_function_args (itf_sum) { 00178 it_function_t f; 00179 it_args_t f_args; 00180 it_function_t g; 00181 it_args_t g_args; 00182 }; 00183 extern it_function_t itf_sum; 00184 00185 /* function product, returns f(x) * g(x) */ 00186 it_function_args (itf_mul) { 00187 it_function_t f; 00188 it_args_t f_args; 00189 it_function_t g; 00190 it_args_t g_args; 00191 }; 00192 extern it_function_t itf_mul; 00193 00194 /*-------------------------------- Wrappers ----------------------------------*/ 00195 double it_integrate (it_function_t function, it_args_t args, double a, 00196 double b); 00197 double it_differentiate (it_function_t function, it_args_t args, double a); 00198 00199 /* @} */ 00200 00201 00202 #endif
|
|