JWS C Library
C language utility library
func.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  (*/
00033 
00044 #include <jwsc/config.h>
00045 
00046 #include <inttypes.h>
00047 #include <math.h>
00048 
00049 #include "jwsc/math/func.h"
00050 
00052 double log_gamma_d(double x)
00053 {
00054     return (x + 0.5) * log(x + 5.5) - x - 5.5 + 
00055              log(2.5066282746310005 * 
00056                 (
00057                     1.000000000190015 + 
00058                     76.18009172947146     /(1 + x) +
00059                     -86.50532032941677    /(2 + x) +
00060                     24.01409824083091     /(3 + x) +
00061                     -1.231739572450155    /(4 + x) +
00062                     0.1208650973866179e-2 /(5 + x) + 
00063                     -0.5395239384953e-5   /(6 + x)
00064                 ) / x);
00065 }
00066 
00067 
00075 double beta_d(double x, double y)
00076 {
00077     // beta must be computed in log space for large x, y, 
00078     return exp(log_beta_d(x, y));
00079 }
00080 
00088 double mult_beta_d(const double* const x, uint32_t num_elts)
00089 {
00090     // beta must be computed in log space for large values
00091     return exp(log_mult_beta_d(x, num_elts));
00092 }
00093 
00099 double log_beta_d(double x, double y)
00100 {
00101     return log_gamma_d(x) + log_gamma_d(y) - log_gamma_d(x + y);
00102 }
00103 
00109 double log_mult_beta_d(const double* const x, uint32_t num_elts)
00110 {
00111     double numerator = 1;
00112     double denominator = 0;
00113 
00114     uint32_t i;
00115     for(i = 0; i < num_elts; i++)
00116     {
00117         numerator += log_gamma_d(x[i]);
00118         denominator += x[i];
00119     }
00120 
00121     denominator = log_gamma_d(denominator);
00122 
00123     return numerator - denominator;
00124 }