symbolic 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -4
- data/Rakefile +5 -16
- data/lib/symbolic.rb +30 -19
- data/lib/symbolic/constant.rb +27 -0
- data/lib/symbolic/constants.rb +15 -0
- data/lib/symbolic/expression.rb +18 -4
- data/lib/symbolic/extensions/kernel.rb +1 -1
- data/lib/symbolic/extensions/matrix.rb +8 -6
- data/lib/symbolic/extensions/module.rb +5 -0
- data/lib/symbolic/extensions/numeric.rb +8 -0
- data/lib/symbolic/extensions/rational.rb +18 -12
- data/lib/symbolic/factors.rb +26 -48
- data/lib/symbolic/function.rb +100 -19
- data/lib/symbolic/math.rb +31 -7
- data/lib/symbolic/misc.rb +5 -0
- data/lib/symbolic/plugins/var_name.rb +42 -0
- data/lib/symbolic/printer.rb +79 -0
- data/lib/symbolic/statistics.rb +13 -8
- data/lib/symbolic/sum.rb +43 -0
- data/lib/symbolic/summands.rb +36 -31
- data/lib/symbolic/variable.rb +23 -11
- data/spec/integration_spec.rb +193 -0
- data/spec/spec_helper.rb +7 -4
- data/spec/symbolic/equal_spec.rb +18 -0
- data/spec/symbolic_spec.rb +31 -123
- data/symbolic.gemspec +64 -0
- metadata +53 -45
- data/.gitignore +0 -22
- data/spec/spec.opts +0 -1
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
end
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
7
|
require 'symbolic'
|
3
|
-
require '
|
4
|
-
require 'spec'
|
5
|
-
require 'spec/autorun'
|
8
|
+
require 'rspec'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Symbolic do
|
4
|
+
let(:x) { var :name => :x, :value => 1 }
|
5
|
+
|
6
|
+
it 'variables equality is instance equality' do
|
7
|
+
x.should_not == var(:name => :x, :value => 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be equal (sub-groups)' do
|
11
|
+
s = 3 + x
|
12
|
+
a = s*3
|
13
|
+
b = s*3
|
14
|
+
a.should == a
|
15
|
+
a.should == b
|
16
|
+
b.should == a
|
17
|
+
end
|
18
|
+
end
|
data/spec/symbolic_spec.rb
CHANGED
@@ -1,141 +1,49 @@
|
|
1
|
-
require File.expand_path(
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
2
|
|
3
|
-
describe
|
4
|
-
|
5
|
-
@x = var :name => :x, :value => 1
|
6
|
-
@y = var :name => :y, :value => 2
|
7
|
-
end
|
3
|
+
describe Symbolic do
|
4
|
+
let(:var) { Object.new.extend Symbolic }
|
8
5
|
|
9
|
-
|
10
|
-
|
6
|
+
it 'should handle unary plus' do
|
7
|
+
(+var).should == var
|
11
8
|
end
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
expression(non_optimized).should == expression(optimized)
|
17
|
-
end
|
18
|
-
end
|
10
|
+
it 'should handle unary minus' do
|
11
|
+
Symbolic::Factors.should_receive(:add).with(var, -1).and_return(:negative_value)
|
12
|
+
(-var).should == :negative_value
|
19
13
|
end
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
expression(symbolic_expression).value.should == result
|
25
|
-
end
|
26
|
-
end
|
15
|
+
it 'should handle binary plus' do
|
16
|
+
Symbolic::Summands.should_receive(:add).with(var, :other_var).and_return(:result)
|
17
|
+
(var + :other_var).should == :result
|
27
18
|
end
|
28
19
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
expression(symbolic_expression).to_s.should == result
|
33
|
-
end
|
34
|
-
end
|
20
|
+
it 'should handle binary minus' do
|
21
|
+
Symbolic::Summands.should_receive(:subtract).with(var, :other_var).and_return(:result)
|
22
|
+
(var - :other_var).should == :result
|
35
23
|
end
|
36
24
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
'+x' => 1,
|
42
|
-
'-x' => -1,
|
43
|
-
'x + 4' => 5,
|
44
|
-
'3 + x' => 4,
|
45
|
-
'x + y' => 3,
|
46
|
-
'x - 1' => 0,
|
47
|
-
'1 - x' => 0,
|
48
|
-
'x - y' => -1,
|
49
|
-
'-x + 3' => 2,
|
50
|
-
'-y - x' => -3,
|
51
|
-
'x*3' => 3,
|
52
|
-
'4*y' => 8,
|
53
|
-
'(+x)*(-y)' => -2,
|
54
|
-
'x/2' => 0.5,
|
55
|
-
'y/2' => 1,
|
56
|
-
'-2/x' => -2,
|
57
|
-
'4/(-y)' => -2,
|
58
|
-
'x**2' => 1,
|
59
|
-
'4**y' => 16,
|
60
|
-
'y**x' => 2
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "optimization:" do
|
64
|
-
should_equal \
|
65
|
-
'-(-x)' => 'x',
|
66
|
-
|
67
|
-
'0 + x' => 'x',
|
68
|
-
'x + 0' => 'x',
|
69
|
-
'x + (-2)' => 'x - 2',
|
70
|
-
'-2 + x' => 'x - 2',
|
71
|
-
'-x + 2' => '2 - x',
|
72
|
-
'x + (-y)' => 'x - y',
|
73
|
-
'-y + x' => 'x - y',
|
74
|
-
|
75
|
-
'0 - x' => '-x',
|
76
|
-
'x - 0' => 'x',
|
77
|
-
'x - (-2)' => 'x + 2',
|
78
|
-
'-2 - (-x)' => 'x - 2',
|
79
|
-
'x - (-y)' => 'x + y',
|
80
|
-
|
81
|
-
'0 * x' => '0',
|
82
|
-
'x * 0' => '0',
|
83
|
-
'1 * x' => 'x',
|
84
|
-
'x * 1' => 'x',
|
85
|
-
'-1 * x' => '-x',
|
86
|
-
'x * (-1)' => '-x',
|
87
|
-
'x * (-3)' => '-(x*3)',
|
88
|
-
'-3 * x' => '-(x*3)',
|
89
|
-
'-3 * (-x)' => 'x*3',
|
90
|
-
'x*(-y)' => '-(x*y)',
|
91
|
-
'-x*y' => '-(x*y)',
|
92
|
-
'(-x)*(-y)' => 'x*y',
|
93
|
-
|
94
|
-
'0 / x' => '0',
|
95
|
-
'x / 1' => 'x',
|
25
|
+
it 'should handle multiplication' do
|
26
|
+
Symbolic::Factors.should_receive(:add).with(var, :other_var).and_return(:result)
|
27
|
+
(var * :other_var).should == :result
|
28
|
+
end
|
96
29
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
'(-x)**1' => '-x',
|
102
|
-
'(-x)**2' => 'x**2',
|
103
|
-
'(x**2)**y' => 'x**(2*y)',
|
30
|
+
it 'should handle division' do
|
31
|
+
Symbolic::Factors.should_receive(:subtract).with(var, :other_var).and_return(:result)
|
32
|
+
(var / :other_var).should == :result
|
33
|
+
end
|
104
34
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
'x + y - x' => 'y',
|
109
|
-
'2*x + x**1 - y**2/y - y' => '3*x - 2*y',
|
110
|
-
'-(x+4)' => '-x-4'
|
35
|
+
it 'should handle exponentation' do
|
36
|
+
Symbolic::Factors.should_receive(:power).with(var, :other_var).and_return(:result)
|
37
|
+
(var ** :other_var).should == :result
|
111
38
|
end
|
112
39
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
'(-(x+y)).variables' => '[x,y]',
|
117
|
-
'(-x**y-4*y+5-y/x).operations' =>
|
118
|
-
'{"+" => 1, "-" => 2, "*" => 1, "/" => 1, "-@" => 1, "**" => 1}'
|
40
|
+
it 'should delegate string representation to Printer' do
|
41
|
+
Symbolic::Printer.should_receive(:print).with(var).and_return(:string)
|
42
|
+
var.to_s.should == :string
|
119
43
|
end
|
120
44
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
'-x' => '-x',
|
125
|
-
'x+1' => 'x+1',
|
126
|
-
'x-4' => 'x-4',
|
127
|
-
'-x-4' => '-x-4',
|
128
|
-
'-(x+y)' => '-x-y',
|
129
|
-
'-(x-y)' => '-x+y',
|
130
|
-
'x*y' => 'x*y',
|
131
|
-
'(-x)*y' => '-x*y',
|
132
|
-
'(x+2)*(y+3)*4' => '4*(x+2)*(y+3)',
|
133
|
-
'4/x' => '4/x',
|
134
|
-
'2*x**(-1)*y**(-1)' => '2/(x*y)',
|
135
|
-
'(-(2+x))/(-(-y))' => '(-x-2)/y',
|
136
|
-
'x**y' => 'x**y',
|
137
|
-
'x**(y-4)' => 'x**(y-4)',
|
138
|
-
'(x+1)**(y*2)' => '(x+1)**(2*y)',
|
139
|
-
'-(x**y -2)+5' => '-x**y+7'
|
45
|
+
it 'should redefine inspect' do
|
46
|
+
var.should_receive(:to_s).and_return(:string)
|
47
|
+
var.inspect.should == "Symbolic(string)"
|
140
48
|
end
|
141
49
|
end
|
data/symbolic.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "symbolic"
|
8
|
+
s.version = "0.3.8"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["brainopia"]
|
12
|
+
s.date = "2012-10-30"
|
13
|
+
s.description = "Symbolic math for ruby. This gem can help if you want to get a simplified form of a big equation or to speed up similar calculations or you need an abstraction layer for math. Symbolic does not have any external dependencies. It uses only pure ruby (less than 400 lines of code)."
|
14
|
+
s.email = "ravwar@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"lib/symbolic.rb",
|
22
|
+
"lib/symbolic/coerced.rb",
|
23
|
+
"lib/symbolic/constant.rb",
|
24
|
+
"lib/symbolic/constants.rb",
|
25
|
+
"lib/symbolic/expression.rb",
|
26
|
+
"lib/symbolic/extensions/kernel.rb",
|
27
|
+
"lib/symbolic/extensions/matrix.rb",
|
28
|
+
"lib/symbolic/extensions/module.rb",
|
29
|
+
"lib/symbolic/extensions/numeric.rb",
|
30
|
+
"lib/symbolic/extensions/rational.rb",
|
31
|
+
"lib/symbolic/factors.rb",
|
32
|
+
"lib/symbolic/function.rb",
|
33
|
+
"lib/symbolic/math.rb",
|
34
|
+
"lib/symbolic/misc.rb",
|
35
|
+
"lib/symbolic/plugins/var_name.rb",
|
36
|
+
"lib/symbolic/printer.rb",
|
37
|
+
"lib/symbolic/statistics.rb",
|
38
|
+
"lib/symbolic/sum.rb",
|
39
|
+
"lib/symbolic/summands.rb",
|
40
|
+
"lib/symbolic/variable.rb",
|
41
|
+
"spec/integration_spec.rb",
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/symbolic/equal_spec.rb",
|
44
|
+
"spec/symbolic_spec.rb",
|
45
|
+
"symbolic.gemspec"
|
46
|
+
]
|
47
|
+
s.homepage = "http://brainopia.github.com/symbolic"
|
48
|
+
s.require_paths = ["lib"]
|
49
|
+
s.rubygems_version = "1.8.24"
|
50
|
+
s.summary = "Symbolic math for ruby"
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_development_dependency(%q<rspec>, [">= 2.4"])
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 2.4"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rspec>, [">= 2.4"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
CHANGED
@@ -1,83 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolic
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.8
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- brainopia
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.4'
|
17
22
|
type: :development
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
description: Symbolic math for ruby. This gem can help if you want to get a simplified
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.4'
|
30
|
+
description: Symbolic math for ruby. This gem can help if you want to get a simplified
|
31
|
+
form of a big equation or to speed up similar calculations or you need an abstraction
|
32
|
+
layer for math. Symbolic does not have any external dependencies. It uses only pure
|
33
|
+
ruby (less than 400 lines of code).
|
26
34
|
email: ravwar@gmail.com
|
27
35
|
executables: []
|
28
|
-
|
29
36
|
extensions: []
|
30
|
-
|
31
|
-
extra_rdoc_files:
|
37
|
+
extra_rdoc_files:
|
32
38
|
- README.rdoc
|
33
|
-
files:
|
34
|
-
- .gitignore
|
39
|
+
files:
|
35
40
|
- README.rdoc
|
36
41
|
- Rakefile
|
37
42
|
- lib/symbolic.rb
|
38
43
|
- lib/symbolic/coerced.rb
|
44
|
+
- lib/symbolic/constant.rb
|
45
|
+
- lib/symbolic/constants.rb
|
39
46
|
- lib/symbolic/expression.rb
|
40
47
|
- lib/symbolic/extensions/kernel.rb
|
41
48
|
- lib/symbolic/extensions/matrix.rb
|
49
|
+
- lib/symbolic/extensions/module.rb
|
42
50
|
- lib/symbolic/extensions/numeric.rb
|
43
51
|
- lib/symbolic/extensions/rational.rb
|
44
52
|
- lib/symbolic/factors.rb
|
45
53
|
- lib/symbolic/function.rb
|
46
54
|
- lib/symbolic/math.rb
|
55
|
+
- lib/symbolic/misc.rb
|
56
|
+
- lib/symbolic/plugins/var_name.rb
|
57
|
+
- lib/symbolic/printer.rb
|
47
58
|
- lib/symbolic/statistics.rb
|
59
|
+
- lib/symbolic/sum.rb
|
48
60
|
- lib/symbolic/summands.rb
|
49
61
|
- lib/symbolic/variable.rb
|
50
|
-
- spec/
|
62
|
+
- spec/integration_spec.rb
|
51
63
|
- spec/spec_helper.rb
|
64
|
+
- spec/symbolic/equal_spec.rb
|
52
65
|
- spec/symbolic_spec.rb
|
53
|
-
|
66
|
+
- symbolic.gemspec
|
54
67
|
homepage: http://brainopia.github.com/symbolic
|
55
68
|
licenses: []
|
56
|
-
|
57
69
|
post_install_message:
|
58
|
-
rdoc_options:
|
59
|
-
|
60
|
-
require_paths:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
61
72
|
- lib
|
62
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
74
85
|
requirements: []
|
75
|
-
|
76
86
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.
|
87
|
+
rubygems_version: 1.8.24
|
78
88
|
signing_key:
|
79
89
|
specification_version: 3
|
80
90
|
summary: Symbolic math for ruby
|
81
|
-
test_files:
|
82
|
-
- spec/spec_helper.rb
|
83
|
-
- spec/symbolic_spec.rb
|
91
|
+
test_files: []
|
data/.gitignore
DELETED
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|