num4simdiff 0.1.1 → 0.2.1

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: bbcf09943d885f3b0cbf0a59397859d39fbb8b74073952b3886e5103da0764d5
4
- data.tar.gz: 5332e037a491ffcf970aa863eb5310d57db35c9e29fdbf3795f9585bab67e086
3
+ metadata.gz: 61244490a74e2b2b27f14967ce4d45cc073cfe2323021a488babcd39f921995b
4
+ data.tar.gz: 57b1d91fa1b8e7c1e0114eb749722f9a25a5bb60613608a4b5db30f8a375910a
5
5
  SHA512:
6
- metadata.gz: 6fbc94177cb48dbb77ce6d173309f132647f70ee42ca03d79ade6033d8e034cbad53288a869e4afacd2db56133425870f55e578a67b44bcc16b6cd0f8dfb43b6
7
- data.tar.gz: 50688bd54df072e060cf2e503de3d9016e8743ca8d9e39c63c1c5c792f060e40c18d9607cb78c11515e9514b3dce303187c75c20507a292cdcfd0391b59d153b
6
+ metadata.gz: f3f0e6cc7764903302b93212e93b58e46a04703950c229bb9e3775bf4cd28a90e1d8f18010f9007c25b762ce76c005f97bb1e19f45a46c4aa72c14c50819248b
7
+ data.tar.gz: f60265b9f41b006209937b8cf055ff7043658033dd66fc92c48f9e098fecbd599c8d5c9cd442dd98e002a251be422338bbbb6b20448c99eaa7a6217995b14bd0
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.2.1] - 2023-05-08
6
+
7
+ ### Fixed
8
+ - change cnvRbAry2pt to private
9
+ - change cnvPt2RbAry to private
10
+
5
11
  ## [0.1.1] - 2023-05-02
6
12
 
7
13
  ### Fixed
@@ -4,12 +4,10 @@
4
4
  #include <stdlib.h>
5
5
  #include "CNum4SimDiff.h"
6
6
 
