dydx 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.pryrc +2 -0
  3. data/.rubocop.yml +25 -0
  4. data/Gemfile +3 -0
  5. data/README.md +29 -64
  6. data/Rakefile +5 -8
  7. data/dydx.gemspec +13 -13
  8. data/lib/dydx.rb +20 -29
  9. data/lib/dydx/algebra.rb +8 -76
  10. data/lib/dydx/algebra/formula.rb +67 -29
  11. data/lib/dydx/algebra/inverse.rb +16 -2
  12. data/lib/dydx/algebra/operator/formula.rb +0 -4
  13. data/lib/dydx/algebra/operator/general.rb +0 -4
  14. data/lib/dydx/algebra/operator/inverse.rb +0 -4
  15. data/lib/dydx/algebra/operator/num.rb +0 -4
  16. data/lib/dydx/algebra/operator/parts/base.rb +2 -2
  17. data/lib/dydx/algebra/operator/parts/formula.rb +61 -40
  18. data/lib/dydx/algebra/operator/parts/general.rb +83 -30
  19. data/lib/dydx/algebra/operator/parts/inverse.rb +4 -4
  20. data/lib/dydx/algebra/operator/parts/num.rb +16 -11
  21. data/lib/dydx/algebra/operator/parts/symbol.rb +2 -2
  22. data/lib/dydx/algebra/set.rb +271 -34
  23. data/lib/dydx/delta.rb +1 -1
  24. data/lib/dydx/function.rb +1 -1
  25. data/lib/dydx/helper.rb +53 -67
  26. data/lib/dydx/integrand.rb +22 -10
  27. data/lib/dydx/version.rb +1 -1
  28. data/spec/dydx_spec.rb +10 -29
  29. data/spec/lib/algebra/formula_spec.rb +44 -38
  30. data/spec/lib/algebra/operator/parts/base_spec.rb +5 -5
  31. data/spec/lib/algebra/operator/parts/formula_spec.rb +57 -57
  32. data/spec/lib/algebra/operator/parts/inverse_spec.rb +8 -8
  33. data/spec/lib/algebra/set_spec.rb +263 -0
  34. data/spec/lib/delta_spec.rb +32 -8
  35. data/spec/lib/function_spec.rb +60 -34
  36. data/spec/lib/helper_spec.rb +44 -51
  37. data/spec/lib/integrand_spec.rb +13 -15
  38. data/spec/spec_helper.rb +2 -1
  39. metadata +20 -64
  40. data/lib/dydx/algebra/operator/common_parts.rb +0 -3
  41. data/lib/dydx/algebra/operator/parts/interface.rb +0 -22
  42. data/lib/dydx/algebra/operator/symbol.rb +0 -15
  43. data/lib/dydx/algebra/set/base.rb +0 -9
  44. data/lib/dydx/algebra/set/cos.rb +0 -22
  45. data/lib/dydx/algebra/set/e.rb +0 -16
  46. data/lib/dydx/algebra/set/fixnum.rb +0 -14
  47. data/lib/dydx/algebra/set/float.rb +0 -14
  48. data/lib/dydx/algebra/set/log.rb +0 -22
  49. data/lib/dydx/algebra/set/num.rb +0 -22
  50. data/lib/dydx/algebra/set/pi.rb +0 -16
  51. data/lib/dydx/algebra/set/sin.rb +0 -22
  52. data/lib/dydx/algebra/set/symbol.rb +0 -14
  53. data/lib/dydx/algebra/set/tan.rb +0 -17
  54. data/spec/lib/algebra/set/cos_spec.rb +0 -18
  55. data/spec/lib/algebra/set/e_spec.rb +0 -27
  56. data/spec/lib/algebra/set/fixnum_spec.rb +0 -65
  57. data/spec/lib/algebra/set/float_spec.rb +0 -65
  58. data/spec/lib/algebra/set/log_spec.rb +0 -15
  59. data/spec/lib/algebra/set/num_spec.rb +0 -23
  60. data/spec/lib/algebra/set/pi_spec.rb +0 -25
  61. data/spec/lib/algebra/set/sin_spec.rb +0 -14
  62. data/spec/lib/algebra/set/symbol_spec.rb +0 -22
  63. data/spec/lib/algebra/set/tan_spec.rb +0 -13
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Dydx::Algebra::Operator::Parts::Inverse do
4
- it{ expect(inverse(:x, :+).to_s).to eq('( - x )') }
5
- it{ expect(inverse(:x, :*).to_s).to eq('( 1 / x )') }
4
+ it { expect(inverse(x, :+).to_s).to eq('( - x )') }
5
+ it { expect(inverse(x, :*).to_s).to eq('( 1 / x )') }
6
6
 
