Module: Eng::Formula
Instance Method Summary collapse
- #liner_function(formula) ⇒ Object
- #new_liner_function(formula) ⇒ Object
- #operator_reg(kind_operator) ⇒ Object
- #reverse_operator(operator, opt = true) ⇒ Object
- #split_by_ari(formula) ⇒ Object
- #split_value_unit(formula) ⇒ Object
Instance Method Details
#liner_function(formula) ⇒ Object
42 43 44 45 46 47 48 49 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 |
# File 'lib/eng_formula.rb', line 42 def liner_function(formula) formula_arr = formula.split(/(\*|\/|\+|-)/) input_num = formula_arr.index("value") mv_num = formula_arr.index{ |n| n =~ /\*|\// } pm_num = formula_arr.index{ |n| n =~ /\+|-/ } if mv_num a_ari = formula_arr[mv_num] if input_num > mv_num a = formula_arr[mv_num - 1] else a = formula_arr[mv_num + 1] end else a_ari = "/" a = 1 end if pm_num b_ari = formula_arr[pm_num] if input_num > pm_num b = formula_arr[pm_num - 1] else b = formula_arr[pm_num + 1] end else b_ari = "+" b = 0 end a_ari == "*" ? a_ari = "/" : a_ari = "*" b_ari == "+" ? b_ari = "-" : b_ari = "+" "value" + a_ari + a.to_s + b_ari + b.to_s + a_ari + a.to_s end |
#new_liner_function(formula) ⇒ Object
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 103 104 105 106 107 108 109 110 111 |
# File 'lib/eng_formula.rb', line 78 def new_liner_function(formula) fl_arr = formula.split(/(\*|\/|\+|-)/) value_i = fl_arr.index("value") a = [] b = [] pl = false (value_i .. fl_arr.size - 1).each do |i| if pl || fl_arr[i] == "+" || fl_arr[i] == "-" b << reverse_operator(fl_arr[i]) pl = true else a << reverse_operator(fl_arr[i]) end end if value_i != 0 (0 .. value_i - 1).reverse_each do |i| if fl_arr[i] == "+" || fl_arr[i] == "-" || i == 0 (i .. value_i - 1).reverse_each do |ii| a.unshift(reverse_operator(fl_arr[ii])) end if i != 0 (0..i).each do |ii| b.unshift(reverse_operator(fl_arr[ii], false)) end end break end end end a.join + "+(" + b.join + ")/value" end |
#operator_reg(kind_operator) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/eng_formula.rb', line 25 def operator_reg(kind_operator) case kind_operator when :ari /(?:[\(\)\+\-\*\/])/ when :double #2乗表現のマッチング (2^1/2) /(?:\^\(?[\+\-]?\d+\/?\d*\)?)/ when :num /\d+(?:\.\d*)?(?:e[+-]?\d+)?/ when :tri /(?:sin|cos|tan)\(.+\)/ end end |
#reverse_operator(operator, opt = true) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/eng_formula.rb', line 113 def reverse_operator(operator, opt = true) case operator when "+" "-" when "-" "+" when "*" opt ? "/" : "*" when "/" opt ? "*" : "/" else operator end end |
#split_by_ari(formula) ⇒ Object
38 39 40 |
# File 'lib/eng_formula.rb', line 38 def split_by_ari(formula) formula.split((/(#{operator_reg(:ari)})/)) end |
#split_value_unit(formula) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/eng_formula.rb', line 3 def split_value_unit(formula) unit_array = [] formula_pre = formula.to_s.delete(" ") formula_pre.scan(/(#{operator_reg(:tri)})| (?:(#{operator_reg(:num)}(?:#{operator_reg(:double)})*)((?:\/?\(?\/?[a-z]+\d*(?:\*[a-z])*(?:\W*[a-z]\d*\)*)*)(?:\d[^[a-z]])*\)?))| (#{operator_reg(:ari)})| ((?:#{operator_reg(:num)})*(?:#{operator_reg(:double)})?)/ix) .each do |data| unit_array << { value: (data[0] || data[1] || data[3] || data[4]), unit: (data[2] || data[3]) } end unit_array.each_with_index do |data, index| if data[:unit] =~ /^[^\(]+\)$/ data[:unit].gsub!(/.$/,'') unit_array.insert(index+1, { value: ")", unit: ")" }) end end unit_array.delete_if{ |data| true if data[:value].empty? && data[:unit].nil? } end |