dydx 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,29 +3,52 @@ It always happens you want to differentiate with ruby. right?
3
3
 
4
4
  Dydx will eliminate this. Like this
5
5
 
6
+
6
7
  ```
7
- ( d/dx(e ^ :x) ).to_s
8
+ ( d/dx(:x ^ 2) ).to_s
9
+ => "( 2 * x )"
10
+
11
+ # pretermit '#to_s'
12
+
13
+ d/dx(e ^ :x)
8
14
  => "( e ^ x )"
9
15
 
16
+ d/dz(log(:z))
17
+ => "( 1 / z )"
18
+
19
+ d/dx(:x^:n)
20
+ => "( n * ( x ^ ( n - 1 ) ) )"
21
+
22
+ ```
23
+
24
+ you can do like ``` dy/dx ```, if you use global var.
25
+
26
+ ```
10
27
  $y = cos(:x)
11
- (dy/dx).to_s
28
+ dy/dx
12
29
  => "( - sin( x ) )"
13
30
 
14
- # pretermit '.to_s'
31
+ $x = :a * ( (:t ^ 2) / 2 )
32
+ dx/dt
33
+ => "( a * t )"
15
34
 
16
- d/dx(log(:x))
17
- => "( 1 / x )"
35
+ d/dt(dx/dt)
36
+ =>"a"
18
37
 
19
- d/dx(:x^:n)
20
- => "( n * ( x ^ ( n - 1 ) ) )"
38
+ ```
21
39
 
40
+ you can use method chaining.
22
41
 
23
- d/dx(:x^2)
24
- => "( 2 * x )"
42
+ ```
43
+ ((:x ^ 2) * :y).d(:x)
44
+ => "( ( 2 * x ) * y )"
25
45
 
46
+ ((:x ^ 2) * :y).d(:x).d(:y)
47
+ => "( 2 * x )"
26
48
  ```
27
49
 
28
- (That's wonderful!!!!! But, I feel there is no meaning ... )
50
+
51
+ (That's wonderful!!!!! ..............)
29
52
 
30
53
  ## Installation
31
54
 
@@ -30,13 +30,13 @@ module Dydx
30
30
  def to_s
31
31
  if (multiplication? && (f.is_minus1? || g.is_minus1?) )
32
32
  "( - #{g.to_s} )"
33
- elsif multiplication? && g.divisor?
33
+ elsif multiplication? && g.inverse?(:*)
34
34
  "( #{f.to_s} / #{g.x.to_s} )"
35
- elsif multiplication? && f.divisor?
35
+ elsif multiplication? && f.inverse?(:*)
36
36
  "( #{g.to_s} / #{f.x.to_s} )"
37
- elsif addition? && g.subtrahend?
37
+ elsif addition? && g.inverse?(:+)
38
38
  "( #{f.to_s} - #{g.x.to_s} )"
39
- elsif addition? && f.subtrahend?
39
+ elsif addition? && f.inverse?(:+)
40
40
  "( #{g.to_s} - #{f.x.to_s} )"
41
41
  else
42
42
  "( #{f.to_s} #{@operator} #{g.to_s} )"
@@ -47,8 +47,9 @@ module Dydx
47
47
  f == x || g == x
48
48
  end
49
49
 
50
- def openable?(x)
51
- x.is_num? && (f.is_num? || g.is_num?)
50
+ def openable?(operator, x)
51
+ distributive?(self.operator, operator) &&
52
+ (f.combinable?(x, operator) || g.combinable?(x, operator))
52
53
  end
53
54
 
54
55
  # TODO: interchangeable
@@ -75,4 +76,4 @@ module Dydx
75
76
  end
76
77
  end
77
78
  end
78
- end
79
+ end
@@ -13,7 +13,7 @@ module Dydx
13
13
  else
14
14
  super(x)
15
15
  end
16
- elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str(super_ope(operator))}?")
16
+ elsif formula?(super_ope(operator)) && x.formula?(super_ope(operator))
17
17
  return super(x) if !common_factors(x) || (operator == :* && common_factors(x)[0] != common_factors(x)[1])
18
18
  w1, w2 = common_factors(x)
19
19
  case operator
@@ -26,7 +26,7 @@ module Dydx
26
26
  send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))).commutate!