7
- it{ expect(inverse(:x, :+)).to eq(inverse(:x, :+)) }
8
- it{ expect(inverse(:x, :*)).to eq(inverse(:x, :*)) }
7
+ it { expect(inverse(x, :+)).to eq(inverse(x, :+)) }
8
+ it { expect(inverse(x, :*)).to eq(inverse(x, :*)) }
9
9
 
10
- it{ expect(:x - :x).to eq(e0) }
11
- it{ expect(:x / :x).to eq(e1) }
12
- it{ expect(inverse(:x, :+) + :x).to eq(e0) }
13
- it{ expect(inverse(:x, :*) * :x).to eq(e1) }
10
+ it { expect(x - x).to eq(0) }
11
+ it { expect(x / x).to eq(1) }
12
+ it { expect(inverse(x, :+) + x).to eq(0) }
13
+ it { expect(inverse(x, :*) * x).to eq(1) }
14
14
  end
@@ -0,0 +1,263 @@
1
+ require 'spec_helper'
2
+ # HOTFIX: Refactor by using context or describe
3
+ describe Dydx::Algebra::Set do
4
+ it { expect(_(1)).to eq(_(1)) }
5
+ it { expect(_(-1)).to eq(_(-1)) }
6
+ it { expect(e0).to eq(e0) }
7
+ it { expect(e1).to eq(e1) }
8
+
9
+ it { expect(e).to eq(e) }
10
+ it { expect(pi).to eq(pi) }
11
+
12
+ it { expect(log10(1)).to eq(e0) }
13
+ it { expect(log10(10)).to eq(e1) }
14
+ it { expect(log10(10 ** n)).to eq(n) }
15
+ it { expect(log10(3 ** n)).to eq(n * log10( 3 )) }
16
+
17
+ it { expect(log2(1)).to eq(e0) }
18
+ it { expect(log2(2)).to eq(e1) }
19
+ it { expect(log2(2 ** n)).to eq(n) }
20
+ it { expect(log2(3 ** n)).to eq(n * log2( 3 )) }
21
+
22
+ it { expect(log(1)).to eq(0) }
23
+ it { expect(log(e)).to eq(1) }
24
+ it { expect(log(e ** n)).to eq(n) }
25
+
26
+ it { expect(sin(pi)).to eq(0) }
27
+
28
+ it { expect(cos(0)).to eq(1) }
29
+ it { expect(cos(pi)).to eq(-1) }
30
+ it { expect(cos(2 * pi)).to eq(1) }
31
+
32
+ describe '#to_s' do
33
+ it { expect(e.to_s).to eq('e') }
34
+
35
+ it { expect(1.to_s).to eq('1') }
36
+
37
+ it { expect(1.0.to_s).to eq('1.0') }
38
+
39
+ it { expect(e1.to_s).to eq('1') }
40
+
41
+ it { expect(pi.to_s).to eq('pi') }
42
+
43
+ it { expect(cos(x).to_s).to eq('cos( x )') }
44
+ it { expect(sin(x).to_s).to eq('sin( x )') }
45
+ it { expect(tan(x).to_s).to eq('tan( x )') }
46
+
47
+ it { expect(x.to_s).to eq('x') }
48
+ end
49
+
50
+ describe '#==' do
51
+ it { expect(_(1) == _(1)).to be true }
52
+ end
53
+
54
+ describe '#subst' do
55
+ it { expect(1.subst).to eq(1) }
56
+ it { expect(1.0.subst).to eq(1.0) }
57
+ it { expect(e.subst).to eq(e) }
58
+ it { expect(pi.subst).to eq(pi) }
59
+ it { expect(sin(x).subst(x: 3)).to eq(sin(3)) }
60
+ it { expect(cos(x).subst(x: pi)).to eq(-1) }
61
+ it { expect(tan(0).subst(x: pi)).to eq(0) }
62
+ it { expect(log(x).subst(x: e)).to eq(1) }
63
+ it { expect(log10(x).subst(x: 7)).to eq(log10(7)) }
64
+ it { expect(log2(2).subst(x: 2)).to eq(1) }
65
+ it { expect(x.subst(x: 2)).to eq(2) }
66
+ it { expect(x.subst(y: 2)).to eq(x) }
67
+ end
68
+
69
+ describe '#differentiate' do
70
+ it { expect(e.d).to eq(0) }
71
+ it { expect((e ** x).d).to eq(e ** x) }
72
+ it { expect((e ** (x + y)).d).to eq(e ** ( x + y )) }
73
+
74
+ it { expect(pi.d(x)).to eq(0) }
75
+
76
+ it { expect(1.d).to eq(0) }
77
+ it { expect(3.d).to eq(0) }
78
+ it { expect(3.0.d).to eq(0) }
79
+
80
+ it { expect(sin(x).d).to eq(cos(x)) }
81
+ it { expect(cos(x).d).to eq(- sin(x)) }
82
+ it { expect(tan(x).d).to eq(1 / cos(x) ** 2) }
83
+
84
+ it { expect(log(x).d).to eq(1 / x) }
85
+ it { expect(log10(x).d).to eq(1 / ( x * log( 10 ) )) }
86
+ it { expect(log2(x).d).to eq(1 / ( x * log( 2 ) )) }
87
+
88
+ it { expect(x.d(x)).to eq(1) }
89
+ end
90
+
91
+ describe 'Calculate' do
92
+ context 'E with Fixnum' do
93
+ it { expect(e + 0).to eq(e) }
94
+ it { expect(e - 0).to eq(e) }
95
+ it { expect((e * 0).to_s).to eq('0') }
96
+ it { expect(e * 1).to eq(e) }
97
+ it { expect { (e / 0).to_s }.to raise_error(ZeroDivisionError) }
98
+ it { expect(e / 1).to eq(e) }
99
+ it { expect((e ** 0).to_s).to eq('1') }
100
+ end
101
+
102
+ context 'Fixnum with Formula' do
103
+ let(:formula) { (x + y) }
104
+ it { expect(0 + formula).to eq(formula) }
105
+ it { expect(0 - formula).to eq( - ( x + y ) ) }
106
+ it { expect(0 * formula).to eq(0) }
107
+ it { expect(1 * formula).to eq(formula) }
108
+ it { expect(0 / formula).to eq(0) }
109
+ it { expect(1 / formula).to eq( 1 / ( x + y ) ) }
110
+ it { expect(0 ** formula).to eq(0) }
111
+ it { expect(1 ** formula).to eq(1) }
112
+ end
113
+
114
+ context 'Fixnum with Symbol' do
115
+ it { expect(0 + x).to eq(x) }
116
+ it { expect(0 - x).to eq(- x) }
117
+ it { expect(0 * x).to eq(0) }
118
+ it { expect(1 * x).to eq(x) }
119
+ it { expect(0 / x).to eq(0) }
120
+ it { expect(1 / x).to eq( 1 / x ) }
121
+ it { expect(0 ** x).to eq(0) }
122
+ it { expect(1 ** x).to eq(1) }
123
+ end
124
+
125
+ context 'Fixnum with Fixnum' do
126
+ it { expect(0 + 3).to eq(3) }
127
+ it { expect(3 + 0).to eq(3) }
128
+ it { expect(2 + 3).to eq(5) }
129
+
130
+ it { expect(0 - 3).to eq(-3) }
131
+ it { expect(3 - 0).to eq(3) }
132
+ it { expect(2 - 3).to eq(-1) }
133
+
134
+ it { expect(0 * 3).to eq(0) }
135
+ it { expect(3 * 0).to eq(0) }
136
+ it { expect(1 * 3).to eq(3) }
137
+ it { expect(3 * 1).to eq(3) }
138
+ it { expect(3 * 2).to eq(6) }
139
+
140
+ it { expect((0 / 3).to_s).to eq('0') }
141
+ it { expect { (3 / 0).to_s }.to raise_error(ZeroDivisionError) }
142
+ it { expect((3 / 1).to_s).to eq('3') }
143
+ # TODO:
144
+ it { expect((2 / 3).to_s).to eq('0') }
145
+
146
+ it { expect((0 ** 3).to_s).to eq('0') }
147
+ it { expect((3 ** 0).to_s).to eq('1') }
148
+ it { expect((1 ** 3).to_s).to eq('1') }
149
+ it { expect((3 ** 1).to_s).to eq('3') }
150
+ it { expect((3 ** 2).to_s).to eq('9') }
151
+ end
152
+
153
+ context 'Float with Formula' do
154
+ let(:formula) { (:x + :y) }
155
+ it { expect((0.0 + formula).to_s).to eq(formula.to_s) }
156
+ it { expect((0.0 - formula).to_s).to eq('( - ( x + y ) )') }
157
+ it { expect((0.0 * formula).to_s).to eq('0') }
158
+ it { expect((1.0 * formula).to_s).to eq(formula.to_s) }
159
+ it { expect((0.0 / formula).to_s).to eq('0') }
160
+ it { expect((1.0 / formula).to_s).to eq('( 1 / ( x + y ) )') }
161
+ it { expect((0.0 ** formula).to_s).to eq('0') }
162
+ it { expect((1.0 ** formula).to_s).to eq('1') }
163
+ end
164
+
165
+ context 'Float with Symbol' do
166
+ it { expect(0.0 + :x).to eq(:x) }
167
+ it { expect((0.0 - :x).to_s).to eq('( - x )') }
168
+ it { expect((0.0 * :x).to_s).to eq('0') }
169
+ it { expect(1.0 * :x).to eq(:x) }
170
+ it { expect((0.0 / :x).to_s).to eq('0') }
171
+ it { expect((1.0 / :x).to_s).to eq('( 1 / x )') }
172
+ it { expect((0.0 ** :x).to_s).to eq('0') }
173
+ it { expect((1.0 ** :x).to_s).to eq('1') }
174
+ end
175
+
176
+ context 'Float with Float' do
177
+ it { expect(0.0 + 3.0).to eq(3.0) }
178
+ it { expect(3.0 + 0.0).to eq(3.0) }
179
+ it { expect(2.0 + 3.0).to eq(5.0) }
180
+
181
+ it { expect(0.0 - 3.0).to eq(-3.0) }
182
+ it { expect(3.0 - 0.0).to eq(3.0) }
183
+ it { expect(2.0 - 3.0).to eq(-1.0) }
184
+
185
+ it { expect(0.0 * 3.0).to eq(0.0) }
186
+ it { expect(3.0 * 0.0).to eq(0.0) }
187
+ it { expect(1.0 * 3.0).to eq(3.0) }
188
+ it { expect(3.0 * 1.0).to eq(3.0) }
189
+ it { expect(3.0 * 2.0).to eq(6.0) }
190
+
191
+ it { expect(0.0 / 3.0).to eq(0.0) }
192
+ it { expect(3.0 / 0.0).to eq(oo) }
193
+ it { expect(3.0 / 1.0).to eq(3.0) }
194
+ # TODO:
195
+ it { expect(2.0 / 3.0).to eq(0.6666666666666666) }
196
+
197
+ it { expect(0.0 ** 3.0).to eq(0.0) }
198
+ it { expect(3.0 ** 0.0).to eq(1.0) }
199
+ it { expect(1.0 ** 3.0).to eq(1.0) }
200
+ it { expect(3.0 ** 1.0).to eq(3.0) }
201
+ it { expect(3.0 ** 2.0).to eq(9.0) }
202
+ end
203
+
204
+ context 'Pi with Fixnum' do
205
+ it { expect(pi + 0).to eq(pi) }
206
+ it { expect(pi - 0).to eq(pi) }
207
+ it { expect((pi * 0).to_s).to eq('0') }
208
+ it { expect(pi * 1).to eq(pi) }
209
+ it { expect { (pi / 0).to_s }.to raise_error(ZeroDivisionError) }
210
+ it { expect(pi / 1).to eq(pi) }
211
+ it { expect((pi ** 0).to_s).to eq('1') }
212
+ end
213
+
214
+ context 'Symbol with Fixnum' do
215
+ it { expect(x + 0).to eq(x) }
216
+ it { expect(x - 0).to eq(x) }
217
+ it { expect(x * 0).to eq(0) }
218
+ it { expect(x * 1).to eq(x) }
219
+ it { expect { x / 0 }.to raise_error(ZeroDivisionError) }
220
+ it { expect(x / 1).to eq(x) }
221
+ it { expect(x ** 0).to eq(1) }
222
+ it { expect(x ** 1).to eq(x) }
223
+ end
224
+
225
+ context 'Num with Num' do
226
+ it { expect(_(1) + _(2)).to eq(3) }
227
+ it { expect(_(1) - _(2)).to eq(-1) }
228
+ it { expect(_(1) * _(2)).to eq(2) }
229
+ it { expect((_(1) / _(2)).to_s).to eq('( 1 / 2 )') }
230
+ it { expect(_(1) % _(2)).to eq(1) }
231
+ end
232
+ end
233
+
234
+ describe Dydx::Algebra::Set::Num do
235
+ describe :< do
236
+ it { expect(_(1) < _(2)).to be(true) }
237
+ it { expect(_(1) < 2).to be(true) }
238
+ it { expect(_(2) < _(1)).to be(false) }
239
+ it { expect(_(2) < 1).to be(false) }
240
+ end
241
+
242
+ describe :<= do
243
+ it { expect(_(1) <= _(2)).to be(true) }
244
+ it { expect(_(1) <= 2).to be(true) }
245
+ it { expect(_(2) <= _(1)).to be(false) }
246
+ it { expect(_(2) <= 1).to be(false) }
247
+ end
248
+
249
+ describe :> do
250
+ it { expect(_(1) > _(2)).to be(false) }
251
+ it { expect(_(1) > 2).to be(false) }
252
+ it { expect(_(2) > _(1)).to be(true) }
253
+ it { expect(_(2) > 1).to be(true) }
254
+ end
255
+
256
+ describe :>= do
257
+ it { expect(_(1) >= _(2)).to be(false) }
258
+ it { expect(_(1) >= 2).to be(false) }
259
+ it { expect(_(2) >= _(1)).to be(true) }
260
+ it { expect(_(2) >= 1).to be(true) }
261
+ end
262
+ end
263
+ end
@@ -1,11 +1,35 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Dydx:Delta do
4
- it{ expect(d.class).to eq(Delta) }
5
- it{ expect(dx.class).to eq(Delta) }
6
- it{ expect(dx.var).to eq(:x) }
7
- it{ expect(dx(y).class).to eq(Delta) }
8
- it{ expect(dx(y).var).to eq(:x) }
9
- it{ expect(dx(y).function).to eq(:y) }
10
- it{ expect{dxy}.to raise_error(NameError) }
3
+ describe Dydx::Delta do
4
+ it { expect(d.class).to eq(Delta) }
5
+ it { expect(dx.class).to eq(Delta) }
6
+ it { expect(dx.var).to eq(:x) }
7
+ it { expect(dx(y).class).to eq(Delta) }
8
+ it { expect(dx(y).var).to eq(:x) }
9
+ it { expect(dx(y).function).to eq(:y) }
10
+ it { expect { dxy }.to raise_error(NameError) }
11
+
12
+ before { reset }
13
+
14
+ it 'ex1' do
15
+ $y = x ** n
16
+ expect(dy/dx).to eq( n * ( x ** ( n - 1 ) ) )
17
+ expect(d/dx($y)).to eq( n * ( x ** ( n - 1 ) ) )
18
+ end
19
+
20
+ it 'ex2' do
21
+ $y = x ** (x * 2)
22
+ expect(dy/dx).to eq(( 2 * x ) * ( x ** ( ( 2 * x ) - 1 ) ))
23
+ expect(d/dx($y)).to eq(( 2 * x ) * ( x ** ( ( 2 * x ) - 1 ) ))
24
+ end
25
+
26
+ it 'ex3' do
27
+ $y = (t ** 2) / 2
28
+ expect(dy/dt).to eq(t)
29
+ end
30
+
31
+ it 'ex4' do
32
+ $y = 2 * (e ** (2 * z))
33
+ expect(dy/dz).to eq(4 * ( e ** ( 2 * z ) ))
34
+ end
11
35
  end
