calculus 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -0
- data/lib/calculus/expression.rb +17 -3
- data/lib/calculus/parser.rb +1 -1
- data/lib/calculus/version.rb +1 -1
- data/test/expression_test.rb +25 -2
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -34,6 +34,11 @@ You can also render expression to PNG image if you have <tt>latex</tt> and <tt>d
|
|
34
34
|
|
35
35
|
{2 + 3 \cdot x}[http://files.avsej.net/expression.png]
|
36
36
|
|
37
|
+
Also you can skip parser if you need only png generation
|
38
|
+
011:0> Calculus::Expression.new("\\hat{f}(\\xi) = \\int_{-\\infty}^{\\infty} f(x)\\ e^{- 2\\pi i x \\xi}\\,dx", :parse => false).to_png
|
39
|
+
|
40
|
+
{\hat{f}(\xi) = \int_{-\infty}^{\infty} f(x)\ e^{- 2\pi i x \xi}\,dx}[http://files.avsej.net/expression-without-parser.png]
|
41
|
+
|
37
42
|
Don't forget to cleanup file after using.
|
38
43
|
|
39
44
|
== Hacking
|
data/lib/calculus/expression.rb
CHANGED
@@ -11,12 +11,19 @@ module Calculus
|
|
11
11
|
attr_reader :postfix_notation
|
12
12
|
alias :rpn :postfix_notation
|
13
13
|
|
14
|
-
def initialize(source)
|
15
|
-
|
14
|
+
def initialize(source, options = {})
|
15
|
+
options = {:parse => true}.merge(options)
|
16
|
+
@source = source
|
17
|
+
@postfix_notation = options[:parse] ? Parser.new(source).parse : []
|
18
|
+
raise ArgumentError, "Should be no more that one equals sign" if @postfix_notation.count(:eql) > 1
|
16
19
|
@variables = extract_variables
|
17
20
|
update_sha1
|
18
21
|
end
|
19
22
|
|
23
|
+
def parsed?
|
24
|
+
!@postfix_notation.empty?
|
25
|
+
end
|
26
|
+
|
20
27
|
def variables
|
21
28
|
@variables.keys
|
22
29
|
end
|
@@ -80,7 +87,14 @@ module Calculus
|
|
80
87
|
alias :ast :abstract_syntax_tree
|
81
88
|
|
82
89
|
def to_s
|
83
|
-
source
|
90
|
+
result = source.dup
|
91
|
+
(variables - unbound_variables).each do |var|
|
92
|
+
result.gsub!(/(\W)#{var}(\W)/, "\\1#{@variables[var]}\\2")
|
93
|
+
result.gsub!(/^#{var}(\W)/, "#{@variables[var]}\\1")
|
94
|
+
result.gsub!(/(\W)#{var}$/, "\\1#{@variables[var]}")
|
95
|
+
result.gsub!(/^#{var}$/, @variables[var].to_s)
|
96
|
+
end
|
97
|
+
result
|
84
98
|
end
|
85
99
|
|
86
100
|
def inspect
|
data/lib/calculus/parser.rb
CHANGED
data/lib/calculus/version.rb
CHANGED
data/test/expression_test.rb
CHANGED
@@ -84,10 +84,33 @@ class TestExpression < MiniTest::Unit::TestCase
|
|
84
84
|
refute_equal old_sha1, exp.sha1
|
85
85
|
end
|
86
86
|
|
87
|
+
def test_that_it_raises_error_if_there_are_more_than_one_equals_sign
|
88
|
+
assert_raises(ArgumentError) { expression("2 * 3 = 2 + 2 + 2 = 6") }
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_it_substitutes_variables_for_string_representation
|
92
|
+
assert_equal "2 + 2 * 4", expression("2 + 2 * x", "x" => 4).to_s
|
93
|
+
assert_equal "2 + 2*4-3", expression("2 + 2*x-3", "x" => 4).to_s
|
94
|
+
assert_equal "\\frac{1}{3}", expression("\\frac{1}{x}", "x" => 3).to_s
|
95
|
+
assert_equal "2 + 2^4", expression("2 + 2^x", "x" => 4).to_s
|
96
|
+
assert_equal "2 + 2 * 4 + x_2", expression("2 + 2 * x_1 + x_2", "x_1" => 4).to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_that_it_allow_skip_parser
|
100
|
+
exp = Calculus::Expression.new("\\hat{f}(\\xi) = \\int_{-\\infty}^{\\infty} f(x)\\ e^{- 2\\pi i x \\xi}\\,dx", :parse => false)
|
101
|
+
refute exp.parsed?
|
102
|
+
assert_equal [], exp.postfix_notation
|
103
|
+
assert_equal Hash.new, exp.instance_variable_get("@variables")
|
104
|
+
end
|
105
|
+
|
87
106
|
protected
|
88
107
|
|
89
|
-
def expression(input)
|
90
|
-
Calculus::Expression.new(input)
|
108
|
+
def expression(input, substitutions = {})
|
109
|
+
exp = Calculus::Expression.new(input)
|
110
|
+
substitutions.keys.each do |var|
|
111
|
+
exp[var] = substitutions[var]
|
112
|
+
end
|
113
|
+
exp
|
91
114
|
end
|
92
115
|
|
93
116
|
end
|