symbolic 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/symbolic/operation.rb +13 -1
- data/lib/symbolic/operation/binary.rb +0 -10
- data/lib/symbolic/operation/binary/division.rb +4 -2
- data/lib/symbolic/operation/binary/exponentiation.rb +4 -2
- data/lib/symbolic/operation/binary/multiplication.rb +3 -1
- data/lib/symbolic/operation/binary/subtraction.rb +2 -0
- data/lib/symbolic/operation/unary/minus.rb +14 -7
- data/lib/symbolic/variable.rb +1 -0
- data/spec/symbolic_spec.rb +29 -4
- metadata +2 -2
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ begin
|
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "symbolic"
|
8
|
-
gem.version = '0.2.
|
8
|
+
gem.version = '0.2.2'
|
9
9
|
gem.summary = 'Symbolic math for ruby'
|
10
10
|
gem.description = %Q{Symbolic math can be really helpful if you want to simplify some giant equation or if you don't want to get a performance hit re-evaluating big math expressions every time when few variables change.}
|
11
11
|
gem.email = "ravwar@gmail.com"
|
data/lib/symbolic/operation.rb
CHANGED
@@ -11,7 +11,7 @@ class Symbolic::Operation
|
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def operation(expression)
|
14
|
+
def self.operation(expression)
|
15
15
|
case expression
|
16
16
|
when Unary::Minus then :unary_minus
|
17
17
|
when Binary::Addition then :addition
|
@@ -22,6 +22,10 @@ class Symbolic::Operation
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def operation(expression)
|
26
|
+
self.class.operation expression
|
27
|
+
end
|
28
|
+
|
25
29
|
def unary_minus?(expression)
|
26
30
|
operation(expression) == :unary_minus
|
27
31
|
end
|
@@ -45,4 +49,12 @@ class Symbolic::Operation
|
|
45
49
|
def variable?(expression)
|
46
50
|
operation(expession) == :variable
|
47
51
|
end
|
52
|
+
|
53
|
+
def brackets(var)
|
54
|
+
brackets_for.include?(operation var) ? "(#{var})" : var.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def brackets_for
|
58
|
+
[]
|
59
|
+
end
|
48
60
|
end
|
@@ -14,12 +14,6 @@ class Symbolic::Operation::Binary < Symbolic::Operation
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def self.brackets_for(*operations)
|
18
|
-
def brackets(var)
|
19
|
-
operations.include?(operation var) ? "(#{var})" : var.to_s
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
17
|
def initialize(var1, var2)
|
24
18
|
@var1, @var2 = var1, var2
|
25
19
|
end
|
@@ -57,8 +51,4 @@ class Symbolic::Operation::Binary < Symbolic::Operation
|
|
57
51
|
def operations_of(var)
|
58
52
|
var.is_a?(Symbolic) ? var.detailed_operations : Hash.new(0)
|
59
53
|
end
|
60
|
-
|
61
|
-
def brackets(var)
|
62
|
-
var.to_s
|
63
|
-
end
|
64
54
|
end
|
@@ -1,6 +1,4 @@
|
|
1
1
|
class Symbolic::Operation::Binary::Division < Symbolic::Operation::Binary
|
2
|
-
brackets_for :unary_minus, :addition, :subtraction
|
3
|
-
|
4
2
|
def self.simplify_first_arg(var1, var2)
|
5
3
|
if var1 == 0
|
6
4
|
0
|
@@ -20,4 +18,8 @@ class Symbolic::Operation::Binary::Division < Symbolic::Operation::Binary
|
|
20
18
|
def sign
|
21
19
|
'/'
|
22
20
|
end
|
21
|
+
|
22
|
+
def brackets_for
|
23
|
+
[:unary_minus, :addition, :subtraction]
|
24
|
+
end
|
23
25
|
end
|
@@ -1,6 +1,4 @@
|
|
1
1
|
class Symbolic::Operation::Binary::Exponentiation < Symbolic::Operation::Binary
|
2
|
-
brackets_for :unary_minus, :addition, :subtraction, :multiplication, :division
|
3
|
-
|
4
2
|
def self.simplify_first_arg(var1, var2)
|
5
3
|
# add conditional for negative?(var1) and even or odd values of var2
|
6
4
|
if var1 == 0
|
@@ -18,6 +16,10 @@ class Symbolic::Operation::Binary::Exponentiation < Symbolic::Operation::Binary
|
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
19
|
+
def brackets_for
|
20
|
+
[:unary_minus, :addition, :subtraction, :multiplication, :division]
|
21
|
+
end
|
22
|
+
|
21
23
|
def sign
|
22
24
|
'**'
|
23
25
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
class Symbolic::Operation::Binary::Multiplication < Symbolic::Operation::Binary
|
2
2
|
symmetric
|
3
|
-
brackets_for :unary_minus, :addition, :subtraction
|
4
3
|
|
5
4
|
def self.simplify_first_arg(var1, var2)
|
6
5
|
if var1 == 0
|
@@ -16,6 +15,9 @@ class Symbolic::Operation::Binary::Multiplication < Symbolic::Operation::Binary
|
|
16
15
|
'*'
|
17
16
|
end
|
18
17
|
|
18
|
+
def brackets_for
|
19
|
+
[:unary_minus, :addition, :subtraction]
|
20
|
+
end
|
19
21
|
# def initialize(var1, var2)
|
20
22
|
# super
|
21
23
|
# coef, numerators, denominators = unite_factors factors_of(var1), factors_of(var2)
|
@@ -1,7 +1,14 @@
|
|
1
1
|
class Symbolic::Operation
|
2
2
|
class Unary::Minus < Unary
|
3
3
|
def self.simplify(expression)
|
4
|
-
|
4
|
+
case operation(expression)
|
5
|
+
when :unary_minus
|
6
|
+
expression.abs
|
7
|
+
when :addition
|
8
|
+
-expression.send(:var1)-expression.send(:var2)
|
9
|
+
when :subtraction
|
10
|
+
expression.send(:var2) - expression.send(:var1)
|
11
|
+
end
|
5
12
|
end
|
6
13
|
|
7
14
|
def abs
|
@@ -9,12 +16,8 @@ class Symbolic::Operation
|
|
9
16
|
@expression
|
10
17
|
end
|
11
18
|
|
12
|
-
def
|
13
|
-
|
14
|
-
"-#{@expression}"
|
15
|
-
else
|
16
|
-
"(-#{@expression})"
|
17
|
-
end
|
19
|
+
def brackets_for
|
20
|
+
[:addition, :subtraction]
|
18
21
|
end
|
19
22
|
|
20
23
|
def value
|
@@ -28,5 +31,9 @@ class Symbolic::Operation
|
|
28
31
|
def detailed_operations
|
29
32
|
@expression.detailed_operations.tap {|it| it['-@'] += 1}
|
30
33
|
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
"-#{brackets @expression}"
|
37
|
+
end
|
31
38
|
end
|
32
39
|
end
|
data/lib/symbolic/variable.rb
CHANGED
data/spec/symbolic_spec.rb
CHANGED
@@ -96,12 +96,37 @@ describe "Symbolic" do
|
|
96
96
|
describe 'general methods:' do
|
97
97
|
should_equal \
|
98
98
|
'x.variables' => '[x]',
|
99
|
-
'(-(x
|
99
|
+
'(-(x+y)).variables' => '[x,y]',
|
100
100
|
'(x+y).undefined_variables' => '[]',
|
101
|
-
'(-x-4*y+5-y/x).detailed_operations' =>
|
102
|
-
'{"+" => 1, "-" => 2, "*" => 1, "/" => 1, "-@" => 1}'
|
101
|
+
'(-x**y-4*y+5-y/x).detailed_operations' =>
|
102
|
+
'{"+" => 1, "-" => 2, "*" => 1, "/" => 1, "-@" => 1, "**" => 1}',
|
103
|
+
'(-x**y-4*y+5-y/x).operations' => '7'
|
103
104
|
end
|
104
105
|
|
105
|
-
describe "
|
106
|
+
describe "to_s:" do
|
107
|
+
def self.should_print(conditions)
|
108
|
+
conditions.each do |symbolic_expression, result|
|
109
|
+
it symbolic_expression do
|
110
|
+
expression(symbolic_expression).to_s.should == result
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
should_print \
|
116
|
+
'x' => 'x',
|
117
|
+
'-x' => '-x',
|
118
|
+
'x+1' => 'x+1',
|
119
|
+
'x-4' => 'x-4',
|
120
|
+
'-(x+y)' => '-x-y',
|
121
|
+
'-(x-y)' => 'y-x',
|
122
|
+
'x*y' => 'x*y',
|
123
|
+
'(-x)*y' => '-x*y',
|
124
|
+
'(x+2)*(y+3)*4' => '(x+2)*(y+3)*4',
|
125
|
+
'4/x' => '4/x',
|
126
|
+
'(-(2+x))/(-(-y))' => '(-2-x)/y',
|
127
|
+
'x**y' => 'x**y',
|
128
|
+
'x**(y-4)' => 'x**(y-4)',
|
129
|
+
'(x+1)**(y*2)' => '(x+1)**(y*2)',
|
130
|
+
'-(x**y -2)+5' => '2-x**y+5'
|
106
131
|
end
|
107
132
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brainopia
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-09 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|