@@ -1,39 +1,65 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Dydx:Function do
4
- # TODO: refactor
5
- it{ expect(f(x, y)).to eq(f(x, y)) }
6
- it{ expect(f(x, y)).to eq($f) }
7
- it{ expect(f(x, y).algebra).to be_nil }
8
- it{ expect(f(x, y).vars).to eq([:x, :y]) }
9
- it{ expect{f(x, y, z)}.to raise_error(ArgumentError) }
10
- it{ expect{f(x)}.to raise_error(ArgumentError) }
11
- it{ expect(f(x, y) <= x * y ).to eq(f(x, y)) }
12
- it{ expect(f(x, y)).to eq(f(x, y)) }
13
- it{ expect(f(x, y)).to eq($f) }
14
- it{ expect(f(x, y).algebra).to eq(x * y) }
15
- it{ expect(f(x, y)).to eq(x * y) }
16
- it{ expect(eval(f(x, y).to_s)).to eq(f(x, y)) }
17
- it{ expect(f(x, y)).to eq(eval(f(x, y).to_s)) }
18
- it{ expect(f(a, b)).to eq(a * b) }
19
- it{ expect(f(2, 3)).to eq(6) }
20
- it{ expect(f(a + b, c)).to eq((a + b) * c) }
21
- it{ expect(d/dx(f(x, y))).to eq(y) }
22
- it{ expect(d/dy(f(x, y))).to eq(x) }
23
- it{ expect(d/dz(f(x, y))).to eq(0) }
24
-
25
- it{ expect(g(a, b) <= f(a + b, b)).to eq(g(a, b)) }
26
- it{ expect(g(a, b)).to eq($g) }
27
- it{ expect(g(2, 3)).to eq(15) }
28
-
29
- it{ expect(h(a, b, c) <= d/db(g(a, b))).to eq(h(a, b, c)) }
30
- it{ expect(h(a, b, c) <= d/db(g(a, b))).to eq($h) }
31
- it{ expect(h(a, b, c)).to eq(( a + ( 2 * b ) )) }
32
- it{ expect(h(a, b, c).algebra).to eq(( a + ( 2 * b ) )) }
33
-
34
- it 'ex.4' do
35
- $f = nil
3
+ describe Dydx::Function do
4
+ before { reset }
5
+
6
+ it 'ex1' do
7
+ expect(f(x, y)).to eq(f(x, y))
8
+ expect(f(x, y)).to eq($f)
9
+ expect(f(x, y).algebra).to be_nil
10
+ expect(f(x, y).vars).to eq([:x, :y])
11
+ expect { f(x, y, z) }.to raise_error(ArgumentError)
12
+ expect { f(x) }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it 'ex2' do
16
+ expect(f(x, y) <= x * y ).to eq(f(x, y))
17
+ expect(f(x, y)).to eq(f(x, y))
18
+ expect(f(x, y)).to eq($f)
19
+ expect(f(x, y).algebra).to eq(x * y)
20
+ expect(f(x, y)).to eq(x * y)
21
+ expect(f(x, y)).to eq(eval(f(x, y).to_s))
22
+ expect(f(a, b)).to eq(a * b)
23
+ expect(f(2, x)).to eq(2 * x)
24
+ expect(f(2, 3)).to eq(6)
25
+ expect(f(a + b, c)).to eq((a + b) * c)
26
+ expect(d/dx(f(x, y))).to eq(y)
27
+ expect(d/dy(f(x, y))).to eq(x)
28
+ expect(d/dz(f(x, y))).to eq(0)
29
+ end
30
+
31
+ it 'ex3' do
32
+ f(x, y) <= x * y
33
+ g(a, b) <= f(a + b, b)
34
+ expect(g(a, b)).to eq((a + b) * b)
35
+ expect(g(a, 3)).to eq(9 + 3 * a)
36
+ expect(g(2, 3)).to eq(15)
37
+ end
38
+
39
+ it 'ex4' do
40
+ f(a, b) <= (a + b) * b
41
+ h(a, b, c) <= d/db(f(a, b))
42
+ expect(h(a, b, c)).to eq(( a + ( 2 * b ) ))
43
+ expect(h(a, b, c).algebra).to eq(( a + ( 2 * b ) ))
44
+ end
45
+
46
+ it 'ex5' do
36
47
  f(x) <= log(x)
