dydx 0.1.0 → 0.1.1

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: 6de4b80471ef9ac33664da33ff3f4a17a4c02714
4
- data.tar.gz: e367d55b3bd7f8a59c1f7a0168f5a37e4d4c4881
3
+ metadata.gz: 895af6b98a1440dd693b35dd9e697ee6838f3a23
4
+ data.tar.gz: 5f987b396090e719f7aa22a524db87f34299b8af
5
5
  SHA512:
6
- metadata.gz: ff580a632c292dc3bed05f880fc641d2308d96237e5c268d6d8a2f2353c0c7c520dc5422e810a39d572bd8ef33374d75f34a1f4615a32d393754a5cd0e108995
7
- data.tar.gz: 8fcfb84b585244622fa596cdb08c2e0419af96d8e5bc384be9a3793e24ace35ad13474edc57301bbb53f7fd5664e0fe8226fd17f59cf20e5368bb1f443c14b3d
6
+ metadata.gz: 88dbb76bf4c5927fee44379a563ace65744db1e39b9215a47f0f94cde888756ce34e203b30a60a02a47f8f2a4d66879c9d5a6d0987c3f4986b1e00cbcfd275e2
7
+ data.tar.gz: 7e879f730952367fd530e3f356292f17c45913eba3aaab1b8989951924da4772b8c1173e78486ee0636045d75e5b9c50d914bfcb3731882ef4497d4e02fb83b6
@@ -18,7 +18,7 @@ module Dydx
18
18
  .gsub('sin', 'Math.sin')
19
19
  .gsub('log', 'Math.log')
20
20
  .gsub('e', 'Math::E')
21
- .gsub('pi', 'Math::Pi')
21
+ .gsub('pi', 'Math::PI')
22
22
  function.vars.each_with_index do |var, i|
23
23
  string.gsub!(var.to_s, vars[i].to_s)
24
24
  end
@@ -16,6 +16,18 @@ module Dydx
16
16
  # TODO: Refactor
17
17
  Symbol.class_eval{ include Operator::Symbol }
18
18
  Fixnum.class_eval do
19
+ alias_method :addition, :+
20
+ alias_method :subtraction, :-
21
+ alias_method :multiplication, :*
22
+ alias_method :division, :/
23
+ alias_method :exponentiation, :**
24
+ ope_to_str = {
25
+ addition: :+,
26
+ subtraction: :-,
27
+ multiplication: :*,
28
+ division: :/,
29
+ exponentiation: :^
30
+ }
19
31
  %w(+ - * / ^).each do |operator|
20
32
  define_method(operator) do |g|
21
33
  if g.is_a?(Symbol) ||
@@ -23,25 +35,35 @@ module Dydx
23
35
  g.is_a?(Base)
24
36
 
25
37
  Num.new(self).send(operator.to_sym, g)
26
- elsif operator == '^' && g.is_a?(Fixnum) || g.is_a?(Float)
27
- self ** g
28
- elsif operator == '^' && g.is_a?(Num)
29
- self ** g.n
30
38
  else
31
- (to_f.send(operator.to_sym, g)).to_i
39
+ send(ope_to_str.key(operator.to_sym), g)
32
40
  end
33
41
  end
34
42
  end
35
43
  end
44
+
36
45
  Float.class_eval do
37
- %w(^).each do |operator|
46
+ alias_method :addition, :+
47
+ alias_method :subtraction, :-
48
+ alias_method :multiplication, :*
49
+ alias_method :division, :/
50
+ alias_method :exponentiation, :**
51
+ ope_to_str = {
52
+ addition: :+,
53
+ subtraction: :-,
54
+ multiplication: :*,
55
+ division: :/,
56
+ exponentiation: :^
57
+ }
58
+ %w(+ - * / ^).each do |operator|
38
59
  define_method(operator) do |g|
39
- if g.is_a?(Fixnum) || g.is_a?(Float)
40
- self ** g
41
- elsif g.is_a?(Num)
42
- self ** g.n
43
- else
60
+ if g.is_a?(Symbol) ||
61
+ g.is_a?(Formula) ||
62
+ g.is_a?(Base)
63
+
44
64
  Num.new(self).send(operator.to_sym, g)
65
+ else
66
+ send(ope_to_str.key(operator.to_sym), g)
45
67
  end
46
68
  end
47
69
  end
@@ -36,6 +36,10 @@ module Dydx
36
36
  $e ||= E.new
37
37
  end
38
38
 
39
+ def oo
40
+ Float::INFINITY
41
+ end
42
+
39
43
  def log(formula)
40
44
  if formula.multiplication?
41
45
  f, g = formula.f, formula.g
@@ -47,11 +47,11 @@ module Dydx
47
47
  end
48
48
 
49
49
  def is_1?