27
27
  end
28
28
  end
29
- elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str_inv(operator)}?") && x.x.send("#{to_str(super_ope(operator))}?")
29
+ elsif formula?(super_ope(operator)) && x.inverse?(operator) && x.x.formula?(super_ope(operator))
30
30
  return super(x) if !common_factors(x.x) || (operator == :* && common_factors(x.x)[0] != common_factors(x.x)[1])
31
31
  w1, w2 = common_factors(x.x)
32
32
  case operator
@@ -46,31 +46,12 @@ module Dydx
46
46
  end
47
47
 
48
48
  def ^(x)
49
- if multiplication? && openable?(x)
49
+ if multiplication? && openable?(:^, x)
50
50
  (f ^ x).send(self.operator, (g ^ x))
51
51
  else
52
52
  super(x)
53
53
  end
54
54
  end
55
-
56
- def to_str(operator)
57
- {
58
- addition: :+,
59
- multiplication: :*,
60
- exponentiation: :^
61
- }.key(operator)
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
74
55
  end
75
56
  end
76
57
  end
@@ -23,9 +23,9 @@ module Dydx
23
23
  when :* then self ^ _(2)
24
24
  when :^ then super(x)
25
25
  end
26
- elsif operator == :+ && inverse?(x, :+)
26
+ elsif operator == :+ && inverse?(:+, x)
27
27
  e0
28
- elsif operator == :* && inverse?(x, :*)
28
+ elsif operator == :* && inverse?(:*, x)
29
29
  e1
30
30
  elsif [:+, :*].include?(operator) && x.send("#{to_str(operator)}?")
31
31
  if combinable?(x.f, operator)
@@ -43,21 +43,13 @@ module Dydx
43
43
  else
44
44
  super(x)
45
45
  end
46
- elsif [:*].include?(operator) && x.subtrahend?
46
+ elsif [:*].include?(operator) && x.inverse?(:+)
47
47
  inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
48
48
  else
49
49
  super(x)
50
50
  end
51
51
  end
52
52
  end
53
-
54
- def to_str(operator)
55
- {
56
- addition: :+,
57
- multiplication: :*,
58
- exponentiation: :^
59
- }.key(operator)
60
- end
61
53
  end
62
54
  end
63
55
  end
@@ -6,12 +6,10 @@ module Dydx
6
6
  %w(+ - * / ^).map(&:to_sym).each do |operator|
7
7
  define_method(operator) do |x|
8
8
  x = ::Set::Num.new(x) if x.is_a?(Fixnum)
9
- case operator
10
- when :-
11
- self + inverse(x, :+)
12
- when :/
13
- raise ZeroDivisionError if x.is_0?
14
- self * inverse(x, :*)
9
+ if operator == :/ && x.is_0?
10
+ raise ZeroDivisionError
11
+ elsif [:-, :/].include?(operator)
12
+ send(inverse_ope(operator), inverse(x, inverse_ope(operator)))
15
13
  else
16
14
  super(x)
17
15
  end
@@ -3,30 +3,20 @@ module Dydx
3
3
  module Operator
4
4
  module Parts
5
5
  module Inverse
