dydx 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07dd79c6718aec02bcbe4274cedadc700d694743
4
- data.tar.gz: f94746b938d94132d40a24cbb65087f2f2a28ff9
3
+ metadata.gz: 50b217dcece3200df260f85267cc8eb9962327d1
4
+ data.tar.gz: c1f0fad073894322d1fe70198ad12ec81eeb7fc3
5
5
  SHA512:
6
- metadata.gz: ea77ed0736af837a9294b2baa9f6d63d50aa8d2dcd146a17e55fa9ad3fcf49574ba30c10ffb2998618c88a1cf6e0b7e652b8b5d406d514477b971aa7059664d7
7
- data.tar.gz: 92c7a5a3fd45764282792b665591f5016a796a2466d0ea5388136f3d80259877a62ff7ec497b5f1eeaaa40605da443b3a3eb056421770784e7a22df3f287e248
6
+ metadata.gz: 20a5be7e5853dbb70910c8595a509a903882a58d7393bffe44d809e37d38910aefbe17024bd82c27e6ed1084895cdd1d74dc0765a033d852f3d4187d1b8e12a1
7
+ data.tar.gz: bcb0c305f6d9befd7de5b1c415bf522c012adfb910fe4707efdaeaf745d3cdb1391c64f6ea3e94f7e39d29c8e5369a12626c0b4eb57f48bdad18bf4f1372ed07
data/lib/dydx/algebra.rb CHANGED
@@ -48,6 +48,8 @@ module Dydx
48
48
  e0
49
49
  elsif operator == :* && x.is_1?
50
50
  e1
51
+ elsif x.is_a?(Inverse) && x.operator == operator
52
+ x.x
51
53
  else
52
54
  Inverse.new(x, operator)
53
55
  end
@@ -51,9 +51,28 @@ module Dydx
51
51
  x.is_num? && (f.is_num? || g.is_num?)
52
52
  end
53
53
 
54
+ # TODO: interchangeable
54
55
  def ==(x)
55
56
  to_s == x.to_s
56
57
  end
58
+
59
+ def common_factors(formula)
60
+ nil unless formula.is_a?(Formula)
61
+ if f == formula.f
62
+ [:f, :f]
63
+ elsif g == formula.g
64
+ [:g, :g]
65
+ elsif f == formula.g
66
+ [:f, :g]
67
+ elsif g == formula.f
68
+ [:g, :f]
69
+ end
70
+ end
71
+
72
+ def commutate!
73
+ @f, @g = @g, @f
74
+ self
75
+ end
57
76
  end
58
77
  end
59
78
  end
@@ -5,36 +5,9 @@ module Dydx
5
5
  module Base
6
6
  %w(+ * ^).map(&:to_sym).each do |operator|
7
7
  define_method(operator) do |x|
8
- if self == x && operator != :^
9
- case operator
10
- when :+
11
- _(2) * self
12
- when :*
13
- self ^ _(2)
14
- end
15
- elsif %w(+ *).map(&:to_sym).include?(operator) && x.send("#{to_str(operator)}?")
16
- if combinable?(x.f, operator)
17
- send(operator, x.f).send(operator, x.g)
18
- elsif combinable?(x.g, operator)
19
- send(operator, x.g).send(operator, x.f)
20
- else
21
- ::Algebra::Formula.new(self, x, operator.to_sym)
22
- end
23
- elsif x.subtrahend? && %w(* ^).map(&:to_sym).include?(operator)
24
- inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
25
- else
26
- ::Algebra::Formula.new(self, x, operator.to_sym)
27
- end
8
+ ::Algebra::Formula.new(self, x, operator.to_sym)
28
9
  end
29
10
  end
30
-
31
- def to_str(operator)
32
- {
33
- addition: :+,
34
- multiplication: :*,
35
- exponentiation: :^
36
- }.key(operator)
37
- end
38
11
  end
39
12
  end
40
13
  end
@@ -13,66 +13,34 @@ module Dydx
13
13
  else
14
14
  super(x)
15
15
  end
