dydx 0.1.4 → 0.1.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -3
- data/README.md +124 -61
- data/Rakefile +8 -5
- data/dydx.gemspec +13 -13
- data/lib/dydx.rb +25 -23
- data/lib/dydx/algebra.rb +76 -8
- data/lib/dydx/algebra/formula.rb +38 -71
- data/lib/dydx/algebra/inverse.rb +12 -19
- data/lib/dydx/algebra/operator/common_parts.rb +3 -0
- data/lib/dydx/algebra/operator/formula.rb +4 -0
- data/lib/dydx/algebra/operator/general.rb +4 -0
- data/lib/dydx/algebra/operator/inverse.rb +4 -0
- data/lib/dydx/algebra/operator/num.rb +4 -0
- data/lib/dydx/algebra/operator/parts/base.rb +2 -2
- data/lib/dydx/algebra/operator/parts/formula.rb +38 -63
- data/lib/dydx/algebra/operator/parts/general.rb +31 -84
- data/lib/dydx/algebra/operator/parts/interface.rb +22 -0
- data/lib/dydx/algebra/operator/parts/inverse.rb +4 -4
- data/lib/dydx/algebra/operator/parts/num.rb +11 -16
- data/lib/dydx/algebra/operator/parts/symbol.rb +2 -2
- data/lib/dydx/algebra/operator/symbol.rb +15 -0
- data/lib/dydx/algebra/set.rb +34 -271
- data/lib/dydx/algebra/set/base.rb +9 -0
- data/lib/dydx/algebra/set/cos.rb +22 -0
- data/lib/dydx/algebra/set/e.rb +16 -0
- data/lib/dydx/algebra/set/fixnum.rb +14 -0
- data/lib/dydx/algebra/set/float.rb +14 -0
- data/lib/dydx/algebra/set/log.rb +22 -0
- data/lib/dydx/algebra/set/num.rb +26 -0
- data/lib/dydx/algebra/set/pi.rb +16 -0
- data/lib/dydx/algebra/set/sin.rb +21 -0
- data/lib/dydx/algebra/set/symbol.rb +14 -0
- data/lib/dydx/algebra/set/tan.rb +17 -0
- data/lib/dydx/delta.rb +1 -1
- data/lib/dydx/function.rb +1 -1
- data/lib/dydx/helper.rb +61 -55
- data/lib/dydx/integrand.rb +10 -22
- data/lib/dydx/version.rb +1 -1
- data/spec/dydx_spec.rb +29 -10
- data/spec/lib/algebra/formula_spec.rb +38 -44
- data/spec/lib/algebra/operator/parts/base_spec.rb +5 -5
- data/spec/lib/algebra/operator/parts/formula_spec.rb +50 -57
- data/spec/lib/algebra/operator/parts/inverse_spec.rb +8 -8
- data/spec/lib/algebra/set/cos_spec.rb +18 -0
- data/spec/lib/algebra/set/e_spec.rb +27 -0
- data/spec/lib/algebra/set/fixnum_spec.rb +65 -0
- data/spec/lib/algebra/set/float_spec.rb +65 -0
- data/spec/lib/algebra/set/log_spec.rb +15 -0
- data/spec/lib/algebra/set/num_spec.rb +23 -0
- data/spec/lib/algebra/set/pi_spec.rb +25 -0
- data/spec/lib/algebra/set/sin_spec.rb +14 -0
- data/spec/lib/algebra/set/symbol_spec.rb +22 -0
- data/spec/lib/algebra/set/tan_spec.rb +13 -0
- data/spec/lib/delta_spec.rb +8 -32
- data/spec/lib/function_spec.rb +34 -60
- data/spec/lib/helper_spec.rb +49 -47
- data/spec/lib/integrand_spec.rb +15 -13
- data/spec/spec_helper.rb +1 -2
- metadata +39 -9
- data/.pryrc +0 -2
- data/.rubocop.yml +0 -25
- data/spec/lib/algebra/set_spec.rb +0 -263
data/lib/dydx/version.rb
CHANGED
data/spec/dydx_spec.rb
CHANGED
@@ -5,16 +5,35 @@ describe Dydx do
|
|
5
5
|
expect(Dydx::VERSION).not_to be nil
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
expect(
|
13
|
-
expect(
|
14
|
-
|
8
|
+
context 'ex1' do
|
9
|
+
$a = (:x ^ :n)
|
10
|
+
let(:d1){ da/dx }
|
11
|
+
let(:d2){ d/dx($a) }
|
12
|
+
it{ expect(d1.to_s).to eq('( n * ( x ^ ( n - 1 ) ) )') }
|
13
|
+
it{ expect(d2.to_s).to eq('( n * ( x ^ ( n - 1 ) ) )') }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'ex2' do
|
17
|
+
$b = (:x ^ (:x * 2))
|
18
|
+
let(:d1){ db/dx }
|
19
|
+
let(:d2){ d/dx($b) }
|
20
|
+
it{ expect(d1.to_s).to eq('( ( x * 2 ) * ( x ^ ( ( x * 2 ) - 1 ) ) )') }
|
21
|
+
it{ expect(d2.to_s).to eq('( ( x * 2 ) * ( x ^ ( ( x * 2 ) - 1 ) ) )') }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'ex3' do
|
25
|
+
$c = (:t ^ 2) / 2
|
26
|
+
let(:d1){ dc/dt }
|
27
|
+
let(:d2){ d/dt($c) }
|
28
|
+
it{ expect(d1.to_s).to eq('t') }
|
29
|
+
it{ expect(d2.to_s).to eq('t') }
|
30
|
+
end
|
15
31
|
|
16
|
-
|
17
|
-
|
18
|
-
|
32
|
+
context 'ex4' do
|
33
|
+
$i = 2 * (e ^ (2 * :z))
|
34
|
+
let(:d1){ di/dz }
|
35
|
+
let(:d2){ d/dz($i) }
|
36
|
+
it{ expect(d1.to_s).to eq('( 4 * ( e ^ ( 2 * z ) ) )') }
|
37
|
+
it{ expect(d2.to_s).to eq('( 4 * ( e ^ ( 2 * z ) ) )') }
|
19
38
|
end
|
20
39
|
end
|
@@ -1,69 +1,63 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Dydx::Algebra::Formula do
|
4
|
-
let(:addition)
|
5
|
-
let(:subtraction)
|
6
|
-
let(:multiplication)
|
7
|
-
let(:division)
|
8
|
-
let(:exponentiation)
|
4
|
+
let(:addition) { (:x + :y) }
|
5
|
+
let(:subtraction) { (:x - :y) }
|
6
|
+
let(:multiplication){ (:x * :y) }
|
7
|
+
let(:division) { (:x / :y) }
|
8
|
+
let(:exponentiation){ (:x ^ :y) }
|
9
9
|
describe 'Calculate' do
|
10
10
|
context 'With Fixnum' do
|
11
11
|
let(:formula) { (:x + :y) }
|
12
|
-
it
|
13
|
-
it
|
14
|
-
it
|
15
|
-
it
|
16
|
-
it
|
17
|
-
it
|
18
|
-
it
|
12
|
+
it{ expect(formula + 0).to eq(formula) }
|
13
|
+
it{ expect(formula - 0).to eq(formula) }
|
14
|
+
it{ expect(formula * 0).to eq(0) }
|
15
|
+
it{ expect(formula * 1).to eq(formula) }
|
16
|
+
it{ expect{(formula / 0).to_s}.to raise_error(ZeroDivisionError) }
|
17
|
+
it{ expect(formula / 1).to eq(formula) }
|
18
|
+
it{ expect(formula ^ 0).to eq(1) }
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe '#to_s' do
|
23
|
-
it
|
24
|
-
it
|
25
|
-
it
|
26
|
-
it
|
27
|
-
it
|
28
|
-
it
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '#subst' do
|
32
|
-
it { expect((x + y).subst(x: 3, y: 3)).to eq(6) }
|
33
|
-
it { expect((x + y).subst(x: 3)).to eq(3 + y) }
|
34
|
-
it { expect((x + y + pi).subst(x: 3, y: 3).to_f).to eq(Math::PI + 6) }
|
23
|
+
it{ expect(addition.to_s).to eq('( x + y )') }
|
24
|
+
it{ expect(subtraction.to_s).to eq('( x - y )') }
|
25
|
+
it{ expect(multiplication.to_s).to eq('( x * y )') }
|
26
|
+
it{ expect(division.to_s).to eq('( x / y )') }
|
27
|
+
it{ expect(exponentiation.to_s).to eq('( x ^ y )') }
|
28
|
+
it{ expect( (addition * multiplication).to_s ).to eq('( ( x + y ) * ( x * y ) )') }
|
35
29
|
end
|
36
30
|
|
37
31
|
describe '#differentiate' do
|
38
|
-
it
|
39
|
-
it
|
40
|
-
it
|
32
|
+
it{ expect(addition.d(:x)).to eq(1) }
|
33
|
+
it{ expect(addition.d(:y)).to eq(1) }
|
34
|
+
it{ expect(addition.d(:z)).to eq(0) }
|
41
35
|
|
42
|
-
it
|
43
|
-
it
|
44
|
-
it
|
36
|
+
it{ expect(subtraction.d(:x)).to eq(1) }
|
37
|
+
it{ expect(subtraction.d(:y)).to eq('( - 1 )') }
|
38
|
+
it{ expect(subtraction.d(:z)).to eq(0) }
|
45
39
|
|
46
|
-
it
|
47
|
-
it
|
48
|
-
it
|
40
|
+
it{ expect(multiplication.d(:x)).to eq(:y) }
|
41
|
+
it{ expect(multiplication.d(:y)).to eq(:x) }
|
42
|
+
it{ expect(multiplication.d(:z)).to eq(0) }
|
49
43
|
|
50
|
-
it
|
51
|
-
it
|
52
|
-
it
|
44
|
+
it{ expect(division.d(:x)).to eq(1/:y) }
|
45
|
+
it{ expect(division.d(:y)).to eq('( - ( x / ( y ^ 2 ) ) )') }
|
46
|
+
it{ expect(division.d(:z)).to eq(0) }
|
53
47
|
|
54
|
-
it
|
55
|
-
it
|
56
|
-
it
|
48
|
+
it{ expect(exponentiation.d(:x).to_s).to eq('( y * ( x ^ ( y - 1 ) ) )') }
|
49
|
+
it{ expect(exponentiation.d(:y)).to eq((:x ^ :y) * log(:x)) }
|
50
|
+
it{ expect(exponentiation.d(:z)).to eq(0) }
|
57
51
|
end
|
58
52
|
|
59
53
|
describe '#include?' do
|
60
|
-
it
|
61
|
-
it
|
54
|
+
it{ expect(addition.include?(:x)).to be_true }
|
55
|
+
it{ expect(addition.include?(:z)).to be_false }
|
62
56
|
end
|
63
57
|
|
64
58
|
describe '#openable?' do
|
65
|
-
it
|
66
|
-
it
|
67
|
-
it
|
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 }
|
68
62
|
end
|
69
63
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Dydx::Algebra::Operator::Parts::Base do
|
4
|
-
it
|
5
|
-
it
|
6
|
-
it
|
7
|
-
it
|
8
|
-
end
|
4
|
+
it{ expect((:x + :x).to_s).to eq('( 2 * x )') }
|
5
|
+
it{ expect((:x - :x).to_s).to eq('0') }
|
6
|
+
it{ expect((:x * :x).to_s).to eq('( x ^ 2 )') }
|
7
|
+
it{ expect((:x / :x).to_s).to eq('1') }
|
8
|
+
end
|
@@ -1,63 +1,56 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Dydx::Algebra::Operator::Parts::Formula do
|
4
|
-
it
|
5
|
-
it
|
6
|
-
it
|
7
|
-
it
|
8
|
-
it
|
9
|
-
it
|
10
|
-
it
|
11
|
-
it
|
12
|
-
|
13
|
-
it
|
14
|
-
it
|
15
|
-
it
|
16
|
-
it
|
17
|
-
it
|
18
|
-
it
|
19
|
-
it
|
20
|
-
it
|
21
|
-
|
22
|
-
it
|
23
|
-
it
|
24
|
-
it
|
25
|
-
it
|
26
|
-
|
27
|
-
it
|
28
|
-
it
|
29
|
-
it
|
30
|
-
it
|
31
|
-
|
32
|
-
it
|
33
|
-
it
|
34
|
-
it
|
35
|
-
it
|
36
|
-
it
|
37
|
-
it
|
38
|
-
it
|
39
|
-
it
|
40
|
-
|
41
|
-
it
|
42
|
-
it
|
43
|
-
it
|
44
|
-
it
|
45
|
-
it
|
46
|
-
it
|
47
|
-
it
|
48
|
-
it
|
49
|
-
|
50
|
-
it
|
51
|
-
|
52
|
-
it { expect((x + 3) * 2).to eq(x * 2 + 6) }
|
53
|
-
it { expect((x + 3) * 2).to eq(x * 2 + 6) }
|
54
|
-
|
55
|
-
it { expect((x * 2) ** 2).to eq( 4 * x ** 2 ) }
|
56
|
-
it { expect((x / 2) ** 2).to eq( x ** 2 / 4 ) }
|
57
|
-
|
58
|
-
it { expect((3 * x + 4 * (x ** 2) + 4 * x).to_s).to eq('( ( 7 * x ) + ( 4 * ( x ** 2 ) ) )') }
|
4
|
+
it{ expect(((:a * :b) + (:a * :c)).to_s).to eq('( a * ( b + c ) )') }
|
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 ) )') }
|
8
|
+
it{ expect(((:a * :b) - (:a * :c)).to_s).to eq('( a * ( b - c ) )') }
|
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 ) )') }
|
12
|
+
|
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 ) )') }
|
21
|
+
|
22
|
+
it{ expect(((:x - 2) + 2).to_s).to eq('x') }
|
23
|
+
it{ expect(((:x + 2) - 2).to_s).to eq('x') }
|
24
|
+
it{ expect(((:x * 2) / 2).to_s).to eq('x') }
|
25
|
+
it{ expect(((:x / 2) * 2).to_s).to eq('x') }
|
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
|
+
|
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 )') }
|
34
|
+
it{ expect(((:x - :y) - :y).to_s).to eq('( x - ( 2 * y ) )') }
|
35
|
+
it{ expect(((:y - :x) - :y).to_s).to eq('( - x )') }
|
36
|
+
it{ expect(((:y * :x) * :y).to_s).to eq('( ( y ^ 2 ) * x )') }
|
37
|
+
it{ expect(((:x * :y) * :y).to_s).to eq('( ( y ^ 2 ) * x )') }
|
38
|
+
it{ expect(((:x / :y) / :y).to_s).to eq('( x / ( y ^ 2 ) )') }
|
39
|
+
it{ expect(((:y / :x) / :y).to_s).to eq('( 1 / x )') }
|
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
|
+
|
50
|
+
it{ expect(((:x * 2) ^ 2).to_s).to eq('( ( x ^ 2 ) * 4 )') }
|
51
|
+
it{ expect(((:x / 2) ^ 2).to_s).to eq('( ( x ^ 2 ) / 4 )') }
|
59
52
|
|
60
53
|
# TODO:
|
61
|
-
it
|
62
|
-
it
|
54
|
+
it{ expect((2 ^ (:x * 2)).to_s).to eq('( 2 ^ ( x * 2 ) )') }
|
55
|
+
it{ expect((2 ^ (:x / 2)).to_s).to eq('( 2 ^ ( x / 2 ) )') }
|
63
56
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Dydx::Algebra::Operator::Parts::Inverse do
|
4
|
-
it
|
5
|
-
it
|
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
|
8
|
-
it
|
7
|
+
it{ expect(inverse(:x, :+)).to eq(inverse(:x, :+)) }
|
8
|
+
it{ expect(inverse(:x, :*)).to eq(inverse(:x, :*)) }
|
9
9
|
|
10
|
-
it
|
11
|
-
it
|
12
|
-
it
|
13
|
-
it
|
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) }
|
14
14
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Dydx::Algebra::Set::Cos do
|
4
|
+
it{ expect(cos(0).to_s).to eq('1') }
|
5
|
+
it{ expect(cos(pi).to_s).to eq('-1') }
|
6
|
+
it{ expect(cos(2 * pi).to_s).to eq('1') }
|
7
|
+
|
8
|
+
describe '#to_s' do
|
9
|
+
it{ expect(cos(:x).to_s).to eq('cos( x )') }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#differentiate' do
|
13
|
+
it{ expect(cos(:x).d.to_s).to eq('( - sin( x ) )') }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'Calculate' do
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Dydx::Algebra::Set::E do
|
4
|
+
it{ expect(e).to eq(e) }
|
5
|
+
|
6
|
+
describe '#to_s' do
|
7
|
+
it{ expect(e.to_s).to eq('e') }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#differentiate' do
|
11
|
+
it{ expect(e.d(:x).to_s).to eq(_(0).to_s) }
|
12
|
+
it{ expect((e ^ :x).d(:x).to_s).to eq('( e ^ x )') }
|
13
|
+
it{ expect((e ^ (:x + :y)).d(:x).to_s).to eq('( e ^ ( x + y ) )') }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'Calculate' do
|
17
|
+
context 'With Fixnum' do
|
18
|
+
it{ expect(e + 0).to eq(e) }
|
19
|
+
it{ expect(e - 0).to eq(e) }
|
20
|
+
it{ expect((e * 0).to_s).to eq('0') }
|
21
|
+
it{ expect(e * 1).to eq(e) }
|
22
|
+
it{ expect{(e / 0).to_s}.to raise_error(ZeroDivisionError) }
|
23
|
+
it{ expect(e / 1).to eq(e) }
|
24
|
+
it{ expect((e ^ 0).to_s).to eq('1') }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fixnum do
|
4
|
+
describe '#to_s' do
|
5
|
+
it{ expect(1.to_s).to eq('1') }
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#differentiate' do
|
9
|
+
it{ expect(3.d(:x).to_s).to eq('0') }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'Calculate' do
|
13
|
+
context 'With Formula' do
|
14
|
+
let(:formula) { (:x + :y) }
|
15
|
+
it{ expect((0 + formula).to_s).to eq(formula.to_s) }
|
16
|
+
it{ expect((0 - formula).to_s).to eq('( - ( x + y ) )') }
|
17
|
+
it{ expect((0 * formula).to_s).to eq('0') }
|
18
|
+
it{ expect((1 * formula).to_s).to eq(formula.to_s) }
|
19
|
+
it{ expect((0 / formula).to_s).to eq('0') }
|
20
|
+
it{ expect((1 / formula).to_s).to eq('( 1 / ( x + y ) )') }
|
21
|
+
it{ expect((0 ^ formula).to_s).to eq('0') }
|
22
|
+
it{ expect((1 ^ formula).to_s).to eq('1') }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'With Symbol' do
|
26
|
+
it{ expect(0 + :x).to eq(:x) }
|
27
|
+
it{ expect((0 - :x).to_s).to eq('( - x )') }
|
28
|
+
it{ expect((0 * :x).to_s).to eq('0') }
|
29
|
+
it{ expect(1 * :x).to eq(:x) }
|
30
|
+
it{ expect((0 / :x).to_s).to eq('0') }
|
31
|
+
it{ expect((1 / :x).to_s).to eq('( 1 / x )') }
|
32
|
+
it{ expect((0 ^ :x).to_s).to eq('0') }
|
33
|
+
it{ expect((1 ^ :x).to_s).to eq('1') }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'With Fixnum' do
|
37
|
+
it{ expect(0 + 3).to eq(3) }
|
38
|
+
it{ expect(3 + 0).to eq(3) }
|
39
|
+
it{ expect(2 + 3).to eq(5) }
|
40
|
+
|
41
|
+
it{ expect(0 - 3).to eq(-3) }
|
42
|
+
it{ expect(3 - 0).to eq(3) }
|
43
|
+
it{ expect(2 - 3).to eq(-1) }
|
44
|
+
|
45
|
+
it{ expect(0 * 3).to eq(0) }
|
46
|
+
it{ expect(3 * 0).to eq(0) }
|
47
|
+
it{ expect(1 * 3).to eq(3) }
|
48
|
+
it{ expect(3 * 1).to eq(3) }
|
49
|
+
it{ expect(3 * 2).to eq(6) }
|
50
|
+
|
51
|
+
it{ expect((0 / 3).to_s).to eq('0') }
|
52
|
+
it{ expect{(3 / 0).to_s}.to raise_error(ZeroDivisionError) }
|
53
|
+
it{ expect((3 / 1).to_s).to eq('3') }
|
54
|
+
# TODO:
|
55
|
+
it{ expect((2 / 3).to_s).to eq('0') }
|
56
|
+
|
57
|
+
|
58
|
+
it{ expect((0 ^ 3).to_s).to eq('0') }
|
59
|
+
it{ expect((3 ^ 0).to_s).to eq('1') }
|
60
|
+
it{ expect((1 ^ 3).to_s).to eq('1') }
|
61
|
+
it{ expect((3 ^ 1).to_s).to eq('3') }
|
62
|
+
it{ expect((3 ^ 2).to_s).to eq('9') }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Float do
|
4
|
+
describe '#to_s' do
|
5
|
+
it{ expect(1.0.to_s).to eq('1.0') }
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#differentiate' do
|
9
|
+
it{ expect(3.0.d(:x).to_s).to eq('0') }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'Calculate' do
|
13
|
+
context 'With Formula' do
|
14
|
+
let(:formula) { (:x + :y) }
|
15
|
+
it{ expect((0.0 + formula).to_s).to eq(formula.to_s) }
|
16
|
+
it{ expect((0.0 - formula).to_s).to eq('( - ( x + y ) )') }
|
17
|
+
it{ expect((0.0 * formula).to_s).to eq('0') }
|
18
|
+
it{ expect((1.0 * formula).to_s).to eq(formula.to_s) }
|
19
|
+
it{ expect((0.0 / formula).to_s).to eq('0') }
|
20
|
+
it{ expect((1.0 / formula).to_s).to eq('( 1 / ( x + y ) )') }
|
21
|
+
it{ expect((0.0 ^ formula).to_s).to eq('0') }
|
22
|
+
it{ expect((1.0 ^ formula).to_s).to eq('1') }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'With Symbol' do
|
26
|
+
it{ expect(0.0 + :x).to eq(:x) }
|
27
|
+
it{ expect((0.0 - :x).to_s).to eq('( - x )') }
|
28
|
+
it{ expect((0.0 * :x).to_s).to eq('0') }
|
29
|
+
it{ expect(1.0 * :x).to eq(:x) }
|
30
|
+
it{ expect((0.0 / :x).to_s).to eq('0') }
|
31
|
+
it{ expect((1.0 / :x).to_s).to eq('( 1 / x )') }
|
32
|
+
it{ expect((0.0 ^ :x).to_s).to eq('0') }
|
33
|
+
it{ expect((1.0 ^ :x).to_s).to eq('1') }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'With Float' do
|
37
|
+
it{ expect(0.0 + 3.0).to eq(3.0) }
|
38
|
+
it{ expect(3.0 + 0.0).to eq(3.0) }
|
39
|
+
it{ expect(2.0 + 3.0).to eq(5.0) }
|
40
|
+
|
41
|
+
it{ expect(0.0 - 3.0).to eq(-3.0) }
|
42
|
+
it{ expect(3.0 - 0.0).to eq(3.0) }
|
43
|
+
it{ expect(2.0 - 3.0).to eq(-1.0) }
|
44
|
+
|
45
|
+
it{ expect(0.0 * 3.0).to eq(0.0) }
|
46
|
+
it{ expect(3.0 * 0.0).to eq(0.0) }
|
47
|
+
it{ expect(1.0 * 3.0).to eq(3.0) }
|
48
|
+
it{ expect(3.0 * 1.0).to eq(3.0) }
|
49
|
+
it{ expect(3.0 * 2.0).to eq(6.0) }
|
50
|
+
|
51
|
+
it{ expect(0.0 / 3.0).to eq(0.0) }
|
52
|
+
it{ expect(3.0 / 0.0).to eq(oo) }
|
53
|
+
it{ expect(3.0 / 1.0).to eq(3.0) }
|
54
|
+
# TODO:
|
55
|
+
it{ expect(2.0 / 3.0).to eq(0.6666666666666666) }
|
56
|
+
|
57
|
+
|
58
|
+
it{ expect(0.0 ^ 3.0).to eq(0.0) }
|
59
|
+
it{ expect(3.0 ^ 0.0).to eq(1.0) }
|
60
|
+
it{ expect(1.0 ^ 3.0).to eq(1.0) }
|
61
|
+
it{ expect(3.0 ^ 1.0).to eq(3.0) }
|
62
|
+
it{ expect(3.0 ^ 2.0).to eq(9.0) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|