dydx 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dydx.rb +1 -1
- data/lib/dydx/algebra.rb +33 -11
- data/lib/dydx/algebra/set.rb +4 -0
- data/lib/dydx/helper.rb +2 -2
- data/lib/dydx/integrand.rb +5 -1
- data/lib/dydx/version.rb +1 -1
- data/spec/lib/algebra/set/fixnum_spec.rb +1 -1
- data/spec/lib/algebra/set/float_spec.rb +65 -0
- data/spec/lib/integrand_spec.rb +18 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 895af6b98a1440dd693b35dd9e697ee6838f3a23
|
4
|
+
data.tar.gz: 5f987b396090e719f7aa22a524db87f34299b8af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88dbb76bf4c5927fee44379a563ace65744db1e39b9215a47f0f94cde888756ce34e203b30a60a02a47f8f2a4d66879c9d5a6d0987c3f4986b1e00cbcfd275e2
|
7
|
+
data.tar.gz: 7e879f730952367fd530e3f356292f17c45913eba3aaab1b8989951924da4772b8c1173e78486ee0636045d75e5b9c50d914bfcb3731882ef4497d4e02fb83b6
|
data/lib/dydx.rb
CHANGED
data/lib/dydx/algebra.rb
CHANGED
@@ -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
|
-
(
|
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
|
-
|
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?(
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
data/lib/dydx/algebra/set.rb
CHANGED
data/lib/dydx/helper.rb
CHANGED
@@ -47,11 +47,11 @@ module Dydx
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def is_1?
|
50
|
-
|
50
|
+
[1, 1.0].include?(self) || (is_a?(Num) && n.is_1?)
|
51
51
|
end
|
52
52
|
|
53
53
|
def is_minus1?
|
54
|
-
|
54
|
+
[1, -1.0].include?(self)|| (is_a?(Num) && n.is_minus1?)
|
55
55
|
end
|
56
56
|
|
57
57
|
def distributive?(ope1, ope2)
|
data/lib/dydx/integrand.rb
CHANGED
@@ -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) )
|
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
|
data/lib/dydx/version.rb
CHANGED
@@ -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(
|
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
|
data/spec/lib/integrand_spec.rb
CHANGED
@@ -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(-
|
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(
|
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.
|
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
|