16
- elsif operator == :+
17
- if multiplication? && x.multiplication?
18
- if f == x.f
19
- f * g.send(operator, x.g)
20
- elsif f == x.g
21
- f * g.send(operator, x.f)
22
- elsif g == x.f
23
- f.send(operator, x.g) * g
24
- elsif g == x.g
25
- f.send(operator, x.f) * g
26
- else
27
- super(x)
16
+ elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str(super_ope(operator))}?")
17
+ return super(x) if !common_factors(x) || (operator == :* && common_factors(x)[0] != common_factors(x)[1])
18
+ w1, w2 = common_factors(x)
19
+ case operator
20
+ when :+
21
+ send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2))))
22
+ when :*
23
+ if w1 == :f
24
+ send(w1).send(super_ope(operator), send(rest(w1)).send(sub_ope(operator), x.send(rest(w2))))
25
+ elsif w1 == :g
26
+ send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))).commutate!
28
27
  end
29
- # expect(((:b * :a) - (:c * :a)).to_s).to eq('( ( b - c ) * a )')
30
- elsif multiplication? && x.subtrahend? && x.x.multiplication?
31
- if f == x.x.f
32
- f * g.send(operator, inverse(x.x.g, :+))
33
- elsif f == x.x.g
34
- f * g.send(operator, inverse(x.x.f, :+))
35
- elsif g == x.x.f
36
- f.send(operator, inverse(x.x.g, :+)) * g
37
- elsif g == x.x.g
38
- f.send(operator, inverse(x.x.f, :+)) * g
39
- else
40
- super(x)
41
- end
42
- else
43
- super(x)
44
28
  end
45
- elsif operator == :*
46
- if exponentiation? && x.exponentiation?
47
- if f == x.f
48
- f ^ g.send(:+, x.g)
49
- elsif g == x.g
50
- f.send(operator, x.f) ^ g
51
- else
52
- super(x)
53
- end
54
- elsif exponentiation? && x.divisor? && x.x.exponentiation?
55
- if f == x.x.f
56
- f ^ g.send(:-, x.x.g)
57
- elsif g == x.x.g
58
- f.send(:/, x.x.f) ^ g
59
- else
60
- super(x)
29
+ elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str_inv(operator)}?") && x.x.send("#{to_str(super_ope(operator))}?")
30
+ return super(x) if !common_factors(x.x) || (operator == :* && common_factors(x.x)[0] != common_factors(x.x)[1])
31
+ w1, w2 = common_factors(x.x)
32
+ case operator
33
+ when :+
34
+ send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2))))
35
+ when :*
36
+ if w1 == :f
37
+ send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(sub_ope(operator)), x.x.send(rest(w2))))
38
+ elsif w1 == :g
39
+ send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))).commutate!
61
40
  end
62
- # x * inverse(:y, :+)
63
- elsif x.subtrahend?
64
- inverse(self * x.x, :+)
65
- elsif multiplication?
66
- if f.combinable?(x, operator)
67
- f.send(operator, x).send(operator, g)
68
- elsif g.combinable?(x, operator)
69
- g.send(operator, x).send(operator, f)
70
- else
71
- super(x)
72
- end
73
- else
74
- super(x)
75
41
  end
42
+ else
43
+ super(x)
76
44
  end
77
45
  end
78
46
  end
@@ -92,6 +60,17 @@ module Dydx
92
60
  exponentiation: :^
93
61
  }.key(operator)
94
62
  end
63
+
64
+ def to_str_inv(operator)
65
+ {
66
+ subtrahend: :+,
67
+ divisor: :*
68
+ }.key(operator)
69
+ end
70
+
71
+ def rest(f_or_g)
72
+ ([:f, :g] - [f_or_g]).first
73
+ end
95
74
  end
96
75
  end
97
76
  end
@@ -3,36 +3,60 @@ module Dydx
3
3
  module Operator
4
4
  module Parts
5
5
  module General
