dydx 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/dydx/algebra.rb +13 -0
- data/lib/dydx/algebra/formula.rb +19 -6
- data/lib/dydx/algebra/inverse.rb +36 -0
- data/lib/dydx/algebra/operator/inverse.rb +15 -0
- data/lib/dydx/algebra/operator/parts/base.rb +22 -8
- data/lib/dydx/algebra/operator/parts/formula.rb +77 -43
- data/lib/dydx/algebra/operator/parts/general.rb +4 -18
- data/lib/dydx/algebra/operator/parts/interface.rb +10 -2
- data/lib/dydx/algebra/operator/parts/inverse.rb +36 -0
- data/lib/dydx/algebra/operator/parts/num.rb +4 -16
- data/lib/dydx/algebra/operator/parts/symbol.rb +0 -10
- data/lib/dydx/algebra/set.rb +7 -0
- data/lib/dydx/helper.rb +77 -10
- data/lib/dydx/version.rb +1 -1
- data/spec/dydx_spec.rb +8 -0
- data/spec/lib/algebra/formula_spec.rb +10 -5
- data/spec/lib/algebra/operator/parts/formula_spec.rb +5 -2
- data/spec/lib/algebra/operator/parts/inverse_spec.rb +14 -0
- data/spec/lib/algebra/set/num_spec.rb +2 -0
- data/spec/lib/helper_spec.rb +33 -15
- metadata +21 -30
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07dd79c6718aec02bcbe4274cedadc700d694743
|
4
|
+
data.tar.gz: f94746b938d94132d40a24cbb65087f2f2a28ff9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea77ed0736af837a9294b2baa9f6d63d50aa8d2dcd146a17e55fa9ad3fcf49574ba30c10ffb2998618c88a1cf6e0b7e652b8b5d406d514477b971aa7059664d7
|
7
|
+
data.tar.gz: 92c7a5a3fd45764282792b665591f5016a796a2466d0ea5388136f3d80259877a62ff7ec497b5f1eeaaa40605da443b3a3eb056421770784e7a22df3f287e248
|
data/lib/dydx/algebra.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'dydx/algebra/formula'
|
2
|
+
require 'dydx/algebra/inverse'
|
2
3
|
|
3
4
|
require 'dydx/algebra/set'
|
4
5
|
|
6
|
+
require 'dydx/algebra/operator/inverse'
|
5
7
|
require 'dydx/algebra/operator/formula'
|
6
8
|
require 'dydx/algebra/operator/symbol'
|
7
9
|
require 'dydx/algebra/operator/num'
|
@@ -39,5 +41,16 @@ module Dydx
|
|
39
41
|
class Tan; include Operator::General; end
|
40
42
|
end
|
41
43
|
class Formula; include Operator::Formula; end
|
44
|
+
class Inverse; include Operator::Inverse; end
|
45
|
+
|
46
|
+
def inverse(x, operator)
|
47
|
+
if operator == :+ && x.is_0?
|
48
|
+
e0
|
49
|
+
elsif operator == :* && x.is_1?
|
50
|
+
e1
|
51
|
+
else
|
52
|
+
Inverse.new(x, operator)
|
53
|
+
end
|
54
|
+
end
|
42
55
|
end
|
43
56
|
end
|
data/lib/dydx/algebra/formula.rb
CHANGED
@@ -12,16 +12,14 @@ module Dydx
|
|
12
12
|
case @operator
|
13
13
|
when :+
|
14
14
|
f.d(sym) + g.d(sym)
|
15
|
-
when :-
|
16
|
-
f.d(sym) - g.d(sym)
|
17
15
|
when :*
|
18
16
|
(f.d(sym) * g) + (f * g.d(sym))
|
19
|
-
when :/
|
20
|
-
((f.d(sym) * g) - (f * g.d(sym)))/(g ^ _(2))
|
21
17
|
when :^
|
22
18
|
# TODO:
|
23
19
|
if f == sym
|
24
20
|
g * (f ^ (g - 1))
|
21
|
+
elsif f == e
|
22
|
+
g.d(sym) * self
|
25
23
|
else
|
26
24
|
self * (g * log(f)).d(sym)
|
27
25
|
end
|
@@ -30,9 +28,16 @@ module Dydx
|
|
30
28
|
alias_method :d, :differentiate
|
31
29
|
|
32
30
|
def to_s
|
33
|
-
if (
|
34
|
-
(multiplication? && (f.is_minus1? || g.is_minus1?) )
|
31
|
+
if (multiplication? && (f.is_minus1? || g.is_minus1?) )
|
35
32
|
"( - #{g.to_s} )"
|
33
|
+
elsif multiplication? && g.divisor?
|
34
|
+
"( #{f.to_s} / #{g.x.to_s} )"
|
35
|
+
elsif multiplication? && f.divisor?
|
36
|
+
"( #{g.to_s} / #{f.x.to_s} )"
|
37
|
+
elsif addition? && g.subtrahend?
|
38
|
+
"( #{f.to_s} - #{g.x.to_s} )"
|
39
|
+
elsif addition? && f.subtrahend?
|
40
|
+
"( #{g.to_s} - #{f.x.to_s} )"
|
36
41
|
else
|
37
42
|
"( #{f.to_s} #{@operator} #{g.to_s} )"
|
38
43
|
end
|
@@ -41,6 +46,14 @@ module Dydx
|
|
41
46
|
def include?(x)
|
42
47
|
f == x || g == x
|
43
48
|
end
|
49
|
+
|
50
|
+
def openable?(x)
|
51
|
+
x.is_num? && (f.is_num? || g.is_num?)
|
52
|
+
end
|
53
|
+
|
54
|
+
def ==(x)
|
55
|
+
to_s == x.to_s
|
56
|
+
end
|
44
57
|
end
|
45
58
|
end
|
46
59
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
class Inverse
|
4
|
+
include Helper
|
5
|
+
attr_accessor :x, :operator
|
6
|
+
|
7
|
+
def initialize(x, operator)
|
8
|
+
@x, @operator = x, operator
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
# sym = {'*'=>'/', '+'=>'-'}[operator.to_s]
|
13
|
+
case operator
|
14
|
+
when :+
|
15
|
+
"( - #{x} )"
|
16
|
+
when :*
|
17
|
+
"( 1 / #{x} )"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def differentiate(sym=:x)
|
22
|
+
case operator
|
23
|
+
when :+
|
24
|
+
inverse(x.differentiate(sym), :+)
|
25
|
+
when :*
|
26
|
+
inverse(x.differentiate(sym) * inverse(x ^ 2, :*), :+)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias_method :d, :differentiate
|
30
|
+
|
31
|
+
def ==(x)
|
32
|
+
to_s == x.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/inverse'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module Inverse
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::Inverse
|
10
|
+
include Parts::General
|
11
|
+
include Parts::Interface
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -3,24 +3,38 @@ module Dydx
|
|
3
3
|
module Operator
|
4
4
|
module Parts
|
5
5
|
module Base
|
6
|
-
%w(+
|
6
|
+
%w(+ * ^).map(&:to_sym).each do |operator|
|
7
7
|
define_method(operator) do |x|
|
8
|
-
if self == x && operator !=
|
8
|
+
if self == x && operator != :^
|
9
9
|
case operator
|
10
|
-
when
|
10
|
+
when :+
|
11
11
|
_(2) * self
|
12
|
-
when
|
13
|
-
_(0)
|
14
|
-
when '*'
|
12
|
+
when :*
|
15
13
|
self ^ _(2)
|
16
|
-
when '/'
|
17
|
-
_(1)
|
18
14
|
end
|
15
|
+
elsif %w(+ *).map(&:to_sym).include?(operator) && x.send("#{to_str(operator)}?")
|
16
|
+
if combinable?(x.f, operator)
|
17
|
+
send(operator, x.f).send(operator, x.g)
|
18
|
+
elsif combinable?(x.g, operator)
|
19
|
+
send(operator, x.g).send(operator, x.f)
|
20
|
+
else
|
21
|
+
::Algebra::Formula.new(self, x, operator.to_sym)
|
22
|
+
end
|
23
|
+
elsif x.subtrahend? && %w(* ^).map(&:to_sym).include?(operator)
|
24
|
+
inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
|
19
25
|
else
|
20
26
|
::Algebra::Formula.new(self, x, operator.to_sym)
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
30
|
+
|
31
|
+
def to_str(operator)
|
32
|
+
{
|
33
|
+
addition: :+,
|
34
|
+
multiplication: :*,
|
35
|
+
exponentiation: :^
|
36
|
+
}.key(operator)
|
37
|
+
end
|
24
38
|
end
|
25
39
|
end
|
26
40
|
end
|
@@ -3,61 +3,95 @@ module Dydx
|
|
3
3
|
module Operator
|
4
4
|
module Parts
|
5
5
|
module Formula
|
6
|
-
%w(+
|
6
|
+
%w(+ *).map(&:to_sym).each do |operator|
|
7
7
|
define_method(operator) do |x|
|
8
|
-
if
|
9
|
-
if f
|
10
|
-
f
|
11
|
-
elsif g
|
12
|
-
|
8
|
+
if self.operator == operator
|
9
|
+
if f.combinable?(x, operator)
|
10
|
+
f.send(operator, x).send(operator, g)
|
11
|
+
elsif g.combinable?(x, operator)
|
12
|
+
g.send(operator, x).send(operator, f)
|
13
13
|
else
|
14
14
|
super(x)
|
15
15
|
end
|
16
|
-
elsif
|
17
|
-
if
|
18
|
-
|
19
|
-
|
20
|
-
f
|
16
|
+
elsif operator == :+
|
17
|
+
if multiplication? && x.multiplication?
|
18
|
+
if f == x.f
|
19
|
+
f * g.send(operator, x.g)
|
20
|
+
elsif f == x.g
|
21
|
+
f * g.send(operator, x.f)
|
22
|
+
elsif g == x.f
|
23
|
+
f.send(operator, x.g) * g
|
24
|
+
elsif g == x.g
|
25
|
+
f.send(operator, x.f) * g
|
26
|
+
else
|
27
|
+
super(x)
|
28
|
+
end
|
29
|
+
# expect(((:b * :a) - (:c * :a)).to_s).to eq('( ( b - c ) * a )')
|
30
|
+
elsif multiplication? && x.subtrahend? && x.x.multiplication?
|
31
|
+
if f == x.x.f
|
32
|
+
f * g.send(operator, inverse(x.x.g, :+))
|
33
|
+
elsif f == x.x.g
|
34
|
+
f * g.send(operator, inverse(x.x.f, :+))
|
35
|
+
elsif g == x.x.f
|
36
|
+
f.send(operator, inverse(x.x.g, :+)) * g
|
37
|
+
elsif g == x.x.g
|
38
|
+
f.send(operator, inverse(x.x.f, :+)) * g
|
39
|
+
else
|
40
|
+
super(x)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
super(x)
|
21
44
|
end
|
22
|
-
elsif
|
23
|
-
if
|
24
|
-
f
|
25
|
-
|
26
|
-
|
45
|
+
elsif operator == :*
|
46
|
+
if exponentiation? && x.exponentiation?
|
47
|
+
if f == x.f
|
48
|
+
f ^ g.send(:+, x.g)
|
49
|
+
elsif g == x.g
|
50
|
+
f.send(operator, x.f) ^ g
|
51
|
+
else
|
52
|
+
super(x)
|
53
|
+
end
|
54
|
+
elsif exponentiation? && x.divisor? && x.x.exponentiation?
|
55
|
+
if f == x.x.f
|
56
|
+
f ^ g.send(:-, x.x.g)
|
57
|
+
elsif g == x.x.g
|
58
|
+
f.send(:/, x.x.f) ^ g
|
59
|
+
else
|
60
|
+
super(x)
|
61
|
+
end
|
62
|
+
# x * inverse(:y, :+)
|
63
|
+
elsif x.subtrahend?
|
64
|
+
inverse(self * x.x, :+)
|
65
|
+
elsif multiplication?
|
66
|
+
if f.combinable?(x, operator)
|
67
|
+
f.send(operator, x).send(operator, g)
|
68
|
+
elsif g.combinable?(x, operator)
|
69
|
+
g.send(operator, x).send(operator, f)
|
70
|
+
else
|
71
|
+
super(x)
|
72
|
+
end
|
73
|
+
else
|
74
|
+
super(x)
|
27
75
|
end
|
28
|
-
else
|
29
|
-
super(x)
|
30
76
|
end
|
31
77
|
end
|
32
78
|
end
|
33
79
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
elsif g == x.g
|
40
|
-
f.send(operator, x.f) ^ g
|
41
|
-
else
|
42
|
-
super(x)
|
43
|
-
end
|
44
|
-
elsif ([self.operator, operator].sort == [:*, :/]) && include?(x)
|
45
|
-
if f == x
|
46
|
-
g
|
47
|
-
elsif g == x
|
48
|
-
f
|
49
|
-
end
|
50
|
-
elsif (self.operator == operator) && include?(x)
|
51
|
-
if f == x
|
52
|
-
f.send(operator, x).send(operator, g)
|
53
|
-
elsif g == x
|
54
|
-
f.send(operator, g.send(:* , x))
|
55
|
-
end
|
56
|
-
else
|
57
|
-
super(x)
|
58
|
-
end
|
80
|
+
def ^(x)
|
81
|
+
if multiplication? && openable?(x)
|
82
|
+
(f ^ x).send(self.operator, (g ^ x))
|
83
|
+
else
|
84
|
+
super(x)
|
59
85
|
end
|
60
86
|
end
|
87
|
+
|
88
|
+
def to_str(operator)
|
89
|
+
{
|
90
|
+
addition: :+,
|
91
|
+
multiplication: :*,
|
92
|
+
exponentiation: :^
|
93
|
+
}.key(operator)
|
94
|
+
end
|
61
95
|
end
|
62
96
|
end
|
63
97
|
end
|
@@ -6,14 +6,8 @@ module Dydx
|
|
6
6
|
def +(x)
|
7
7
|
if x.is_0?
|
8
8
|
self
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def -(x)
|
15
|
-
if x.is_0?
|
16
|
-
self
|
9
|
+
elsif inverse?(x, :+)
|
10
|
+
e0
|
17
11
|
else
|
18
12
|
super(x)
|
19
13
|
end
|
@@ -24,16 +18,8 @@ module Dydx
|
|
24
18
|
x
|
25
19
|
elsif x.is_1?
|
26
20
|
self
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def /(x)
|
33
|
-
if x.is_0?
|
34
|
-
raise ZeroDivisionError
|
35
|
-
elsif x.is_1?
|
36
|
-
self
|
21
|
+
elsif inverse?(x, :*)
|
22
|
+
e1
|
37
23
|
else
|
38
24
|
super(x)
|
39
25
|
end
|
@@ -3,10 +3,18 @@ module Dydx
|
|
3
3
|
module Operator
|
4
4
|
module Parts
|
5
5
|
module Interface
|
6
|
-
%w(+ - * / ^).each do |operator|
|
6
|
+
%w(+ - * / ^).map(&:to_sym).each do |operator|
|
7
7
|
define_method(operator) do |x|
|
8
8
|
x = ::Set::Num.new(x) if x.is_a?(Fixnum)
|
9
|
-
|
9
|
+
case operator
|
10
|
+
when :-
|
11
|
+
self + inverse(x, :+)
|
12
|
+
when :/
|
13
|
+
raise ZeroDivisionError if x.is_0?
|
14
|
+
self * inverse(x, :*)
|
15
|
+
else
|
16
|
+
super(x)
|
17
|
+
end
|
10
18
|
end
|
11
19
|
end
|
12
20
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module Inverse
|
6
|
+
def +(x)
|
7
|
+
if self.x == x && operator == :+
|
8
|
+
e0
|
9
|
+
elsif !x.is_a?(Inverse)
|
10
|
+
x + self
|
11
|
+
else
|
12
|
+
super(x)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def *(x)
|
17
|
+
if self.x == x && operator == :*
|
18
|
+
e1
|
19
|
+
else
|
20
|
+
super(x)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def ^(x)
|
25
|
+
case operator
|
26
|
+
when :+
|
27
|
+
super(x)
|
28
|
+
when :*
|
29
|
+
inverse(self.x ^ x, :*)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -8,14 +8,8 @@ module Dydx
|
|
8
8
|
x
|
9
9
|
elsif x.is_a?(Num)
|
10
10
|
_(n + x.n)
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def -(x)
|
17
|
-
if x.is_a?(Num)
|
18
|
-
_(n - x.n)
|
11
|
+
elsif x.subtrahend? && x.x.is_a?(Num)
|
12
|
+
_(n - x.x.n)
|
19
13
|
else
|
20
14
|
super(x)
|
21
15
|
end
|
@@ -28,14 +22,8 @@ module Dydx
|
|
28
22
|
x
|
29
23
|
elsif x.is_a?(Num)
|
30
24
|
_(n * x.n)
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def /(x)
|
37
|
-
if (n == 0)
|
38
|
-
self
|
25
|
+
elsif x.divisor? && x.x.is_a?(Num)
|
26
|
+
_(n / x.x.n)
|
39
27
|
else
|
40
28
|
super(x)
|
41
29
|
end
|
data/lib/dydx/algebra/set.rb
CHANGED
data/lib/dydx/helper.rb
CHANGED
@@ -1,17 +1,50 @@
|
|
1
1
|
module Dydx
|
2
2
|
module Helper
|
3
|
+
OP_SYM_STR = {
|
4
|
+
addition: :+,
|
5
|
+
subtraction: :-,
|
6
|
+
multiplication: :*,
|
7
|
+
exponentiation: :^
|
8
|
+
}
|
9
|
+
|
3
10
|
def is_0?
|
4
|
-
self == 0 || (is_a?(Num) && n == 0)
|
11
|
+
self == 0 || (is_a?(Num) && n == 0) || (subtrahend? && x.is_0?)
|
5
12
|
end
|
6
13
|
|
7
14
|
def is_1?
|
8
|
-
self == 1 || (is_a?(Num) && n == 1)
|
15
|
+
self == 1 || (is_a?(Num) && n == 1) || (divisor? && x.is_1?)
|
9
16
|
end
|
10
17
|
|
11
18
|
def is_minus1?
|
12
19
|
self == -1 || (is_a?(Num) && n == -1)
|
13
20
|
end
|
14
21
|
|
22
|
+
def is_num?
|
23
|
+
if is_a?(Inverse)
|
24
|
+
x.is_num?
|
25
|
+
else
|
26
|
+
is_a?(Num) || is_a?(Fixnum)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def combinable?(x, operator)
|
31
|
+
case operator
|
32
|
+
when :*
|
33
|
+
self == x ||
|
34
|
+
(is_num? && x.is_num?) ||
|
35
|
+
inverse?(x, :*)
|
36
|
+
when :+
|
37
|
+
like_term?(x)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def like_term?(x)
|
42
|
+
self == x ||
|
43
|
+
(is_num? && x.is_num?) ||
|
44
|
+
(multiplication? && (f == x || g == x)) ||
|
45
|
+
inverse?(x, :+)
|
46
|
+
end
|
47
|
+
|
15
48
|
def is_multiple_of(x)
|
16
49
|
is_multiple = if is_0?
|
17
50
|
_(0)
|
@@ -25,14 +58,48 @@ module Dydx
|
|
25
58
|
end
|
26
59
|
end
|
27
60
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
61
|
+
OP_SYM_STR.each do |operator_name, operator|
|
62
|
+
define_method("#{operator_name}?") do
|
63
|
+
(@operator == operator) && is_a?(Formula)
|
64
|
+
# is_a?(Inverse) && self.operator == operator
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_str(sym)
|
69
|
+
OP_SYM_STR.key(sym)
|
70
|
+
end
|
71
|
+
|
72
|
+
def str_to_sym(str)
|
73
|
+
OP_SYM_STR[str]
|
74
|
+
end
|
75
|
+
|
76
|
+
def super_ope(operator)
|
77
|
+
case operator
|
78
|
+
when :+
|
79
|
+
:*
|
80
|
+
when :-
|
81
|
+
:/
|
82
|
+
when :*
|
83
|
+
:^
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def inverse?(x, operator)
|
88
|
+
if is_a?(Algebra::Inverse)
|
89
|
+
self.operator == operator && self.x == x
|
90
|
+
elsif x.is_a?(Algebra::Inverse)
|
91
|
+
self == x.x
|
92
|
+
else
|
93
|
+
false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def subtrahend?
|
98
|
+
is_a?(Inverse) && operator == :+
|
99
|
+
end
|
100
|
+
|
101
|
+
def divisor?
|
102
|
+
is_a?(Inverse) && operator == :*
|
36
103
|
end
|
37
104
|
end
|
38
105
|
end
|
data/lib/dydx/version.rb
CHANGED
data/spec/dydx_spec.rb
CHANGED
@@ -28,4 +28,12 @@ describe Dydx do
|
|
28
28
|
it{ expect(d1.to_s).to eq('t') }
|
29
29
|
it{ expect(d2.to_s).to eq('t') }
|
30
30
|
end
|
31
|
+
|
32
|
+
context 'ex4' do
|
33
|
+
$f = 2 * (e ^ (2 * :z))
|
34
|
+
let(:d1){ df/dz }
|
35
|
+
let(:d2){ d/dz($f) }
|
36
|
+
it{ expect(d1.to_s).to eq('( 4 * ( e ^ ( 2 * z ) ) )') }
|
37
|
+
it{ expect(d2.to_s).to eq('( 4 * ( e ^ ( 2 * z ) ) )') }
|
38
|
+
end
|
31
39
|
end
|
@@ -9,8 +9,8 @@ describe Dydx::Algebra::Formula do
|
|
9
9
|
describe 'Calculate' do
|
10
10
|
context 'With Fixnum' do
|
11
11
|
let(:formula) { (:x + :y) }
|
12
|
-
it{ expect(
|
13
|
-
it{ expect(
|
12
|
+
it{ expect(formula + 0).to eq(formula + 0) }
|
13
|
+
it{ expect(formula - 0).to eq(formula - 0) }
|
14
14
|
it{ expect((formula * 0).to_s).to eq('0') }
|
15
15
|
it{ expect((formula * 1).to_s).to eq(formula.to_s) }
|
16
16
|
it{ expect{(formula / 0).to_s}.to raise_error(ZeroDivisionError) }
|
@@ -34,15 +34,15 @@ describe Dydx::Algebra::Formula do
|
|
34
34
|
it{ expect(addition.d(:z).to_s).to eq('0') }
|
35
35
|
|
36
36
|
it{ expect(subtraction.d(:x).to_s).to eq('1') }
|
37
|
-
it{ expect(subtraction.d(:y).to_s).to eq('-1') }
|
37
|
+
it{ expect(subtraction.d(:y).to_s).to eq('( - 1 )') }
|
38
38
|
it{ expect(subtraction.d(:z).to_s).to eq('0') }
|
39
39
|
|
40
40
|
it{ expect(multiplication.d(:x)).to eq(:y) }
|
41
41
|
it{ expect(multiplication.d(:y)).to eq(:x) }
|
42
42
|
it{ expect(multiplication.d(:z).to_s).to eq('0') }
|
43
43
|
|
44
|
-
it{ expect(division.d(:x).to_s).to eq("(
|
45
|
-
it{ expect(division.d(:y).to_s).to eq('( (
|
44
|
+
it{ expect(division.d(:x).to_s).to eq("( 1 / y )") }
|
45
|
+
it{ expect(division.d(:y).to_s).to eq('( - ( x / ( y ^ 2 ) ) )') }
|
46
46
|
it{ expect(division.d(:z).to_s).to eq('0') }
|
47
47
|
|
48
48
|
it{ expect(exponentiation.d(:x).to_s).to eq('( y * ( x ^ ( y - 1 ) ) )') }
|
@@ -54,4 +54,9 @@ describe Dydx::Algebra::Formula do
|
|
54
54
|
it{ expect(addition.include?(:x)).to be_true }
|
55
55
|
it{ expect(addition.include?(:z)).to be_false }
|
56
56
|
end
|
57
|
+
|
58
|
+
describe '#openable?' do
|
59
|
+
it{ expect((1 + :x).openable?(_(1))).to be_true }
|
60
|
+
it{ expect((1 + :x).openable?(:z)).to be_false }
|
61
|
+
end
|
57
62
|
end
|
@@ -16,10 +16,13 @@ describe Dydx::Algebra::Operator::Parts::Formula do
|
|
16
16
|
it{ expect(((:x * 2) / 2).to_s).to eq('x') }
|
17
17
|
it{ expect(((:x / 2) * 2).to_s).to eq('x') }
|
18
18
|
|
19
|
-
it{ expect(((:x + :y) + :y).to_s).to eq('(
|
19
|
+
it{ expect(((:x + :y) + :y).to_s).to eq('( ( 2 * y ) + x )') }
|
20
20
|
it{ expect(((:x - :y) - :y).to_s).to eq('( x - ( 2 * y ) )') }
|
21
21
|
it{ expect(((:y - :x) - :y).to_s).to eq('( - x )') }
|
22
|
-
it{ expect(((:x * :y) * :y).to_s).to eq('(
|
22
|
+
it{ expect(((:x * :y) * :y).to_s).to eq('( ( y ^ 2 ) * x )') }
|
23
23
|
it{ expect(((:x / :y) / :y).to_s).to eq('( x / ( y ^ 2 ) )') }
|
24
24
|
it{ expect(((:y / :x) / :y).to_s).to eq('( 1 / x )') }
|
25
|
+
|
26
|
+
it{ expect(((:x * 2) ^ 2).to_s).to eq('( ( x ^ 2 ) * 4 )') }
|
27
|
+
it{ expect(((:x / 2) ^ 2).to_s).to eq('( ( x ^ 2 ) / 4 )') }
|
25
28
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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 )') }
|
6
|
+
|
7
|
+
it{ expect(inverse(:x, :+)).to eq(inverse(:x, :+)) }
|
8
|
+
it{ expect(inverse(:x, :*)).to eq(inverse(:x, :*)) }
|
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) }
|
14
|
+
end
|
data/spec/lib/helper_spec.rb
CHANGED
@@ -1,22 +1,37 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Helper do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
context '#is_n?' do
|
5
|
+
it{ expect(0.is_0?).to be_true }
|
6
|
+
it{ expect(_(0).is_0?).to be_true }
|
7
|
+
it{ expect(inverse(0, :+).is_0?).to be_true }
|
8
|
+
it{ expect(1.is_1?).to be_true }
|
9
|
+
it{ expect(_(1).is_1?).to be_true }
|
10
|
+
it{ expect(inverse(1, :*).is_1?).to be_true }
|
11
|
+
it{ expect(-1.is_minus1?).to be_true }
|
12
|
+
it{ expect(_(-1).is_minus1?).to be_true }
|
13
|
+
end
|
10
14
|
|
11
|
-
|
12
|
-
|
15
|
+
context '#is_multiple_of' do
|
16
|
+
it{ expect(0.is_multiple_of(:x).to_s).to eq('0') }
|
17
|
+
it{ expect(_(0).is_multiple_of(:y).to_s).to eq('0')}
|
13
18
|
|
14
|
-
|
15
|
-
|
19
|
+
it{ expect(:x.is_multiple_of(:x).to_s).to eq('1') }
|
20
|
+
it{ expect(:x.is_multiple_of(:y)).to be_false }
|
16
21
|
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
it{ expect((:x * :y).is_multiple_of(:x)).to eq(:y) }
|
23
|
+
it{ expect((:x * :y).is_multiple_of(:y)).to eq(:x) }
|
24
|
+
it{ expect((:x * :y).is_multiple_of(:z)).to be_false }
|
25
|
+
end
|
26
|
+
|
27
|
+
context '#combinable?' do
|
28
|
+
it{ expect(:x.combinable?(:x, :+)).to be_true }
|
29
|
+
it{ expect(:x.combinable?(:y, :+)).to be_false }
|
30
|
+
it{ expect(:x.combinable?(:x, :*)).to be_true }
|
31
|
+
it{ expect(:x.combinable?(:y, :*)).to be_false }
|
32
|
+
it{ expect(1.combinable?(2, :+)).to be_true }
|
33
|
+
it{ expect(1.combinable?(2, :*)).to be_true }
|
34
|
+
end
|
20
35
|
|
21
36
|
let(:addition) { (:x + :y) }
|
22
37
|
let(:subtraction) { (:x - :y) }
|
@@ -25,8 +40,11 @@ describe Helper do
|
|
25
40
|
let(:exponentiation){ (:x ^ :y) }
|
26
41
|
|
27
42
|
it{ expect(addition.addition?).to be_true }
|
28
|
-
it{ expect(subtraction.subtraction?).to be_true }
|
29
43
|
it{ expect(multiplication.multiplication?).to be_true }
|
30
|
-
it{ expect(division.division?).to be_true }
|
31
44
|
it{ expect(exponentiation.exponentiation?).to be_true }
|
45
|
+
|
46
|
+
it{ expect(inverse(:x, :+).inverse?(:x, :+)).to be_true }
|
47
|
+
it{ expect(:x.inverse?(inverse(:x, :+), :+)).to be_true }
|
48
|
+
it{ expect(inverse(:x, :*).inverse?(:x, :*)).to be_true }
|
49
|
+
it{ expect(:x.inverse?(inverse(:x, :*), :*)).to be_true }
|
32
50
|
end
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dydx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- gogotanaka
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-18 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.6'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '1.6'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: It is possible to use the differential using the Symbol and Fixnum by
|
@@ -67,9 +60,9 @@ executables: []
|
|
67
60
|
extensions: []
|
68
61
|
extra_rdoc_files: []
|
69
62
|
files:
|
70
|
-
- .gitignore
|
71
|
-
- .rspec
|
72
|
-
- .travis.yml
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
73
66
|
- Gemfile
|
74
67
|
- LICENSE.txt
|
75
68
|
- README.md
|
@@ -78,14 +71,17 @@ files:
|
|
78
71
|
- lib/dydx.rb
|
79
72
|
- lib/dydx/algebra.rb
|
80
73
|
- lib/dydx/algebra/formula.rb
|
74
|
+
- lib/dydx/algebra/inverse.rb
|
81
75
|
- lib/dydx/algebra/operator/common_parts.rb
|
82
76
|
- lib/dydx/algebra/operator/formula.rb
|
83
77
|
- lib/dydx/algebra/operator/general.rb
|
78
|
+
- lib/dydx/algebra/operator/inverse.rb
|
84
79
|
- lib/dydx/algebra/operator/num.rb
|
85
80
|
- lib/dydx/algebra/operator/parts/base.rb
|
86
81
|
- lib/dydx/algebra/operator/parts/formula.rb
|
87
82
|
- lib/dydx/algebra/operator/parts/general.rb
|
88
83
|
- lib/dydx/algebra/operator/parts/interface.rb
|
84
|
+
- lib/dydx/algebra/operator/parts/inverse.rb
|
89
85
|
- lib/dydx/algebra/operator/parts/num.rb
|
90
86
|
- lib/dydx/algebra/operator/parts/symbol.rb
|
91
87
|
- lib/dydx/algebra/operator/symbol.rb
|
@@ -106,6 +102,7 @@ files:
|
|
106
102
|
- spec/lib/algebra/formula_spec.rb
|
107
103
|
- spec/lib/algebra/operator/parts/base_spec.rb
|
108
104
|
- spec/lib/algebra/operator/parts/formula_spec.rb
|
105
|
+
- spec/lib/algebra/operator/parts/inverse_spec.rb
|
109
106
|
- spec/lib/algebra/set/cos_spec.rb
|
110
107
|
- spec/lib/algebra/set/e_spec.rb
|
111
108
|
- spec/lib/algebra/set/fixnum_spec.rb
|
@@ -120,39 +117,33 @@ files:
|
|
120
117
|
homepage: https://github.com/gogotanaka
|
121
118
|
licenses:
|
122
119
|
- MIT
|
120
|
+
metadata: {}
|
123
121
|
post_install_message:
|
124
122
|
rdoc_options: []
|
125
123
|
require_paths:
|
126
124
|
- lib
|
127
125
|
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
-
none: false
|
129
126
|
requirements:
|
130
|
-
- -
|
127
|
+
- - ">="
|
131
128
|
- !ruby/object:Gem::Version
|
132
129
|
version: '0'
|
133
|
-
segments:
|
134
|
-
- 0
|
135
|
-
hash: -708242121294730939
|
136
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
131
|
requirements:
|
139
|
-
- -
|
132
|
+
- - ">="
|
140
133
|
- !ruby/object:Gem::Version
|
141
134
|
version: '0'
|
142
|
-
segments:
|
143
|
-
- 0
|
144
|
-
hash: -708242121294730939
|
145
135
|
requirements: []
|
146
136
|
rubyforge_project:
|
147
|
-
rubygems_version:
|
137
|
+
rubygems_version: 2.2.2
|
148
138
|
signing_key:
|
149
|
-
specification_version:
|
139
|
+
specification_version: 4
|
150
140
|
summary: We can enjoy the derivative.
|
151
141
|
test_files:
|
152
142
|
- spec/dydx_spec.rb
|
153
143
|
- spec/lib/algebra/formula_spec.rb
|
154
144
|
- spec/lib/algebra/operator/parts/base_spec.rb
|
155
145
|
- spec/lib/algebra/operator/parts/formula_spec.rb
|
146
|
+
- spec/lib/algebra/operator/parts/inverse_spec.rb
|
156
147
|
- spec/lib/algebra/set/cos_spec.rb
|
157
148
|
- spec/lib/algebra/set/e_spec.rb
|
158
149
|
- spec/lib/algebra/set/fixnum_spec.rb
|