num4inte 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c48de95bf99ed8d7bbaf4dd32898e1b987c2255cbf6a36c1f963fe2334077fa
4
- data.tar.gz: a50e8f20a59355f2e1517c54566eeee53335e8f0dee9fe0272c70fb31a8d06fd
3
+ metadata.gz: 1f3bd51f9372806b1b4251367e31852d5fa1ceab2a154b96481d8c193867e1ba
4
+ data.tar.gz: 0b40e2490195c0b39c71fd39d64c6c63f375ffc585376dc2727255d0297accc5
5
5
  SHA512:
6
- metadata.gz: b17771e09ba3d7adae0e5c0fd261d9a6cf5fe5709ea0e3416befd16199189a58d19a9509700ae22b2d9e4a147278c02266c1dfe6f5cc0ce6608c1711597729f7
7
- data.tar.gz: 821494a9475350c8a3a17b896fe433fe7c7204113552a9333cab29cb4f91ddd2fb1617c05f5c7b0edd0074e047532bbff347b3ca8f978a88c8a0e71e8f91390b
6
+ metadata.gz: 983873e08341371a05526319aaede30562dac9c1a3ba29ea42538aedfdffd4c2f00300df688c745fb971bee89172ea22d56832fd343d44167ba751a9acb6d3f0
7
+ data.tar.gz: cf9f75b196b7c711084b63d0fad68ade85866073c609ca6c8cfe50b68d137c2dc8af43c4d4aca6b2db52ce331a172b2859644c958c2570277fbdf9bd1e021a1a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.1.2] - 2023-05-24
6
+
7
+ ### Added
8
+ - add function of gaussLegendreRule.
9
+
5
10
  ## [0.1.1] - 2023-04-27
6
11
 
7
12
  ### Added
@@ -7,6 +7,7 @@ static double CNum4Inte_doLeftReimannSumMethod(double a, double b, double h, Fun
7
7
  static double CNum4Inte_doRigtReimannSumMetod(double a, double b, double h, Func func);
8
8
  static double CNum4Inte_doTrapezioidalRule(double a, double b, double h, Func func);
9
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);
10
11
  static CNum4Inte _cNum4Inte = {
11
12
  .reimann = {
12
13
  .FP_leftReimannSumMethod = CNum4Inte_doLeftReimannSumMethod,
@@ -16,6 +17,36 @@ static CNum4Inte _cNum4Inte = {
16
17
  .FP_trapezioidalRule = CNum4Inte_doTrapezioidalRule,
17
18
  .FP_simpsonRule = CNum4Inte_doSimpsonRule,
18
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}},
19
50
  };
20
51
  /**************************************/
21
52
  /* InterFface部 */
@@ -51,6 +82,14 @@ double CNum4Inte_rewton_simpsonRule(double a, double b, double h, Func func)
51
82
 
52
83
  return _cNum4Inte.newton.FP_simpsonRule(a, b, h, func);
53
84
  }
85
+ double CNum4Inte_gauss_gaussLegendreRule(int n, double a, double b, double h, Func func)
86
+ {
87
+ assert(func != 0);
88
+ assert(a < b);
89
+ assert((1 <= n) && (n <= 5));
90
+
91
+ return _cNum4Inte.gauss.FP_gaussLegendreRule(n, a, b, h, func);
92
+ }
54
93
  /**************************************/
55
94
  /* 処理実行部 */
56
95
  /**************************************/
@@ -118,6 +157,30 @@ static double CNum4Inte_doSimpsonRule(double a, double b, double h, Func func)
118
157
  return simpson * h / 6.0;
119
158
 
120
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;
183
+ }
121
184
 
122
185
 
123
186
 
@@ -7,7 +7,9 @@
7
7
  typedef struct _CNum4Inte CNum4Inte;
8
8
  typedef struct _CReimann CReimann;
9
9
  typedef struct _CNewton CNewton;
10
+ typedef struct _CGauss CGauss;
10
11
  typedef double (*Func)(double x);
12
+ typedef struct _GaussTbl GaussTbl;
11
13
 
12
14
  struct _CReimann
13
15
  {
@@ -19,19 +21,30 @@ struct _CNewton
19
21
  double (*FP_trapezioidalRule)(double a, double b, double h, Func func);
20
22
  double (*FP_simpsonRule)(double a, double b, double h, Func func);
21
23
  };
24
+ struct _CGauss
25
+ {
26
+ double (*FP_gaussLegendreRule)(int n, double a, double b, double h, Func func);
27
+ };
22
28
  struct _CNum4Inte
23
29
  {
24
30
  CReimann reimann;
25
31
  CNewton newton;
32
+ CGauss gauss;
33
+ };
34
+ struct _GaussTbl
35
+ {
36
+ double xi[5];
37
+ double wi[5];
26
38
  };
27
39
  /**************************************/
28
40
  /* define宣言 */
29
41
  /**************************************/
30
42
  /**************************************/
31
- /* プロトタイプ宣言 */
43
+ /* プロトタイプ宣言 */
32
44
  /**************************************/
33
45
  double CNum4Inte_reimann_leftReimannSumMethod(double a, double b, double h, Func func);
34
46
  double CNum4Inte_reimann_rigtReimannSumMethod(double a, double b, double h, Func func);
35
47
  double CNum4Inte_rewton_trapezioidalRule(double a, double b, double h, Func func);
36
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);
37
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
@@ -63,5 +63,17 @@ module Num4InteLib
63
63
  #
64
64
  attach_function :simpsonRule,
65
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
66
78
  end
67
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.1.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-27 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.1.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: