Class: LatexEval::Equation
- Inherits:
-
Object
- Object
- LatexEval::Equation
- Defined in:
- lib/latex_eval/equation.rb
Instance Attribute Summary collapse
-
#binary_key ⇒ Object
readonly
Returns the value of attribute binary_key.
-
#equation ⇒ Object
readonly
Returns the value of attribute equation.
-
#unary_key ⇒ Object
readonly
Returns the value of attribute unary_key.
Instance Method Summary collapse
-
#initialize(equation) ⇒ Equation
constructor
A new instance of Equation.
- #postfix_notation ⇒ Object
Constructor Details
#initialize(equation) ⇒ Equation
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/latex_eval/equation.rb', line 6 def initialize(equation) @equation = equation @binary_key = { "+" => { symbol: :add, priority: 0, right_priority: false }, "-" => { symbol: :subtract, priority: 0, right_priority: false }, "*" => { symbol: :multiply, priority: 1, right_priority: false }, "/" => { symbol: :divide, priority: 1, right_priority: false }, "%" => { symbol: :mod, priority: 1, right_priority: false }, "^" => { symbol: :power, priority: 2, right_priority: true } } @unary_key = { "-" => { symbol: :negative, }, "+" => { symbol: :positive, }, } end |
Instance Attribute Details
#binary_key ⇒ Object (readonly)
Returns the value of attribute binary_key.
4 5 6 |
# File 'lib/latex_eval/equation.rb', line 4 def binary_key @binary_key end |
#equation ⇒ Object (readonly)
Returns the value of attribute equation.
4 5 6 |
# File 'lib/latex_eval/equation.rb', line 4 def equation @equation end |
#unary_key ⇒ Object (readonly)
Returns the value of attribute unary_key.
4 5 6 |
# File 'lib/latex_eval/equation.rb', line 4 def unary_key @unary_key end |
Instance Method Details
#postfix_notation ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/latex_eval/equation.rb', line 50 def postfix_notation out = [] bank = [] bracket = [] unary = [] # start with an assumed array bank.push [] bracket.push [] unary.push [] equation.gsub(" ", "").split(/([%\)\(\^*+-\/])/).each do |value| if value != "" if value == "(" bank.push [] bracket.push [] unary.push [] elsif value == ")" last = bracket.pop() bracket.last.concat last bracket.last.concat bank.pop().reverse.map { |e| binary_key[e][:symbol] } bracket.last.concat unary.pop().reverse.map { |e| unary_key[e][:symbol] } elsif unary_key.has_key? value and (bracket.last.empty? || bracket.last.length == bank.last.length) unary.last.push value elsif binary_key.has_key? value num_popped = 0 bank.last.reverse_each do |b| if (binary_key[b][:right_priority] ? binary_key[b][:priority] > binary_key[value][:priority] : binary_key[b][:priority] >= binary_key[value][:priority]) num_popped += 1 else break end end bracket.last.concat unary.pop().reverse.map { |e| unary_key[e][:symbol] } unary.push [] bracket.last.concat bank.last.pop(num_popped).reverse.map { |e| binary_key[e][:symbol] } bank.last.push value else if /[a-zA-Z]+/.match(value) bracket.last.push value.to_sym else bracket.last.push value.to_f end end end end out.concat bracket.pop() out.concat unary.pop().reverse.map { |e| unary_key[e][:symbol] } out.concat bank.pop().reverse.map { |e| binary_key[e][:symbol] } return out end |