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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -3
  3. data/README.md +124 -61
  4. data/Rakefile +8 -5
  5. data/dydx.gemspec +13 -13
  6. data/lib/dydx.rb +25 -23
  7. data/lib/dydx/algebra.rb +76 -8
  8. data/lib/dydx/algebra/formula.rb +38 -71
  9. data/lib/dydx/algebra/inverse.rb +12 -19
  10. data/lib/dydx/algebra/operator/common_parts.rb +3 -0
  11. data/lib/dydx/algebra/operator/formula.rb +4 -0
  12. data/lib/dydx/algebra/operator/general.rb +4 -0
  13. data/lib/dydx/algebra/operator/inverse.rb +4 -0
  14. data/lib/dydx/algebra/operator/num.rb +4 -0
  15. data/lib/dydx/algebra/operator/parts/base.rb +2 -2
  16. data/lib/dydx/algebra/operator/parts/formula.rb +38 -63
  17. data/lib/dydx/algebra/operator/parts/general.rb +31 -84
  18. data/lib/dydx/algebra/operator/parts/interface.rb +22 -0
  19. data/lib/dydx/algebra/operator/parts/inverse.rb +4 -4
  20. data/lib/dydx/algebra/operator/parts/num.rb +11 -16
  21. data/lib/dydx/algebra/operator/parts/symbol.rb +2 -2
  22. data/lib/dydx/algebra/operator/symbol.rb +15 -0
  23. data/lib/dydx/algebra/set.rb +34 -271
  24. data/lib/dydx/algebra/set/base.rb +9 -0
  25. data/lib/dydx/algebra/set/cos.rb +22 -0
  26. data/lib/dydx/algebra/set/e.rb +16 -0
  27. data/lib/dydx/algebra/set/fixnum.rb +14 -0
  28. data/lib/dydx/algebra/set/float.rb +14 -0
  29. data/lib/dydx/algebra/set/log.rb +22 -0
  30. data/lib/dydx/algebra/set/num.rb +26 -0
  31. data/lib/dydx/algebra/set/pi.rb +16 -0
  32. data/lib/dydx/algebra/set/sin.rb +21 -0
  33. data/lib/dydx/algebra/set/symbol.rb +14 -0
  34. data/lib/dydx/algebra/set/tan.rb +17 -0
  35. data/lib/dydx/delta.rb +1 -1
  36. data/lib/dydx/function.rb +1 -1
  37. data/lib/dydx/helper.rb +61 -55
  38. data/lib/dydx/integrand.rb +10 -22
  39. data/lib/dydx/version.rb +1 -1
  40. data/spec/dydx_spec.rb +29 -10
  41. data/spec/lib/algebra/formula_spec.rb +38 -44
  42. data/spec/lib/algebra/operator/parts/base_spec.rb +5 -5
  43. data/spec/lib/algebra/operator/parts/formula_spec.rb +50 -57
  44. data/spec/lib/algebra/operator/parts/inverse_spec.rb +8 -8
  45. data/spec/lib/algebra/set/cos_spec.rb +18 -0
  46. data/spec/lib/algebra/set/e_spec.rb +27 -0
  47. data/spec/lib/algebra/set/fixnum_spec.rb +65 -0
  48. data/spec/lib/algebra/set/float_spec.rb +65 -0
  49. data/spec/lib/algebra/set/log_spec.rb +15 -0
  50. data/spec/lib/algebra/set/num_spec.rb +23 -0
  51. data/spec/lib/algebra/set/pi_spec.rb +25 -0
  52. data/spec/lib/algebra/set/sin_spec.rb +14 -0
  53. data/spec/lib/algebra/set/symbol_spec.rb +22 -0
  54. data/spec/lib/algebra/set/tan_spec.rb +13 -0
  55. data/spec/lib/delta_spec.rb +8 -32
  56. data/spec/lib/function_spec.rb +34 -60
  57. data/spec/lib/helper_spec.rb +49 -47
  58. data/spec/lib/integrand_spec.rb +15 -13
  59. data/spec/spec_helper.rb +1 -2
  60. metadata +39 -9
  61. data/.pryrc +0 -2
  62. data/.rubocop.yml +0 -25
  63. data/spec/lib/algebra/set_spec.rb +0 -263