6
- def +(x)
7
- if x.is_0?
8
- self
9
- elsif inverse?(x, :+)
10
- e0
11
- else
12
- super(x)
6
+ %w(+ * ^).map(&:to_sym).each do |operator|
7
+ define_method(operator) do |x|
8
+ if x.is_0?
9
+ case operator
10
+ when :+ then self
11
+ when :* then x
12
+ when :^ then e1
13
+ end
14
+ elsif x.is_1?
15
+ case operator
16
+ when :+ then super(x)
17
+ when :* then self
18
+ when :^ then self
19
+ end
20
+ elsif self == x
21
+ case operator
22
+ when :+ then _(2) * self
23
+ when :* then self ^ _(2)
24
+ when :^ then super(x)
25
+ end
26
+ elsif operator == :+ && inverse?(x, :+)
27
+ e0
28
+ elsif operator == :* && inverse?(x, :*)
29
+ e1
30
+ elsif [:+, :*].include?(operator) && x.send("#{to_str(operator)}?")
31
+ if combinable?(x.f, operator)
32
+ send(operator, x.f).send(operator, x.g)
33
+ elsif combinable?(x.g, operator)
34
+ send(operator, x.g).send(operator, x.f)
35
+ else
36
+ super(x)
37
+ end
38
+ elsif x.is_a?(Inverse) && x.operator == operator && x.x.send("#{to_str(operator)}?")
39
+ if combinable?(x.x.f, operator)
40
+ send(operator, inverse(x.x.f, operator)).send(operator, inverse(x.x.g, operator))
41
+ elsif combinable?(x.x.g, operator)
42
+ send(operator, inverse(x.x.g, operator)).send(operator, inverse(x.x.f, operator))
43
+ else
44
+ super(x)
45
+ end
46
+ elsif [:*].include?(operator) && x.subtrahend?
47
+ inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
48
+ else
49
+ super(x)
50
+ end
13
51
  end
14
52
  end
15
53
 
16
- def *(x)
17
- if x.is_0?
18
- x
19
- elsif x.is_1?
20
- self
21
- elsif inverse?(x, :*)
22
- e1
23
- else
24
- super(x)
25
- end
26
- end
27
-
28
- def ^(x)
29
- if x.is_0?
30
- _(1)
31
- elsif x.is_1?
32
- self
33
- else
34
- super(x)
35
- end
54
+ def to_str(operator)
55
+ {
56
+ addition: :+,
57
+ multiplication: :*,
58
+ exponentiation: :^
59
+ }.key(operator)
36
60
  end
37
61
  end
38
62
  end
data/lib/dydx/helper.rb CHANGED
@@ -84,6 +84,26 @@ module Dydx
84
84
  end
85
85
  end
86
86
 
87
+ def sub_ope(operator)
88
+ case operator
89
+ when :*
90
+ :+
91
+ end
92
+ end
93
+
94
+ def inverse_ope(operator)
95
+ case operator
96
+ when :+
97
+ :-
98
+ when :*
99
+ :/
100
+ end
101
+ end
102
+
103
+ def commutative?(operator)
104
+ [:+, :*].include?(operator)
105
+ end
106
+
87
107
  def inverse?(x, operator)
88
108
  if is_a?(Algebra::Inverse)
89
109
  self.operator == operator && self.x == x
data/lib/dydx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dydx
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -2,27 +2,54 @@ require 'spec_helper'
2
2
 
3
3
  describe Dydx::Algebra::Operator::Parts::Formula do
4
4
  it{ expect(((:a * :b) + (:a * :c)).to_s).to eq('( a * ( b + c ) )') }
5
- it{ expect(((:b * :a) + (:c * :a)).to_s).to eq('( ( b + c ) * a )') }
5
+ it{ expect(((:a * :b) + (:c * :a)).to_s).to eq('( a * ( b + c ) )') }
6
+ it{ expect(((:b * :a) + (:c * :a)).to_s).to eq('( a * ( b + c ) )') }
7
+ it{ expect(((:b * :a) + (:a * :c)).to_s).to eq('( a * ( b + c ) )') }
6
8
  it{ expect(((:a * :b) - (:a * :c)).to_s).to eq('( a * ( b - c ) )') }
7
- it{ expect(((:b * :a) - (:c * :a)).to_s).to eq('( ( b - c ) * a )') }
9
+ it{ expect(((:a * :b) - (:c * :a)).to_s).to eq('( a * ( b - c ) )') }
10
+ it{ expect(((:b * :a) - (:c * :a)).to_s).to eq('( a * ( b - c ) )') }
11
+ it{ expect(((:b * :a) - (:a * :c)).to_s).to eq('( a * ( b - c ) )') }
8
12
 