6
- def +(x)
7
- if self.x == x && operator == :+
8
- e0
9
- elsif !x.is_a?(Inverse)
10
- x + self
11
- else
12
- super(x)
13
- end
14
- end
15
-
16
- def *(x)
17
- if self.x == x && operator == :*
18
- e1
19
- else
20
- super(x)
21
- end
22
- end
23
-
24
- def ^(x)
25
- case operator
26
- when :+
27
- super(x)
28
- when :*
29
- inverse(self.x ^ x, :*)
6
+ %w(+ * ^).map(&:to_sym).each do |operator|
7
+ define_method(operator) do |x|
8
+ if inverse?(operator, x)
9
+ case operator
10
+ when :+ then e0
11
+ when :* then e1
12
+ end
13
+ elsif !x.is_a?(Inverse) && operator == :+
14
+ x + self
15
+ elsif self.operator == :* && operator == :^
16
+ inverse(self.x ^ x, :*)
17
+ else
18
+ super(x)
19
+ end
30
20
  end
31
21
  end
32
22
  end
@@ -3,39 +3,29 @@ module Dydx
3
3
  module Operator
4
4
  module Parts
5
5
  module Num
6
- def +(x)
7
- if n == 0
8
- x
9
- elsif x.is_a?(Num)
10
- _(n + x.n)
11
- elsif x.subtrahend? && x.x.is_a?(Num)
12
- _(n - x.x.n)
13
- else
14
- super(x)
15
- end
16
- end
17
-
18
- def *(x)
19
- if n == 0
20
- self
21
- elsif n == 1
22
- x
23
- elsif x.is_a?(Num)
24
- _(n * x.n)
25
- elsif x.divisor? && x.x.is_a?(Num)
26
- _(n / x.x.n)
27
- else
28
- super(x)
29
- end
30
- end
31
-
32
- def ^(x)
33
- if (n == 0) || (n == 1)
34
- self
35
- elsif x.is_a?(Num)
36
- _(n ^ x.n)
37
- else
38
- super(x)
6
+ %w(+ * ^).map(&:to_sym).each do |operator|
7
+ define_method(operator) do |x|
8
+ if is_0?
9
+ case operator
10
+ when :+ then x
11
+ when :* then e0
12
+ when :^ then e0
13
+ end
14
+ elsif is_1?
15
+ case operator
16
+ when :+ then super(x)
17
+ when :* then x
18
+ when :^ then e1
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
+ else
27
+ super(x)
28
+ end
39
29
  end
40
30
  end
41
31
  end
data/lib/dydx/helper.rb CHANGED
@@ -2,11 +2,46 @@ module Dydx
2
2
  module Helper
3
3
  OP_SYM_STR = {
4
4
  addition: :+,
5
- subtraction: :-,
6
5
  multiplication: :*,
7
6
  exponentiation: :^
8
7
  }
9
8
 
9
+ SUPER_OPE_RELATION = {
10
+ :+ => :*,
11
+ :- => :/,
12
+ :* => :^,
13
+ :/ => :|
14
+ }
15
+
16
+ INVERSE_OPE_RELATION = {
17
+ :+ => :-,
18
+ :- => :+,
19
+ :* => :/,
20
+ :/ => :*,
21
+ :^ => :|,
22
+ :| => :^
23
+ }
24
+
25
+ def super_ope(operator)
26
+ SUPER_OPE_RELATION[operator]
27
+ end
28
+
29
+ def sub_ope(operator)
30
+ SUPER_OPE_RELATION.invert[operator]
31
+ end
32
+
33
+ def inverse_ope(operator)
34
+ INVERSE_OPE_RELATION[operator]
35
+ end
36
+
37
+ def inverse_super_ope(operator)
38
+ inverse_ope(super_ope(operator))
39
+ end
40
+
41
+ def is_num?
42
+ (is_a?(Num) || is_a?(Fixnum)) || (is_a?(Inverse) && x.is_num?)
43
+ end
44
+
10
45
  def is_0?
11
46
  self == 0 || (is_a?(Num) && n == 0)
12
47
  end
@@ -19,39 +54,35 @@ module Dydx
19
54
  self == -1 || (is_a?(Num) && n == -1)
20
55
  end
21
56
 
22
- def is_num?
23
- if is_a?(Inverse)
24
- x.is_num?
25
- else
26
- is_a?(Num) || is_a?(Fixnum)
27
- end
57
+ def distributive?(ope1, ope2)
58
+ [super_ope(ope1), inverse_super_ope(ope1)].include?(ope2)
28
59
  end