37
- expect(f(a)).to eq(log(a))
48
+ expect(f(e)).to eq(1)
49
+ expect(f(0)).to eq(-oo)
50
+ expect(f(y)).to eq(log(y))
51
+
52
+ g(x) <= d/dx(f(x))
53
+ expect(g(1)).to eq(1)
54
+ end
55
+
56
+ it 'ex6' do
57
+ f(x) <= sin(x)
58
+ expect(f(pi)).to eq(0)
59
+ expect(f(pi / 2)).to eq(1)
60
+ expect(f(y)).to eq(sin(y))
61
+
62
+ g(x) <= d/dx(f(x))
63
+ expect(g(pi)).to eq(-1)
38
64
  end
39
65
  end
@@ -3,70 +3,63 @@ require 'spec_helper'
3
3
  describe Helper do
4
4
  include Helper
5
5
  context '#is_n?' do
6
- it{ expect(0.is_0?).to be_true }
7
- it{ expect(_(0).is_0?).to be_true }
8
- it{ expect(inverse(0, :+).is_0?).to be_true }
9
- it{ expect(1.is_1?).to be_true }
10
- it{ expect(_(1).is_1?).to be_true }
11
- it{ expect(inverse(1, :*).is_1?).to be_true }
12
- it{ expect(-1.is_minus1?).to be_true }
13
- it{ expect(_(-1).is_minus1?).to be_true }
6
+ it { expect(0.zero?).to be true }
7
+ it { expect(_(0).zero?).to be true }
8
+ it { expect(inverse(0, :+).zero?).to be true }
9
+ it { expect(1.one?).to be true }
10
+ it { expect(_(1).one?).to be true }
11
+ it { expect(inverse(1, :*).one?).to be true }
12
+ it { expect(-1.minus1?).to be true }
13
+ it { expect(_(-1).minus1?).to be true }
14
14
  end