9
- it{ expect(((:x ^ 3) * (:x ^ 2)).to_s).to eq('( x ^ 5 )') }
10
- it{ expect(((:x ^ 3) / (:x ^ 2)).to_s).to eq('x') }
11
- it{ expect(((:x ^ :n) * (:y ^ :n)).to_s).to eq('( ( x * y ) ^ n )') }
12
- it{ expect(((:x ^ :n) / (:y ^ :n)).to_s).to eq('( ( x / y ) ^ n )') }
13
+ it{ expect(((:a ^ :b) * (:a ^ :c)).to_s).to eq('( a ^ ( b + c ) )') }
14
+ it{ expect(((:a ^ :b) * (:c ^ :a)).to_s).to eq('( ( a ^ b ) * ( c ^ a ) )') }
15
+ it{ expect(((:b ^ :a) * (:c ^ :a)).to_s).to eq('( ( b * c ) ^ a )') }
16
+ it{ expect(((:b ^ :a) * (:a ^ :c)).to_s).to eq('( ( b ^ a ) * ( a ^ c ) )') }
17
+ it{ expect(((:a ^ :b) / (:a ^ :c)).to_s).to eq('( a ^ ( b - c ) )') }
18
+ it{ expect(((:a ^ :b) / (:c ^ :a)).to_s).to eq('( ( a ^ b ) / ( c ^ a ) )') }
19
+ it{ expect(((:b ^ :a) / (:c ^ :a)).to_s).to eq('( ( b / c ) ^ a )') }
20
+ it{ expect(((:b ^ :a) / (:a ^ :c)).to_s).to eq('( ( b ^ a ) / ( a ^ c ) )') }
13
21
 
14
22
  it{ expect(((:x - 2) + 2).to_s).to eq('x') }
15
23
  it{ expect(((:x + 2) - 2).to_s).to eq('x') }
16
24
  it{ expect(((:x * 2) / 2).to_s).to eq('x') }
17
25
  it{ expect(((:x / 2) * 2).to_s).to eq('x') }
18
26
 
27
+ it{ expect((2 + (:x - 2)).to_s).to eq('x') }
28
+ it{ expect((2 - (:x + 2)).to_s).to eq('( - x )') }
29
+ it{ expect((2 * (:x / 2)).to_s).to eq('x') }
30
+ it{ expect((2 / (:x * 2)).to_s).to eq('( 1 / x )') }
31
+
19
32
  it{ expect(((:x + :y) + :y).to_s).to eq('( ( 2 * y ) + x )') }
33
+ it{ expect(((:y + :x) + :y).to_s).to eq('( ( 2 * y ) + x )') }
20
34
  it{ expect(((:x - :y) - :y).to_s).to eq('( x - ( 2 * y ) )') }
21
35
  it{ expect(((:y - :x) - :y).to_s).to eq('( - x )') }
36
+ it{ expect(((:y * :x) * :y).to_s).to eq('( ( y ^ 2 ) * x )') }
22
37
  it{ expect(((:x * :y) * :y).to_s).to eq('( ( y ^ 2 ) * x )') }
23
38
  it{ expect(((:x / :y) / :y).to_s).to eq('( x / ( y ^ 2 ) )') }
24
39
  it{ expect(((:y / :x) / :y).to_s).to eq('( 1 / x )') }
25
40
 
41
+ it{ expect((:y + (:x + :y)).to_s).to eq('( ( 2 * y ) + x )') }
42
+ it{ expect((:y + (:y + :x)).to_s).to eq('( ( 2 * y ) + x )') }
43
+ it{ expect((:y - (:x - :y)).to_s).to eq('( ( 2 * y ) - x )') }
44
+ it{ expect((:y - (:y - :x)).to_s).to eq('x') }
45
+ it{ expect((:y * (:y * :x)).to_s).to eq('( ( y ^ 2 ) * x )') }
46
+ it{ expect((:y * (:x * :y)).to_s).to eq('( ( y ^ 2 ) * x )') }
47
+ it{ expect((:y - (:x - :y)).to_s).to eq('( ( 2 * y ) - x )') }
48
+ it{ expect((:y - (:y - :x)).to_s).to eq('x') }
49
+
26
50
  it{ expect(((:x * 2) ^ 2).to_s).to eq('( ( x ^ 2 ) * 4 )') }
27
51
  it{ expect(((:x / 2) ^ 2).to_s).to eq('( ( x ^ 2 ) / 4 )') }
52
+
53
+ it{ expect((2 ^ (:x * 2)).to_s).to eq('( ( x ^ 2 ) * 4 )') }
54
+ it{ expect((2 ^ (:x / 2)).to_s).to eq('( ( x ^ 2 ) / 4 )') }
28
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dydx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka