dydx 0.1.31 → 0.1.314

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -59
  3. data/lib/dydx.rb +7 -4
  4. data/lib/dydx/algebra.rb +0 -66
  5. data/lib/dydx/algebra/formula.rb +15 -2
  6. data/lib/dydx/algebra/inverse.rb +14 -0
  7. data/lib/dydx/algebra/set.rb +253 -19
  8. data/lib/dydx/helper.rb +5 -0
  9. data/lib/dydx/integrand.rb +7 -2
  10. data/lib/dydx/version.rb +1 -1
  11. data/spec/dydx_spec.rb +10 -29
  12. data/spec/lib/algebra/formula_spec.rb +6 -0
  13. data/spec/lib/algebra/set_spec.rb +227 -0
  14. data/spec/lib/delta_spec.rb +26 -0
  15. data/spec/lib/function_spec.rb +61 -33
  16. data/spec/lib/integrand_spec.rb +2 -2
  17. metadata +4 -40
  18. data/lib/dydx/algebra/operator.rb +0 -5
  19. data/lib/dydx/algebra/set/base.rb +0 -9
  20. data/lib/dydx/algebra/set/cos.rb +0 -22
  21. data/lib/dydx/algebra/set/e.rb +0 -16
  22. data/lib/dydx/algebra/set/fixnum.rb +0 -14
  23. data/lib/dydx/algebra/set/float.rb +0 -14
  24. data/lib/dydx/algebra/set/log.rb +0 -22
  25. data/lib/dydx/algebra/set/log10.rb +0 -22
  26. data/lib/dydx/algebra/set/log2.rb +0 -22
  27. data/lib/dydx/algebra/set/num.rb +0 -22
  28. data/lib/dydx/algebra/set/pi.rb +0 -16
  29. data/lib/dydx/algebra/set/sin.rb +0 -22
  30. data/lib/dydx/algebra/set/symbol.rb +0 -14
  31. data/lib/dydx/algebra/set/tan.rb +0 -17
  32. data/spec/lib/algebra/set/cos_spec.rb +0 -18
  33. data/spec/lib/algebra/set/e_spec.rb +0 -27
  34. data/spec/lib/algebra/set/fixnum_spec.rb +0 -65
  35. data/spec/lib/algebra/set/float_spec.rb +0 -65
  36. data/spec/lib/algebra/set/log10_spec.rb +0 -16
  37. data/spec/lib/algebra/set/log2_spec.rb +0 -16
  38. data/spec/lib/algebra/set/log_spec.rb +0 -15
  39. data/spec/lib/algebra/set/num_spec.rb +0 -23
  40. data/spec/lib/algebra/set/pi_spec.rb +0 -25
  41. data/spec/lib/algebra/set/sin_spec.rb +0 -14
  42. data/spec/lib/algebra/set/symbol_spec.rb +0 -22
  43. data/spec/lib/algebra/set/tan_spec.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e4de7f815619a3ce01c34bc76a405749bd71f3b
4
- data.tar.gz: 957aef1d10863b8fbacf8d142c5a8fd71d9c77e5
3
+ metadata.gz: c3575108c38dd6f4c3e191dcada2dfcc03e11b10
4
+ data.tar.gz: 37dbeb379531178c52253e213f6b79b930b0d9b7
5
5
  SHA512:
6
- metadata.gz: 5236b464cc82383856640addf8bf5c0f725e68648282278195ac839050df72b35e65e444c15dbbd657b20a05dcb0fdf9df8046d123e735198dee306f764f8b07
7
- data.tar.gz: a26cc47a1cb5be11eecb54c7358122dcd9d556644bda14a3a01e222db53779320736a9557036e9af9d967abfb37714f7019e559495bbb49e024f82bad9d5d20a
6
+ metadata.gz: 047b709dd5da3ec9b0058c86ced1755fbee7928279794c1f4d366c9f3e50c27a948e6feba092425073a3a3dc6781dfdacd4d5724aa947a41393ef6ae4e511c2f
7
+ data.tar.gz: 846c7e77de2aad86f4e65a8b7e6cac39f88c9427b00c39fe7297efc6aa2d34f260450233c09ecca108200e2f8eb1cec492f7d0af48815bf24d1e85c0f916b6f4
data/README.md CHANGED
@@ -19,29 +19,31 @@ require 'dydx'
19
19
  include Dydx
20
20
 
21
21
  # Define the function. syntax is not good enough...
