num4inte 0.0.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 391c7af590564ae676a706c71d1c3d67ba1d8f3c3d01cece8bc5b298d13de1c9
4
- data.tar.gz: fae05e502bb9acc4a4456fb285a9cae64d43d895e8822e21b68a7b7b31be7bdd
3
+ metadata.gz: 1f3bd51f9372806b1b4251367e31852d5fa1ceab2a154b96481d8c193867e1ba
4
+ data.tar.gz: 0b40e2490195c0b39c71fd39d64c6c63f375ffc585376dc2727255d0297accc5
5
5
  SHA512:
6
- metadata.gz: 19add3204882ae70fe3173567f13e1f268da893df1a46f73dd58833191f5ec194d9200cc4769527a4a74995f5a0257a794acf4c1b27692e31763c5254ff4240d
7
- data.tar.gz: 9d81760ab9526a4ddc621ba374f1e30ae35d339903f9c301e3aee15bf64b857768044d74f09893011f11e81c38b526fd6f9fe75356475372bcd10f843abd0799
6
+ metadata.gz: 983873e08341371a05526319aaede30562dac9c1a3ba29ea42538aedfdffd4c2f00300df688c745fb971bee89172ea22d56832fd343d44167ba751a9acb6d3f0
7
+ data.tar.gz: cf9f75b196b7c711084b63d0fad68ade85866073c609ca6c8cfe50b68d137c2dc8af43c4d4aca6b2db52ce331a172b2859644c958c2570277fbdf9bd1e021a1a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.1.2] - 2023-05-24
6
+
7
+ ### Added
8
+ - add function of gaussLegendreRule.
9
+
10
+ ## [0.1.1] - 2023-04-27
11
+
12
+ ### Added
13
+ - add function of simpsonRule.
14
+ - add assert(a < b);
15
+
16
+ ### Fixed
17
+ - change from leftReimannSumMetod to leftReimannSumMethod.
18
+ - change from rigtReimannSumMetod to rigtReimannSumMethod.
19
+ - change from newtonMetod to trapezioidalRule.
20
+
5
21
  ## [0.0.1] - 2023-04-25
6
22
 
7
23
  ### Fixed
@@ -3,13 +3,50 @@
3
3
  #include <assert.h>
4
4
  #include "CNum4Inte.h"
5
5
 
6
- static double CNum4Inte_doLeftReimannSumMetod(double a, double b, double h, Func func);
6
+ static double CNum4Inte_doLeftReimannSumMethod(double a, double b, double h, Func func);
7
7
  static double CNum4Inte_doRigtReimannSumMetod(double a, double b, double h, Func func);