@@ -0,0 +1,9 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ class Base
5
+ include Helper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -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,16 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ class E < Base
5
+ def differentiate(sym=:x)
6
+ e0
7
+ end
8
+ alias_method :d, :differentiate
9
+
10
+ def to_s
11
+ 'e'
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ Fixnum.class_eval do
5
+ include Helper
6
+
7
+ def differentiate(sym=:x)
8
+ e0
9
+ end
10
+ alias_method :d, :differentiate
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ Float.class_eval do
5
+ include Helper
6
+
7
+ def differentiate(sym=:x)
8
+ e0
9
+ end
10
+ alias_method :d, :differentiate
11
+ end
12
+ end
13
+ end
14
+ 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
@@ -0,0 +1,16 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ class Pi < Base
5
+ def differentiate(sym=:x)
6
+ _(0)
7
+ end
8
+ alias_method :d, :differentiate
9
+
10
+ def to_s
11
+ 'pi'
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ class Sin < Base
5
+ attr_accessor :x
6
+
7
+ def initialize(x)
8
+ @x = x
9
+ end
10
+
11
+ def to_s
12
+ "sin( #{x.to_s} )"
13
+ end
14
+
15
+ def d(sym=:x)
16
+ cos(x) * x.d(sym)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ Symbol.class_eval do
5
+ include Helper
6
+
7
+ def differentiate(sym=:x)
8
+ self == sym ? e1 : e0
9
+ end
10
+ alias_method :d, :differentiate
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module Dydx
2
+ module Algebra
3
+ module Set
4
+ class Tan < Base
5
+ attr_accessor :x
6
+
7
+ def initialize(x)
8
+ @x = x
9
+ end
10
+
11
+ def to_s
12
+ "tan( #{x.to_s} )"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/dydx/delta.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Dydx
2
2
  class Delta
3
3
  attr_accessor :var, :function
4
- def initialize(var = nil, function = nil)
4
+ def initialize(var=nil, function=nil)
5
5
  @var = var
6
6
  @function = function
7
7
  end
data/lib/dydx/function.rb CHANGED
@@ -10,7 +10,7 @@ module Dydx
10
10
  self
11
11
  end
12
12
 
13
- def differentiate(sym = :x)
13
+ def differentiate(sym=:x)
14
14
  @algebra.differentiate(sym)
15
15
  end
16
16
  alias_method :d, :differentiate
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 num?
20
- is_a?(Num) || is_a?(Numeric)
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 to_numeric
24
- is_a?(Num) ? n : self
33
+ def inverse_ope(operator)
34
+ INVERSE_OPE_RELATION[operator]
25
35
  end
26
36
 
27
- def zero?
28
- [0, 0.0].include?(self) || (is_a?(Num) && n.zero?)
37
+ def inverse_super_ope(operator)
38
+ inverse_ope(super_ope(operator))
29
39
  end
30
40
 
31
- def one?
32
- [1, 1.0].include?(self) || (is_a?(Num) && n.one?)
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 minus1?
36
- [-1, -1.0].include?(self) || (is_a?(Num) && n.minus1?)
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.super, ope1.inv_super].include?(ope2)
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
- (num? && x.num?) ||
48
- (formula?(:*) && (f.num? || g.num?)) && x.num? ||
49
- like_term?(x) ||
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
- (num? && x.num?) ||
71
+ (is_num? && x.is_num?) ||
54
72
  inverse?(:*, x)
55
- when :**
56
- (num? && x.num?) || zero? || one?
73
+ when :^
74
+ (is_num? && x.is_num?) || is_0? || is_1?
57
75
  end
58
76
  end
59
77
 
60
- # TODO: Cyclomatic complexity for combinable? is too high. [9/6]
61
- def like_term?(x)
62
- self == x ||
63
- formula?(:*) && include?(x) ||
64
- x.formula?(:*) && x.include?(self)||
65
- (formula?(:*) && formula?(:*) && !([f, g] & [x.f, x.g]).empty?)
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
- # TODO: Cyclomatic complexity for combinable? is too high. [7/6]
69
- def multiple_of?(x)
70
- zero? ||
71
- self == x ||
72
- (num? && x.num? && self % x == 0) ||
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 distributable?(_operator)
103
+ def commutative?
81
104
  end
82
105
 
83
- def inverse?(operator, x = nil)
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)
@@ -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 = 1000)
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
- x = ->(i) { a + h * i }
23
-
24
- odd_sum = (1..n - 1).to_a.select(&:odd?).inject(0) { |sum, i| sum += f(x.(i)) }
25
- even_sum = (1..n - 1).to_a.select(&:even?).inject(0) { |sum, i| sum += f(x.(i)) }
26
- round_8( (h / 3) * (f(a) + f(b) + 2 * even_sum + 4 * odd_sum) )
27
- end
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