Changeset 2639 in Sophya
- Timestamp:
- Nov 22, 2004, 5:57:40 PM (21 years ago)
- Location:
- trunk/SophyaLib/NTools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/nbmath.c
r682 r2639 2734 2734 return(nsuse); 2735 2735 } 2736 2737 /*=========================================== cmv 22/11/04 ===*/ 2738 /* 2739 ++ 2740 double log_factoriel(unsigned int n) 2741 2742 Compute the neperian log of n! 2743 -- 2744 */ 2745 double log_factoriel(unsigned int n) 2746 { 2747 unsigned int i; 2748 double sum=0.; 2749 if(n<2) return sum; 2750 for(i=n;i>1;i--) sum += log((double)i); 2751 return sum; 2752 } 2753 2754 /* 2755 ++ 2756 double log_stirling(unsigned int n) 2757 2758 Compute the neperian log of the Stirling approx of n! 2759 -- 2760 */ 2761 double log_stirling(unsigned int n) 2762 /* 2763 Approx: n! ~ sqrt(2Pi n) n^n exp(-n) = sqrt(2Pi) n^(n+1/2) exp(-n) 2764 log(n!) ~ (n+1/2) log(n) -n + 1/2 log(2Pi) 2765 (ordre 0: log(n!) ~ n (log(n)-1) 2766 */ 2767 { 2768 if(n<1) return 0.; 2769 return (n+0.5)*log(n) - n + Hln2pi; 2770 } 2771 2772 /* 2773 ++ 2774 double log_gosper(unsigned int n); 2775 2776 Compute the neperian log of the Gosper approx of n! 2777 -- 2778 */ 2779 double log_gosper(unsigned int n) 2780 /* 2781 Approx: n! ~ sqrt( (2n+1/3) Pi ) n^n exp(-n) 2782 log(n!) ~ 1/2 log( (2n+1/3) Pi ) + n log(n) -n 2783 */ 2784 { 2785 if(n<1) return 0.; 2786 return 0.5*log((2.*n+1./3.)*M_PI) + n*log(n) -n; 2787 } 2788 2789 /* 2790 ++ 2791 double give_log_factoriel(unsigned int n); 2792 2793 Compute the neperian log of the approx of n! 2794 -- 2795 */ 2796 static short give_log_factoriel_OK = 0; 2797 #define give_log_factoriel_LIM 21 2798 static double give_log_factoriel_TAB[give_log_factoriel_LIM]; 2799 double give_log_factoriel(unsigned int n) 2800 { 2801 int i; 2802 if(give_log_factoriel_OK==0) { 2803 give_log_factoriel_OK=1; 2804 for(i=0;i<give_log_factoriel_LIM;i++) 2805 give_log_factoriel_TAB[i]=log_factoriel(i); 2806 } 2807 2808 if(n<give_log_factoriel_LIM) return give_log_factoriel_TAB[n]; 2809 return log_gosper(n); 2810 } 2811 #undef give_log_factoriel_LIM -
trunk/SophyaLib/NTools/nbmath.h
r756 r2639 90 90 ,int npass,float perclean,int lp); 91 91 92 double log_factoriel(unsigned int n); 93 double log_stirling(unsigned int n); 94 double log_gosper(unsigned int n); 95 double give_log_factoriel(unsigned int n); 96 92 97 #ifdef __cplusplus 93 98 }
Note:
See TracChangeset
for help on using the changeset viewer.