8
- static double CNum4Inte_doNewtonMethod(double a, double b, double h, Func func);
8
+ static double CNum4Inte_doTrapezioidalRule(double a, double b, double h, Func func);
9
+ static double CNum4Inte_doSimpsonRule(double a, double b, double h, Func func);
10
+ static double CNum4Inte_doGaussLegendreRule(int n, double a, double b, double h, Func func);
9
11
  static CNum4Inte _cNum4Inte = {
10
- .FP_leftReimannSumMetod = CNum4Inte_doLeftReimannSumMetod,
11
- .FP_rigtReimannSumMetod = CNum4Inte_doRigtReimannSumMetod,
12
- .FP_newtonMethod = CNum4Inte_doNewtonMethod,
12
+ .reimann = {
13
+ .FP_leftReimannSumMethod = CNum4Inte_doLeftReimannSumMethod,
14
+ .FP_rigtReimannSumMethod = CNum4Inte_doRigtReimannSumMetod,
15
+ },
16
+ .newton = {
17
+ .FP_trapezioidalRule = CNum4Inte_doTrapezioidalRule,
18
+ .FP_simpsonRule = CNum4Inte_doSimpsonRule,
19
+ },
20
+ .gauss = {
21
+ .FP_gaussLegendreRule = CNum4Inte_doGaussLegendreRule,
22
+ },
23
+ };
24
+ // ガウス・テーブル
25
+ // n = 1
26
+ // xi = 0 wi = 2
27
+ // n = 2
28
+ // xi = ±1/sqrt(3)=0.577350 wi = 1
29
+ // n = 3
30
+ // xi = 0 wi = 8/9
31
+ // xi = ±sqrt(3/5)=0.774597 wi = 5/9
32
+ // n = 4
33
+ // xi = ±sqrt(3/7-2/7sqrt(6/5))=0.339981 wi = (18+sqrt(30))/36=0.652145
34
+ // xi = ±sqrt(3/7+2/7sqrt(6/5))=0.861136 wi = (18-sqrt(30))/36=0.347855
35
+ // n = 5
36
+ // xi = 0 wi = 128/225
37
+ // xi = 1/3*sqrt(5-2sqrt(10/7))=0.538469 wi = (322+13sqrt(70))/900:0.478629
38
+ // xi = 1/3*sqrt(5+2sqrt(10/7))=0.906180 wi = (322-13sqrt(70))/900:0.236927
39
+ static GaussTbl gaussTbl[] = {
40
+ [0] = {.xi = {[0] = 0.0},
41
+ .wi = {[0] = 2.0}},
42
+ [1] = {.xi = {[0] = -0.577350, [1] = 0.577350},
43
+ .wi = {[0] = 1.0, [1] = 1.0}},
44
+ [2] = {.xi = {[0] = 0.0, [1] = -0.774597, [2] = 0.774597},
45
+ .wi = {[0] = 0.888889, [1] = 0.555556, [2] = 0.555556}},
46
+ [3] = {.xi = {[0] = -0.339981, [1] = 0.339981, [2] = -0.861136, [3] = 0.861136},
47
+ .wi = {[0] = 0.652145, [1] = 0.652145, [2] = 0.347855, [3] = 0.347855}},
48
+ [4] = {.xi = {[0] = 0.0, [1] = -0.538469, [2] = 0.538469, [3] = -0.906180, [4] = 0.906180},
49
+ .wi = {[0] = 0.568889, [1] = 0.478629, [2] = 0.478629, [3] = 0.236927, [4] = 0.236927}},
13
50
  };
14
51
  /**************************************/
15
52
  /* InterFface部 */
