calculus 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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
- @postfix_notation = Parser.new(@source = source).parse
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
@@ -8,7 +8,7 @@ module Calculus
8
8
  def initialize(source)
9
9
  @operators = {:sqrt => 3, :exp => 3, :div => 2, :mul => 2, :plus => 1, :minus => 1, :eql => 0}
10
10
 
11
- super(source)
11
+ super(source.dup)
12
12
  end
13
13
 
14
14
  def parse
@@ -1,3 +1,3 @@
1
1
  module Calculus
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: calculus
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sergey Avseyev