29
60
 
30
61
  def combinable?(x, operator)
31
62
  case operator
63
+ when :+
64
+ self == x ||
65
+ (is_num? && x.is_num?) ||
66
+ (multiplication? && (f == x || g == x)) ||
67
+ (x.multiplication? && (x.f == self || x.g == self)) ||
68
+ inverse?(:+, x)
32
69
  when :*
33
70
  self == x ||
34
71
  (is_num? && x.is_num?) ||
35
- inverse?(x, :*)
36
- when :+
37
- like_term?(x)
72
+ inverse?(:*, x)
73
+ when :^
74
+ (is_num? && x.is_num?) || is_0? || is_1?
38
75
  end
39
76
  end
40
77
 
41
- def like_term?(x)
42
- self == x ||
43
- (is_num? && x.is_num?) ||
44
- (multiplication? && (f == x || g == x)) ||
45
- inverse?(x, :+)
46
- end
47
-
48
78
  def is_multiple_of(x)
49
- is_multiple = if is_0?
50
- _(0)
79
+ if is_0?
80
+ e0
51
81
  elsif self == x
52
- _(1)
53
- elsif is_a?(Formula) &&
54
- (f == x || g == x)
82
+ e1
83
+ # elsif is_num? && x.is_num? && (self % x == 0)
84
+ # _(n / x.n)
85
+ elsif multiplication? && (f == x || g == x)
55
86
  f == x ? g : f
56
87
  else
57
88
  false
@@ -60,7 +91,7 @@ module Dydx
60
91
 
61
92
  OP_SYM_STR.each do |operator_name, operator|
62
93
  define_method("#{operator_name}?") do
63
- (@operator == operator) && is_a?(Formula)
94
+ is_a?(Formula) && (@operator == operator)
64
95
  # is_a?(Inverse) && self.operator == operator
65
96
  end
66
97
  end
@@ -73,40 +104,26 @@ module Dydx
73
104
  OP_SYM_STR[str]
74
105
  end
75
106
 
76
- def super_ope(operator)
77
- case operator
78
- when :+
79
- :*
80
- when :-
81
- :/
82
- when :*
83
- :^
84
- end
107
+ def to_str_inv(operator)
108
+ {
109
+ subtrahend: :+,
110
+ divisor: :*
111
+ }.key(operator)
85
112
  end
86
113
 
87
- def sub_ope(operator)
88
- case operator
89
- when :*
90
- :+
91
- end
114
+ def rest(f_or_g)
115
+ ([:f, :g] - [f_or_g]).first
92
116
  end
93
117
 
94
- def inverse_ope(operator)
95
- case operator
96
- when :+
97
- :-
98
- when :*
99
- :/
100
- end
118
+ def commutative?
101
119
  end
102
120
 
103
- def commutative?(operator)
104
- [:+, :*].include?(operator)
121
+ def distributable?(operator)
105
122
  end
106
123
 
107
- def inverse?(x, operator)
124
+ def inverse?(operator, x=nil)
108
125
  if is_a?(Algebra::Inverse)
109
- self.operator == operator && self.x == x
126
+ self.operator == operator && (self.x == x || x.nil?)
110
127
  elsif x.is_a?(Algebra::Inverse)
111
128
  self == x.x
112
129
  else
@@ -114,12 +131,8 @@ module Dydx
114
131
  end
115
132
  end
116
133
 
117
- def subtrahend?
118
- is_a?(Inverse) && operator == :+
119
- end
120
-
121
- def divisor?
122
- is_a?(Inverse) && operator == :*
134
+ def formula?(operator)
135
+ is_a?(Formula) && (@operator == operator)
123
136
  end
124
137
  end
125
138
  end
data/lib/dydx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dydx
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -56,7 +56,8 @@ describe Dydx::Algebra::Formula do
56
56
  end
