differentiation 0.1.0
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 +7 -0
- data/.gitignore +10 -0
- data/Gemfile +7 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/differentiation.gemspec +25 -0
- data/lib/differentiation/ext/kernel.rb +23 -0
- data/lib/differentiation/version.rb +3 -0
- data/lib/differentiation.rb +154 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4f77d01605afbbd8c64d7a36c2f0f88540846c4c103a3bd97b98d493abded3cf
|
4
|
+
data.tar.gz: 2049debc4169d0b1f214517475e5864775932afa6e3a02aad27029be0ee97483
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 11ee0a83cf38b016b79004723a966d82651b24bb459d7fe4dfd55e1fe3a18c9a80d58155ce8cfe2e84870467833c6e5d01edb4bd0cb6707822df113dafef86b2
|
7
|
+
data.tar.gz: 97fee6518f5abd937ec55d2361b8359b850743fee62324a3c41ab817bbf7c6dadfa91c6eb8e639fe243a261e250d02e8ce905150ec0894caf755e8719d3a77fd
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Differentiation
|
2
|
+
|
3
|
+
differentiation.gem make ruby numeric Method/Proc differentiable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'differentiation'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install differentiation
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require "differentiation"
|
25
|
+
|
26
|
+
differential def f(x, y)
|
27
|
+
(x + 1) * (y - 2)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
result = f(1, 2)
|
32
|
+
|
33
|
+
p result # => -2
|
34
|
+
|
35
|
+
p result.gradients(:x, :y) # => {:x=>1, :y=>0}
|
36
|
+
```
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nagachika/differentiation.
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "differentiation"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'differentiation/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "differentiation"
|
9
|
+
spec.version = Differentiation::VERSION
|
10
|
+
spec.authors = ["nagachika"]
|
11
|
+
spec.email = ["nagachika@ruby-lang.org"]
|
12
|
+
|
13
|
+
spec.summary = %q{Make Ruby Differentiable.}
|
14
|
+
spec.description = %q{differentiation.gem implement a kind of Automatic differentiation algorithm. It can convert Method/Proc to differentiable version.}
|
15
|
+
spec.homepage = "https://github.com/nagachika/differentiation"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "test-unit"
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def differential(m)
|
5
|
+
if m.is_a?(Symbol)
|
6
|
+
f = self.method(m)
|
7
|
+
elsif m.is_a?(Proc) or m.is_a?(Method)
|
8
|
+
f = m
|
9
|
+
else
|
10
|
+
raise TypeError, "differential requires Method/Proc/Symbol argument."
|
11
|
+
end
|
12
|
+
f = Differentiation.differential(f)
|
13
|
+
if m.is_a?(Symbol)
|
14
|
+
if self.is_a?(Module)
|
15
|
+
self.define_method(m, f)
|
16
|
+
else
|
17
|
+
self.define_singleton_method(m, f)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
f
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# fronzen_string_literal: true
|
2
|
+
|
3
|
+
module Differentiation
|
4
|
+
|
5
|
+
def self.differentiable?(o)
|
6
|
+
# TODO: support Vector and Matrix
|
7
|
+
o.is_a?(Numeric)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.convert_to_dual_number(o, key: nil)
|
11
|
+
if o.is_a?(DualNumber)
|
12
|
+
o
|
13
|
+
elsif differentiable?(o)
|
14
|
+
DualNumber.new(o, lambda{|_key| _key == key ? 1 : 0})
|
15
|
+
else
|
16
|
+
o
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.differential(f)
|
21
|
+
unless f.is_a?(Proc) or f.is_a?(Method)
|
22
|
+
raise TypeError, "Only Proc or Method can be differentiable"
|
23
|
+
end
|
24
|
+
|
25
|
+
# [[:req, :x], [:req, :y], [:opt, :c], [:rest, :rest], [:keyreq, :k2], [:key, :k1], [:keyrest, :kw]]
|
26
|
+
parameters = f.parameters
|
27
|
+
positional = []
|
28
|
+
parameters.each do |tuple|
|
29
|
+
if [:req, :opt].include?(tuple[0])
|
30
|
+
positional << tuple[1]
|
31
|
+
elsif [:key, :keyreq, :kwrest].include?(tuple[0])
|
32
|
+
else
|
33
|
+
# TODO: support rest arguments
|
34
|
+
raise "rest arguments are currently not supported"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
f_prime = lambda {|*args, **kwargs|
|
38
|
+
args.map!.with_index do |a, i|
|
39
|
+
Differentiation.convert_to_dual_number(a, key: positional[i])
|
40
|
+
end
|
41
|
+
kwargs = Hash[kwargs.map{|k,v| [k, Differentiation.convert_to_dual_number(v, key: k)] }]
|
42
|
+
if kwargs.empty?
|
43
|
+
f.call(*args)
|
44
|
+
else
|
45
|
+
f.call(*args, **kwargs)
|
46
|
+
end
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
class DualNumber
|
51
|
+
def initialize(n, diff=lambda{|_| 0})
|
52
|
+
@n = n
|
53
|
+
@diff = diff
|
54
|
+
end
|
55
|
+
|
56
|
+
attr_reader :n, :diff
|
57
|
+
|
58
|
+
def derivative(key)
|
59
|
+
@diff.call(key)
|
60
|
+
end
|
61
|
+
|
62
|
+
def gradients(*keys)
|
63
|
+
keys.each_with_object({}) do |k, o|
|
64
|
+
o[k] = @diff.call(k)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_i
|
69
|
+
@n.to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_int
|
73
|
+
@n.to_int
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_f
|
77
|
+
@n.to_f
|
78
|
+
end
|
79
|
+
|
80
|
+
def coerce(other)
|
81
|
+
if Differentiation.differentiable?(other)
|
82
|
+
[DualNumber.new(other), self]
|
83
|
+
else
|
84
|
+
super
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def +(other)
|
89
|
+
if other.is_a?(DualNumber)
|
90
|
+
n = @n + other.n
|
91
|
+
diff = ->(key) { @diff.call(key) + other.diff.call(key) }
|
92
|
+
else
|
93
|
+
n = @n + other
|
94
|
+
diff = @diff
|
95
|
+
end
|
96
|
+
DualNumber.new(n, diff)
|
97
|
+
end
|
98
|
+
|
99
|
+
def -(other)
|
100
|
+
if other.is_a?(DualNumber)
|
101
|
+
n = @n - other.n
|
102
|
+
diff = ->(key) { @diff.call(key) - other.diff.call(key) }
|
103
|
+
else
|
104
|
+
n = @n - other
|
105
|
+
diff = @diff
|
106
|
+
end
|
107
|
+
DualNumber.new(n, diff)
|
108
|
+
end
|
109
|
+
|
110
|
+
def *(other)
|
111
|
+
if other.is_a?(DualNumber)
|
112
|
+
n = @n * other.n
|
113
|
+
diff = ->(key) { @n * other.diff.call(key) + @diff.call(key) * other.n }
|
114
|
+
else
|
115
|
+
n = @n * other
|
116
|
+
diff = ->(key) { @diff.call(key) * other }
|
117
|
+
end
|
118
|
+
DualNumber.new(n, diff)
|
119
|
+
end
|
120
|
+
|
121
|
+
def /(other)
|
122
|
+
if other.is_a?(DualNumber)
|
123
|
+
n = @n / other.n
|
124
|
+
diff = ->(key) { (@diff.call(key) / other) + (@n * other.diff.call(key)) / (other.n ** 2) }
|
125
|
+
else
|
126
|
+
n = @n / other
|
127
|
+
diff = ->(key) { @diff.call(key) / other }
|
128
|
+
end
|
129
|
+
DualNumber.new(n, diff)
|
130
|
+
end
|
131
|
+
|
132
|
+
def **(other)
|
133
|
+
if other.is_a?(DualNumber)
|
134
|
+
n = @n ** other.n
|
135
|
+
diff = ->(key) { (@n ** other.n) * (other.diff(key) * Math.log(@n) + (other.n / @n)) }
|
136
|
+
else
|
137
|
+
n = @n ** other
|
138
|
+
diff = ->(key) { (@n ** (other-1)) * other }
|
139
|
+
end
|
140
|
+
DualNumber.new(n, diff)
|
141
|
+
end
|
142
|
+
|
143
|
+
def inspect
|
144
|
+
if $DEBUG
|
145
|
+
"<DualNumber: #{@n} >"
|
146
|
+
else
|
147
|
+
@n.inspect
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
require "differentiation/ext/kernel"
|
154
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: differentiation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nagachika
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: differentiation.gem implement a kind of Automatic differentiation algorithm.
|
56
|
+
It can convert Method/Proc to differentiable version.
|
57
|
+
email:
|
58
|
+
- nagachika@ruby-lang.org
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- differentiation.gemspec
|
70
|
+
- lib/differentiation.rb
|
71
|
+
- lib/differentiation/ext/kernel.rb
|
72
|
+
- lib/differentiation/version.rb
|
73
|
+
homepage: https://github.com/nagachika/differentiation
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.7.6.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Make Ruby Differentiable.
|
96
|
+
test_files: []
|