dydx 0.1.4 → 0.1.25

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 +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
data/lib/dydx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dydx
2
- VERSION = '0.1.4'
2
+ VERSION = "0.1.25"
3
3
  end
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
- it 'demo' do
9
- f(x, y) <= x + x * y + y
10
- expect(f(x, y)).to eq(x * (1 + y) + y)
11
- expect(f(a, 2)).to eq(3 * a + 2)
12
- expect(f(1, a + b)).to eq(1 + 2 * ( a + b ))
13
- expect(f(1, 2)).to eq(5)
14
- expect(d/dx(f(x, y))).to eq(1 + y)
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
- g(x) <= sin(x)
17
- expect(d/dx(g(x))).to eq(cos(x))
18
- expect(S(g(x), dx)[0, pi / 2]).to eq(1.0)
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) { (x + y) }
5
- let(:subtraction) { (x - y) }
6
- let(:multiplication) { (x * y) }
7
- let(:division) { (x / y) }
8
- let(:exponentiation) { (x ** y) }
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 { 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) }
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 { 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 ) )') }
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 { expect(addition.d(x)).to eq(1) }
39
- it { expect(addition.d(y)).to eq(1) }
40
- it { expect(addition.d(z)).to eq(0) }
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 { expect(subtraction.d(x)).to eq(1) }
43
- it { expect(subtraction.d(y)).to eq(-1) }
44
- it { expect(subtraction.d(z)).to eq(0) }
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 { expect(multiplication.d(x)).to eq(y) }
47
- it { expect(multiplication.d(y)).to eq(x) }
48
- it { expect(multiplication.d(z)).to eq(0) }
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 { expect(division.d(x)).to eq(1 / y) }
51
- it { expect(division.d(y)).to eq(- ( x / y ** 2 ) ) }
52
- it { expect(division.d(z)).to eq(0) }
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 { expect(exponentiation.d(x)).to eq(y * x ** ( y - 1 )) }
55
- it { expect(exponentiation.d(y)).to eq(x ** y * log(x)) }
56
- it { expect(exponentiation.d(z)).to eq(0) }
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 { expect(addition.include?(x)).to be true }
61
- it { expect(addition.include?(z)).to be false }
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 { expect((x + y).openable?(:*, x)).to be true }
66
- it { expect((x + y).openable?(:*, y)).to be true }
67
- it { expect((x + y).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 }
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 { 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
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 { expect(a * b + a * c).to eq('( a * ( b + c ) )') }
5
- it { expect(a * b + c * a).to eq('( a * ( b + c ) )') }
6
- it { expect(b * a + c * a).to eq('( a * ( b + c ) )') }
7
- it { expect(b * a + a * c).to eq('( a * ( b + c ) )') }
8
- it { expect(a * b - a * c).to eq('( a * ( b - c ) )') }
9
- it { expect(a * b - c * a).to eq('( a * ( b - c ) )') }
10
- it { expect(b * a - c * a).to eq('( a * ( b - c ) )') }
11
- it { expect(b * a - a * c).to eq('( a * ( b - c ) )') }
12
-
13
- it { expect(a ** b * a ** c).to eq('( a ** ( b + c ) )') }
14
- it { expect(a ** b * c ** a).to eq('( ( a ** b ) * ( c ** a ) )') }
15
- it { expect(b ** a * c ** a).to eq('( ( b * c ) ** a )') }
16
- it { expect(b ** a * a ** c).to eq('( ( b ** a ) * ( a ** c ) )') }
17
- it { expect(a ** b / a ** c).to eq('( a ** ( b - c ) )') }
18
- it { expect(a ** b / c ** a).to eq('( ( a ** b ) / ( c ** a ) )') }
19
- it { expect(b ** a / c ** a).to eq('( ( b / c ) ** a )') }
20
- it { expect(b ** a / a ** c).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 eq('( ( 2 * y ) + x )') }
33
- it { expect((y + x) + y).to eq('( ( 2 * y ) + x )') }
34
- it { expect((x - y) - y).to eq('( x - ( 2 * y ) )') }
35
- it { expect((y - x) - y).to eq('( - x )') }
36
- it { expect((y * x) * y).to eq('( ( y ** 2 ) * x )') }
37
- it { expect((x * y) * y).to eq('( ( y ** 2 ) * x )') }
38
- it { expect((x / y) / y).to eq('( x / ( y ** 2 ) )') }
39
- it { expect((y / x) / y).to eq('( 1 / x )') }
40
-
41
- it { expect(y + (x + y)).to eq('( ( 2 * y ) + x )') }
42
- it { expect(y + (y + x)).to eq('( ( 2 * y ) + x )') }
43
- it { expect(y - (x - y)).to eq('( ( 2 * y ) - x )') }
44
- it { expect(y - (y - x)).to eq(x) }
45
- it { expect(y * (y * x)).to eq('( ( y ** 2 ) * x )') }
46
- it { expect(y * (x * y)).to eq('( ( y ** 2 ) * x )') }
47
- it { expect(y - (x - y)).to eq('( ( 2 * y ) - x )') }
48
- it { expect(y - (y - x)).to eq(x) }
49
-
50
- it { expect((x + 3) * 2).to eq(x * 2 + 6) }
51
- # it { expect((x - 3) * 2).to eq(x * 2 - 6) }
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 { expect((2 ** (:x * 2)).to_s).to eq('( 2 ** ( 2 * x ) )') }
62
- it { expect((2 ** (:x / 2)).to_s).to eq('( 2 ** ( x / 2 ) )') }
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 { 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(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) }
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