Module: Eng::Formula

Included in:
Calc, Unit, Unit, UnitCalc
Defined in:
lib/eng_formula.rb

Instance Method Summary collapse

Instance Method Details

#liner_function(formula) ⇒ Object



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
77
# File 'lib/eng_formula.rb', line 43

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



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
112
# File 'lib/eng_formula.rb', line 79

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



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eng_formula.rb', line 26

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/eng_formula.rb', line 114

def reverse_operator(operator, opt = true)
  case operator
  when "+"
    "-"
  when "-"
    "+"
  when "*"
    opt ? "/" : "*"
  when "/"
    opt ? "*" : "/"
  else
    operator
  end
end

#split_by_ari(formula) ⇒ Object



39
40
41
# File 'lib/eng_formula.rb', line 39

def split_by_ari(formula)
  formula.split((/(#{operator_reg(:ari)})/)).reject(&:empty?)
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
24
# 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)})*)([^#{operator_reg(:num)}#{operator_reg(:double)}#{operator_reg(:ari)}]+))|
                  (?:(#{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[3] || data[5] || data[6] || data[1]),
                      unit: (data[4] || data[5]) || data[2] }
  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