57
57
 
58
58
  describe '#openable?' do
59
- it{ expect((1 + :x).openable?(_(1))).to be_true }
60
- it{ expect((1 + :x).openable?(:z)).to be_false }
59
+ it{ expect((:x + :y).openable?(:*, :x)).to be_true }
60
+ it{ expect((:x + :y).openable?(:*, :y)).to be_true }
61
+ it{ expect((:x + :y).openable?(:*, :z)).to be_false }
61
62
  end
62
63
  end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Helper do
4
+ include Helper
4
5
  context '#is_n?' do
5
6
  it{ expect(0.is_0?).to be_true }
6
7
  it{ expect(_(0).is_0?).to be_true }
@@ -26,11 +27,27 @@ describe Helper do
26
27
 
27
28
  context '#combinable?' do
28
29
  it{ expect(:x.combinable?(:x, :+)).to be_true }
30
+ it{ expect(:x.combinable?(2 * :x, :+)).to be_true }
31
+ it{ expect((2 * :x).combinable?(:x, :+)).to be_true }
32
+ it{ expect((2 * :x).combinable?(2 * :x, :+)).to be_true }
29
33
  it{ expect(:x.combinable?(:y, :+)).to be_false }
34
+ it{ expect(1.combinable?(2, :+)).to be_true }
30
35
  it{ expect(:x.combinable?(:x, :*)).to be_true }
31
36
  it{ expect(:x.combinable?(:y, :*)).to be_false }
32
- it{ expect(1.combinable?(2, :+)).to be_true }
33
37
  it{ expect(1.combinable?(2, :*)).to be_true }
38
+ it{ expect(0.combinable?(:x, :^)).to be_true }
39
+ it{ expect(1.combinable?(:y, :^)).to be_true }
40
+ end
41
+
42
+ context '#distributive?' do
43
+ it{ expect(distributive?(:+, :*)).to be_true }
44
+ it{ expect(distributive?(:+, :/)).to be_true }
45
+ it{ expect(distributive?(:-, :*)).to be_true }
46
+ it{ expect(distributive?(:-, :/)).to be_true }
47
+ it{ expect(distributive?(:*, :^)).to be_true }
48
+ it{ expect(distributive?(:/, :^)).to be_true }
49
+ it{ expect(distributive?(:*, :+)).to be_false }
50
+ it{ expect(distributive?(:^, :*)).to be_false }
34
51
  end
35
52
 
36
53
  let(:addition) { (:x + :y) }
@@ -43,8 +60,8 @@ describe Helper do
43
60
  it{ expect(multiplication.multiplication?).to be_true }
44
61
  it{ expect(exponentiation.exponentiation?).to be_true }
45
62
 
46
- it{ expect(inverse(:x, :+).inverse?(:x, :+)).to be_true }
47
- it{ expect(:x.inverse?(inverse(:x, :+), :+)).to be_true }
48
- it{ expect(inverse(:x, :*).inverse?(:x, :*)).to be_true }
49
- it{ expect(:x.inverse?(inverse(:x, :*), :*)).to be_true }
50
- end
63
+ it{ expect(inverse(:x, :+).inverse?(:+, :x)).to be_true }
64
+ it{ expect(:x.inverse?(:+, inverse(:x, :+))).to be_true }
65
+ it{ expect(inverse(:x, :*).inverse?(:*, :x)).to be_true }
66
+ it{ expect(:x.inverse?(:*, inverse(:x, :*))).to be_true }
67
+ 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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-19 00:00:00.000000000 Z
12
+ date: 2014-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -136,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  segments:
138
138
  - 0
139
- hash: -4588855111168317577
139
+ hash: -728077447189178322
140
140
  required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  none: false
142
142
  requirements:
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  segments:
147
147
  - 0
148
- hash: -4588855111168317577
148
+ hash: -728077447189178322
149
149
  requirements: []
150
150
  rubyforge_project:
151
151
  rubygems_version: 1.8.23