22
- f(x) <= x ^ 2
22
+ f(x, y) <= x + x*y + y
23
23
 
24
- f(3)
25
- => 9
24
+ # simplify
25
+ f(x, y) == x * (1 + y) + y
26
+ => true
26
27
 
27
- f(x).to_s
28
- => "( x ^ 2 )"
28
+ #part substitution
29
+ f(a, 2) == 3*a + 2
30
+ => true
29
31
 
30
- f(x) == eval(f(x).to_s)
32
+ f(1, a + b) == 1 + 2 * ( a + b )
31
33
  => true
32
34
 
35
+
33
36
  # Differentiate
34
- g(x) <= d/dx(f(x))
37
+ d/dx(f(x, y)) == 1 + y
38
+ => true
35
39
 
36
- g(3)
37
- => 6
40
+ g(x) <= sin(x)
38
41
 
39
- g(x).to_s
40
- => '2 * x'
42
+ d/dx(g(x)) == cos(x)
41
43
 
42
44
  # Integrate
43
- S(f(x), dx)[0, 1]
44
- => 0.3333333333333334
45
+ S(g(x), dx)[0, pi/2]
46
+ => 1.0
45
47
  ```
46
48
 
47
49
 
@@ -52,18 +54,14 @@ f(z) <= log(z)
52
54
  S(f(z), dz)[0,1]
53
55
  => -Infinity
54
56
 
55
- ( d/dx(log(x)) ).to_s
56
- => "( 1 / x )"
57
-
58
- ( d/dx(cos(x)) ).to_s
59
- => "( - sin( x ) )"
57
+ d/dx(log(x)) == 1 / x
58
+ => true
60
59
 
61
- ( d/dx(e ^ x) ).to_s
62
- => "( e ^ x )"
60
+ d/dx(cos(x)) == -cos(x)
61
+ => true
63
62
 
64
- f(x) <= sin(x)
65
- S(f(x), dx)[0, Math::PI/2]
66
- => 1.0
63
+ d/dx(e ^ x) == e ^ x
64
+ => true
67
65
 
68
66
  # standard normal distribution;
69
67
  f(x) <= (1.0 / ( ( 2.0 * pi ) ^ 0.5 ) ) * ( e ^ (- (x ^ 2) / 2) )
@@ -71,38 +69,6 @@ S(f(x), dx)[-oo, oo]
71
69
  => 1.0
72
70
  ```
73
71
 
74
- #### it's like a magic...
75
-
76
- ```ruby:
77
- f(x) <= x ^ 2
78
-
79
- f(a + b).to_s
80
- => "( ( a + b ) ^ 2 )"
81
-
82
- #↓it's like a magic!!!
83
- g(a, b) <= f(a + b)
84
-
85
- g(a, b).to_s
86
- => "( ( a + b ) ^ 2 )"
87
-
88
- g(2, 2)
89
- => 16
90
-
91
- ( d/da(g(a, b)) ).to_s
92
- => "( 2 * ( a + b ) )"
93
-
94
- # simplify
95
- ((x * y) + (z * x)).to_s
96
- => "( x * ( y + z ) )"
97
-
98
- ((x ^ y) / (x ^ z)).to_s
99
- => "( x ^ ( y - z ) )"
100
-
101
- (x + x).to_s
102
- => "( 2 * x )"
103
- ```
104
-
105
-
106
72
  ## Documents
107
73
  I'm going to write now...cominng soon....
108
74
 
