JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
mt.cpp
Go to the documentation of this file.
1
7#include "mt.h"
8
9double EPS = 1.0e-6;
10
11
17double power(double x, double y)
18{
19 if (y==0.0) return 1.0;
20 else if (x==0.0) return 0.0;
21 else return exp((y)*log(x));
22}
23
24
30double fact(int n)
31{
32 int i;
33 double ret = 1.0;
34
35 if (n>0) for (i=1; i<=n; i++) ret = ret*(double)i;
36 return ret;
37}
38
39
45double perm(int n, int m)
46{
47 int i;
48 double p;
49
50 if (m>n || n<=0 || m<=0) return 0.;
51
52 p = 1.0;
53 for (i=n-m+1; i<=n; i++) p = p*(double)i;
54
55 return p;
56}
57
58
64double comb(int n, int m)
65{
66 int i;
67 double c, p;
68
69 if (m>n || n<=0 || m<=0) return 0.;
70
71 p = c = 1.0;
72 for (i=n-m+1; i<=n; i++) p = p*(double)i;
73 for (i=1; i<=m; i++) c = c*(double)i;
74
75 return p/c;
76}
77
78
79#define MAX_ITRTN_NEWTON_METHOD 30
80
95int newton_method(PTR_DFFUNC func, PTR_DFFUNC dfunc, double* t0, double eps)
96{
97 double tt = *t0;
98 double ff = (*func)(tt);
99
100 int n = 1;
101 while (n<MAX_ITRTN_NEWTON_METHOD && Xabs(ff)>eps) {
102 //
103 double df = (*dfunc)(tt);
104 if (Xabs(df)<eps) return 0;
105
106 tt = tt - ff/df;
107 ff = (*func)(tt);
108 n++;
109 }
110
111 if (n==MAX_ITRTN_NEWTON_METHOD) return 0;
112 *t0 = tt;
113 return n;
114}
115
#define Xabs(x)
Definition common.h:257
#define MAX_ITRTN_NEWTON_METHOD
ニュートン法で解を求める際の,最大繰り返し回数.
Definition mt.cpp:79
double perm(int n, int m)
Definition mt.cpp:45
double power(double x, double y)
Definition mt.cpp:17
double comb(int n, int m)
Definition mt.cpp:64
double EPS
Definition mt.cpp:9
int newton_method(PTR_DFFUNC func, PTR_DFFUNC dfunc, double *t0, double eps)
ニュートン法で解を求める
Definition mt.cpp:95
double fact(int n)
Definition mt.cpp:30
数学関数ヘッダ mt.h
double(* PTR_DFFUNC)(double)
ニュートン法で解を求める場合の関数の型
Definition mt.h:20