dydx 0.1.4 → 0.1.25
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 +4 -4
- data/Gemfile +0 -3
- data/README.md +124 -61
- data/Rakefile +8 -5
- data/dydx.gemspec +13 -13
- data/lib/dydx.rb +25 -23
- data/lib/dydx/algebra.rb +76 -8
- data/lib/dydx/algebra/formula.rb +38 -71
- data/lib/dydx/algebra/inverse.rb +12 -19
- data/lib/dydx/algebra/operator/common_parts.rb +3 -0
- data/lib/dydx/algebra/operator/formula.rb +4 -0
- data/lib/dydx/algebra/operator/general.rb +4 -0
- data/lib/dydx/algebra/operator/inverse.rb +4 -0
- data/lib/dydx/algebra/operator/num.rb +4 -0
- data/lib/dydx/algebra/operator/parts/base.rb +2 -2
- data/lib/dydx/algebra/operator/parts/formula.rb +38 -63
- data/lib/dydx/algebra/operator/parts/general.rb +31 -84
- data/lib/dydx/algebra/operator/parts/interface.rb +22 -0
- data/lib/dydx/algebra/operator/parts/inverse.rb +4 -4
- data/lib/dydx/algebra/operator/parts/num.rb +11 -16
- data/lib/dydx/algebra/operator/parts/symbol.rb +2 -2
- data/lib/dydx/algebra/operator/symbol.rb +15 -0
- data/lib/dydx/algebra/set.rb +34 -271
- data/lib/dydx/algebra/set/base.rb +9 -0
- data/lib/dydx/algebra/set/cos.rb +22 -0
- data/lib/dydx/algebra/set/e.rb +16 -0
- data/lib/dydx/algebra/set/fixnum.rb +14 -0
- data/lib/dydx/algebra/set/float.rb +14 -0
- data/lib/dydx/algebra/set/log.rb +22 -0
- data/lib/dydx/algebra/set/num.rb +26 -0
- data/lib/dydx/algebra/set/pi.rb +16 -0
- data/lib/dydx/algebra/set/sin.rb +21 -0
- data/lib/dydx/algebra/set/symbol.rb +14 -0
- data/lib/dydx/algebra/set/tan.rb +17 -0
- data/lib/dydx/delta.rb +1 -1
- data/lib/dydx/function.rb +1 -1
- data/lib/dydx/helper.rb +61 -55
- data/lib/dydx/integrand.rb +10 -22
- data/lib/dydx/version.rb +1 -1
- data/spec/dydx_spec.rb +29 -10
- data/spec/lib/algebra/formula_spec.rb +38 -44
- data/spec/lib/algebra/operator/parts/base_spec.rb +5 -5
- data/spec/lib/algebra/operator/parts/formula_spec.rb +50 -57
- data/spec/lib/algebra/operator/parts/inverse_spec.rb +8 -8
- data/spec/lib/algebra/set/cos_spec.rb +18 -0
- data/spec/lib/algebra/set/e_spec.rb +27 -0
- data/spec/lib/algebra/set/fixnum_spec.rb +65 -0
- data/spec/lib/algebra/set/float_spec.rb +65 -0
- data/spec/lib/algebra/set/log_spec.rb +15 -0
- data/spec/lib/algebra/set/num_spec.rb +23 -0
- data/spec/lib/algebra/set/pi_spec.rb +25 -0
- data/spec/lib/algebra/set/sin_spec.rb +14 -0
- data/spec/lib/algebra/set/symbol_spec.rb +22 -0
- data/spec/lib/algebra/set/tan_spec.rb +13 -0
- data/spec/lib/delta_spec.rb +8 -32
- data/spec/lib/function_spec.rb +34 -60
- data/spec/lib/helper_spec.rb +49 -47
- data/spec/lib/integrand_spec.rb +15 -13
- data/spec/spec_helper.rb +1 -2
- metadata +39 -9
- data/.pryrc +0 -2
- data/.rubocop.yml +0 -25
- data/spec/lib/algebra/set_spec.rb +0 -263
@@ -0,0 +1,22 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Cos < Base
|
5
|
+
attr_accessor :x
|
6
|
+
|
7
|
+
def initialize(x)
|
8
|
+
@x = x
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
"cos( #{x.to_s} )"
|
13
|
+
end
|
14
|
+
|
15
|
+
def differentiate(sym=:x)
|
16
|
+
-1 * sin(x) * x.d(sym)
|
17
|
+
end
|
18
|
+
alias_method :d, :differentiate
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Log < Base
|
5
|
+
attr_accessor :f
|
6
|
+
|
7
|
+
def initialize(f)
|
8
|
+
@f = f
|
9
|
+
end
|
10
|
+
|
11
|
+
def differentiate(sym=:x)
|
12
|
+
f.d(sym) / (f)
|
13
|
+
end
|
14
|
+
alias_method :d, :differentiate
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"log( #{f.to_s} )"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Num < Base
|
5
|
+
attr_accessor :n
|
6
|
+
|
7
|
+
def initialize(n)
|
8
|
+
@n = n
|
9
|
+
end
|
10
|
+
|
11
|
+
def differentiate(sym=:x)
|
12
|
+
e0
|
13
|
+
end
|
14
|
+
alias_method :d, :differentiate
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@n.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(x)
|
21
|
+
to_s == x.to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/dydx/delta.rb
CHANGED
data/lib/dydx/function.rb
CHANGED
data/lib/dydx/helper.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
module Dydx
|
2
2
|
module Helper
|
3
|
+
OP_SYM_STR = {
|
4
|
+
addition: :+,
|
5
|
+
multiplication: :*,
|
6
|
+
exponentiation: :^
|
7
|
+
}
|
8
|
+
|
3
9
|
SUPER_OPE_RELATION = {
|
4
10
|
:+ => :*,
|
5
11
|
:- => :/,
|
6
|
-
:* =>
|
12
|
+
:* => :^,
|
7
13
|
:/ => :|
|
8
14
|
}
|
9
15
|
|
@@ -12,75 +18,95 @@ module Dydx
|
|
12
18
|
:- => :+,
|
13
19
|
:* => :/,
|
14
20
|
:/ => :*,
|
15
|
-
|
16
|
-
:| =>
|
21
|
+
:^ => :|,
|
22
|
+
:| => :^
|
17
23
|
}
|
18
24
|
|
19
|
-
def
|
20
|
-
|
25
|
+
def super_ope(operator)
|
26
|
+
SUPER_OPE_RELATION[operator]
|
27
|
+
end
|
28
|
+
|
29
|
+
def sub_ope(operator)
|
30
|
+
SUPER_OPE_RELATION.invert[operator]
|
21
31
|
end
|
22
32
|
|
23
|
-
def
|
24
|
-
|
33
|
+
def inverse_ope(operator)
|
34
|
+
INVERSE_OPE_RELATION[operator]
|
25
35
|
end
|
26
36
|
|
27
|
-
def
|
28
|
-
|
37
|
+
def inverse_super_ope(operator)
|
38
|
+
inverse_ope(super_ope(operator))
|
29
39
|
end
|
30
40
|
|
31
|
-
def
|
32
|
-
|
41
|
+
def is_num?
|
42
|
+
(is_a?(Num) || is_a?(Fixnum) || is_a?(Float)) || (is_a?(Inverse) && x.is_num?)
|
33
43
|
end
|
34
44
|
|
35
|
-
def
|
36
|
-
[
|
45
|
+
def is_0?
|
46
|
+
[0, 0.0].include?(self) || (is_a?(Num) && n.is_0?)
|
47
|
+
end
|
48
|
+
|
49
|
+
def is_1?
|
50
|
+
[1, 1.0].include?(self) || (is_a?(Num) && n.is_1?)
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_minus1?
|
54
|
+
[1, -1.0].include?(self)|| (is_a?(Num) && n.is_minus1?)
|
37
55
|
end
|
38
56
|
|
39
57
|
def distributive?(ope1, ope2)
|
40
|
-
[ope1
|
58
|
+
[super_ope(ope1), inverse_super_ope(ope1)].include?(ope2)
|
41
59
|
end
|
42
60
|
|
43
|
-
# TODO: Cyclomatic complexity for combinable? is too high. [17/6]
|
44
61
|
def combinable?(x, operator)
|
45
62
|
case operator
|
46
63
|
when :+
|
47
|
-
|
48
|
-
(
|
49
|
-
|
64
|
+
self == x ||
|
65
|
+
(is_num? && x.is_num?) ||
|
66
|
+
(multiplication? && (f == x || g == x)) ||
|
67
|
+
(x.multiplication? && (x.f == self || x.g == self)) ||
|
50
68
|
inverse?(:+, x)
|
51
69
|
when :*
|
52
70
|
self == x ||
|
53
|
-
(
|
71
|
+
(is_num? && x.is_num?) ||
|
54
72
|
inverse?(:*, x)
|
55
|
-
when
|
56
|
-
(
|
73
|
+
when :^
|
74
|
+
(is_num? && x.is_num?) || is_0? || is_1?
|
57
75
|
end
|
58
76
|
end
|
59
77
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
78
|
+
def is_multiple_of(x)
|
79
|
+
if is_0?
|
80
|
+
e0
|
81
|
+
elsif self == x
|
82
|
+
e1
|
83
|
+
# elsif is_num? && x.is_num? && (self % x == 0)
|
84
|
+
# _(n / x.n)
|
85
|
+
elsif multiplication? && (f == x || g == x)
|
86
|
+
f == x ? g : f
|
87
|
+
else
|
88
|
+
false
|
89
|
+
end
|
66
90
|
end
|
67
91
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
(formula?(:*) && (f == x || g == x))
|
92
|
+
OP_SYM_STR.each do |operator_name, operator|
|
93
|
+
define_method("#{operator_name}?") do
|
94
|
+
is_a?(Formula) && (@operator == operator)
|
95
|
+
# is_a?(Inverse) && self.operator == operator
|
96
|
+
end
|
74
97
|
end
|
75
98
|
|
76
99
|
def rest(f_or_g)
|
77
100
|
([:f, :g] - [f_or_g]).first
|
78
101
|
end
|
79
102
|
|
80
|
-
def
|
103
|
+
def commutative?
|
81
104
|
end
|
82
105
|
|
83
|
-
def
|
106
|
+
def distributable?(operator)
|
107
|
+
end
|
108
|
+
|
109
|
+
def inverse?(operator, x=nil)
|
84
110
|
if is_a?(Algebra::Inverse)
|
85
111
|
self.operator == operator && (self.x == x || x.nil?)
|
86
112
|
elsif x.is_a?(Algebra::Inverse)
|
@@ -98,26 +124,6 @@ module Dydx
|
|
98
124
|
def commutative?
|
99
125
|
[:+, :*].include?(self)
|
100
126
|
end
|
101
|
-
|
102
|
-
def associative?
|
103
|
-
[:+, :*].include?(self)
|
104
|
-
end
|
105
|
-
|
106
|
-
def super
|
107
|
-
SUPER_OPE_RELATION[self] || self
|
108
|
-
end
|
109
|
-
|
110
|
-
def sub
|
111
|
-
SUPER_OPE_RELATION.invert[self] || self
|
112
|
-
end
|
113
|
-
|
114
|
-
def inv
|
115
|
-
INVERSE_OPE_RELATION[self] || self
|
116
|
-
end
|
117
|
-
|
118
|
-
def inv_super
|
119
|
-
self.super.inv
|
120
|
-
end
|
121
127
|
end
|
122
128
|
|
123
129
|
def ==(x)
|
data/lib/dydx/integrand.rb
CHANGED
@@ -1,38 +1,26 @@
|
|
1
1
|
module Dydx
|
2
2
|
class Integrand
|
3
3
|
attr_accessor :function, :var
|
4
|
-
|
5
4
|
def initialize(function, var)
|
6
5
|
@function = function
|
7
6
|
@var = var
|
8
7
|
end
|
9
8
|
|
10
|
-
def [](a, b, n =
|
9
|
+
def [](a, b, n = 100)
|
10
|
+
f = function
|
11
|
+
a, b = [a, b].map(&:to_f)
|
12
|
+
raise ArgumentError, 'b should be greater than a' if a > b
|
11
13
|
# HOT FIX: should implement Infinity class
|
12
14
|
a = - 1000 if a == - Float::INFINITY
|
13
15
|
b = 1000 if b == Float::INFINITY
|
14
16
|
|
15
|
-
a, b = [a, b].map(&:to_f)
|
16
|
-
fail ArgumentError, 'b should be greater than a' if a > b
|
17
|
-
$temp_cal_f = function
|
18
|
-
|
19
|
-
n = [n, (b - a) * 2].max
|
20
|
-
n += 1 if n.to_i.odd?
|
21
17
|
h = (b - a) / n
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def f(vars)
|
30
|
-
temp_cal_f(vars)
|
31
|
-
end
|
32
|
-
|
33
|
-
def round_8(num)
|
34
|
-
return num if num.abs == Float::INFINITY
|
35
|
-
(num * 10 ** 8).round * 10.0 ** (-8)
|
18
|
+
sum = 0.0
|
19
|
+
xi = ->(i){ a + h * i }
|
20
|
+
n.to_i.times do |i|
|
21
|
+
sum += ( f(xi.(i)) + 4.0 * f(xi.(i) + h / 2.0 ) + f(xi.(i) + h) )
|
22
|
+
end
|
23
|
+
( h * sum ) / 6.0
|
36
24
|
end
|
37
25
|
end
|
38
26
|
end
|