7
- static double* CNum4SimDiff_doDmy(int n, double *yi, double h, Func func);
8
- static double* CNum4SimDiff_doEulerMethod(int n, double *yi, double h, Func func);
9
- static double* CNum4SimDiff_doHeunMethod(int n, double *yi, double h, Func func);
10
- static double* CNum4SimDiff_doRungeKuttaMethod(int n, double *yi, double h, Func func);
7
+ static double* CNum4SimDiff_doEulerMethod(int n, double *yi, double h, double *f);
8
+ static double* CNum4SimDiff_doHeunMethod(int n, double *yi, double h, double *f);
9
+ static double* CNum4SimDiff_doRungeKuttaMethod(int n, double *yi, double h, double *f);
11
10
  static CNum4SimDiff _cNum4SiDiff = {
12
- .FP_dmy = CNum4SimDiff_doDmy,
13
11
  .FP_eulerMethod = CNum4SimDiff_doEulerMethod,
14
12
  .FP_heunMethod = CNum4SimDiff_doHeunMethod,
15
13
  .FP_rungeKuttaMethod = CNum4SimDiff_doRungeKuttaMethod,
@@ -20,46 +18,42 @@ static CNum4SimDiff _cNum4SiDiff = {
20
18
  /**************************************/
21
19
  /* Class部 */
22
20
  /**************************************/
23
- double* CNum4SimDiff_dmy(int n, double *yi, double h, Func func)
21
+ double* CNum4SimDiff_eulerMethod(int n, double *yi, double h, double *f)
24
22
  {
25
- assert(func != 0);
23
+ assert(f != 0);
24
+ assert(yi != 0);
25
+ assert( n > 0);
26
26
 
27
- return _cNum4SiDiff.FP_dmy(n, yi, h, func);
27
+ return _cNum4SiDiff.FP_eulerMethod(n, yi, h, f);
28
28
  }
29
- double* CNum4SimDiff_eulerMethod(int n, double *yi, double h, Func func)
29
+ double* CNum4SimDiff_heunMethod(int n, double *yi, double h, double *f)
30
30
  {
31
- assert(func != 0);
31
+ assert(f != 0);
32
+ assert(yi != 0);
33
+ assert( n > 0);
32
34
 
33
- return _cNum4SiDiff.FP_eulerMethod(n, yi, h, func);
35
+ return _cNum4SiDiff.FP_heunMethod(n, yi, h, f);
34
36
  }
35
- double* CNum4SimDiff_heunMethod(int n, double *yi, double h, Func func)
37
+ double* CNum4SimDiff_rungeKuttaMethod(int n, double *yi, double h, double *f)
36
38
  {
37
- assert(func != 0);
39
+ assert(f != 0);
40
+ assert(yi != 0);
41
+ assert( n > 0);
38
42
 
39
- return _cNum4SiDiff.FP_heunMethod(n, yi, h, func);
40
- }
41
- double* CNum4SimDiff_rungeKuttaMethod(int n, double *yi, double h, Func func)
42
- {
43
- assert(func != 0);
44
-
45
- return _cNum4SiDiff.FP_rungeKuttaMethod(n, yi, h, func);
43
+ return _cNum4SiDiff.FP_rungeKuttaMethod(n, yi, h, f);
46
44
  }
47
45
  /**************************************/
48
46
  /* 処理実行部 */
49
47
  /**************************************/
50
- static double* CNum4SimDiff_doDmy(int n, double *yi, double h, Func func)
51
- {
52
- return func(2, yi);
53
- }
54
48
  /*
55
49
  * オイラー法
56
50
  */
57
- static double* CNum4SimDiff_doEulerMethod(int n, double *yi, double h, Func func)
51
+ static double* CNum4SimDiff_doEulerMethod(int n, double *yi, double h, double *f)
58
52
  {
59
53
  int i;
60
- double *f = func(n, yi);
61
54
  double *yi_1 = malloc(sizeof(double) * n);
62
55
 
56
+ assert(yi_1 != 0);
63
57
  // yi_1 = yi + h * f(xi, y)
64
58
  for (i = 0; i < n; i++) {
65
59
  yi_1[i] = yi[i] + h * f[i];
@@ -69,14 +63,16 @@ static double* CNum4SimDiff_doEulerMethod(int n, double *yi, double h, Func func
69
63
  /*
70
64
  * ホイン法
71
65
  */
72
- static double* CNum4SimDiff_doHeunMethod(int n, double *yi, double h, Func func)
66
+ static double* CNum4SimDiff_doHeunMethod(int n, double *yi, double h, double *f)
73
67
  {
74
68
  int i;
75
- double *f = func(n, yi);
76
69
  double *yi_1 = malloc(sizeof(double) * n);
77
70
  double *k1 = malloc(sizeof(double) * n);
78
71
  double *k2 = malloc(sizeof(double) * n);
79
72
 
73
+ assert(yi_1 != 0);
74
+ assert(k1 != 0);
75
+ assert(k2 != 0);
80
76
  // k1 = h * f(xi, y)
81
77
  // k2 = h * f(xi + h, h * f(xi + h, yi + k1)
82
78
  // yi_1 = yi + (k1 + k2) / 2.0;
@@ -87,16 +83,20 @@ static double* CNum4SimDiff_doHeunMethod(int n, double *yi, double h, Func func)
87
83
  }
88
84
  return yi_1;
89
85
  }
90
- static double* CNum4SimDiff_doRungeKuttaMethod(int n, double *yi, double h, Func func)
86
+ static double* CNum4SimDiff_doRungeKuttaMethod(int n, double *yi, double h, double *f)
91
87
  {
92
88
  int i;
93
- double *f = func(n, yi);
94
89
  double *yi_1 = malloc(sizeof(double) * n);
95
90
  double *k1 = malloc(sizeof(double) * n);
96
91
  double *k2 = malloc(sizeof(double) * n);
97
92
  double *k3 = malloc(sizeof(double) * n);
98
93
  double *k4 = malloc(sizeof(double) * n);
99
94
 
95
+ assert(yi_1 != 0);
96
+ assert(k1 != 0);
97
+ assert(k2 != 0);
98
+ assert(k3 != 0);
99
+ assert(k4 != 0);
100
100
  // k1 = h * f(xi, y)
101
101
  // k2 = h * f(xi + h / 2, yi + k1 / 2)
102
102
  // k3 = h * f(xi + h / 2, y1 + k2 / 2)
@@ -5,14 +5,12 @@
5
5
  /* 構造体宣言 */
6
6
  /**************************************/
7
7
  typedef struct _CNum4SimDiff CNum4SimDiff;
8
- typedef double* (*Func)(int n, double* yi);
9
8
 
10
9
  struct _CNum4SimDiff
11
10
  {
12
- double* (*FP_dmy)(int n, double *yi, double h, Func func);
13
- double* (*FP_eulerMethod)(int n, double *yi, double h, Func func);
14
- double* (*FP_heunMethod)(int n, double *yi, double h, Func func);
15
- double* (*FP_rungeKuttaMethod)(int n, double *yi, double h, Func func);
11
+ double* (*FP_eulerMethod)(int n, double *yi, double h, double *f);
12
+ double* (*FP_heunMethod)(int n, double *yi, double h, double *f);
13
+ double* (*FP_rungeKuttaMethod)(int n, double *yi, double h, double *f);
16
14
  };
17
15
  /**************************************/
18
16
  /* define宣言 */
@@ -20,8 +18,7 @@ struct _CNum4SimDiff
20
18
  /**************************************/
21
19
  /* プロトタイプ宣言 */
22
20
  /**************************************/
23
- double* CNum4SimDiff_dmy(int n, double *yi, double h, Func func);
24
- double* CNum4SimDiff_eulerMethod(int n, double *yi, double h, Func func);
25
- double* CNum4SimDiff_heunMethod(int n, double *yi, double h, Func func);
26
- double* CNum4SimDiff_rungeKuttaMethod(int n, double *yi, double h, Func func);
21
+ double* CNum4SimDiff_eulerMethod(int n, double *yi, double h, double *f);
22
+ double* CNum4SimDiff_heunMethod(int n, double *yi, double h, double *f);
23
+ double* CNum4SimDiff_rungeKuttaMethod(int n, double *yi, double h, double *f);
27
24
  #endif
data/lib/num4simdiff.rb CHANGED
@@ -6,31 +6,13 @@ module Num4SimDiffLib
6
6
  extend FFI::Library
7
7
 
8
8
  ffi_lib FFI::Compiler::Loader.find('num4simdiff')
9
-
10
- # @overload f(n, yn)
11
- # dy = f(n, yn)
12
- # @yield [n, yn] dy = f(n, yn)
13
- # @yieldparam [int] n ynの個数
14
- # @yieldparam [pointer] yn FFI::Pointer
15
- # @return [pointer] xiに対するyの値(FFI::Pointer)
16
- callback :f, [:int, :pointer], :pointer
17
-
18
- attach_function :dmyFFI,
19
- :CNum4SimDiff_dmy, [:int, :buffer_in, :double, :f], :pointer
20
9
  attach_function :eulerMethodFFI,
21
- :CNum4SimDiff_eulerMethod, [:int, :buffer_in, :double, :f], :pointer
10
+ :CNum4SimDiff_eulerMethod, [:int, :buffer_in, :double, :buffer_in], :pointer
22
11
  attach_function :heunMethodFFI,
23
- :CNum4SimDiff_heunMethod, [:int, :buffer_in, :double, :f], :pointer
12
+ :CNum4SimDiff_heunMethod, [:int, :buffer_in, :double, :buffer_in], :pointer
24
13
  attach_function :rungeKuttaMethodFFI,
25
- :CNum4SimDiff_rungeKuttaMethod, [:int, :buffer_in, :double, :f], :pointer
14
+ :CNum4SimDiff_rungeKuttaMethod, [:int, :buffer_in, :double, :buffer_in], :pointer
26
15
  class << self
27
- # @private
28
- def dmy(yi, h, func)
29
- n = yi.size
30
- yi_ptr = cnvRbAry2pt(n, yi)
31
- yi_1_ptr = dmyFFI(n, yi_ptr, h, func)
32
- yi_1 = cnvPt2RbAry(n, yi_1_ptr)
33
- end
34
16
  #
35
17
  # オイラー法による数値計算
36
18
  # @overload eulerMethod(yi, h, func)
@@ -39,12 +21,24 @@ module Num4SimDiffLib
39
21
  # @param [double] h 刻み幅
40
22
  # @param [callback] func xiに対する傾きを計算する関数
41
23
  # @return [double[]] xi+hに対するyi_1の値(配列)
24
+ # @example
25
+ # func = Proc.new do | n, yi |
26
+ # f = []
27
+ # f[0] = yi[0]
28
+ # f[1] = yi[1]
29
+ # next f
30
+ # end
31
+ # yi = [1.0, 1.0]
32
+ # yi_1 = Num4SimDiffLib.eulerMethod(yi, 0.001, func)
42
33
  #
43
34
  def eulerMethod(yi, h, func)
44
35
  n = yi.size
36
+ f = func.call(n, yi)
37
+ f_ptr = cnvRbAry2pt(n, f)
45
38
  yi_ptr = cnvRbAry2pt(n, yi)
46
- yi_1_ptr = eulerMethodFFI(n, yi_ptr, h, func)
39
+ yi_1_ptr = eulerMethodFFI(n, yi_ptr, h, f_ptr)
47
40
  yi_1 = cnvPt2RbAry(n, yi_1_ptr)
41
+ f_ptr.free()
48
42
  return yi_1
49
43
  end
50
44
  #
@@ -55,12 +49,24 @@ module Num4SimDiffLib
55
49
  # @param [double] h 刻み幅
56
50
  # @param [callback] func xiに対する傾きを計算する関数
57
51
  # @return [double[]] xi+hに対するyi_1の値(配列)
52
+ # @example
53
+ # func = Proc.new do | n, yi |
54
+ # f = []
55
+ # f[0] = yi[0]
56
+ # f[1] = yi[1]
57
+ # next f
58
+ # end
59
+ # yi = [1.0, 1.0]
60
+ # yi_1 = Num4SimDiffLib.heunMethod(yi, 0.001, func)
58
61
  #
59
62
  def heunMethod(yi, h, func)
60
63
  n = yi.size
64
+ f = func.call(n, yi)
65
+ f_ptr = cnvRbAry2pt(n, f)
61
66
  yi_ptr = cnvRbAry2pt(n, yi)
62
- yi_1_ptr = heunMethodFFI(n, yi_ptr, h, func)
67
+ yi_1_ptr = heunMethodFFI(n, yi_ptr, h, f_ptr)
63
68
  yi_1 = cnvPt2RbAry(n, yi_1_ptr)
69
+ f_ptr.free()
64
70
  return yi_1
65
71
  end
66
72
  #
@@ -71,21 +77,29 @@ module Num4SimDiffLib
71
77
  # @param [double] h 刻み幅
72
78
  # @param [callback] func xiに対する傾きを計算する関数
73
79
  # @return [double[]] xi+hに対するyi_1の値(配列)
80
+ # @example
81
+ # func = Proc.new do | n, yi |
82
+ # f = []
83
+ # f[0] = yi[0]
84
+ # f[1] = yi[1]
85
+ # next f
86
+ # end
87
+ # yi = [1.0, 1.0]
88
+ # yi_1 = Num4SimDiffLib.rungeKuttaMethod(yi, 0.001, func)
74
89
  #
75
90
  def rungeKuttaMethod(yi, h, func)
76
91
  n = yi.size
92
+ f = func.call(n, yi)
93
+ f_ptr = cnvRbAry2pt(n, f)
77
94
  yi_ptr = cnvRbAry2pt(n, yi)
78
- yi_1_ptr = rungeKuttaMethodFFI(n, yi_ptr, h, func)
95
+ yi_1_ptr = rungeKuttaMethodFFI(n, yi_ptr, h, f_ptr)
79
96
  yi_1 = cnvPt2RbAry(n, yi_1_ptr)
97
+ f_ptr.free()
80
98
  return yi_1
81
99
  end
82
100
 
83
101
  #
84
- # ruby配列からFFI::Pointer型に変換
85
- # @overload cnvRbAry2pt(n, ary)
86
- # @param [int] n 配列の個数
87
- # @param [double[]] ary yiの値(配列)
88
- # @return [pointer] FFI::Pointer
102
+ # @private
89
103
  def cnvRbAry2pt(n, ary)
90
104
  yi_ptr = FFI::MemoryPointer.new(:double, n)
91
105
  n.times.map { |i|
@@ -94,17 +108,15 @@ module Num4SimDiffLib
94
108
  return yi_ptr
95
109
  end
96
110
  #
97
- # FFI::Pointer型からruby配列に変換
98
- # @overload cnvPt2RbAry(n, pt)
99
- # @param [int] n 配列の個数
100
- # @param [pointer] pt FFI::Pointer
101
- # @return [double[]] yiの値(配列)
111
+ # @private
102
112
  def cnvPt2RbAry(n, pt)
103
113
  rbAry = n.times.map { |i|
104
114
  pt.get_double(i * Fiddle::SIZEOF_DOUBLE)
105
115
  }
106
116
  return rbAry
107
117
  end
118
+ private :cnvRbAry2pt
119
+ private :cnvPt2RbAry
108
120
  private :eulerMethodFFI
109
121
  private :heunMethodFFI
110
122
  private :rungeKuttaMethodFFI
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4simdiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.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-05-02 00:00:00.000000000 Z
11
+ date: 2023-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi-compiler
@@ -70,8 +70,9 @@ licenses:
70
70
  - MIT
71
71
  metadata:
72
72
  changelog_uri: http://github.com/siranovel/num4simdiffrent/blob/main/CHANGELOG.md
73
- documentation_uri: https://rubydoc.info/gems/num4simdiff/0.1.1
73
+ documentation_uri: https://rubydoc.info/gems/num4simdiff/0.2.1
74
74
  homepage_uri: http://github.com/siranovel/num4simdiffrent
75
+ wiki_uri: https://github.com/siranovel/mydocs/tree/main/num4simdiff
75
76
  post_install_message:
76
77
  rdoc_options:
77
78
  - "--no-private"