symbolic 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/symbolic/core.rb +32 -29
- data/lib/symbolic/optimizations/subtraction.rb +6 -7
- data/lib/symbolic/optimizations.rb +7 -14
- data/spec/symbolic_spec.rb +1 -2
- metadata +1 -1
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.1.
|
8
|
+
gem.version = '0.1.3'
|
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/core.rb
CHANGED
@@ -21,48 +21,51 @@ module Symbolic
|
|
21
21
|
|
22
22
|
private
|
23
23
|
|
24
|
-
def
|
24
|
+
def numerical_eval(*args)
|
25
25
|
[Fixnum, Bignum, Float].each do |klass|
|
26
|
-
klass.class_eval
|
26
|
+
klass.class_eval *args
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
def redefine_numerical_methods
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
Symbolic.operations.each do |sign, name|
|
32
|
+
numerical_eval numerical_method(sign,name), __FILE__, __LINE__
|
33
|
+
end
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
Optimizations.#{operation_name} self, value
|
39
|
-
else
|
40
|
-
non_symbolic_#{operation_name}(value)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
CODE
|
36
|
+
def numerical_method(sign, name)
|
37
|
+
<<-CODE
|
38
|
+
alias non_symbolic_#{name} #{sign}
|
44
39
|
|
45
|
-
|
40
|
+
def #{sign}(value)
|
41
|
+
if value.is_a?(Operatable)
|
42
|
+
Optimizations.#{name} self, value
|
43
|
+
else
|
44
|
+
non_symbolic_#{name}(value)
|
45
|
+
end
|
46
46
|
end
|
47
|
-
|
48
|
-
end
|
47
|
+
CODE
|
48
|
+
end
|
49
49
|
|
50
50
|
def redefine_math_methods
|
51
51
|
Symbolic.math_operations.each do |operation|
|
52
|
-
|
53
|
-
|
52
|
+
Math.instance_eval math_method(operation), __FILE__, __LINE__
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def math_method(operation)
|
57
|
+
<<-CODE
|
58
|
+
alias non_symbolic_#{operation} #{operation}
|
54
59
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
60
|
+
def #{operation}(value)
|
61
|
+
if value.is_a?(Operatable)
|
62
|
+
Symbolic::Method.new value, :#{operation}
|
63
|
+
else
|
64
|
+
non_symbolic_#{operation} value
|
61
65
|
end
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
end # redefine_math_methods
|
66
|
+
end
|
67
|
+
CODE
|
68
|
+
end
|
66
69
|
|
67
70
|
def restore_numerical_methods
|
68
71
|
numerical_context do
|
@@ -2,15 +2,14 @@ module Symbolic::Optimizations::Subtraction
|
|
2
2
|
extend Symbolic::Optimizations::Base
|
3
3
|
|
4
4
|
def self.optimize_first_arg(var1, var2)
|
5
|
-
if var1 == 0
|
6
|
-
-var2
|
7
|
-
elsif negative?(var1)
|
8
|
-
-(var1.abs + var2)
|
9
|
-
end
|
5
|
+
-var2 if var1 == 0
|
10
6
|
end
|
11
7
|
|
12
8
|
def self.optimize_second_arg(var1, var2)
|
13
|
-
|
14
|
-
|
9
|
+
if var2 == 0
|
10
|
+
var1
|
11
|
+
elsif negative?(var2)
|
12
|
+
var1 + var2.abs
|
13
|
+
end
|
15
14
|
end
|
16
15
|
end
|
@@ -1,19 +1,12 @@
|
|
1
1
|
module Symbolic
|
2
2
|
module Optimizations
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def self.multiplication(var1, var2)
|
12
|
-
Multiplication.optimize(var1, var2) || Expression.new(var1, var2, '*')
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.division(var1, var2)
|
16
|
-
Division.optimize(var1, var2) || Expression.new(var1, var2, '/')
|
3
|
+
Symbolic.operations.each do |operation_sign, operation_name|
|
4
|
+
method = <<-CODE
|
5
|
+
def #{operation_name}(var1, var2)
|
6
|
+
#{operation_name.to_s.capitalize}.optimize(var1, var2) || Expression.new(var1, var2, '#{operation_sign}')
|
7
|
+
end
|
8
|
+
CODE
|
9
|
+
instance_eval method, __FILE__, __LINE__
|
17
10
|
end
|
18
11
|
end
|
19
12
|
end
|
data/spec/symbolic_spec.rb
CHANGED
@@ -61,12 +61,11 @@ describe "Symbolic" do
|
|
61
61
|
'-x + 2' => '2 - x',
|
62
62
|
'x + (-y)' => 'x - y',
|
63
63
|
'-y + x' => 'x - y',
|
64
|
-
'-y + (-x)' => '-(y + x)',
|
65
64
|
|
66
65
|
'0 - x' => '-x',
|
67
66
|
'x - 0' => 'x',
|
68
67
|
'x - (-2)' => 'x + 2',
|
69
|
-
|
68
|
+
'-2 - (-x)' => 'x - 2',
|
70
69
|
'x - (-y)' => 'x + y',
|
71
70
|
|
72
71
|
'0 * x' => '0',
|