symbolic 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -0
- data/Rakefile +1 -1
- data/lib/extensions/matrix.rb +0 -4
- data/lib/extensions/numeric.rb +0 -4
- data/lib/symbolic/core.rb +2 -2
- data/lib/symbolic/expression.rb +12 -4
- data/lib/symbolic/method.rb +9 -1
- data/lib/symbolic/unary_minus.rb +10 -2
- data/lib/symbolic/variable.rb +7 -1
- data/lib/symbolic.rb +25 -1
- metadata +2 -3
- data/lib/symbolic/operatable.rb +0 -28
data/README.rdoc
CHANGED
@@ -17,6 +17,8 @@ Currently I've implemented only naive optimizations to simplify math expressions
|
|
17
17
|
f = symbolic { (4 - 2*(-x) + y*(-1.0))*x - 0*y + 0 - 2 }
|
18
18
|
puts f # => (4+2*x-y)*x-2
|
19
19
|
|
20
|
+
p f.detailed_operations # => {"+"=>1, "-"=>2, "*"=>2}
|
21
|
+
p f.operations # => 5
|
20
22
|
p f.undefined_variables.map &:name # => ["x", "y"]
|
21
23
|
x.value = 2
|
22
24
|
y.value = 4
|
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.9'
|
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/extensions/matrix.rb
CHANGED
data/lib/extensions/numeric.rb
CHANGED
data/lib/symbolic/core.rb
CHANGED
@@ -38,7 +38,7 @@ module Symbolic
|
|
38
38
|
alias non_symbolic_#{name} #{sign}
|
39
39
|
|
40
40
|
def #{sign}(value)
|
41
|
-
if value.
|
41
|
+
if value.is_a? Symbolic
|
42
42
|
Optimization.#{name} self, value
|
43
43
|
else
|
44
44
|
non_symbolic_#{name}(value)
|
@@ -58,7 +58,7 @@ module Symbolic
|
|
58
58
|
alias non_symbolic_#{operation} #{operation}
|
59
59
|
|
60
60
|
def #{operation}(value)
|
61
|
-
if value.
|
61
|
+
if value.is_a? Symbolic
|
62
62
|
Symbolic::Method.new value, :#{operation}
|
63
63
|
else
|
64
64
|
non_symbolic_#{operation} value
|
data/lib/symbolic/expression.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Symbolic
|
2
|
-
class Expression
|
2
|
+
class Expression
|
3
|
+
include Symbolic
|
4
|
+
|
3
5
|
attr_reader :var1, :var2, :operation
|
4
6
|
|
5
7
|
def initialize(var1, var2, operation)
|
@@ -33,7 +35,7 @@ module Symbolic
|
|
33
35
|
end
|
34
36
|
|
35
37
|
def variables
|
36
|
-
|
38
|
+
@var1.variables | @var2.variables
|
37
39
|
end
|
38
40
|
|
39
41
|
def undefined_variables
|
@@ -45,6 +47,12 @@ module Symbolic
|
|
45
47
|
((object.var1 == @var1 && object.var2 == @var2) || ((plus? || multiply?) && (object.var1 == @var2 && object.var2 == @var1)))
|
46
48
|
end
|
47
49
|
|
50
|
+
def detailed_operations
|
51
|
+
stats = operations_of(@var1).merge(operations_of @var2) {|k,v1,v2| v1 + v2 }
|
52
|
+
stats[@operation] += 1
|
53
|
+
stats
|
54
|
+
end
|
55
|
+
|
48
56
|
private
|
49
57
|
|
50
58
|
def brackets(var)
|
@@ -55,8 +63,8 @@ module Symbolic
|
|
55
63
|
%w(* /).include?(@operation) && (var.is_a?(UnaryMinus) || var.is_a?(Expression) && (var.plus? || var.minus?))
|
56
64
|
end
|
57
65
|
|
58
|
-
def
|
59
|
-
|
66
|
+
def operations_of(var)
|
67
|
+
var.is_a?(Symbolic) ? var.detailed_operations : Hash.new(0)
|
60
68
|
end
|
61
69
|
end
|
62
70
|
end
|
data/lib/symbolic/method.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Symbolic
|
2
|
-
class Method
|
2
|
+
class Method
|
3
|
+
include Symbolic
|
4
|
+
|
3
5
|
def initialize(variable, operation)
|
4
6
|
@variable, @operation = variable, operation
|
5
7
|
end
|
@@ -19,5 +21,11 @@ module Symbolic
|
|
19
21
|
def undefined_variables
|
20
22
|
@variable.undefined_variables
|
21
23
|
end
|
24
|
+
|
25
|
+
def detailed_operations
|
26
|
+
stats = @variable.detailed_operations.dup
|
27
|
+
stats[@operation] += 1
|
28
|
+
stats
|
29
|
+
end
|
22
30
|
end
|
23
31
|
end
|
data/lib/symbolic/unary_minus.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Symbolic
|
2
|
-
class UnaryMinus
|
2
|
+
class UnaryMinus
|
3
|
+
include Symbolic
|
4
|
+
|
3
5
|
def self.create(var)
|
4
6
|
var.is_a?(UnaryMinus) ? var.abs : new(var)
|
5
7
|
end
|
@@ -17,7 +19,7 @@ module Symbolic
|
|
17
19
|
end
|
18
20
|
|
19
21
|
def abs
|
20
|
-
# add a common method for
|
22
|
+
# TODO: add a common method for Symbolic which will point to an instance of Abs(magnitude) class
|
21
23
|
@variable
|
22
24
|
end
|
23
25
|
|
@@ -36,5 +38,11 @@ module Symbolic
|
|
36
38
|
def ==(object)
|
37
39
|
object.is_a?(UnaryMinus) && object.abs == @variable
|
38
40
|
end
|
41
|
+
|
42
|
+
def detailed_operations
|
43
|
+
stats = @variable.detailed_operations.dup
|
44
|
+
stats['-@'] += 1
|
45
|
+
stats
|
46
|
+
end
|
39
47
|
end
|
40
48
|
end
|
data/lib/symbolic/variable.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Symbolic
|
2
|
-
class Variable
|
2
|
+
class Variable
|
3
|
+
include Symbolic
|
4
|
+
|
3
5
|
attr_accessor :name, :proc
|
4
6
|
attr_writer :value
|
5
7
|
|
@@ -23,5 +25,9 @@ module Symbolic
|
|
23
25
|
def undefined_variables
|
24
26
|
value ? [] : variables
|
25
27
|
end
|
28
|
+
|
29
|
+
def detailed_operations
|
30
|
+
Hash.new 0
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
data/lib/symbolic.rb
CHANGED
@@ -6,13 +6,37 @@ module Symbolic
|
|
6
6
|
:/ => :division }
|
7
7
|
end
|
8
8
|
|
9
|
+
operations.each do |sign, name|
|
10
|
+
method = <<-CODE
|
11
|
+
def #{sign}(value)
|
12
|
+
Optimization.#{name} self, value
|
13
|
+
end
|
14
|
+
CODE
|
15
|
+
class_eval method, __FILE__, __LINE__
|
16
|
+
end
|
17
|
+
|
9
18
|
def self.math_operations
|
10
19
|
[:cos, :sin]
|
11
20
|
end
|
21
|
+
|
22
|
+
def -@
|
23
|
+
UnaryMinus.create self
|
24
|
+
end
|
25
|
+
|
26
|
+
def +@
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def undefined?
|
31
|
+
!value
|
32
|
+
end
|
33
|
+
|
34
|
+
def operations
|
35
|
+
detailed_operations.values.inject(0) {|sum,it| sum + it }
|
36
|
+
end
|
12
37
|
end
|
13
38
|
|
14
39
|
require 'symbolic/core'
|
15
|
-
require 'symbolic/operatable'
|
16
40
|
require 'symbolic/optimizations'
|
17
41
|
require 'symbolic/optimizations/base'
|
18
42
|
require 'symbolic/optimizations/addition'
|
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.
|
4
|
+
version: 0.1.9
|
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-04 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -41,7 +41,6 @@ files:
|
|
41
41
|
- lib/symbolic/core.rb
|
42
42
|
- lib/symbolic/expression.rb
|
43
43
|
- lib/symbolic/method.rb
|
44
|
-
- lib/symbolic/operatable.rb
|
45
44
|
- lib/symbolic/optimizations.rb
|
46
45
|
- lib/symbolic/optimizations/addition.rb
|
47
46
|
- lib/symbolic/optimizations/base.rb
|
data/lib/symbolic/operatable.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module Symbolic
|
2
|
-
class Operatable
|
3
|
-
def -@
|
4
|
-
UnaryMinus.create self
|
5
|
-
end
|
6
|
-
|
7
|
-
def +@
|
8
|
-
self
|
9
|
-
end
|
10
|
-
|
11
|
-
Symbolic.operations.each do |operation_sign, operation_name|
|
12
|
-
method = <<-CODE
|
13
|
-
def #{operation_sign}(value)
|
14
|
-
Optimization.#{operation_name} self, value
|
15
|
-
end
|
16
|
-
CODE
|
17
|
-
class_eval method, __FILE__, __LINE__
|
18
|
-
end
|
19
|
-
|
20
|
-
def undefined?
|
21
|
-
!value
|
22
|
-
end
|
23
|
-
|
24
|
-
def symbolic?
|
25
|
-
true
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|