num4equ 0.0.9 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/ext/num4equ/CNum4Equ.c +26 -17
- data/ext/num4equ/CNum4Equ.h +4 -4
- data/lib/num4equ.rb +4 -11
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f4d45a4303869d3bc6c7a47da7dddf6cc67f47e6de2f96be6d0e04a2f0fefd0
|
4
|
+
data.tar.gz: 4c4cc2f0c5c88d016826d5575df652ce113a7c934bf693332eac4e5d8d8a43fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d897598486152d6fba6a732d36a4d4b725fe99b78b2720c12c43e0bf39d8f0c0e0db2825068c6a2a4c91490f806596279b2b429e3d2e267fe9dd87ee1bf6d498
|
7
|
+
data.tar.gz: 19c36fde2ef67d634e5bb6c206a94b589ca302cc0d3ddafb3fefea902dac03a0e326b3005b702f6a65fa4e016c26c0a668e2a5fdba627822cb3c1b6a80f2209d
|
data/CHANGELOG.md
CHANGED
data/ext/num4equ/CNum4Equ.c
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
#include <math.h>
|
5
5
|
#include "CNum4Equ.h"
|
6
6
|
|
7
|
-
static double CNum4Equ_doNewtonMethodFFI(double a, Func func,
|
7
|
+
static double CNum4Equ_doNewtonMethodFFI(double a, Func func, int *ok);
|
8
8
|
static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *ok);
|
9
9
|
static double CNum4Equ_doSecantMethodFFI(double a, double b, Func func, int *ok);
|
10
10
|
|
@@ -20,12 +20,11 @@ static CNum4Equ _cNum4Equ = {
|
|
20
20
|
/**************************************/
|
21
21
|
/* Class部 */
|
22
22
|
/**************************************/
|
23
|
-
double CNum4Equ_newtonMethodFFI(double a, Func func,
|
23
|
+
double CNum4Equ_newtonMethodFFI(double a, Func func, int *ok)
|
24
24
|
{
|
25
25
|
assert(func != 0);
|
26
|
-
assert(dFunc != 0);
|
27
26
|
|
28
|
-
return _cNum4Equ.FP_newtonMethodFFI(a, func,
|
27
|
+
return _cNum4Equ.FP_newtonMethodFFI(a, func, ok);
|
29
28
|
}
|
30
29
|
double CNum4Equ_bisectionMethodFFI(double a, double b, Func func, int *ok)
|
31
30
|
{
|
@@ -42,32 +41,40 @@ double CNum4Equ_secantMethodFFI(double a, double b, Func func, int *ok)
|
|
42
41
|
return _cNum4Equ.FP_secantMethodFFI(a, b, func, ok);
|
43
42
|
}
|
44
43
|
/**************************************/
|
45
|
-
/* 処理実行部
|
44
|
+
/* 処理実行部 */
|
46
45
|
/**************************************/
|
47
|
-
|
46
|
+
/*
|
47
|
+
* ニュートン法
|
48
|
+
*/
|
49
|
+
static double CNum4Equ_doNewtonMethodFFI(double a, Func func, int *ok)
|
48
50
|
{
|
49
|
-
double xn;
|
50
|
-
double
|
51
|
+
double xn = a;
|
52
|
+
double xn_1;
|
51
53
|
long idx = 0;
|
52
54
|
|
53
55
|
*ok = 0;
|
54
|
-
xn = x0;
|
55
56
|
do {
|
56
|
-
double
|
57
|
+
double fx;
|
58
|
+
double fxh;
|
57
59
|
double df;
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
-
df =
|
62
|
-
|
61
|
+
fx = func(xn);
|
62
|
+
fxh = func(xn + DX);
|
63
|
+
df = (fxh - fx) / DX;
|
64
|
+
xn_1 = xn + -1 * fx / df;
|
65
|
+
if (fabs(xn_1 - xn) < EPS) break;
|
66
|
+
xn = xn_1;
|
63
67
|
idx++;
|
64
68
|
if (100000 < idx) {
|
65
69
|
*ok = -1;
|
66
70
|
break;
|
67
71
|
}
|
68
|
-
} while(
|
72
|
+
} while(1);
|
69
73
|
return xn;
|
70
74
|
}
|
75
|
+
/*
|
76
|
+
* 2分法
|
77
|
+
*/
|
71
78
|
static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *ok)
|
72
79
|
{
|
73
80
|
double fa = func(a);
|
@@ -76,7 +83,7 @@ static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *
|
|
76
83
|
double xc;
|
77
84
|
|
78
85
|
*ok = (fa * fb) < 0 ? 0 : -1;
|
79
|
-
if (0 != ok) { return 0; }
|
86
|
+
if (0 != *ok) { return 0; }
|
80
87
|
do {
|
81
88
|
xc = (a + b) / 2.0;
|
82
89
|
fxc = func(xc);
|
@@ -90,6 +97,9 @@ static double CNum4Equ_doBisectionMethodFFI(double a, double b, Func func, int *
|
|
90
97
|
} while(fxc != 0 && fabs(b - a) > EPS);
|
91
98
|
return xc;
|
92
99
|
}
|
100
|
+
/*
|
101
|
+
* 割線法
|
102
|
+
*/
|
93
103
|
static double CNum4Equ_doSecantMethodFFI(double a, double b, Func func, int *ok)
|
94
104
|
{
|
95
105
|
double new_x = 0.0;
|
@@ -119,4 +129,3 @@ static double CNum4Equ_doSecantMethodFFI(double a, double b, Func func, int *ok)
|
|
119
129
|
return new_x;
|
120
130
|
}
|
121
131
|
|
122
|
-
|
data/ext/num4equ/CNum4Equ.h
CHANGED
@@ -6,22 +6,22 @@
|
|
6
6
|
/**********************************************/
|
7
7
|
typedef struct _CNum4Equ CNum4Equ;
|
8
8
|
typedef double (*Func)(double x);
|
9
|
-
typedef double (*DFunc)(double x);
|
10
9
|
|
11
10
|
struct _CNum4Equ
|
12
11
|
{
|
13
|
-
double (*FP_newtonMethodFFI)(double a, Func func,
|
12
|
+
double (*FP_newtonMethodFFI)(double a, Func func, int *ok);
|
14
13
|
double (*FP_bisectionMethodFFI)(double a, double b, Func func, int *ok);
|
15
14
|
double (*FP_secantMethodFFI)(double a, double b, Func func, int *ok);
|
16
15
|
};
|
17
16
|
/**********************************************/
|
18
17
|
/* definen宣言 */
|
19
18
|
/**********************************************/
|
20
|
-
#define EPS (1.0e-
|
19
|
+
#define EPS (1.0e-7)
|
20
|
+
#define DX 0.001
|
21
21
|
/**********************************************/
|
22
22
|
/* プロトタイプ宣言 */
|
23
23
|
/**********************************************/
|
24
|
-
double CNum4Equ_newtonMethodFFI(double a, Func func,
|
24
|
+
double CNum4Equ_newtonMethodFFI(double a, Func func, int *ok);
|
25
25
|
double CNum4Equ_bisectionMethodFFI(double a, double b, Func func, int *ok);
|
26
26
|
double CNum4Equ_secantMethodFFI(double a, double b, Func func, int *ok);
|
27
27
|
#endif
|
data/lib/num4equ.rb
CHANGED
@@ -13,29 +13,23 @@ module Num4EquLib
|
|
13
13
|
# @yieldparam [double] aの値
|
14
14
|
# @return [double] xの値
|
15
15
|
callback :f, [:double], :double
|
16
|
-
# @overload dfunc(a)
|
17
|
-
# @yield [a] 関数に対する微分関数
|
18
|
-
# @yieldparam [double] aの値
|
19
|
-
# @return [double] xの値
|
20
|
-
callback :df, [:double], :double
|
21
16
|
|
22
17
|
attach_function :newtonMethodFFI,
|
23
|
-
:CNum4Equ_newtonMethodFFI, [:double, :f, :
|
18
|
+
:CNum4Equ_newtonMethodFFI, [:double, :f, :buffer_out], :double
|
24
19
|
attach_function :bisectionMethodFFI,
|
25
20
|
:CNum4Equ_bisectionMethodFFI, [:double, :double, :f, :buffer_out], :double
|
26
21
|
attach_function :secantMethodFFI,
|
27
22
|
:CNum4Equ_secantMethodFFI, [:double, :double, :f, :buffer_out], :double
|
28
23
|
class << self
|
29
|
-
# @overload newtonMethod(a, func
|
24
|
+
# @overload newtonMethod(a, func)
|
30
25
|
# ニュートン法による解法
|
31
26
|
# @param [double] a aの値
|
32
27
|
# @param [callback] func aに対する値を計算
|
33
|
-
# @param [callback] dfunc fに対する微分関数
|
34
28
|
# @return [double] xの値
|
35
29
|
# @raise RangeError
|
36
|
-
def newtonMethod(a, func
|
30
|
+
def newtonMethod(a, func)
|
37
31
|
ok_ptr = FFI::MemoryPointer.new :int
|
38
|
-
x = newtonMethodFFI(a, func,
|
32
|
+
x = newtonMethodFFI(a, func, ok_ptr)
|
39
33
|
ok = ok_ptr.read_int
|
40
34
|
ok_ptr.free()
|
41
35
|
if ok < 0 then
|
@@ -77,7 +71,6 @@ module Num4EquLib
|
|
77
71
|
end
|
78
72
|
return x
|
79
73
|
end
|
80
|
-
|
81
74
|
private :newtonMethodFFI
|
82
75
|
private :bisectionMethodFFI
|
83
76
|
private :secantMethodFFI
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: num4equ
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.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-
|
11
|
+
date: 2023-05-20 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/num4equation/blob/main/CHANGELOG.md
|
72
|
-
documentation_uri: https://rubydoc.info/gems/num4equ/0.
|
72
|
+
documentation_uri: https://rubydoc.info/gems/num4equ/0.1.1
|
73
73
|
homepage_uri: http://github.com/siranovel/num4equation
|
74
|
+
wiki_uri: https://github.com/siranovel/mydocs/tree/main/num4equation
|
74
75
|
post_install_message:
|
75
76
|
rdoc_options: []
|
76
77
|
require_paths:
|