@@ -17,23 +54,41 @@ static CNum4Inte _cNum4Inte = {
17
54
  /**************************************/
18
55
  /* Class部 */
19
56
  /**************************************/
20
- double CNum4Inte_leftReimannSumMetod(double a, double b, double h, Func func)
57
+ double CNum4Inte_reimann_leftReimannSumMethod(double a, double b, double h, Func func)
58
+ {
59
+ assert(func != 0);
60
+ assert(a < b);
61
+
62
+ return _cNum4Inte.reimann.FP_leftReimannSumMethod(a, b, h, func);
63
+ }
64
+ double CNum4Inte_reimann_rigtReimannSumMethod(double a, double b, double h, Func func)
65
+ {
66
+ assert(func != 0);
67
+ assert(a < b);
68
+
69
+ return _cNum4Inte.reimann.FP_rigtReimannSumMethod(a, b, h, func);
70
+ }
71
+ double CNum4Inte_rewton_trapezioidalRule(double a, double b, double h, Func func)
21
72
  {
22
73
  assert(func != 0);
74
+ assert(a < b);
23
75
 
24
- return _cNum4Inte.FP_leftReimannSumMetod(a, b, h, func);
76
+ return _cNum4Inte.newton.FP_trapezioidalRule(a, b, h, func);
25
77
  }
26
- double CNum4Inte_rigtReimannSumMetod(double a, double b, double h, Func func)
78
+ double CNum4Inte_rewton_simpsonRule(double a, double b, double h, Func func)
27
79
  {
28
80
  assert(func != 0);
81
+ assert(a < b);
29
82
 
30
- return _cNum4Inte.FP_rigtReimannSumMetod(a, b, h, func);
83
+ return _cNum4Inte.newton.FP_simpsonRule(a, b, h, func);
31
84
  }
32
- double CNum4Inte_newtonMethod(double a, double b, double h, Func func)
85
+ double CNum4Inte_gauss_gaussLegendreRule(int n, double a, double b, double h, Func func)
33
86
  {
34
87
  assert(func != 0);
88
+ assert(a < b);
89
+ assert((1 <= n) && (n <= 5));
35
90
 
36
- return _cNum4Inte.FP_newtonMethod(a, b, h, func);
91
+ return _cNum4Inte.gauss.FP_gaussLegendreRule(n, a, b, h, func);
37
92
  }
38
93
  /**************************************/
39
94
  /* 処理実行部 */
@@ -41,10 +96,10 @@ double CNum4Inte_newtonMethod(double a, double b, double h, Func func)
41
96
  /*
42
97
  * 左リーマン和法
43
98
  */
44
- static double CNum4Inte_doLeftReimannSumMetod(double a, double b, double h, Func func)
99
+ static double CNum4Inte_doLeftReimannSumMethod(double a, double b, double h, Func func)
45
100
  {
46
101
  double rimann = 0.0;
47
- double x = 0.0;
102
+ double x;
48
103
 
49
104
  for (x = a; x < b; x += h) {
50
105
  rimann += func(x);
@@ -57,7 +112,7 @@ static double CNum4Inte_doLeftReimannSumMetod(double a, double b, double h, Func
57
112
  static double CNum4Inte_doRigtReimannSumMetod(double a, double b, double h, Func func)
58
113
  {
59
114
  double rimann = 0.0;
60
- double x = 0.0;
115
+ double x;
61
116
 
62
117
  for (x = a; x < b; x += h) {
63
118
  rimann += func(x + h);
@@ -65,21 +120,66 @@ static double CNum4Inte_doRigtReimannSumMetod(double a, double b, double h, Func
65
120
  return rimann * h;
66
121
  }
67
122
  /*
68
- * ニュートン・シンプソン法
123
+ * 台形公式
69
124
  */
70
- static double CNum4Inte_doNewtonMethod(double a, double b, double h, Func func)
125
+ static double CNum4Inte_doTrapezioidalRule(double a, double b, double h, Func func)
71
126
  {
72
- double newton = 0.0;
73
- double x = 0.0;
127
+ double trapezoidal = 0.0;
74
128
  double fx = func(a);
75
129
  double fxh = 0.0;
130
+ double x;
76
131
 
77
132
  for (x = a; x < b; x += h) {
78
133
  fxh = func(x + h);
79
- newton += (fx + fxh) /2;
134
+ trapezoidal += (fx + fxh);
80
135
  fx = fxh;
81
136
  }
82
- return newton * h;
137
+ return trapezoidal * h / 2.0;
138
+ }
139
+ /*
140
+ * シンプソンの公式
141
+ */
142
+ static double CNum4Inte_doSimpsonRule(double a, double b, double h, Func func)
143
+ {
144
+ double simpson = 0.0;
145
+ double fx = func(a);
146
+ double fxh = 0.0;
147
+ double h2 = h / 2.0;
148
+ double x;
149
+
150
+ for (x = a; x < b; x += h) {
151
+ double fxh2 = func(x + h2);
152
+ double fxh = func(x + h);
153
+
154
+ simpson += (fx + fxh + 4 * fxh2);
155
+ fx = fxh2;
156
+ }
157
+ return simpson * h / 6.0;
158
+
159
+ }
160
+ /*
161
+ * ガウス・ルジャンドルの公式
162
+ */
163
+ static double CNum4Inte_doGaussLegendreRule(int n, double a, double b, double h, Func func)
164
+ {
165
+ double x;
166
+ double gauss = 0.0;
167
+ GaussTbl *pt = &gaussTbl[n - 1];
168
+
169
+ for (x = a; x < b; x += h) {
170
+ int i;
171
+ double xa = x;
172
+ double xb = x + h;
173
+ double bMa = (xb - xa) / 2.0;
174
+ double aPb = (xa + xb) / 2.0;
175
+ double wifi = 0.0;
176
+
177
+ for (i = 0; i < n; i++) {
178
+ wifi += pt->wi[i] * func(bMa * pt->xi[i] + aPb);
179
+ }
180
+ gauss += bMa * wifi;
181
+ }
182
+ return gauss;
83
183
  }
84
184
 
85
185
 
@@ -5,21 +5,46 @@
5
5
  /* 構造体宣言 */
6
6
  /**************************************/
7
7
  typedef struct _CNum4Inte CNum4Inte;
8
+ typedef struct _CReimann CReimann;
9
+ typedef struct _CNewton CNewton;
10
+ typedef struct _CGauss CGauss;
8
11
  typedef double (*Func)(double x);
12
+ typedef struct _GaussTbl GaussTbl;
9
13
 
14
+ struct _CReimann
15
+ {
16
+ double (*FP_leftReimannSumMethod)(double a, double b, double h, Func func);
17
+ double (*FP_rigtReimannSumMethod)(double a, double b, double h, Func func);
18
+ };
19
+ struct _CNewton
20
+ {
21
+ double (*FP_trapezioidalRule)(double a, double b, double h, Func func);
22
+ double (*FP_simpsonRule)(double a, double b, double h, Func func);
23
+ };
24
+ struct _CGauss
25
+ {
26
+ double (*FP_gaussLegendreRule)(int n, double a, double b, double h, Func func);
27
+ };
10
28
  struct _CNum4Inte
11
29
  {
12
- double (*FP_leftReimannSumMetod)(double a, double b, double h, Func func);
13
- double (*FP_rigtReimannSumMetod)(double a, double b, double h, Func func);
14
- double (*FP_newtonMethod)(double a, double b, double h, Func func);
30
+ CReimann reimann;
31
+ CNewton newton;
32
+ CGauss gauss;
33
+ };
34
+ struct _GaussTbl
35
+ {
36
+ double xi[5];
37
+ double wi[5];
15
38
  };
16
39
  /**************************************/
17
40
  /* define宣言 */
18
41
  /**************************************/
19
42
  /**************************************/
20
- /* プロトタイプ宣言 */
43
+ /* プロトタイプ宣言 */
21
44
  /**************************************/
22
- double CNum4Inte_leftReimannSumMetod(double a, double b, double h, Func func);
23
- double CNum4Inte_rigtReimannSumMetod(double a, double b, double h, Func func);
24
- double CNum4Inte_newtonMethod(double a, double b, double h, Func func);
45
+ double CNum4Inte_reimann_leftReimannSumMethod(double a, double b, double h, Func func);
46
+ double CNum4Inte_reimann_rigtReimannSumMethod(double a, double b, double h, Func func);
47
+ double CNum4Inte_rewton_trapezioidalRule(double a, double b, double h, Func func);
48
+ double CNum4Inte_rewton_simpsonRule(double a, double b, double h, Func func);
49
+ double CNum4Inte_gauss_gaussLegendreRule(int n, double a, double b, double h, Func func);
25
50
  #endif
@@ -1,5 +1,5 @@
1
1
  require 'ffi-compiler/compile_task'
2
2
 
3
3
  FFI::Compiler::CompileTask.new('num4inte') do |c|
4
- c.have_header?('CNum4Inte', '.')
4
+ c.have_header?('CNum4Inte.h', '.')
5
5
  end
data/lib/num4inte.rb CHANGED
@@ -17,39 +17,63 @@ module Num4InteLib
17
17
 
18
18
  #
19
19
  # 左リーマン和法
20
- # @overload leftReimannSumMetod(a, b, h, func)
21
- # y = leftReimannSumMetod(a, b, h, func)
20
+ # @overload leftReimannSumMethod(a, b, h, func)
21
+ # y = leftReimannSumMethod(a, b, h, func)
22
22
  # @param [double] a aの値
23
23
  # @param [double] b bの値
24
24
  # @param [double] h 刻み幅
25
25
  # @param [callback] func xiに対する傾きを計算する関数
26
26
  # @return [double] [a,b]の間の積分値
27
27
  #
28
- attach_function :leftReimannSumMetod,
29
- :CNum4Inte_leftReimannSumMetod, [:double, :double, :double, :f], :double
28
+ attach_function :leftReimannSumMethod,
29
+ :CNum4Inte_reimann_leftReimannSumMethod, [:double, :double, :double, :f], :double
30
30
  #
31
31
  # 右リーマン和法
32
- # @overload rigtReimannSumMetod(a, b, h, func)
33
- # y = rigtReimannSumMetod(a, b, h, func)
32
+ # @overload rigtReimannSumMethod(a, b, h, func)
33
+ # y = rigtReimannSumMethod(a, b, h, func)
34
34
  # @param [double] a aの値
35
35
  # @param [double] b bの値
36
36
  # @param [double] h 刻み幅
37
37
  # @param [callback] func xiに対する傾きを計算する関数
38
38
  # @return [double] [a,b]の間の積分値
39
39
  #
40
- attach_function :rigtReimannSumMetod,
41
- :CNum4Inte_rigtReimannSumMetod, [:double, :double, :double, :f], :double
40
+ attach_function :rigtReimannSumMethod,
41
+ :CNum4Inte_reimann_rigtReimannSumMethod, [:double, :double, :double, :f], :double
42
42
  #
43
- # ニュートン・シンプソン法
44
- # @overload newtonMethod(a, b, h, func)
45
- # y = newtonMethod(a, b, h, func)
43
+ # ニュートン・コーツ法(1次:台形公式)
44
+ # @overload trapezioidalRule(a, b, h, func)
45
+ # y = trapezioidalRule(a, b, h, func)
46
46
  # @param [double] a aの値
47
47
  # @param [double] b bの値
48
48
  # @param [double] h 刻み幅
49
49
  # @param [callback] func xiに対する傾きを計算する関数
50
50
  # @return [double] [a,b]の間の積分値
51
51
  #
52
- attach_function :newtonMethod,
53
- :CNum4Inte_newtonMethod, [:double, :double, :double, :f], :double
52
+ attach_function :trapezioidalRule,
53
+ :CNum4Inte_rewton_trapezioidalRule, [:double, :double, :double, :f], :double
54
+ #
55
+ # ニュートン・コーツ法(2次:シンプソンの公式)
56
+ # @overload simpsonRule(a, b, h, func)
57
+ # y = simpsonRule(a, b, h, func)
58
+ # @param [double] a aの値
59
+ # @param [double] b bの値
60
+ # @param [double] h 刻み幅
61
+ # @param [callback] func xiに対する傾きを計算する関数
62
+ # @return [double] [a,b]の間の積分値
63
+ #
64
+ attach_function :simpsonRule,
65
+ :CNum4Inte_rewton_simpsonRule, [:double, :double, :double, :f], :double
66
+ #
67
+ # ガウス求積法
68
+ # @overload gaussLegendreRule(n, a, b, h, func)
69
+ # y = gaussLegendreRule(n, a, b, h, func)
70
+ # @param [int] n 分割数
71
+ # @param [double] a aの値
72
+ # @param [double] b bの値
73
+ # @param [double] h 刻み幅
74
+ # @param [callback] func xiに対する傾きを計算する関数
75
+ # @return [double] [a,b]の間の積分値
76
+ attach_function :gaussLegendreRule,
77
+ :CNum4Inte_gauss_gaussLegendreRule, [:int, :double, :double, :double, :f], :double
54
78
  end
55
79
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4inte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-25 00:00:00.000000000 Z
11
+ date: 2023-05-24 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/num4integral/blob/main/CHANGELOG.md
72
- documentation_uri: https://rubydoc.info/gems/num4inte/0.0.1
72
+ documentation_uri: https://rubydoc.info/gems/num4inte/0.1.2
73
73
  homepage_uri: http://github.com/siranovel/num4integral
74
+ wiki_uri: https://github.com/siranovel/mydocs/tree/main/num4integral
74
75
  post_install_message:
75
76
  rdoc_options: []
76
77
  require_paths: