-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC2DLibrary.c
91 lines (76 loc) · 2.37 KB
/
C2DLibrary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// MathLibrary.cpp
// compile with: cl /c /EHsc MathLibrary.cpp
// post-build command: lib MathLibrary.obj
#include "C2DLibrary.h"
double fact(int n){
if (n == 0 || n == 1)return 1;
return n * fact(n - 1);
}
double comb(int i, int j){
return fact(i) / fact(i - j) / fact(j);
}
double fastpow(double base, int n) {
if (base == -1) {
if (n % 2 == 0)return 1;
return -1;
}
if (n == 0)return 1;
if (n == 1)return base;
else {
double k = fastpow(base, n / 2);
double tmp = k * k;
if (n % 2 == 0)return tmp;
else return base * tmp;
}
}
//분모 차수, 분자 차수, 분모 계수, 분자 계수, 샘플링 주기, 결과 저장할 이차원 배열
void blt(int m, int n, double *a, double *b, double T, double** ret){
double K = 2 / T;
double *c;
int8_t sz = m;
c = (double*)malloc((int64_t)sizeof(double) * (int64_t)(m + 1));
double *d;
d = (double*)malloc((int64_t)sizeof(double) * (int64_t)(n + 1));
for (int t = 0; t <= n; t++) {
double tm = 0;
for (int i = 0; i <= n; i++) {
double tmp = 0;
for (int j = 0; j <= t; j++) {
if (n - i < t - j)continue;
if (i < j)continue;
tmp += comb(i, j) * fastpow(-1, i - j) * comb(n - i, n - i - t + j);
}
tmp *= b[i] * fastpow(K,i);
tm += tmp;
}
d[t] = tm;
}
for (int t = 0; t <= m; t++) {
double tm = 0;
for (int i = 0; i <= m; i++) {
double tmp = 0;
for (int j = 0; j <= t; j++) {
if (n - i < t - j)continue;
if (i < j)continue;
tmp += comb(i, j) * fastpow(-1, i - j) * comb(n - i, n - i - t + j);
}
tmp *= a[i] * fastpow(K, i);
tm += tmp;
}
c[t] = tm;
}
double *dnew;
dnew = (double*)malloc((int64_t)sizeof(double) * (int64_t)(m + 1));
for (int i = 0; i <= m; i++) {
double tmp = 0;
for (int j = 0; j <= i; j++) {
if (m - n < i - j)continue;
tmp += d[j] * comb(m - n, i - j);
}
dnew[i] = tmp;
}
for (int i = 0; i <= m; i++)ret[0][i] = dnew[m - i];
for (int i = 0; i <= m; i++)ret[1][i] = c[m - i];
free(c);
free(d);
}