num4simdiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 84111ebd98c9c8f7f000733a780973aa030185287c716b15d4a6d7ef5c62424d
4
+ data.tar.gz: 86d00d4725e4af07cfb5aff14647be96645f8c8fa17080f310da93817e0ca4cd
5
+ SHA512:
6
+ metadata.gz: 93a012aff1ff1fd8f8614c483f12836e3ff5e8c0d39adbf1f8c277503c9669d36f4665b7a9d9397cd5ce5d572ceefd8b8a49e13b4c073074e3176fa9be0d0ad4
7
+ data.tar.gz: c6c1d8bb3a3f35f82becdf0a584d7c9971593d3435296e39c9cb349a8b82095193c979a6172f2423b719780430a5afc1edf7f20e2c984f1e6d14a72094207cba
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Change Log
2
+
3
+ ## Unreleased
4
+
5
+ ## [0.0.1] - 2023-04-29
6
+
7
+ ### Fixed
8
+ - fix first fixed.
9
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 siranovel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,110 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <assert.h>
4
+ #include <stdlib.h>
5
+ #include "CNum4SimDiff.h"
6
+
7
+ static double* CNum4SimDiff_doEulerMethod(double *yi, double xi, double h, Func func1, Func func2);
8
+ static double* CNum4SimDiff_doHeunMethod(double *yi, double xi, double h, Func func1, Func func2);
9
+ static double* CNum4SimDiff_doRungeKuttaMethod(double *yi, double xi, double h, Func func1, Func func2);
10
+ static CNum4SimDiff _cNum4SiDiff = {
11
+ .FP_eulerMethod = CNum4SimDiff_doEulerMethod,
12
+ .FP_heunMethod = CNum4SimDiff_doHeunMethod,
13
+ .FP_rungeKuttaMethod = CNum4SimDiff_doRungeKuttaMethod,
14
+ };
15
+ /**************************************/
16
+ /* InterFface部 */
17
+ /**************************************/
18
+ /**************************************/
19
+ /* Class部 */
20
+ /**************************************/
21
+ double* CNum4SimDiff_eulerMethod(double *yi, double xi, double h, Func func1, Func func2)
22
+ {
23
+ assert(func1 != 0);
24
+ assert(func2 != 0);
25
+
26
+ return _cNum4SiDiff.FP_eulerMethod(yi, xi, h, func1, func2);
27
+ }
28
+ double* CNum4SimDiff_heunMethod(double *yi, double xi, double h, Func func1, Func func2)
29
+ {
30
+ assert(func1 != 0);
31
+ assert(func2 != 0);
32
+
33
+ return _cNum4SiDiff.FP_heunMethod(yi, xi, h, func1, func2);
34
+ }
35
+ double* CNum4SimDiff_rungeKuttaMethod(double *yi, double xi, double h, Func func1, Func func2)
36
+ {
37
+ assert(func1 != 0);
38
+ assert(func2 != 0);
39
+
40
+ return _cNum4SiDiff.FP_rungeKuttaMethod(yi, xi, h, func1, func2);
41
+ }
42
+ /**************************************/
43
+ /* 処理実行部 */
44
+ /**************************************/
45
+ /*
46
+ * オイラー法
47
+ */
48
+ static double* CNum4SimDiff_doEulerMethod(double *yi, double xi, double h, Func func1, Func func2)
49
+ {
50
+ int i;
51
+ double *f = malloc(sizeof(double) * N);
52
+ double *yi_1 = malloc(sizeof(double) * N);
53
+
54
+ f[0] = func1(xi, yi[1]);
55
+ f[1] = func2(xi, yi[1]);
56
+ // yi_1 = yi + h * f(xi, y)
57
+ for (i = 0; i < N; i++) {
58
+ yi_1[i] = yi[i] + h * f[i];
59
+ }
60
+ return yi_1;
61
+ }
62
+ /*
63
+ * ホイン法
64
+ */
65
+ static double* CNum4SimDiff_doHeunMethod(double *yi, double xi, double h, Func func1, Func func2)
66
+ {
67
+ int i;
68
+ double *f = malloc(sizeof(double) * N);
69
+ double *yi_1 = malloc(sizeof(double) * N);
70
+ double *k1 = malloc(sizeof(double) * N);
71
+ double *k2 = malloc(sizeof(double) * N);
72
+
73
+ f[0] = func1(xi, yi[1]);
74
+ f[1] = func2(xi, yi[1]);
75
+ // k1 = h * f(xi, y)
76
+ // k2 = h * f(xi + h, h * f(xi + h, yi + k1)
77
+ // yi_1 = yi + (k1 + k2) / 2.0;
78
+ for (i = 0; i < N; i++) {
79
+ k1[i] = h * f[i];
80
+ k2[i] = h * (yi[i] + k1[i]);
81
+ yi_1[i] = yi[i] + (k1[i] + k2[i]) / 2.0;
82
+ }
83
+ return yi_1;
84
+ }
85
+ static double* CNum4SimDiff_doRungeKuttaMethod(double *yi, double xi, double h, Func func1, Func func2)
86
+ {
87
+ int i;
88
+ double *f = malloc(sizeof(double) * N);
89
+ double *yi_1 = malloc(sizeof(double) * N);
90
+ double *k1 = malloc(sizeof(double) * N);
91
+ double *k2 = malloc(sizeof(double) * N);
92
+ double *k3 = malloc(sizeof(double) * N);
93
+ double *k4 = malloc(sizeof(double) * N);
94
+
95
+ f[0] = func1(xi, yi[1]);
96
+ f[1] = func2(xi, yi[1]);
97
+ // k1 = h * f(xi, y)
98
+ // k2 = h * f(xi + h / 2, yi + k1 / 2)
99
+ // k3 = h * f(xi + h / 2, y1 + k2 / 2)
100
+ // k4 = h * f(xi + h, yi + k3)
101
+ for (i = 0; i < N; i++) {
102
+ k1[i] = h * f[i];
103
+ k2[i] = h * (yi[i] + k1[i] / 2.0);
104
+ k3[i] = h * (yi[i] + k2[i] / 2.0);
105
+ k4[i] = h * (yi[i] + k3[i]);
106
+ yi_1[i] = yi[i] + (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) / 6.0;
107
+ }
108
+ return yi_1;
109
+ }
110
+
@@ -0,0 +1,26 @@
1
+ #ifndef _CNum4SimDiff_H_
2
+ #define _CNum4SimDiff_H_
3
+
4
+ /**************************************/
5
+ /* 構造体宣言 */
6
+ /**************************************/
7
+ typedef struct _CNum4SimDiff CNum4SimDiff;
8
+ typedef double (*Func)(double x, double dx);
9
+
10
+ struct _CNum4SimDiff
11
+ {
12
+ double* (*FP_eulerMethod)(double *yi, double xi, double h, Func func1, Func func2);
13
+ double* (*FP_heunMethod)(double *yi, double xi, double h, Func func1, Func func2);
14
+ double* (*FP_rungeKuttaMethod)(double *yi, double xi, double h, Func func1, Func func2);
15
+ };
16
+ /**************************************/
17
+ /* define宣言 */
18
+ /**************************************/
19
+ #define N 2
20
+ /**************************************/
21
+ /* プロトタイプ宣言 */
22
+ /**************************************/
23
+ double* CNum4SimDiff_eulerMethod(double *yi, double xi, double h, Func func1, Func func2);
24
+ double* CNum4SimDiff_heunMethod(double *yi, double xi, double h, Func func1, Func func2);
25
+ double* CNum4SimDiff_rungeKuttaMethod(double *yi, double xi, double h, Func func1, Func func2);
26
+ #endif
@@ -0,0 +1,5 @@
1
+ require 'ffi-compiler/compile_task'
2
+
3
+ FFI::Compiler::CompileTask.new('num4simdiff') do |c|
4
+ c.have_header?('CNum4SimDiff.h', '.')
5
+ end
@@ -0,0 +1,97 @@
1
+ require 'ffi'
2
+ require 'ffi-compiler/loader'
3
+ require 'fiddle'
4
+
5
+ module Num4SimDiffLib
6
+ extend FFI::Library
7
+
8
+ ffi_lib FFI::Compiler::Loader.find('num4simdiff')
9
+
10
+ # @overload f(x, dx)
11
+ # dy = f(x, dx)
12
+ # @yield [x] dy = f(x, dx)
13
+ # @yieldparam [double] x xiの値
14
+ # @yieldparam [double] dx xiの値
15
+ # @return [double] xiに対するyの値
16
+ callback :f, [:double, :double], :double
17
+
18
+ #
19
+ # オイラー法による数値計算
20
+ # @overload eulerMethod(yi, xi, h, func1, func2)
21
+ # yi_1 = eulerMethod(yi, xi, h, func1, func2)
22
+ # @param [double] yi xiに対するyiの値(配列)
23
+ # @param [double] xi xiの値
24
+ # @param [double] h 刻み幅
25
+ # @param [callback] func1 xiに対する傾きを計算する関数
26
+ # @param [callback] func2 xiに対する傾きを計算する関数
27
+ # @return [double] xi+hに対するyi_1の値(配列)
28
+ #
29
+ attach_function :eulerMethodFFI,
30
+ :CNum4SimDiff_eulerMethod, [:buffer_in, :double, :double, :f, :f], :pointer
31
+ #
32
+ # ホイン法による数値計算
33
+ # @overload heunMethod(yi, xi, h, func1, func2)
34
+ # yi_1 = heunMethod(yi, xi, h, func1, func2)
35
+ # @param [double] yi xiに対するyiの値(配列)
36
+ # @param [double] xi xiの値
37
+ # @param [double] h 刻み幅
38
+ # @param [callback] func1 xiに対する傾きを計算する関数
39
+ # @param [callback] func2 xiに対する傾きを計算する関数
40
+ # @return [double] xi+hに対するyi_1の値(配列)
41
+ #
42
+ attach_function :heunMethodFFI,
43
+ :CNum4SimDiff_heunMethod, [:buffer_in, :double, :double, :f, :f], :pointer
44
+ #
45
+ # 4次のルンゲ=クッタ法による数値計算
46
+ # @overload rungeKuttaMethod(yi, xi, h, func1, func2)
47
+ # yi_1 = rungeKuttaMethod(yi, xi, h, func1, func2)
48
+ # @param [double] yi xiに対するyiの値(配列)
49
+ # @param [double] xi xiの値
50
+ # @param [double] h 刻み幅
51
+ # @param [callback] func1 xiに対する傾きを計算する関数
52
+ # @param [callback] func2 xiに対する傾きを計算する関数
53
+ # @return [double] xi+hに対するyi_1の値(配列)
54
+ #
55
+ attach_function :rungeKuttaMethodFFI,
56
+ :CNum4SimDiff_rungeKuttaMethod, [:buffer_in, :double, :double, :f, :f], :pointer
57
+ class << self
58
+ def eulerMethod(yi, x, h, func1, func2)
59
+ yi_ptr = cnvRbAry2pt(2, yi)
60
+ yi_1_ptr = eulerMethodFFI(yi_ptr, x, h, func1, func2)
61
+ yi_1 = cnvPt2RbAry(2, yi_1_ptr)
62
+ return 1 + yi_1[0] + 0.5 * yi_1[1]
63
+ end
64
+ def heunMethod(yi, x, h, func1, func2)
65
+ yi_ptr = cnvRbAry2pt(2, yi)
66
+ yi_1_ptr = heunMethodFFI(yi_ptr, x, h, func1, func2)
67
+ yi_1 = cnvPt2RbAry(2, yi_1_ptr)
68
+ return 1 + yi_1[0] + 0.5 * yi_1[1]
69
+ end
70
+ def rungeKuttaMethod(yi, x, h, func1, func2)
71
+ yi_ptr = cnvRbAry2pt(2, yi)
72
+ yi_1_ptr = rungeKuttaMethodFFI(yi_ptr, x, h, func1, func2)
73
+ yi_1 = cnvPt2RbAry(2, yi_1_ptr)
74
+ return 1 + yi_1[0] + 0.5 * yi_1[1]
75
+ end
76
+
77
+
78
+ def cnvRbAry2pt(n, ary)
79
+ yi_ptr = FFI::MemoryPointer.new(:double, n)
80
+ n.times.map { |i|
81
+ yi_ptr.put_double(i * Fiddle::SIZEOF_DOUBLE, ary[i].to_f)
82
+ }
83
+ return yi_ptr
84
+ end
85
+ def cnvPt2RbAry(n, pt)
86
+ rbAry = n.times.map { |i|
87
+ pt.get_double(i * Fiddle::SIZEOF_DOUBLE)
88
+ }
89
+ return rbAry
90
+ end
91
+ private :eulerMethodFFI
92
+ private :heunMethodFFI
93
+ private :rungeKuttaMethodFFI
94
+ private :cnvRbAry2pt
95
+ private :cnvPt2RbAry
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: num4simdiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - siranovel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '12.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 12.3.3
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '12.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 12.3.3
53
+ description: numerical solution for simultaneous ordinaray differential equations
54
+ email: siranovel@gmail.com
55
+ executables: []
56
+ extensions:
57
+ - ext/num4simdiff/Rakefile
58
+ extra_rdoc_files: []
59
+ files:
60
+ - CHANGELOG.md
61
+ - Gemfile
62
+ - LICENSE
63
+ - ext/num4simdiff/CNum4SimDiff.c
64
+ - ext/num4simdiff/CNum4SimDiff.h
65
+ - ext/num4simdiff/Rakefile
66
+ - lib/num4simdiff.rb
67
+ homepage: http://github.com/siranovel/num4simdiffrent
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ changelog_uri: http://github.com/siranovel/num4simdiffrent/blob/main/CHANGELOG.md
72
+ documentation_uri: https://rubydoc.info/gems/num4simdiff/0.0.1
73
+ homepage_uri: http://github.com/siranovel/num4simdiffrent
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.3.7
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: num for simultaneous different
93
+ test_files: []