50
- self == 1 || (is_a?(Num) && n == 1)
50
+ [1, 1.0].include?(self) || (is_a?(Num) && n.is_1?)
51
51
  end
52
52
 
53
53
  def is_minus1?
54
- self == -1 || (is_a?(Num) && n == -1)
54
+ [1, -1.0].include?(self)|| (is_a?(Num) && n.is_minus1?)
55
55
  end
56
56
 
57
57
  def distributive?(ope1, ope2)
@@ -10,11 +10,15 @@ module Dydx
10
10
  f = function
11
11
  a, b = [a, b].map(&:to_f)
12
12
  raise ArgumentError, 'b should be greater than a' if a > b
13
+ # HOT FIX: should implement Infinity class
14
+ a = - 1000 if a == - Float::INFINITY
15
+ b = 1000 if b == Float::INFINITY
16
+
13
17
  h = (b - a) / n
14
18
  sum = 0.0
15
19
  xi = ->(i){ a + h * i }
16
20
  n.to_i.times do |i|
17
- sum += ( f(xi.(i)) + 4.0 * f(xi.(i) + h / 2.0 ) + f(xi.(i) + h) ) rescue binding.pry
21
+ sum += ( f(xi.(i)) + 4.0 * f(xi.(i) + h / 2.0 ) + f(xi.(i) + h) )
18
22
  end
19
23
  ( h * sum ) / 6.0
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module Dydx
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -49,7 +49,7 @@ describe Fixnum do
49
49
  it{ expect(3 * 2).to eq(6) }
50
50
 
51
51
  it{ expect((0 / 3).to_s).to eq('0') }
52
- it{ expect{(3 / 0).to_s}.to raise_error(FloatDomainError) }
52
+ it{ expect{(3 / 0).to_s}.to raise_error(ZeroDivisionError) }
53
53
  it{ expect((3 / 1).to_s).to eq('3') }
54
54
  # TODO:
55
55
  it{ expect((2 / 3).to_s).to eq('0') }
@@ -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
@@ -30,13 +30,27 @@ describe Dydx:Integrand do
30
30
  it 'ex5' do
31
31
  $f = nil
32
32
  f(x) <= log(x)
33
- expect(S(f(x), dx)[0, 1]).to eq(- Float::INFINITY)
33
+ expect(S(f(x), dx)[0, 1]).to eq(-oo)
34
34
  end
35
35
 
36
- # TODO
37
36
  it 'ex6' do
38
37
  $f = nil
39
- f(x) <= e ^ (- (x^2))
40
- expect(S(f(x), dx)[-100, 100]).to eq(1.672428604536991)
38
+ f(x) <= e ^ (- (x ^ 2))
39
+ expect(f(0)).to eq(1)
40
+ expect(f(1)).to eq(1.0/Math::E)
41
+ expect(f(1000)).to eq(0)
42
+ expect(S(f(x), dx)[-1000, 1000, 3000]).to eq(1.7724538506374117)
43
+ end
44
+
45
+ it 'ex7' do
46
+ $f = nil
47
+ f(x) <= (1.0 / ( ( 2.0 * Math::PI ) ^ 0.5 ) ) * ( e ^ (- (x ^ 2) / 2) )
48
+ expect(S(f(x), dx)[-1000, 1000, 1000]).to eq(0.9952054164466917)
49
+ end
50
+
51
+ it 'ex8' do
52
+ $f = nil
53
+ f(x) <= (1.0 / ( ( 2.0 * pi ) ^ 0.5 ) ) * ( e ^ (- (x ^ 2) / 2) )
54
+ expect(S(f(x), dx)[-oo, oo, 1000]).to eq(0.9952054164466917)
41
55
  end
42
56
  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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
@@ -110,6 +110,7 @@ files:
110
110
  - spec/lib/algebra/set/cos_spec.rb
111
111
  - spec/lib/algebra/set/e_spec.rb
112
112
  - spec/lib/algebra/set/fixnum_spec.rb
113
+ - spec/lib/algebra/set/float_spec.rb
113
114
  - spec/lib/algebra/set/log_spec.rb
114
115
  - spec/lib/algebra/set/num_spec.rb
115
116
  - spec/lib/algebra/set/pi_spec.rb
@@ -154,6 +155,7 @@ test_files:
154
155
  - spec/lib/algebra/set/cos_spec.rb
155
156
  - spec/lib/algebra/set/e_spec.rb
156
157
  - spec/lib/algebra/set/fixnum_spec.rb
158
+ - spec/lib/algebra/set/float_spec.rb
157
159
  - spec/lib/algebra/set/log_spec.rb
158
160
  - spec/lib/algebra/set/num_spec.rb
159
161
  - spec/lib/algebra/set/pi_spec.rb