calculus 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b35d43f055328b7d3abdb3a3d3f1f126d19b33c
4
+ data.tar.gz: 38314dc9dac26b58ae7ccb5b41d1d6a7efea1a7c
5
+ SHA512:
6
+ metadata.gz: 2be2752f313d8e107919f602ab980d22a52a060036c396ed03c66ebf7a66fd5b2ee46baf2a280e1fb95e0eb2b7e712740ae7d6e3277f9ba4896ae5ab7a8fe8d4
7
+ data.tar.gz: cf0451676cf5c4513eda0f6f4f0516bb582dbc4a07d4c65cc761c71c8c9c1bc30fa3501299c07c68ae258f3843924ee575fced2fa8660029d586ccb0a59df23a
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
@@ -2,6 +2,8 @@
2
2
 
3
3
  Calculus is utility library which allow to parse some subset of latex equations and store them in {Postfix notation}[http://en.wikipedia.org/wiki/Reverse_Polish_notation] It also allows translate it to {Abstract syntax tree}[http://en.wikipedia.org/wiki/Abstract_syntax_tree] and calculate (implemented for simple expressions).
4
4
 
5
+ Build status: {<img src="http://travis-ci.org/avsej/calculus.png" />}[http://travis-ci.org/avsej/calculus]
6
+
5
7
  == Installation
6
8
 
7
9
  gem install calculus
@@ -23,4 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
24
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
25
  s.require_paths = ["lib"]
26
+
27
+ s.add_development_dependency 'minitest'
28
+ s.add_development_dependency 'rake'
26
29
  end
@@ -97,7 +97,7 @@ module Calculus
97
97
  @postfix_notation.each do |node|
98
98
  case node
99
99
  when Symbol
100
- operation, right, left = node, stack.pop, stack.pop
100
+ operation, right, left = node, (node == :uminus ? nil : stack.pop), stack.pop
101
101
  stack.push(yield(operation, left, right, stack))
102
102
  when Numeric
103
103
  stack.push(node)
@@ -124,12 +124,13 @@ module Calculus
124
124
 
125
125
  traverse do |operation, left, right, stack|
126
126
  case operation
127
- when :sqrt then left ** (1.0 / right) # could cause some rounding errors
128
- when :exp then left ** right
129
- when :plus then left + right
130
- when :minus then left - right
131
- when :mul then left * right
132
- when :div then left / right
127
+ when :uminus then -left
128
+ when :sqrt then left ** (1.0 / right) # could cause some rounding errors
129
+ when :exp then left ** right
130
+ when :plus then left + right
131
+ when :minus then left - right
132
+ when :mul then left * right
133
+ when :div then left / right
133
134
  end
134
135
  end
135
136
  end
@@ -2,7 +2,7 @@ require 'tmpdir'
2
2
 
3
3
  module Calculus
4
4
 
5
- # Renders expression to PNG image using <tt>latex<tt> and
5
+ # Renders expression to PNG image using <tt>latex</tt> and
6
6
  # <tt>dvipng</tt>
7
7
  module Latex
8
8
 
@@ -46,9 +46,9 @@ module Calculus
46
46
 
47
47
  # Initialize parser with given source string. It could simple
48
48
  # (native expression like <tt>2 + 3 * (4 / 3)</tt>, but also in TeX
49
- # style <tt>2 + 3 \cdot \frac{4}{3}.
49
+ # style <tt>2 + 3 \cdot \frac{4}{3}</tt>.
50
50
  def initialize(source)
51
- @operators = {:sqrt => 3, :exp => 3, :div => 2, :mul => 2, :plus => 1, :minus => 1, :eql => 0}
51
+ @operators = {:uminus => 4, :sqrt => 3, :exp => 3, :div => 2, :mul => 2, :plus => 1, :minus => 1, :eql => 0}
52
52
 
53
53
  super(source.dup)
54
54
  end
@@ -61,18 +61,22 @@ module Calculus
61
61
  def parse
62
62
  exp = []
63
63
  stack = []
64
+ token = :none
64
65
  while true
65
- case token = fetch_token
66
+ prev, token = token, fetch_token
67
+ case token
66
68
  when :open
67
69
  stack.push(token)
68
70
  when :close
69
71
  exp << stack.pop while operators.keys.include?(stack.last)
70
72
  stack.pop if stack.last == :open
71
73
  when :plus, :minus, :mul, :div, :exp, :sqrt, :eql
74
+ token = :uminus if prev && (prev == :none || prev != :close) && token == :minus
72
75
  exp << stack.pop while operators.keys.include?(stack.last) && operators[stack.last] >= operators[token]
73
76
  stack.push(token)
74
77
  when Numeric, String
75
78
  exp << token
79
+ token = nil
76
80
  when nil
77
81
  break
78
82
  else
@@ -1,3 +1,3 @@
1
1
  module Calculus
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'calculus'
3
3
 
4
- class TestExpression < MiniTest::Unit::TestCase
4
+ class TestExpression < MiniTest::Test
5
5
 
6
6
  def test_that_it_extract_variables_properly
7
7
  assert_equal ["x", "y"], expression("x + 2^x = y").variables
@@ -103,6 +103,10 @@ class TestExpression < MiniTest::Unit::TestCase
103
103
  assert_equal Hash.new, exp.instance_variable_get("@variables")
104
104
  end
105
105
 
106
+ def test_that_it_handles_unary_minus
107
+ assert_equal 6, expression("-3 * -2").calculate
108
+ end
109
+
106
110
  protected
107
111
 
108
112
  def expression(input, substitutions = {})
@@ -1,7 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'calculus'
3
3
 
4
- class TestParser < MiniTest::Unit::TestCase
4
+ class TestParser < MiniTest::Test
5
5
 
6
6
  def test_that_it_parses_simple_arithmetic
7
7
  assert_equal [1, 2, :plus], parse("1+2")
@@ -61,6 +61,11 @@ class TestParser < MiniTest::Unit::TestCase
61
61
  assert_raises(Calculus::ParserError) { assert_equal [2, "x__2", :mul, 16, :eql], parse("2 \\cdot x__2 = 16") }
62
62
  end
63
63
 
64
+ def test_that_it_handles_unary_minus
65
+ assert_equal [2, :uminus], parse("-2")
66
+ assert_equal [2, :uminus, 2, :uminus, :mul], parse("-2 * -2")
67
+ end
68
+
64
69
  protected
65
70
 
66
71
  def parse(input)
metadata CHANGED
@@ -1,31 +1,55 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: calculus
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Sergey Avseyev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-05-13 00:00:00 +03:00
14
- default_executable:
15
- dependencies: []
16
-
17
- description: A ruby parser for TeX equations. It parses equations to postfix (reverse polish) notation and can build abstract syntax tree (AST). Also it can render images via latex. Requres modern ruby 1.9.x because of using advanced oniguruma regex engine
18
- email:
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A ruby parser for TeX equations. It parses equations to postfix (reverse
42
+ polish) notation and can build abstract syntax tree (AST). Also it can render images
43
+ via latex. Requres modern ruby 1.9.x because of using advanced oniguruma regex engine
44
+ email:
19
45
  - sergey.avseyev@gmail.com
20
46
  executables: []
21
-
22
47
  extensions: []
23
-
24
48
  extra_rdoc_files: []
25
-
26
- files:
49
+ files:
27
50
  - .gitignore
28
51
  - .rvmrc
52
+ - .travis.yml
29
53
  - Gemfile
30
54
  - README.rdoc
31
55
  - Rakefile
@@ -37,35 +61,32 @@ files:
37
61
  - lib/calculus/version.rb
38
62
  - test/expression_test.rb
39
63
  - test/parser_test.rb
40
- has_rdoc: true
41
64
  homepage: http://avsej.net/calculus
42
65
  licenses: []
43
-
66
+ metadata: {}
44
67
  post_install_message:
45
- rdoc_options:
68
+ rdoc_options:
46
69
  - --main
47
70
  - README.rdoc
48
- require_paths:
71
+ require_paths:
49
72
  - lib
50
- required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "1.9"
56
- required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: "0"
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '1.9'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
62
83
  requirements: []
63
-
64
84
  rubyforge_project: calculus
65
- rubygems_version: 1.6.2
85
+ rubygems_version: 2.0.14
66
86
  signing_key:
67
- specification_version: 3
87
+ specification_version: 4
68
88
  summary: A ruby parser for TeX equations
69
- test_files:
89
+ test_files:
70
90
  - test/expression_test.rb
71
91
  - test/parser_test.rb
92
+ has_rdoc: true