dydx 0.0.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.
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +13 -0
- data/dydx.gemspec +25 -0
- data/lib/dydx.rb +28 -0
- data/lib/dydx/algebra.rb +90 -0
- data/lib/dydx/algebra/formula.rb +42 -0
- data/lib/dydx/algebra/operator/common_parts.rb +3 -0
- data/lib/dydx/algebra/operator/formula.rb +15 -0
- data/lib/dydx/algebra/operator/general.rb +14 -0
- data/lib/dydx/algebra/operator/num.rb +15 -0
- data/lib/dydx/algebra/operator/parts/base.rb +28 -0
- data/lib/dydx/algebra/operator/parts/formula.rb +41 -0
- data/lib/dydx/algebra/operator/parts/general.rb +55 -0
- data/lib/dydx/algebra/operator/parts/interface.rb +16 -0
- data/lib/dydx/algebra/operator/parts/num.rb +55 -0
- data/lib/dydx/algebra/operator/parts/symbol.rb +29 -0
- data/lib/dydx/algebra/operator/symbol.rb +15 -0
- data/lib/dydx/algebra/set/base.rb +9 -0
- data/lib/dydx/algebra/set/cos.rb +21 -0
- data/lib/dydx/algebra/set/e.rb +16 -0
- data/lib/dydx/algebra/set/fixnum.rb +28 -0
- data/lib/dydx/algebra/set/log.rb +22 -0
- data/lib/dydx/algebra/set/num.rb +22 -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/helper.rb +38 -0
- data/lib/dydx/version.rb +3 -0
- data/spec/dydx_spec.rb +7 -0
- data/spec/lib/algebra/formula_spec.rb +68 -0
- data/spec/lib/algebra/operator/parts/base_spec.rb +8 -0
- data/spec/lib/algebra/operator/parts/formula_spec.rb +13 -0
- 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 +64 -0
- data/spec/lib/algebra/set/log_spec.rb +15 -0
- data/spec/lib/algebra/set/num_spec.rb +17 -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/helper_spec.rb +32 -0
- data/spec/spec_helper.rb +4 -0
- metadata +165 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module Num
|
6
|
+
def +(x)
|
7
|
+
if n == 0
|
8
|
+
x
|
9
|
+
elsif x.is_a?(Num)
|
10
|
+
_(n + x.n)
|
11
|
+
else
|
12
|
+
super(x)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def -(x)
|
17
|
+
if x.is_a?(Num)
|
18
|
+
_(n - x.n)
|
19
|
+
else
|
20
|
+
super(x)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def *(x)
|
25
|
+
if n == 0
|
26
|
+
self
|
27
|
+
elsif n == 1
|
28
|
+
x
|
29
|
+
elsif x.is_a?(Num)
|
30
|
+
_(n * x.n)
|
31
|
+
else
|
32
|
+
super(x)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def /(x)
|
37
|
+
if (n == 0)
|
38
|
+
self
|
39
|
+
else
|
40
|
+
super(x)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def ^(x)
|
45
|
+
if (n == 0) || (n == 1)
|
46
|
+
self
|
47
|
+
else
|
48
|
+
super(x)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module Symbol
|
6
|
+
def *(x)
|
7
|
+
if x.exponentiation? &&
|
8
|
+
self == x.f
|
9
|
+
|
10
|
+
self ^ (1 + x.g)
|
11
|
+
else
|
12
|
+
super(x)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def /(x)
|
17
|
+
if x.exponentiation? &&
|
18
|
+
self == x.f
|
19
|
+
|
20
|
+
self ^ (1 - x.g)
|
21
|
+
else
|
22
|
+
super(x)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/symbol'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module Symbol
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::Symbol
|
10
|
+
include Parts::General
|
11
|
+
include Parts::Interface
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Cos < Base
|
5
|
+
attr_accessor :x
|
6
|
+
|
7
|
+
def initialize(x)
|
8
|
+
@x = x
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
"cos( #{x.to_s} )"
|
13
|
+
end
|
14
|
+
|
15
|
+
def d(sym=:x)
|
16
|
+
-1 * sin(x) * x.d(sym)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
Fixnum.class_eval do
|
5
|
+
include Helper
|
6
|
+
|
7
|
+
%w(+ - * / ^).each do |operator|
|
8
|
+
define_method(operator) do |g|
|
9
|
+
if g.is_a?(Symbol) ||
|
10
|
+
g.is_a?(Formula) ||
|
11
|
+
g.is_a?(Base) ||
|
12
|
+
%w(/ ^).include?(operator)
|
13
|
+
|
14
|
+
_(self).send(operator.to_sym, g)
|
15
|
+
else
|
16
|
+
(to_f.send(operator.to_sym, g)).to_i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def differentiate(sym=:x)
|
22
|
+
_(0)
|
23
|
+
end
|
24
|
+
alias_method :d, :differentiate
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Log < Base
|
5
|
+
attr_accessor :f
|
6
|
+
|
7
|
+
def initialize(f)
|
8
|
+
@f = f
|
9
|
+
end
|
10
|
+
|
11
|
+
def differentiate(sym=:x)
|
12
|
+
f.d(sym) / (f)
|
13
|
+
end
|
14
|
+
alias_method :d, :differentiate
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"log( #{f.to_s} )"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Set
|
4
|
+
class Num < Base
|
5
|
+
attr_accessor :n
|
6
|
+
|
7
|
+
def initialize(n)
|
8
|
+
@n = n
|
9
|
+
end
|
10
|
+
|
11
|
+
def differentiate(sym=:x)
|
12
|
+
_(0)
|
13
|
+
end
|
14
|
+
alias_method :d, :differentiate
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@n.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/dydx/helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Helper
|
3
|
+
def is_0?
|
4
|
+
self == 0 || (is_a?(Num) && n == 0)
|
5
|
+
end
|
6
|
+
|
7
|
+
def is_1?
|
8
|
+
self == 1 || (is_a?(Num) && n == 1)
|
9
|
+
end
|
10
|
+
|
11
|
+
def is_minus1?
|
12
|
+
self == -1 || (is_a?(Num) && n == -1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def is_multiple_of(x)
|
16
|
+
is_multiple = if is_0?
|
17
|
+
_(0)
|
18
|
+
elsif self == x
|
19
|
+
_(1)
|
20
|
+
elsif is_a?(Formula) &&
|
21
|
+
(f == x || g == x)
|
22
|
+
f == x ? g : f
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
{
|
29
|
+
addition: :+,
|
30
|
+
subtraction: :-,
|
31
|
+
multiplication: :*,
|
32
|
+
division: :/,
|
33
|
+
exponentiation: :^
|
34
|
+
}.each do |operator_name, operator|
|
35
|
+
define_method("#{operator_name}?") { @operator == operator }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/dydx/version.rb
ADDED
data/spec/dydx_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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) }
|
9
|
+
describe 'Calculate' do
|
10
|
+
context 'With Fixnum' do
|
11
|
+
let(:formula) { (:x + :y) }
|
12
|
+
it{ expect((formula + 0).to_s).to eq(formula.to_s) }
|
13
|
+
it{ expect((formula - 0).to_s).to eq(formula.to_s) }
|
14
|
+
it{ expect((formula * 0).to_s).to eq('0') }
|
15
|
+
it{ expect((formula * 1).to_s).to eq(formula.to_s) }
|
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_s).to eq('1') }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
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 '#differentiate' do
|
32
|
+
it{ expect(addition.d(:x).to_s).to eq('1') }
|
33
|
+
it{ expect(addition.d(:y).to_s).to eq('1') }
|
34
|
+
it{ expect(addition.d(:z).to_s).to eq('0') }
|
35
|
+
|
36
|
+
it{ expect(subtraction.d(:x).to_s).to eq('1') }
|
37
|
+
it{ expect(subtraction.d(:y).to_s).to eq('-1') }
|
38
|
+
it{ expect(subtraction.d(:z).to_s).to eq('0') }
|
39
|
+
|
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_s).to eq('0') }
|
43
|
+
|
44
|
+
it{ expect(division.d(:x).to_s).to eq("( y ^ -1 )") }
|
45
|
+
it{ expect(division.d(:y).to_s).to eq('( ( - x ) / ( y ^ 2 ) )') }
|
46
|
+
it{ expect(division.d(:z).to_s).to eq('0') }
|
47
|
+
|
48
|
+
it{ expect(exponentiation.d(:x).to_s).to eq('( y * ( x ^ ( y - 1 ) ) )') }
|
49
|
+
it{ expect(exponentiation.d(:y).to_s).to eq('( ( x ^ y ) * log( x ) )') }
|
50
|
+
it{ expect(exponentiation.d(:z).to_s).to eq('0') }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'With Symbol' do
|
54
|
+
$a = (:x ^ :n)
|
55
|
+
let(:d1){ da/dx }
|
56
|
+
let(:d2){ d/dx($a) }
|
57
|
+
it{ expect(d1.to_s).to eq('( n * ( x ^ ( n - 1 ) ) )') }
|
58
|
+
it{ expect(d2.to_s).to eq('( n * ( x ^ ( n - 1 ) ) )') }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'With Symbol' do
|
62
|
+
$b = (:x ^ (:x * 2))
|
63
|
+
let(:d1){ db/dx }
|
64
|
+
let(:d2){ d/dx($b) }
|
65
|
+
it{ expect(d1.to_s).to eq('( ( x * 2 ) * ( x ^ ( ( x * 2 ) - 1 ) ) )') }
|
66
|
+
it{ expect(d2.to_s).to eq('( ( x * 2 ) * ( x ^ ( ( x * 2 ) - 1 ) ) )') }
|
67
|
+
end
|
68
|
+
end
|