15
15
 
16
- context '#is_multiple_of' do
17
- it{ expect(0.is_multiple_of(:x).to_s).to eq('0') }
18
- it{ expect(_(0).is_multiple_of(:y).to_s).to eq('0')}
16
+ context '#multiple_of?' do
17
+ it { expect(0.multiple_of?(x)).to be(true) }
19
18
 
20
- it{ expect(:x.is_multiple_of(:x).to_s).to eq('1') }
21
- it{ expect(:x.is_multiple_of(:y)).to be_false }
19
+ it { expect(4.multiple_of?(_(2))).to be(true) }
20
+ it { expect(_(4).multiple_of?(2)).to be(true) }
21
+ it { expect(_(4).multiple_of?(_(2))).to be(true) }
22
22
 
23
- it{ expect((:x * :y).is_multiple_of(:x)).to eq(:y) }
24
- it{ expect((:x * :y).is_multiple_of(:y)).to eq(:x) }
25
- it{ expect((:x * :y).is_multiple_of(:z)).to be_false }
23
+ it { expect(x.multiple_of?(x)).to be(true) }
24
+ it { expect(x.multiple_of?(y)).to be(false) }
25
+
26
+ it { expect((x * y).multiple_of?(x)).to be(true) }
27
+ it { expect((x * y).multiple_of?(x)).to be(true) }
28
+ it { expect((x * y).multiple_of?(z)).to be(false) }
26
29
  end
