num4equ 0.0.7

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: 846b376182aec92ffed0f060ef0d57d1d096d12697d4538e2197202b87afc3a5
4
+ data.tar.gz: bb2ac4a150591ad1e536195a76632a8fce5c986a39e5568f0d5ad3c79b809b59
5
+ SHA512:
6
+ metadata.gz: 30628042787f597a38cc35524a0e242dee82e8c02d9dbeb802281c02d346be92074b2226244c9faa574df021a53ccbfbb108ff9eba9657b8af8a8225d227781a
7
+ data.tar.gz: b4ee2e0cdd81b78544a2750598f9c10c987b9e7f11031b4d02ad6b5ee1555de74d8633ac53c516a543772abe9db361041f89ac145847cfe9d237f7b73a6495a6
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ 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,84 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <assert.h>
4
+ #include <math.h>
5
+ #include "CNum4Equ.h"
6
+
7
+ static double CNum4Equ_doNewtonMethodFFI(double a, Func func, DFunc dFunc, int *ok);
8
+ static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *ok);
9
+
10
+ static CNum4Equ _cNum4Equ = {
11
+ .FP_newtonMethodFFI = CNum4Equ_doNewtonMethodFFI,
12
+ .FP_bisectionMethodFFI = CNum4Equ_doBisectionMethodFFI,
13
+ };
14
+
15
+ /**************************************/
16
+ /* InterFface部 */
17
+ /**************************************/
18
+ /**************************************/
19
+ /* Class部 */
20
+ /**************************************/
21
+ double CNum4Equ_newtonMethodFFI(double a, Func func, DFunc dFunc, int *ok)
22
+ {
23
+ assert(func != 0);
24
+ assert(dFunc != 0);
25
+
26
+ return _cNum4Equ.FP_newtonMethodFFI(a, func, dFunc, ok);
27
+ }
28
+ double CNum4Equ_bisectionMethodFFI(double a, double b, Func func, int *ok)
29
+ {
30
+ assert(func != 0);
31
+ assert(a < b);
32
+
33
+ return _cNum4Equ.FP_bisectionMethodFFI(a, b, func, ok);
34
+ }
35
+ /**************************************/
36
+ /* 処理実行部 */
37
+ /**************************************/
38
+ static double CNum4Equ_doNewtonMethodFFI(double a, Func func, DFunc dFunc, int *ok)
39
+ {
40
+ double xn;
41
+ double x0 = a;
42
+ long idx = 0;
43
+
44
+ *ok = 0;
45
+ xn = x0;
46
+ do {
47
+ double f;
48
+ double df;
49
+
50
+ x0 = xn;
51
+ f = func(x0);
52
+ df = dFunc(x0);
53
+ xn = -1 * f / df + x0;
54
+ idx++;
55
+ if (100000 < idx) {
56
+ *ok = -1;
57
+ break;
58
+ }
59
+ } while(fabs(xn - x0) > EPS);
60
+ return xn;
61
+ }
62
+ static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *ok)
63
+ {
64
+ double fa = func(a);
65
+ double fb = func(b);
66
+ double fxc;
67
+ double xc;
68
+
69
+ *ok = (fa * fb) < 0 ? 0 : -1;
70
+ if (0 != *ok) { return 0; }
71
+ do {
72
+ xc = (a + b) / 2.0;
73
+ fxc = func(xc);
74
+ if (0.0 >= fa * fxc) {
75
+ b = xc;
76
+ fb = fxc;
77
+ } else {
78
+ a = xc;
79
+ fa = fxc;
80
+ }
81
+ } while(fxc != 0 && fabs(b - a) > EPS);
82
+ return xc;
83
+ }
84
+
@@ -0,0 +1,25 @@
1
+ #ifndef _CNum4Equ_H_
2
+ #define _CNum4Equ_H_
3
+
4
+ /**********************************************/
5
+ /* 構造体宣言 */
6
+ /**********************************************/
7
+ typedef struct _CNum4Equ CNum4Equ;
8
+ typedef double (*Func)(double x);
9
+ typedef double (*DFunc)(double x);
10
+
11
+ struct _CNum4Equ
12
+ {
13
+ double (*FP_newtonMethodFFI)(double a, Func func, DFunc dFunc, int *ok);
14
+ double (*FP_bisectionMethodFFI)(double a, double b, Func func, int *ok);
15
+ };
16
+ /**********************************************/
17
+ /* definen宣言 */
18
+ /**********************************************/
19
+ #define EPS (1.0e-6)
20
+ /**********************************************/
21
+ /* プロトタイプ宣言 */
22
+ /**********************************************/
23
+ double CNum4Equ_newtonMethodFFI(double a, Func func, DFunc dFunc, int *ok);
24
+ double CNum4Equ_bisectionMethodFFI(double a, double b, Func func, int *ok);
25
+ #endif
@@ -0,0 +1,5 @@
1
+ require 'ffi-compiler/compile_task'
2
+
3
+ FFI::Compiler::CompileTask.new('num4equ') do |c|
4
+ c.ldflags << "-lm"
5
+ end
data/lib/num4equ.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'ffi'
2
+ require 'ffi-compiler/loader'
3
+
4
+ #
5
+ # 数値計算による方程式の解法ライブラリ
6
+ #
7
+ module Num4EquLib
8
+ extend FFI::Library
9
+
10
+ ffi_lib FFI::Compiler::Loader.find('num4equ')
11
+ callback :f, [:double], :double
12
+ callback :df, [:double], :double
13
+
14
+ attach_function :newtonMethodFFI,
15
+ :CNum4Equ_newtonMethodFFI, [:double, :f, :df, :buffer_out], :double
16
+ attach_function :bisectionMethodFFI,
17
+ :CNum4Equ_bisectionMethodFFI, [:double, :double, :f, :buffer_out], :double
18
+ class << self
19
+ # @overload newtonMethod(a, func, dfunc)
20
+ # ニュートン法による解法
21
+ # @param [double] a aの値
22
+ # @param [callback] func aに対する値を計算
23
+ # @param [callback] dfunc fに対する微分関数
24
+ # @return [double] xの値
25
+ # @raise RangeError
26
+ def newtonMethod(a, func, dfunc)
27
+ ok_ptr = FFI::MemoryPointer.new :int
28
+ x = newtonMethodFFI(a, func, dfunc, ok_ptr)
29
+ ok = ok_ptr.read_int
30
+ ok_ptr.free()
31
+ if ok < 0 then
32
+ raise RangeError.new("a:" + a.to_s)
33
+ end
34
+ return x
35
+ end
36
+ # @overload bisection(a, b, func)
37
+ # 二分法による解法
38
+ # @param [double] a aの値
39
+ # @param [double] b bの値
40
+ # @param [callback] func aに対する値を計算
41
+ # @return [double] xの値
42
+ # @raise RangeError
43
+ def bisectionMethod(a, b, func)
44
+ ok_ptr = FFI::MemoryPointer.new :int
45
+ x = bisectionMethodFFI(a, b, func, ok_ptr)
46
+ ok = ok_ptr.read_int
47
+ ok_ptr.free()
48
+ if ok < 0 then
49
+ raise RangeError.new("a:" + a.to_s + " " + "b:" + b.to_s)
50
+ end
51
+ return x
52
+ end
53
+
54
+ private :newtonMethodFFI, :bisectionMethodFFI
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: num4equ
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - siranovel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-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 equation
54
+ email: siranovel@gmail.com
55
+ executables: []
56
+ extensions:
57
+ - ext/num4equ/Rakefile
58
+ extra_rdoc_files: []
59
+ files:
60
+ - Gemfile
61
+ - LICENSE
62
+ - ext/num4equ/CNum4Equ.c
63
+ - ext/num4equ/CNum4Equ.h
64
+ - ext/num4equ/Rakefile
65
+ - lib/num4equ.rb
66
+ homepage: http://github.com/siranovel/num4equation
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.7
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: num for equation
89
+ test_files: []