@@ -111,14 +77,11 @@ I'm going to write now...cominng soon....
111
77
  ```
112
78
  Dydx
113
79
  |- Algebra
114
- | |- Set
115
- | | |- Num
116
- | | |- ....
117
- | |
118
80
  | |- Operator
119
81
  | | |- Interface
120
82
  | | |- ....
121
83
  | |
84
+ | |- Set
122
85
  | |- Formula
123
86
  | |- inverse
124
87
  |
data/lib/dydx.rb CHANGED
@@ -6,7 +6,7 @@ require 'dydx/integrand'
6
6
 
7
7
  module Dydx
8
8
  include Algebra
9
- %w(f g h).each do |functioner|
9
+ %w(f g h int_f).each do |functioner|
10
10
  define_method(functioner) do |*vars|
11
11
  function = eval("$#{functioner}")
12
12
  return eval("$#{functioner} = Function.new(*vars)") unless function
@@ -15,9 +15,12 @@ module Dydx
15
15
  return function if function.vars == vars
16
16
  return function unless function.algebra
17
17
 
18
- string = substitute(vars, function)
19
- string = rename_for_calc(string) if all_vars_num?(vars)
20
- eval(string)
18
+ subst_hash = Hash[*[function.vars, vars].transpose.flatten]
19
+ begin
20
+ function.algebra.subst(subst_hash).to_f
21
+ rescue ArgumentError
22
+ eval(function.algebra.subst(subst_hash).to_s)
23
+ end
21
24
  end
22
25
  end
23
26
 
data/lib/dydx/algebra.rb CHANGED
@@ -1,77 +1,11 @@
1
1
  require 'dydx/algebra/set'
2
- require 'dydx/algebra/operator'
3
2
  require 'dydx/algebra/formula'
4
3
  require 'dydx/algebra/inverse'
5
4
 
6
5
  module Dydx
7
6
  module Algebra
8
7
  include Set
9
- module Set
10
- # TODO: Refactor
11
- Fixnum.class_eval do
12
- alias_method :addition, :+
13
- alias_method :subtraction, :-
14
- alias_method :multiplication, :*
15
- alias_method :division, :/
16
- alias_method :exponentiation, :**
17
- ope_to_str = {
18
- addition: :+,
19
- subtraction: :-,
20
- multiplication: :*,
21
- division: :/,
22
- exponentiation: :^
23
- }
24
- %w(+ - * / ^).each do |operator|
25
- define_method(operator) do |g|
26
- if g.is_a?(Symbol) ||
27
- g.is_a?(Formula) ||
28
- g.is_a?(Base)
29
8
 
30
- Num.new(self).send(operator.to_sym, g)
31
- else
32
- send(ope_to_str.key(operator.to_sym), g)
33
- end
34
- end
35
- end
36
- end
37
-
38
- Float.class_eval do
39
- alias_method :addition, :+
40
- alias_method :subtraction, :-
41
- alias_method :multiplication, :*
42
- alias_method :division, :/
43
- alias_method :exponentiation, :**
44
- ope_to_str = {
45
- addition: :+,
46
- subtraction: :-,
47
- multiplication: :*,
48
- division: :/,
49
- exponentiation: :^
50
- }
51
- %w(+ - * / ^).each do |operator|
52
- define_method(operator) do |g|
53
- if g.is_a?(Symbol) ||
54
- g.is_a?(Formula) ||
55
- g.is_a?(Base)
56
-
57
- Num.new(self).send(operator.to_sym, g)
58
- else
59
- send(ope_to_str.key(operator.to_sym), g)
60
- end
61
- end
62
- end
63
- end
64
- Symbol.class_eval{ include Operator::General }
65
- class Num; include Operator::Num; end
66
- class E; include Operator::General; end
67
- class Pi; include Operator::General; end
68
- class Log; include Operator::General; end
69
- class Log2; include Operator::General; end
70
- class Log10; include Operator::General; end
71
- class Sin; include Operator::General; end
72
- class Cos; include Operator::General; end
73
- class Tan; include Operator::General; end
74
- end
75
9
  class Formula; include Operator::Formula; end
76
10
  class Inverse; include Operator::Inverse; end
77
11
 
@@ -40,6 +40,14 @@ module Dydx
40
40
  end
41
41
  end
42
42
 
43
+ def subst(hash = {})
44
+ f.subst(hash).send(operator, g.subst(hash))
45
+ end
46
+
47
+ def to_f
48
+ f.to_f.send(operator, g.to_f)
49
+ end
50
+
43
51
  def include?(x)
44
52
  f == x || g == x
45
53
  end
@@ -49,9 +57,14 @@ module Dydx
49
57
  (f.combinable?(x, operator) || g.combinable?(x, operator))
50
58
  end
51
59
 
52
- # TODO: interchangeable
53
60
  def ==(x)
54
- to_s == x.to_s
61
+ if to_s == x.to_s
62
+ true
63
+ else
64
+ result = commutate!.to_s == x.to_s
65
+ commutate!
66
+ result
67
+ end
55
68
  end
56
69
 
57
70
  def common_factors(formula)
@@ -15,6 +15,20 @@ module Dydx
15
15
  end
16
16
  end
17
17
 
18
+ def subst(hash = {})
19
+ case operator
20
+ when :+ then x.subst(hash) * -1
21
+ when :* then x.subst(hash) ** -1
22
+ end
23
+ end
24
+
25
+ def to_f
26
+ case operator
27
+ when :+ then x.to_f * -1
28
+ when :* then x.to_f ** -1
29
+ end
30
+ end
31
+
18
32
  def differentiate(sym=:x)
19
33
  case operator
20
34
  when :+
@@ -1,20 +1,253 @@
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/log2'
10
- require 'dydx/algebra/set/log10'
11
- require 'dydx/algebra/set/sin'
12
- require 'dydx/algebra/set/cos'
13
- require 'dydx/algebra/set/tan'
1
+ require 'dydx/algebra/operator/parts'
2
+ require 'dydx/algebra/operator/inverse'
3
+ require 'dydx/algebra/operator/formula'
4
+ require 'dydx/algebra/operator/num'
5
+ require 'dydx/algebra/operator/general'
14
6
 
15
7
  module Dydx
16
8
  module Algebra
17
9
  module Set
10
+ module Base
11
+ include Helper
12
+
13
+ # TODO: Pi should not have attr_accessor
14
+ def self.included(klass)
15
+ attr_accessor :n, :x
16
+ alias_method :d, :differentiate
17
+ end
18
+
19
+ def initialize(x=nil)
20
+ case self
21
+ when Num
22
+ @n = x
23
+ when Sin, Cos, Tan, Log, Log10, Log2
24
+ @x = x
25
+ end
26
+ end
27
+
28
+ def to_s
29
+ case self
30
+ when Num then n.to_s
31
+ when Pi then 'pi'
32
+ when E then 'e'
33
+ when Sin then "sin( #{x.to_s} )"
34
+ when Cos then "cos( #{x.to_s} )"
35
+ when Tan then "tan( #{x.to_s} )"
36
+ when Log then "log( #{x.to_s} )"
37
+ when Log10 then "log10( #{x.to_s} )"
38
+ when Log2 then "log2( #{x.to_s} )"
39
+ end
40
+ end
41
+
42
+ def to_f
43
+ case self
44
+ when Num then n.to_f
45
+ when Pi then Math::PI
46
+ when E then Math::E
47
+ when Symbol then raise ArgumentError
48
+ when Sin then Math.sin(x.to_f)
49
+ when Cos then Math.cos(x.to_f)
50
+ when Tan then Math.tan(x.to_f)
51
+ when Log then Math.log(x.to_f)
52
+ when Log10 then Math.log(x.to_f, 10)
53
+ when Log2 then Math.log(x.to_f, 2)
54
+ end
55
+ end
56
+
57
+ def subst(hash = {})
58
+ case self
59
+ when Num, Pi, E
60
+ self
61
+ when Symbol
62
+ hash[self] || self
63
+ when Sin then sin(x.subst(hash))
64
+ when Cos then cos(x.subst(hash))
65
+ when Tan then tan(x.subst(hash))
66
+ when Log then log(x.subst(hash))
67
+ when Log10 then log10(x.subst(hash))
68
+ when Log2 then log2(x.subst(hash))
69
+ end
70
+ end
71
+
72
+ def differentiate(sym = :x)
73
+ case self
74
+ when Num, Pi, E then e0
75
+ when Symbol then self == sym ? e1 : e0
76
+ when Sin then cos(x) * x.d(sym)
77
+ when Cos then -1 * sin(x) * x.d(sym)
78
+ when Tan then 1 / (cos(x) ** 2)
79
+ when Log then x.d(sym) / (x)
80
+ when Log10 then x.d(sym) / (x * log(10))
81
+ when Log2 then x.d(sym) / (x * log(2))
82
+ end
83
+ end
84
+ end
85
+
86
+ class Num
87
+ include Base
88
+ include Operator::Num
89
+ end
90
+
91
+ class Pi
92
+ include Base
93
+ include Operator::General
94
+ end
95
+
96
+ class E
97
+ include Base
98
+ include Operator::General
99
+ end
100
+
101
+ class Sin
102
+ include Base
103
+ include Operator::General
104
+ end
105
+
106
+ class Cos
107
+ include Base
108
+ include Operator::General
109
+ end
110
+
111
+ class Tan
112
+ include Base
113
+ include Operator::General
114
+ end
115
+
116
+ class Log
117
+ include Base
118
+ include Operator::General
119
+ end
120
+
121
+ class Log10
122
+ include Base
123
+ include Operator::General
124
+ end
125
+
126
+ class Log2
127
+ include Base
128
+ include Operator::General
129
+ end
130
+
131
+ Symbol.class_eval do
132
+ include Base
133
+ include Operator::General
134
+ end
135
+
136
+ # FIX: Numeric
137
+ Fixnum.class_eval do
138
+ include Helper
139
+
140
+ def subst(hash = {})
141
+ self
142
+ end
143
+
144
+ def differentiate(sym=:x)
145
+ e0
146
+ end
147
+ alias_method :d, :differentiate
148
+
149
+ alias_method :addition, :+
150
+ alias_method :subtraction, :-
151
+ alias_method :multiplication, :*
152
+ alias_method :division, :/
153
+ alias_method :exponentiation, :**
154
+ ope_to_str = {
155
+ addition: :+,
156
+ subtraction: :-,
157
+ multiplication: :*,
158
+ division: :/,
159
+ exponentiation: :^
160
+ }
161
+ %w(+ - * / ^).each do |operator|
162
+ define_method(operator) do |g|
163
+ if g.is_a?(Symbol) ||
164
+ g.is_a?(Formula) ||
165
+ g.is_a?(Base)
166
+
167
+ Num.new(self).send(operator.to_sym, g)
168
+ else
169
+ send(ope_to_str.key(operator.to_sym), g)
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ Float.class_eval do
176
+ include Helper
177
+
178
+ def subst(hash = {})
179
+ self
180
+ end
181
+
182
+ def differentiate(sym=:x)
183
+ e0
184
+ end
185
+ alias_method :d, :differentiate
186
+
187
+ alias_method :addition, :+
188
+ alias_method :subtraction, :-
189
+ alias_method :multiplication, :*
190
+ alias_method :division, :/
191
+ alias_method :exponentiation, :**
192
+ ope_to_str = {
193
+ addition: :+,
194
+ subtraction: :-,
195
+ multiplication: :*,
196
+ division: :/,
197
+ exponentiation: :^
198
+ }
199
+ %w(+ - * / ^).each do |operator|
200
+ define_method(operator) do |g|
201
+ if g.is_a?(Symbol) ||
202
+ g.is_a?(Formula) ||
203
+ g.is_a?(Base)
204
+
205
+ Num.new(self).send(operator.to_sym, g)
206
+ else
207
+ send(ope_to_str.key(operator.to_sym), g)
208
+ end
209
+ end
210
+ end
211
+ end
212
+
213
+ Rational.class_eval do
214
+ include Helper
215
+
216
+ def subst(hash = {})
217
+ self
218
+ end
219
+
220
+ def differentiate(sym=:x)
221
+ e0
222
+ end
223
+ alias_method :d, :differentiate
224
+
225
+ alias_method :addition, :+
226
+ alias_method :subtraction, :-
227
+ alias_method :multiplication, :*
228
+ alias_method :division, :/
229
+ alias_method :exponentiation, :**
230
+ ope_to_str = {
231
+ addition: :+,
232
+ subtraction: :-,
233
+ multiplication: :*,
234
+ division: :/,
235
+ exponentiation: :^
236
+ }
237
+ %w(+ - * / ^).each do |operator|
238
+ define_method(operator) do |g|
239
+ if g.is_a?(Symbol) ||
240
+ g.is_a?(Formula) ||
241
+ g.is_a?(Base)
242
+
243
+ Num.new(self).send(operator.to_sym, g)
244
+ else
245
+ send(ope_to_str.key(operator.to_sym), g)
246
+ end
247
+ end
248
+ end
249
+ end
250
+
18
251
  def e0
19
252
  eval("$e0 ||= Num.new(0)")
20
253
  end
@@ -22,12 +255,9 @@ module Dydx
22
255
  def e1
23
256
  eval("$e1 ||= Num.new(1)")
24
257
  end
258
+
25
259
  def _(num)
26
- if num >= 0
27
- eval("$p#{num} ||= Num.new(num)")
28
- else
29
- eval("$n#{-1 * num} ||= Num.new(num)")
30
- end
260
+ Num.new(num)
31
261
  end
32
262
 
33
263
  def pi
@@ -117,7 +347,11 @@ module Dydx
117
347
  end
118
348
 
119
349
  def tan(x)
120
- Tan.new(x)
350
+ if x == 0
351
+ 0
352
+ else
353
+ Tan.new(x)
354
+ end
121
355
  end
122
356
  end
123
357
  end