27
30
 
28
31
  context '#like_term?' do
29
- it{ expect(x.like_term?(x)).to be_true }
30
- it{ expect((2 * x).like_term?((3 * x))).to be_true }
32
+ it { expect(x.like_term?(x)).to be true }
33
+ it { expect((2 * x).like_term?((3 * x))).to be true }
31
34
  end
32
35
 
33
36
  context '#combinable?' do
34
- it{ expect(:x.combinable?(:x, :+)).to be_true }
35
- it{ expect(:x.combinable?(2 * :x, :+)).to be_true }
36
- it{ expect((2 * :x).combinable?(:x, :+)).to be_true }
37
- it{ expect((2 * :x).combinable?(2 * :x, :+)).to be_true }
38
- it{ expect(:x.combinable?(:y, :+)).to be_false }
39
- it{ expect(1.combinable?(2, :+)).to be_true }
40
- it{ expect(:x.combinable?(:x, :*)).to be_true }
41
- it{ expect(:x.combinable?(:y, :*)).to be_false }
42
- it{ expect(1.combinable?(2, :*)).to be_true }
43
- it{ expect(0.combinable?(:x, :^)).to be_true }
44
- it{ expect(1.combinable?(:y, :^)).to be_true }
37
+ it { expect(:x.combinable?(:x, :+)).to be true }
38
+ it { expect(:x.combinable?(2 * :x, :+)).to be true }
39
+ it { expect((2 * :x).combinable?(:x, :+)).to be true }
40
+ it { expect((2 * :x).combinable?(2 * :x, :+)).to be true }
41
+ it { expect(:x.combinable?(:y, :+)).to be false }
42
+ it { expect(1.combinable?(2, :+)).to be true }
43
+ it { expect(:x.combinable?(:x, :*)).to be true }
44
+ it { expect(:x.combinable?(:y, :*)).to be false }
45
+ it { expect(1.combinable?(2, :*)).to be true }
46
+ it { expect(0.combinable?(:x, :**)).to be true }
47
+ it { expect(1.combinable?(:y, :**)).to be true }
45
48
  end
