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 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.2'
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 numerical_context(&proc)
24
+ def numerical_eval(*args)
25
25
  [Fixnum, Bignum, Float].each do |klass|
26
- klass.class_eval &proc
26
+ klass.class_eval *args
27
27
  end
28
28
  end
29
29
 
30
30
  def redefine_numerical_methods
31
- numerical_context do
32
- Symbolic.operations.each do |operation_sign, operation_name|
33
- alias_method "non_symbolic_#{operation_name}", operation_sign
31
+ Symbolic.operations.each do |sign, name|
32
+ numerical_eval numerical_method(sign,name), __FILE__, __LINE__
33
+ end
34
+ end
34
35
 
35
- method = <<-CODE
36
- def #{operation_sign}(value)
37
- if value.is_a?(Operatable)
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
- class_eval method, __FILE__, __LINE__
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
- end # numerical_context
48
- end # redefine_numerical_methods
47
+ CODE
48
+ end
49
49
 
50
50
  def redefine_math_methods
51
51
  Symbolic.math_operations.each do |operation|
52
- code = <<-CODE
53
- alias non_symbolic_#{operation} #{operation}
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
- def #{operation}(value)
56
- if value.is_a? Operatable
57
- Symbolic::Method.new value, :#{operation}
58
- else
59
- non_symbolic_#{operation} value
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
- CODE
63
- Math.instance_eval code, __FILE__, __LINE__
64
- end
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
- reverse_optimization = optimize_first_arg(var2, var1)
14
- -reverse_optimization if reverse_optimization
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
- def self.addition(var1, var2)
4
- Addition.optimize(var1, var2) || Expression.new(var1, var2, '+')
5
- end
6
-
7
- def self.subtraction(var1, var2)
8
- Subtraction.optimize(var1, var2) || Expression.new(var1, var2, '-')
9
- end
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
@@ -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
- # '-2 - (-x)' => 'x - 2',
68
+ '-2 - (-x)' => 'x - 2',
70
69
  'x - (-y)' => 'x + y',
71
70
 
72
71
  '0 * x' => '0',
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.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - brainopia