module Mathpack::IO

Public Class Methods

count_diff(first_array, second_array) click to toggle source
# File lib/mathpack/io.rb, line 100
def self.count_diff(first_array, second_array)
  if first_array.length == second_array.length
    diff = Array.new(first_array.length){ |i| (first_array[i] - second_array[i]).abs }
  elsif first_array.length == second_array.length * 2 - 1
    diff = Array.new(second_array.length) { |i| (first_array[2 * i] - second_array[i]).abs }
  else
    fail 'Arrays length mismatch'
  end
  diff.max
end
is_valid_function?(function_string) click to toggle source
# File lib/mathpack/io.rb, line 40
def self.is_valid_function?(function_string)
  str = function_string.dup
  @@math_functions.values.each do |math_func|
    str.gsub!(math_func, '(')
  end
  str.delete!(' ')
  str.gsub!('x', '')
  (@@operators + @@delimiters + @@numbers).each do |sym|
    str.gsub!(sym, '')
  end
  brackets_stack = []
  str.each_char do |bracket|
    if brackets_stack.empty?
      brackets_stack << bracket
      next
    end
    if bracket.eql? '('
      brackets_stack << bracket
    else
      if brackets_stack.last.eql? '('
        brackets_stack.pop
      else
        brackets_stack << bracket
      end
    end
  end
  str.gsub!(/[\(\)]/, '')
  brackets_stack.empty? && str.empty?
end
parse_function(string_function) click to toggle source
# File lib/mathpack/io.rb, line 35
def self.parse_function(string_function)
  transformed_function = transform_string_function string_function
  produce_block(transformed_function) if is_valid_function?(transformed_function)
end
print_table_function(params = {}) click to toggle source
produce_block(valid_function) click to toggle source
# File lib/mathpack/io.rb, line 93
def self.produce_block(valid_function)
  valid_function.gsub!('x', 'var')
  valid_function.gsub!('evarp', 'exp')
  calculate = -> str { ->x { eval str.gsub('var', x.to_s) } }
  calculate.(valid_function)
end
read_table_function(filename) click to toggle source
# File lib/mathpack/io.rb, line 21
def self.read_table_function(filename)
  x = []
  y = []
  data = File.read(filename)
  rows = data.split("\n")
  rows.delete!(0) unless rows[0].split(';')[0].to_f
  rows.each do |row|
    parts = row.split(';')
    x << parts[0].to_f
    y << parts[1].to_f
  end
  { x: x, y: y}
end
transform_string_function(function_string) click to toggle source
# File lib/mathpack/io.rb, line 70
def self.transform_string_function(function_string)
  function = function_string.dup
  i_next = 0
  while i_next != function.length do
    i = function.index('^', i_next)
    break if i.nil?
    if function[i + 1].eql? '('
      function[i..i + 1] = '**('
    else
      i_next = function.index(@@next_stop, i)
      i_next = function.length unless i_next
      function[i...i_next] = '**('+ function[i + 1...i_next] + ')'
    end
  end
  @@math_functions.each do |key, value|
    function.gsub!(key, value)
  end
  @@math_constants.each do |key, value|
    function.gsub!(key, value)
  end
  function
end