dydx 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 gogotanaka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Dydx
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dydx'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dydx
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/dydx/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/dydx.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dydx/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dydx"
|
8
|
+
spec.version = Dydx::VERSION
|
9
|
+
spec.authors = ["gogotanaka"]
|
10
|
+
spec.email = ["qlli.illb@gmail.com"]
|
11
|
+
spec.homepage = "https://github.com/gogotanaka"
|
12
|
+
spec.summary = %q{We can enjoy the derivative.}
|
13
|
+
spec.description = %q{It is possible to use the differential using the Symbol and Fixnum by including the Dydx. And, we can use the natural logarithm and log.}
|
14
|
+
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/lib/dydx.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'dydx/helper'
|
2
|
+
require 'dydx/algebra'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
include Algebra
|
6
|
+
class Delta
|
7
|
+
attr_accessor :var, :function
|
8
|
+
def initialize(var, function)
|
9
|
+
@var = var
|
10
|
+
@function = function
|
11
|
+
end
|
12
|
+
|
13
|
+
def /(delta)
|
14
|
+
if var
|
15
|
+
eval("$#{var}").differentiate(delta.var)
|
16
|
+
elsif delta.function
|
17
|
+
delta.function.differentiate(delta.var)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(method, *args, &block)
|
23
|
+
method_name = method.to_s
|
24
|
+
return super unless (method_name[0] == 'd' && method_name.size <= 2)
|
25
|
+
method_name.slice!(0)
|
26
|
+
Delta.new(method_name.empty? ? nil : method_name.to_sym, args.first)
|
27
|
+
end
|
28
|
+
end
|
data/lib/dydx/algebra.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'dydx/algebra/formula'
|
2
|
+
|
3
|
+
require 'dydx/algebra/set/base'
|
4
|
+
require 'dydx/algebra/set/num'
|
5
|
+
require 'dydx/algebra/set/fixnum'
|
6
|
+
require 'dydx/algebra/set/symbol'
|
7
|
+
require 'dydx/algebra/set/e'
|
8
|
+
require 'dydx/algebra/set/pi'
|
9
|
+
require 'dydx/algebra/set/log'
|
10
|
+
require 'dydx/algebra/set/sin'
|
11
|
+
require 'dydx/algebra/set/cos'
|
12
|
+
require 'dydx/algebra/set/tan'
|
13
|
+
|
14
|
+
require 'dydx/algebra/operator/formula'
|
15
|
+
require 'dydx/algebra/operator/symbol'
|
16
|
+
require 'dydx/algebra/operator/num'
|
17
|
+
require 'dydx/algebra/operator/general'
|
18
|
+
|
19
|
+
module Dydx
|
20
|
+
module Algebra
|
21
|
+
include Set
|
22
|
+
module Set
|
23
|
+
Symbol.class_eval{ include Operator::Symbol }
|
24
|
+
class Num; include Operator::Num; end
|
25
|
+
class E; include Operator::General; end
|
26
|
+
class Pi; include Operator::General; end
|
27
|
+
class Log; include Operator::General; end
|
28
|
+
class Sin; include Operator::General; end
|
29
|
+
class Cos; include Operator::General; end
|
30
|
+
class Tan; include Operator::General; end
|
31
|
+
end
|
32
|
+
class Formula; include Operator::Formula; end
|
33
|
+
|
34
|
+
def _(num)
|
35
|
+
if num >= 0
|
36
|
+
eval("@p#{num} ||= Num.new(num)")
|
37
|
+
else
|
38
|
+
eval("@n#{-1 * num} ||= Num.new(num)")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def pi
|
43
|
+
@pi ||= Pi.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def e
|
47
|
+
@e ||= E.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def log(formula)
|
51
|
+
if formula.multiplication?
|
52
|
+
f, g = formula.f, formula.g
|
53
|
+
log(f) + log(g)
|
54
|
+
elsif formula.exponentiation?
|
55
|
+
f, g = formula.f, formula.g
|
56
|
+
g * log(f)
|
57
|
+
elsif formula.is_1?
|
58
|
+
_(0)
|
59
|
+
elsif formula.is_a?(E)
|
60
|
+
_(1)
|
61
|
+
else
|
62
|
+
Log.new(formula)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def sin(x)
|
67
|
+
multiplier = x.is_multiple_of(pi)
|
68
|
+
if multiplier.is_a?(Num)
|
69
|
+
_(0)
|
70
|
+
else
|
71
|
+
Sin.new(x)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def cos(x)
|
76
|
+
multiplier = x.is_multiple_of(pi)
|
77
|
+
if multiplier.is_a?(Num) && multiplier.n % 2 == 0
|
78
|
+
_(1)
|
79
|
+
elsif multiplier.is_a?(Num) && multiplier.n % 2 == 1
|
80
|
+
_(-1)
|
81
|
+
else
|
82
|
+
Cos.new(x)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def tan(x)
|
87
|
+
Tan.new(x)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
class Formula
|
4
|
+
include Helper
|
5
|
+
attr_accessor :f, :g, :operator
|
6
|
+
|
7
|
+
def initialize(f, g, operator)
|
8
|
+
@f, @g, @operator = f, g, operator
|
9
|
+
end
|
10
|
+
|
11
|
+
def differentiate(sym=:x)
|
12
|
+
case @operator
|
13
|
+
when :+
|
14
|
+
f.d(sym) + g.d(sym)
|
15
|
+
when :-
|
16
|
+
f.d(sym) - g.d(sym)
|
17
|
+
when :*
|
18
|
+
(f.d(sym) * g) + (f * g.d(sym))
|
19
|
+
when :/
|
20
|
+
((f.d(sym) * g) - (f * g.d(sym)))/(g ^ _(2))
|
21
|
+
when :^
|
22
|
+
# TODO:
|
23
|
+
if f == sym
|
24
|
+
g * (f ^ (g - 1))
|
25
|
+
else
|
26
|
+
self * (g * log(f)).d(sym)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias_method :d, :differentiate
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
if (subtraction? && f.is_0?) ||
|
34
|
+
(multiplication? && (f.is_minus1? || g.is_minus1?) )
|
35
|
+
"( - #{g.to_s} )"
|
36
|
+
else
|
37
|
+
"( #{f.to_s} #{@operator} #{g.to_s} )"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/formula'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module Formula
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::Formula
|
10
|
+
include Parts::General
|
11
|
+
include Parts::Interface
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/general'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module General
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::General
|
10
|
+
include Parts::Interface
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'dydx/algebra/operator/common_parts'
|
2
|
+
require 'dydx/algebra/operator/parts/num'
|
3
|
+
|
4
|
+
module Dydx
|
5
|
+
module Algebra
|
6
|
+
module Operator
|
7
|
+
module Num
|
8
|
+
include Parts::Base
|
9
|
+
include Parts::Num
|
10
|
+
include Parts::General
|
11
|
+
include Parts::Interface
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module Base
|
6
|
+
%w(+ - * / ^).each do |operator|
|
7
|
+
define_method(operator) do |x|
|
8
|
+
if self == x && operator != '^'
|
9
|
+
case operator
|
10
|
+
when '+'
|
11
|
+
_(2) * self
|
12
|
+
when '-'
|
13
|
+
_(0)
|
14
|
+
when '*'
|
15
|
+
self ^ _(2)
|
16
|
+
when '/'
|
17
|
+
_(1)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
::Algebra::Formula.new(self, x, operator.to_sym)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module Formula
|
6
|
+
%w(+ -).each do |operator|
|
7
|
+
define_method(operator) do |x|
|
8
|
+
if multiplication? && x.multiplication?
|
9
|
+
if f == x.f
|
10
|
+
f * g.send(operator, x.g)
|
11
|
+
elsif g == x.g
|
12
|
+
f.send(operator, x.f) * g
|
13
|
+
else
|
14
|
+
super(x)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
super(x)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
%w(* /).each do |operator|
|
23
|
+
define_method(operator) do |x|
|
24
|
+
if exponentiation? && x.exponentiation?
|
25
|
+
if f == x.f
|
26
|
+
f ^ g.send({'*'=>'+', '/'=>'-'}[operator], x.g)
|
27
|
+
elsif g == x.g
|
28
|
+
f.send(operator, x.f) ^ g
|
29
|
+
else
|
30
|
+
super(x)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
super(x)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Dydx
|
2
|
+
module Algebra
|
3
|
+
module Operator
|
4
|
+
module Parts
|
5
|
+
module General
|
6
|
+
def +(x)
|
7
|
+
if x.is_0?
|
8
|
+
self
|
9
|
+
else
|
10
|
+
super(x)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def -(x)
|
15
|
+
if x.is_0?
|
16
|
+
self
|
17
|
+
else
|
18
|
+
super(x)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def *(x)
|
23
|
+
if x.is_0?
|
24
|
+
x
|
25
|
+
elsif x.is_1?
|
26
|
+
self
|
27
|
+
else
|
28
|
+
super(x)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def /(x)
|
33
|
+
if x.is_0?
|
34
|
+
raise ZeroDivisionError
|
35
|
+
elsif x.is_1?
|
36
|
+
self
|
37
|
+
else
|
38
|
+
super(x)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def ^(x)
|
43
|
+
if x.is_0?
|
44
|
+
_(1)
|
45
|
+
elsif x.is_1?
|
46
|
+
self
|
47
|
+
else
|
48
|
+
super(x)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|