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 Operator
|
4
|
+
module Parts
|
5
|
+
module Interface
|
6
|
+
%w(+ - * / ^).map(&:to_sym).each do |operator|
|
7
|
+
define_method(operator) do |x|
|
8
|
+
x = ::Set::Num.new(x) if x.is_a?(Fixnum)
|
9
|
+
if operator == :/ && x.is_0?
|
10
|
+
raise ZeroDivisionError
|
11
|
+
elsif [:-, :/].include?(operator)
|
12
|
+
send(inverse_ope(operator), inverse(x, inverse_ope(operator)))
|
13
|
+
else
|
14
|
+
super(x)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -3,17 +3,17 @@ module Dydx
|
|
3
3
|
module Operator
|
4
4
|
module Parts
|
5
5
|
module Inverse
|
6
|
-
%w(+ *
|
6
|
+
%w(+ * ^).map(&:to_sym).each do |operator|
|
7
7
|
define_method(operator) do |x|
|
8
8
|
if inverse?(operator, x)
|
9
9
|
case operator
|
10
10
|
when :+ then e0
|
11
11
|
when :* then e1
|
12
12
|
end
|
13
|
-
elsif
|
13
|
+
elsif !x.is_a?(Inverse) && operator == :+
|
14
14
|
x + self
|
15
|
-
elsif operator
|
16
|
-
inverse(self.x
|
15
|
+
elsif self.operator == :* && operator == :^
|
16
|
+
inverse(self.x ^ x, :*)
|
17
17
|
else
|
18
18
|
super(x)
|
19
19
|
end
|
@@ -3,36 +3,31 @@ module Dydx
|
|
3
3
|
module Operator
|
4
4
|
module Parts
|
5
5
|
module Num
|
6
|
-
%w(+ *
|
6
|
+
%w(+ * ^).map(&:to_sym).each do |operator|
|
7
7
|
define_method(operator) do |x|
|
8
|
-
if
|
9
|
-
_(n.send(operator, x.n))
|
10
|
-
elsif operator == :+ && x.inverse?(:+) && x.x.is_a?(Num)
|
11
|
-
_(n - x.x.n)
|
12
|
-
elsif operator == :* && x.inverse?(:*) && x.x.is_a?(Num) && n % x.x.n == 0
|
13
|
-
_(n / x.x.n)
|
14
|
-
elsif zero?
|
8
|
+
if is_0?
|
15
9
|
case operator
|
16
10
|
when :+ then x
|
17
11
|
when :* then e0
|
18
|
-
when
|
12
|
+
when :^ then e0
|
19
13
|
end
|
20
|
-
elsif
|
14
|
+
elsif is_1?
|
21
15
|
case operator
|
22
16
|
when :+ then super(x)
|
23
17
|
when :* then x
|
24
|
-
when
|
18
|
+
when :^ then e1
|
25
19
|
end
|
20
|
+
elsif x.is_a?(Num)
|
21
|
+
_(n.send(operator, x.n))
|
22
|
+
elsif operator == :+ && x.inverse?(:+) && x.x.is_a?(Num)
|
23
|
+
_(n - x.x.n)
|
24
|
+
elsif operator == :* && x.inverse?(:*) && x.x.is_a?(Num)
|
25
|
+
_(n / x.x.n)
|
26
26
|
else
|
27
27
|
super(x)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
32
|
-
def %(num)
|
33
|
-
fail ArgumentError, "#{num} should be Num class object" unless num.is_a?(Num)
|
34
|
-
_(n % num.n)
|
35
|
-
end
|
36
31
|
end
|
37
32
|
end
|
38
33
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/symbol'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module Symbol
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::Symbol
|
10
|
+
include Parts::General
|
11
|
+
include Parts::Interface
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/dydx/algebra/set.rb
CHANGED
@@ -1,208 +1,31 @@
|
|
1
|
-
require 'dydx/algebra/
|
2
|
-
require 'dydx/algebra/
|
3
|
-
require 'dydx/algebra/
|
4
|
-
require 'dydx/algebra/
|
5
|
-
require 'dydx/algebra/
|
6
|
-
|
7
|
-
require 'dydx/algebra/
|
8
|
-
require 'dydx/algebra/
|
9
|
-
require 'dydx/algebra/
|
10
|
-
require 'dydx/algebra/
|
11
|
-
|
12
|
-
require 'dydx/algebra/formula'
|
13
|
-
require 'dydx/algebra/inverse'
|
1
|
+
require 'dydx/algebra/set/base'
|
2
|
+
require 'dydx/algebra/set/num'
|
3
|
+
require 'dydx/algebra/set/fixnum'
|
4
|
+
require 'dydx/algebra/set/float'
|
5
|
+
require 'dydx/algebra/set/symbol'
|
6
|
+
require 'dydx/algebra/set/e'
|
7
|
+
require 'dydx/algebra/set/pi'
|
8
|
+
require 'dydx/algebra/set/log'
|
9
|
+
require 'dydx/algebra/set/sin'
|
10
|
+
require 'dydx/algebra/set/cos'
|
11
|
+
require 'dydx/algebra/set/tan'
|
14
12
|
|
15
13
|
module Dydx
|
16
14
|
module Algebra
|
17
15
|
module Set
|
18
|
-
module Base
|
19
|
-
include Helper
|
20
|
-
|
21
|
-
# TODO: Pi should not have attr_accessor
|
22
|
-
def self.included(_klass)
|
23
|
-
attr_accessor :n, :x
|
24
|
-
alias_method :d, :differentiate
|
25
|
-
end
|
26
|
-
|
27
|
-
def initialize(x = nil)
|
28
|
-
case self
|
29
|
-
when Num
|
30
|
-
@n = x
|
31
|
-
when Sin, Cos, Tan, Log, Log10, Log2
|
32
|
-
@x = x
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_s
|
37
|
-
case self
|
38
|
-
when Num then n.to_s
|
39
|
-
when Pi then 'pi'
|
40
|
-
when E then 'e'
|
41
|
-
when Sin then "sin( #{x} )"
|
42
|
-
when Cos then "cos( #{x} )"
|
43
|
-
when Tan then "tan( #{x} )"
|
44
|
-
when Log then "log( #{x} )"
|
45
|
-
when Log10 then "log10( #{x} )"
|
46
|
-
when Log2 then "log2( #{x} )"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def to_f
|
51
|
-
case self
|
52
|
-
when Num then n.to_f
|
53
|
-
when Pi then Math::PI
|
54
|
-
when E then Math::E
|
55
|
-
when Symbol then fail ArgumentError
|
56
|
-
when Sin then Math.sin(x.to_f)
|
57
|
-
when Cos then Math.cos(x.to_f)
|
58
|
-
when Tan then Math.tan(x.to_f)
|
59
|
-
when Log then Math.log(x.to_f)
|
60
|
-
when Log10 then Math.log(x.to_f, 10)
|
61
|
-
when Log2 then Math.log(x.to_f, 2)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def subst(hash = {})
|
66
|
-
case self
|
67
|
-
when Num, Pi, E
|
68
|
-
self
|
69
|
-
when Symbol
|
70
|
-
hash[self] || self
|
71
|
-
when Sin then sin(x.subst(hash))
|
72
|
-
when Cos then cos(x.subst(hash))
|
73
|
-
when Tan then tan(x.subst(hash))
|
74
|
-
when Log then log(x.subst(hash))
|
75
|
-
when Log10 then log10(x.subst(hash))
|
76
|
-
when Log2 then log2(x.subst(hash))
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def differentiate(sym = :x)
|
81
|
-
case self
|
82
|
-
when Num, Pi, E then e0
|
83
|
-
when Symbol then self == sym ? e1 : e0
|
84
|
-
when Sin then cos(x) * x.d(sym)
|
85
|
-
when Cos then -1 * sin(x) * x.d(sym)
|
86
|
-
when Tan then 1 / (cos(x) ** 2)
|
87
|
-
when Log then x.d(sym) / (x)
|
88
|
-
when Log10 then x.d(sym) / (x * log(10))
|
89
|
-
when Log2 then x.d(sym) / (x * log(2))
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class Num
|
95
|
-
include Base
|
96
|
-
include Operator::Num
|
97
|
-
%w(> >= < <=).each do |operator|
|
98
|
-
define_method(operator) do |x|
|
99
|
-
x = x.n if x.is_a?(Num)
|
100
|
-
n.send(operator, x)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
class Pi
|
106
|
-
include Base
|
107
|
-
include Operator::General
|
108
|
-
end
|
109
|
-
|
110
|
-
class E
|
111
|
-
include Base
|
112
|
-
include Operator::General
|
113
|
-
end
|
114
|
-
|
115
|
-
class Sin
|
116
|
-
include Base
|
117
|
-
include Operator::General
|
118
|
-
end
|
119
|
-
|
120
|
-
class Cos
|
121
|
-
include Base
|
122
|
-
include Operator::General
|
123
|
-
end
|
124
|
-
|
125
|
-
class Tan
|
126
|
-
include Base
|
127
|
-
include Operator::General
|
128
|
-
end
|
129
|
-
|
130
|
-
class Log
|
131
|
-
include Base
|
132
|
-
include Operator::General
|
133
|
-
end
|
134
|
-
|
135
|
-
class Log10
|
136
|
-
include Base
|
137
|
-
include Operator::General
|
138
|
-
end
|
139
|
-
|
140
|
-
class Log2
|
141
|
-
include Base
|
142
|
-
include Operator::General
|
143
|
-
end
|
144
|
-
|
145
|
-
Symbol.class_eval do
|
146
|
-
include Base
|
147
|
-
include Operator::General
|
148
|
-
end
|
149
|
-
|
150
|
-
numeric_proc = Proc.new do
|
151
|
-
include Helper
|
152
|
-
|
153
|
-
def subst(_hash = {})
|
154
|
-
self
|
155
|
-
end
|
156
|
-
|
157
|
-
def differentiate(_sym = :x)
|
158
|
-
e0
|
159
|
-
end
|
160
|
-
alias_method :d, :differentiate
|
161
|
-
|
162
|
-
alias_method :addition, :+
|
163
|
-
alias_method :subtraction, :-
|
164
|
-
alias_method :multiplication, :*
|
165
|
-
alias_method :division, :/
|
166
|
-
alias_method :exponentiation, :**
|
167
|
-
alias_method :modulation, :%
|
168
|
-
|
169
|
-
ope_to_str = {
|
170
|
-
addition: :+,
|
171
|
-
subtraction: :-,
|
172
|
-
multiplication: :*,
|
173
|
-
division: :/,
|
174
|
-
exponentiation: :**,
|
175
|
-
modulation: :%
|
176
|
-
}
|
177
|
-
%w(+ - * / ** %).each do |operator|
|
178
|
-
define_method(operator) do |g|
|
179
|
-
if g.is_a?(Symbol) ||
|
180
|
-
g.is_a?(Formula) ||
|
181
|
-
g.is_a?(Base)
|
182
|
-
|
183
|
-
_(self).send(operator.to_sym, g)
|
184
|
-
else
|
185
|
-
send(ope_to_str.key(operator.to_sym), g)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
if self == Rational
|
190
|
-
def to_s
|
191
|
-
"( #{numerator} / #{denominator} )"
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
Float.class_eval(&numeric_proc)
|
197
|
-
Fixnum.class_eval(&numeric_proc)
|
198
|
-
Rational.class_eval(&numeric_proc)
|
199
|
-
|
200
16
|
def e0
|
201
|
-
eval(
|
17
|
+
eval("$e0 ||= Num.new(0)")
|
202
18
|
end
|
203
19
|
|
204
20
|
def e1
|
205
|
-
eval(
|
21
|
+
eval("$e1 ||= Num.new(1)")
|
22
|
+
end
|
23
|
+
def _(num)
|
24
|
+
if num >= 0
|
25
|
+
eval("$p#{num} ||= Num.new(num)")
|
26
|
+
else
|
27
|
+
eval("$n#{-1 * num} ||= Num.new(num)")
|
28
|
+
end
|
206
29
|
end
|
207
30
|
|
208
31
|
def pi
|
@@ -217,15 +40,14 @@ module Dydx
|
|
217
40
|
Float::INFINITY
|
218
41
|
end
|
219
42
|
|
220
|
-
# TODO: Method has too many lines. [13/10]
|
221
43
|
def log(formula)
|
222
|
-
if formula.
|
44
|
+
if formula.multiplication?
|
223
45
|
f, g = formula.f, formula.g
|
224
46
|
log(f) + log(g)
|
225
|
-
elsif formula.
|
47
|
+
elsif formula.exponentiation?
|
226
48
|
f, g = formula.f, formula.g
|
227
49
|
g * log(f)
|
228
|
-
elsif formula.
|
50
|
+
elsif formula.is_1?
|
229
51
|
e0
|
230
52
|
elsif formula.is_a?(E)
|
231
53
|
e1
|
@@ -234,87 +56,28 @@ module Dydx
|
|
234
56
|
end
|
235
57
|
end
|
236
58
|
|
237
|
-
def
|
238
|
-
|
239
|
-
if
|
240
|
-
f, g = formula.f, formula.g
|
241
|
-
log2(f) + log2(g)
|
242
|
-
elsif formula.formula?(:**)
|
243
|
-
f, g = formula.f, formula.g
|
244
|
-
g * log2(f)
|
245
|
-
elsif formula.one?
|
59
|
+
def sin(x)
|
60
|
+
multiplier = x.is_multiple_of(pi)
|
61
|
+
if multiplier.is_a?(Num)
|
246
62
|
e0
|
247
|
-
elsif formula.is_a?(Num)
|
248
|
-
(formula.n == 2) ? e1 : log2(formula.n)
|
249
|
-
elsif formula == 2
|
250
|
-
e1
|
251
63
|
else
|
252
|
-
|
64
|
+
Sin.new(x)
|
253
65
|
end
|
254
66
|
end
|
255
67
|
|
256
|
-
def
|
257
|
-
|
258
|
-
if
|
259
|
-
f, g = formula.f, formula.g
|
260
|
-
log10(f) + log10(g)
|
261
|
-
elsif formula.formula?(:**)
|
262
|
-
f, g = formula.f, formula.g
|
263
|
-
g * log10(f)
|
264
|
-
elsif formula.one?
|
265
|
-
e0
|
266
|
-
elsif formula.is_a?(Num)
|
267
|
-
(formula.n == 10) ? e1 : log10(formula.n)
|
268
|
-
elsif formula == 10
|
68
|
+
def cos(x)
|
69
|
+
multiplier = x.is_multiple_of(pi)
|
70
|
+
if multiplier.is_a?(Num) && multiplier.n % 2 == 0
|
269
71
|
e1
|
72
|
+
elsif multiplier.is_a?(Num) && multiplier.n % 2 == 1
|
73
|
+
_(-1)
|
270
74
|
else
|
271
|
-
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
# TODO: We should negative num
|
276
|
-
def sin(x)
|
277
|
-
return Sin.new(x) unless x.multiple_of?(pi) && (x / pi).num?
|
278
|
-
|
279
|
-
radn = (x / pi)
|
280
|
-
loop do
|
281
|
-
break if radn < 2
|
282
|
-
radn -= 2
|
283
|
-
end
|
284
|
-
|
285
|
-
case radn
|
286
|
-
when 0 then 0
|
287
|
-
when _(1) / 2 then 1
|
288
|
-
when 1 then 0
|
289
|
-
when _(3) / 2 then -1
|
290
|
-
else Sin.new(x)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
def cos(x)
|
295
|
-
return Cos.new(x) unless x.multiple_of?(pi) && (x / pi).num?
|
296
|
-
|
297
|
-
radn = (x / pi)
|
298
|
-
loop do
|
299
|
-
break if radn < 2
|
300
|
-
radn -= 2
|
301
|
-
end
|
302
|
-
|
303
|
-
case radn
|
304
|
-
when 0 then 1
|
305
|
-
when _(1) / 2 then 0
|
306
|
-
when 1 then -1
|
307
|
-
when _(3) / 2 then 0
|
308
|
-
else Sin.new(x)
|
75
|
+
Cos.new(x)
|
309
76
|
end
|
310
77
|
end
|
311
78
|
|
312
79
|
def tan(x)
|
313
|
-
|
314
|
-
0
|
315
|
-
else
|
316
|
-
Tan.new(x)
|
317
|
-
end
|
80
|
+
Tan.new(x)
|
318
81
|
end
|
319
82
|
end
|
320
83
|
end
|