num4diff 0.4.6 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/ext/num4diff/CNum4Diff.c +86 -33
- data/ext/num4diff/CNum4Diff.h +10 -4
- data/lib/num4diff.rb +8 -6
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 236c79b20fa3b5aff7de7ac41181c458a3d42cf2c846e8a1d9e01265729fd762
|
4
|
+
data.tar.gz: d0ace9350577ba3cadeaa37f05061a406274194357b8912df2890efc1c0f4e38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d125cbc9bfdc8cc65ff57c9057c579ce1030c167f60af69c5723dc3c08d71fcb2c36822df3671dab5b0ca944b62de885e21e795943739e31b22d8505464ed3c9
|
7
|
+
data.tar.gz: ee2914de197552cd9be54a17e135da56cfb2a0ef36076fb14d66bedf379b3acf31d07f4a1db95a14d9d2aa0e39fdf38b8326979f200f59e4cb8b6fed44306b67
|
data/CHANGELOG.md
CHANGED
@@ -2,7 +2,15 @@
|
|
2
2
|
|
3
3
|
## Unreleased
|
4
4
|
|
5
|
-
## [0.
|
5
|
+
## [0.5.1] - 2023-05-18
|
6
|
+
|
7
|
+
### Fixed
|
8
|
+
- Fix correspondence of k step.
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
- chg wiki-uri
|
12
|
+
|
13
|
+
## [0.4.6] - 2023-04-24
|
6
14
|
|
7
15
|
### Fixed
|
8
16
|
- Fix Split into single-step and multi-step solutions.
|
data/ext/num4diff/CNum4Diff.c
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
static double CNum4Diff_doEulerMethod(double yi, double xi, double h, Func func);
|
7
7
|
static double CNum4Diff_doHeunMethod(double yi, double xi, double h, Func func);
|
8
8
|
static double CNum4Diff_doRungeKuttaMethod(double yi, double xi, double h, Func func);
|
9
|
-
static double CNum4Diff_doAdamsBashforthMethod(double a, double b, double y0, double h, Func func);
|
10
|
-
static double CNum4Diff_doAdamsMoultonMethod(double a, double b, double y0, double h, Func func);
|
9
|
+
static double CNum4Diff_doAdamsBashforthMethod(int k, double a, double b, double y0, double h, Func func);
|
10
|
+
static double CNum4Diff_doAdamsMoultonMethod(int k, double a, double b, double y0, double h, Func func);
|
11
11
|
static CNum4Diff _cNum4Diff = {
|
12
12
|
.tierMethod = {
|
13
13
|
.FP_eulerMethod = CNum4Diff_doEulerMethod,
|
@@ -19,6 +19,35 @@ static CNum4Diff _cNum4Diff = {
|
|
19
19
|
.FP_adamsMoultonMethod = CNum4Diff_doAdamsMoultonMethod,
|
20
20
|
},
|
21
21
|
};
|
22
|
+
/* アダムステーブル */
|
23
|
+
static AdamsTbl bash[] = {
|
24
|
+
[0] = {.s = 2, // k = 2
|
25
|
+
.bv = {[0] = 3, [1] = -1}
|
26
|
+
},
|
27
|
+
[1] = {.s = 12, // k = 3
|
28
|
+
.bv = {[0] = 23, [1] = -16, [2] = 5}
|
29
|
+
},
|
30
|
+
[2] = {.s = 24, // k = 4
|
31
|
+
.bv = {[0] = 55, [1] = -59, [2] = 37, [3] = -9}
|
32
|
+
},
|
33
|
+
[3] = {.s = 720, // k = 5
|
34
|
+
.bv = {[0] = 1901,[1] = -2774,[2] = 2616,[3] = -1274, [4] = 251}
|
35
|
+
},
|
36
|
+
};
|
37
|
+
static AdamsTbl moulton[] = {
|
38
|
+
[0] = {.s = 2, // k = 2
|
39
|
+
.bv = {[0] = 1, [1] = 1}
|
40
|
+
},
|
41
|
+
[1] = {.s = 12, // k = 3
|
42
|
+
.bv = {[0] = 5, [1] = 8, [2] = -1}
|
43
|
+
},
|
44
|
+
[2] = {.s = 24, // k = 4
|
45
|
+
.bv = {[0] = 9, [1] = 19, [2] = -5, [3] = 1}
|
46
|
+
},
|
47
|
+
[3] = {.s = 720, // k = 5
|
48
|
+
.bv = {[0] = 251,[1] = 646,[2] = -264,[3] = 106,[4] = -19}
|
49
|
+
},
|
50
|
+
};
|
22
51
|
/**************************************/
|
23
52
|
/* InterFface部 */
|
24
53
|
/**************************************/
|
@@ -43,19 +72,21 @@ double CNum4Diff_Tier_rungeKuttaMethod(double yi, double xi, double h, Func func
|
|
43
72
|
|
44
73
|
return _cNum4Diff.tierMethod.FP_rungeKuttaMethod(yi, xi, h, func);
|
45
74
|
}
|
46
|
-
double CNum4Diff_Multistage_adamsBashforthMethod(double a, double b, double y0, double h, Func func)
|
75
|
+
double CNum4Diff_Multistage_adamsBashforthMethod(int k, double a, double b, double y0, double h, Func func)
|
47
76
|
{
|
48
77
|
assert(func != 0);
|
49
78
|
assert(a < b);
|
79
|
+
assert((1 < k) && (k < 6));
|
50
80
|
|
51
|
-
return _cNum4Diff.multistageMethod.FP_adamsBashforthMethod(a, b, y0, h, func);
|
81
|
+
return _cNum4Diff.multistageMethod.FP_adamsBashforthMethod(k, a, b, y0, h, func);
|
52
82
|
}
|
53
|
-
double CNum4Diff_Multistage_adamsMoultonMethod(double a, double b, double y0, double h, Func func)
|
83
|
+
double CNum4Diff_Multistage_adamsMoultonMethod(int k, double a, double b, double y0, double h, Func func)
|
54
84
|
{
|
55
85
|
assert(func != 0);
|
56
86
|
assert(a < b);
|
87
|
+
assert((1 < k) && (k < 6));
|
57
88
|
|
58
|
-
return _cNum4Diff.multistageMethod.FP_adamsMoultonMethod(a, b, y0, h, func);
|
89
|
+
return _cNum4Diff.multistageMethod.FP_adamsMoultonMethod(k, a, b, y0, h, func);
|
59
90
|
}
|
60
91
|
/**************************************/
|
61
92
|
/* 処理実行部 */
|
@@ -101,50 +132,72 @@ static double CNum4Diff_doRungeKuttaMethod(double yi, double xi, double h, Func
|
|
101
132
|
return yi + (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;
|
102
133
|
}
|
103
134
|
/*
|
104
|
-
* アダムス・バッシュフォース法(
|
135
|
+
* アダムス・バッシュフォース法(k段)
|
105
136
|
*/
|
106
|
-
static double CNum4Diff_doAdamsBashforthMethod(double a, double b, double y0, double h, Func func)
|
137
|
+
static double CNum4Diff_doAdamsBashforthMethod(int k, double a, double b, double y0, double h, Func func)
|
107
138
|
{
|
108
139
|
double xi = a;
|
109
|
-
double fi;
|
110
|
-
double fi1 = 0;
|
111
|
-
double fi2 = 0;
|
112
140
|
double y = 0.0;
|
141
|
+
double *f = malloc(sizeof(double) * k);
|
142
|
+
int i;
|
143
|
+
double bk;
|
113
144
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
145
|
+
f[k - 1] = y0;
|
146
|
+
for (i = 0; i < k - 2; i++) {
|
147
|
+
f[k - (i + 2)] = CNum4Diff_Tier_rungeKuttaMethod(f[k - (i + 1)], xi, h, func);
|
148
|
+
}
|
118
149
|
for (xi = xi + h; xi < b; xi += h) {
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
150
|
+
f[0] = CNum4Diff_Tier_rungeKuttaMethod(f[1], xi, h, func);
|
151
|
+
// 予測子
|
152
|
+
bk = 0.0;
|
153
|
+
for (i = 0; i < k; i++) {
|
154
|
+
bk += bash[k - 2].bv[i] * f[i];
|
155
|
+
}
|
156
|
+
y = f[0] + h * bk / bash[k - 2].s;
|
157
|
+
// f値をずらす
|
158
|
+
for (i = 0; i < k - 1; i++) {
|
159
|
+
f[k - (i + 1)] = f[k - (i + 2)];
|
160
|
+
}
|
123
161
|
}
|
124
162
|
return y;
|
125
163
|
}
|
126
164
|
/*
|
127
|
-
* アダムス・ムルトン法(
|
165
|
+
* アダムス・ムルトン法(k段)
|
128
166
|
*/
|
129
|
-
static double CNum4Diff_doAdamsMoultonMethod(double a, double b, double y0, double h, Func func)
|
167
|
+
static double CNum4Diff_doAdamsMoultonMethod(int k, double a, double b, double y0, double h, Func func)
|
130
168
|
{
|
131
169
|
double xi = a;
|
132
|
-
double fi;
|
133
|
-
double fi1 = 0;
|
134
|
-
double fi2 = 0;
|
135
170
|
double y_pred = 0.0;
|
136
171
|
double y = 0.0;
|
172
|
+
double *f = malloc(sizeof(double) * k);
|
173
|
+
double *f2 = malloc(sizeof(double) * (k + 1));
|
174
|
+
int i;
|
175
|
+
double bk;
|
137
176
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
177
|
+
f[k - 1] = y0;
|
178
|
+
for (i = 0; i < k - 2; i++) {
|
179
|
+
f[k - (i + 2)] = CNum4Diff_Tier_rungeKuttaMethod(f[k - (i + 1)], xi, h, func);
|
180
|
+
}
|
142
181
|
for (xi = xi + h; xi < b; xi += h) {
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
182
|
+
f[0] = CNum4Diff_Tier_rungeKuttaMethod(f[1], xi, h, func);
|
183
|
+
// 予測子
|
184
|
+
bk = 0.0;
|
185
|
+
for (i = 0; i < k; i++) {
|
186
|
+
bk += bash[k - 2].bv[i] * f[i];
|
187
|
+
f2[i + 1] = f[i];
|
188
|
+
}
|
189
|
+
y_pred = f[0] + h * bk / bash[k - 2].s;
|
190
|
+
f2[0] = y_pred;
|
191
|
+
// 修正子
|
192
|
+
bk = 0.0;
|
193
|
+
for (i = 0; i < k; i++) {
|
194
|
+
bk += moulton[k - 2].bv[i] * f2[i];
|
195
|
+
}
|
196
|
+
y = f[0] + h * bk / moulton[k - 2].s;
|
197
|
+
// f値をずらす
|
198
|
+
for (i = 0; i < k - 1; i++) {
|
199
|
+
f[k - (i + 1)] = f[k - (i + 2)];
|
200
|
+
}
|
148
201
|
}
|
149
202
|
return y;
|
150
203
|
}
|
data/ext/num4diff/CNum4Diff.h
CHANGED
@@ -8,7 +8,13 @@ typedef struct _CNum4Diff CNum4Diff;
|
|
8
8
|
typedef struct _CNum4DiffTier CNum4DiffTier;
|
9
9
|
typedef struct _CNum4DiffMultistage CNum4DiffMultistage;
|
10
10
|
typedef double (*Func)(double x);
|
11
|
+
typedef struct _AdamsTbl AdamsTbl;
|
11
12
|
|
13
|
+
struct _AdamsTbl
|
14
|
+
{
|
15
|
+
int s;
|
16
|
+
int bv[5];
|
17
|
+
};
|
12
18
|
struct _CNum4DiffTier
|
13
19
|
{
|
14
20
|
double (*FP_eulerMethod)(double yi, double xi, double h, Func func);
|
@@ -17,8 +23,8 @@ struct _CNum4DiffTier
|
|
17
23
|
};
|
18
24
|
struct _CNum4DiffMultistage
|
19
25
|
{
|
20
|
-
|
21
|
-
|
26
|
+
double (*FP_adamsBashforthMethod)(int k, double a, double b, double y0, double h, Func func);
|
27
|
+
double (*FP_adamsMoultonMethod)(int k, double a, double b, double y0, double h, Func func);
|
22
28
|
;
|
23
29
|
};
|
24
30
|
struct _CNum4Diff
|
@@ -35,6 +41,6 @@ struct _CNum4Diff
|
|
35
41
|
double CNum4Diff_Tier_eulerMethod(double yi, double xi, double h, Func func);
|
36
42
|
double CNum4Diff_Tier_heunMethod(double yi, double xi, double h, Func func);
|
37
43
|
double CNum4Diff_Tier_rungeKuttaMethod(double yi, double xi, double h, Func func);
|
38
|
-
double CNum4Diff_Multistage_adamsBashforthMethod(double a, double b, double y0, double h, Func func);
|
39
|
-
double CNum4Diff_Multistage_adamsMoultonMethod(double a, double b, double y0, double h, Func func);
|
44
|
+
double CNum4Diff_Multistage_adamsBashforthMethod(int k, double a, double b, double y0, double h, Func func);
|
45
|
+
double CNum4Diff_Multistage_adamsMoultonMethod(int k, double a, double b, double y0, double h, Func func);
|
40
46
|
#endif
|
data/lib/num4diff.rb
CHANGED
@@ -52,8 +52,9 @@ module Num4DiffLib
|
|
52
52
|
attach_function :rungeKuttaMethod,
|
53
53
|
:CNum4Diff_Tier_rungeKuttaMethod, [:double, :double, :double, :f], :double
|
54
54
|
#
|
55
|
-
# アダムス・バッシュフォース法(
|
56
|
-
# @overload adamsBashforthMethod(a, b, y0, h, func)
|
55
|
+
# アダムス・バッシュフォース法(k段)による数値計算
|
56
|
+
# @overload adamsBashforthMethod(k, a, b, y0, h, func)
|
57
|
+
# @param [int] k k段アダムス法
|
57
58
|
# @param [double] a 下限値
|
58
59
|
# @param [double] b 上限値
|
59
60
|
# @param [double] y0 y(x0=a)の値
|
@@ -62,10 +63,11 @@ module Num4DiffLib
|
|
62
63
|
# @return [double] [a,b]の積分値
|
63
64
|
#
|
64
65
|
attach_function :adamsBashforthMethod,
|
65
|
-
:CNum4Diff_Multistage_adamsBashforthMethod, [:double, :double, :double, :double, :f], :double
|
66
|
+
:CNum4Diff_Multistage_adamsBashforthMethod, [:int, :double, :double, :double, :double, :f], :double
|
66
67
|
#
|
67
|
-
# アダムス・ムルトン法(
|
68
|
-
# @overload adamsMoultonMethod(a, b, y0, xi, h, func)
|
68
|
+
# アダムス・ムルトン法(k段)による数値計算
|
69
|
+
# @overload adamsMoultonMethod(k, a, b, y0, xi, h, func)
|
70
|
+
# @param [int] k k段アダムス法
|
69
71
|
# @param [double] a 下限値
|
70
72
|
# @param [double] b 上限値
|
71
73
|
# @param [double] y0 y0 y(x0=a)の値
|
@@ -74,5 +76,5 @@ module Num4DiffLib
|
|
74
76
|
# @return [double] [a,b]の積分値
|
75
77
|
#
|
76
78
|
attach_function :adamsMoultonMethod,
|
77
|
-
:CNum4Diff_Multistage_adamsMoultonMethod, [:double, :double, :double, :double, :f], :double
|
79
|
+
:CNum4Diff_Multistage_adamsMoultonMethod, [:int, :double, :double, :double, :double, :f], :double
|
78
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: num4diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- siranovel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi-compiler
|
@@ -69,8 +69,9 @@ licenses:
|
|
69
69
|
- MIT
|
70
70
|
metadata:
|
71
71
|
changelog_uri: http://github.com/siranovel/num4different/blob/main/CHANGELOG.md
|
72
|
-
documentation_uri: https://rubydoc.info/gems/num4diff/0.
|
72
|
+
documentation_uri: https://rubydoc.info/gems/num4diff/0.5.1
|
73
73
|
homepage_uri: http://github.com/siranovel/num4different
|
74
|
+
wiki_uri: https://github.com/siranovel/mydocs/tree/main/num4different
|
74
75
|
post_install_message:
|
75
76
|
rdoc_options: []
|
76
77
|
require_paths:
|