46
49
 
47
50
  context '#distributive?' do
48
- it{ expect(distributive?(:+, :*)).to be_true }
49
- it{ expect(distributive?(:+, :/)).to be_true }
50
- it{ expect(distributive?(:-, :*)).to be_true }
51
- it{ expect(distributive?(:-, :/)).to be_true }
52
- it{ expect(distributive?(:*, :^)).to be_true }
53
- it{ expect(distributive?(:/, :^)).to be_true }
54
- it{ expect(distributive?(:*, :+)).to be_false }
55
- it{ expect(distributive?(:^, :*)).to be_false }
51
+ it { expect(distributive?(:+, :*)).to be true }
52
+ it { expect(distributive?(:+, :/)).to be true }
53
+ it { expect(distributive?(:-, :*)).to be true }
54
+ it { expect(distributive?(:-, :/)).to be true }
55
+ it { expect(distributive?(:*, :**)).to be true }
56
+ it { expect(distributive?(:/, :**)).to be true }
57
+ it { expect(distributive?(:*, :+)).to be false }
58
+ it { expect(distributive?(:**, :*)).to be false }
56
59
  end
57
60
 
58
- let(:addition) { (:x + :y) }
59
- let(:subtraction) { (:x - :y) }
60
- let(:multiplication){ (:x * :y) }
61
- let(:division) { (:x / :y) }
62
- let(:exponentiation){ (:x ^ :y) }
63
-
64
- it{ expect(addition.addition?).to be_true }
65
- it{ expect(multiplication.multiplication?).to be_true }
66
- it{ expect(exponentiation.exponentiation?).to be_true }
67
-
68
- it{ expect(inverse(:x, :+).inverse?(:+, :x)).to be_true }
69
- it{ expect(:x.inverse?(:+, inverse(:x, :+))).to be_true }
70
- it{ expect(inverse(:x, :*).inverse?(:*, :x)).to be_true }
71
- it{ expect(:x.inverse?(:*, inverse(:x, :*))).to be_true }
61
+ it { expect(inverse(x, :+).inverse?(:+, x)).to be true }
62
+ it { expect(x.inverse?(:+, inverse(x, :+))).to be true }
63
+ it { expect(inverse(x, :*).inverse?(:*, x)).to be true }
64
+ it { expect(x.inverse?(:*, inverse(x, :*))).to be true }
72
65
  end