Module: Mathpack::Approximation

Defined in:
lib/mathpack/approximation.rb

Class Method Summary collapse

Class Method Details

.approximate_by_polynom(params = {}, &function) ⇒ Object



7
8
9
10
11
# File 'lib/mathpack/approximation.rb', line 7

def self.approximate_by_polynom(params = {}, &function)
  f = block_given? ? fill_f(params[:x], &function) : params[:f]
  mnk_matrix, mnk_vector = count_mnk_values(params[:x], f, params[:polynom_power])
  Mathpack::SLE.solve(matrix: mnk_matrix, f: mnk_vector).reverse
end

.basic_fi(x, power) ⇒ Object



3
4
5
# File 'lib/mathpack/approximation.rb', line 3

def self.basic_fi(x, power)
  x**power
end

.count_mnk_values(x, f, polynom_power) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mathpack/approximation.rb', line 21

def self.count_mnk_values(x, f, polynom_power)
  mnk_matrix = Array.new(polynom_power + 1) { Array.new(polynom_power + 1) }
  mnk_vector = Array.new(polynom_power + 1)
  0.upto(polynom_power) do |i|
    0.upto(polynom_power) do |j|
      mnk_matrix[i][j] = scalar_function_composition(x, x, i, j)
    end
    mnk_vector[i] = scalar_function_composition(x, f, i, 1)
  end
  [mnk_matrix, mnk_vector]
end

.fill_f(x, &function) ⇒ Object



68
69
70
# File 'lib/mathpack/approximation.rb', line 68

def self.fill_f(x, &function)
  x.map { |val| function.call(val) }
end

.generate_nodes(params = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mathpack/approximation.rb', line 56

def self.generate_nodes(params = {})
  nodes = []
  i = 0
  loop do
    nodes << params[:from] + i * params[:step]
    i += 1
    break if params[:from] + i * params[:step] > params[:to]
  end
  nodes << params[:to] if nodes.last != params[:to]
  nodes
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mathpack/approximation.rb', line 33

def self.print_polynom(coefficients)
  polynom = ''
  coefficients.each_index do |i|
    next if coefficients[i] == 0
    if i == 0
      polynom += '-' if coefficients[i] < 0.0
    else
      polynom += coefficients[i] < 0.0 ? ' - ' : ' + '
    end
    polynom += coefficients[i].abs.to_s if coefficients[i].abs != 1 || i == coefficients.length - 1
    polynom += '*' if i != coefficients.length - 1 && coefficients[i].abs != 1
    case i
      when coefficients.length - 1

      when coefficients.length - 2
        polynom += 'x'
      else
        polynom += coefficients.length - i >= 11 ? "x^(#{coefficients.length - 1 - i})" : "x^#{coefficients.length - 1 - i}"
    end
  end
  polynom
end

.scalar_function_composition(first_vect, second_vect, first_power, second_power) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mathpack/approximation.rb', line 13

def self.scalar_function_composition(first_vect, second_vect, first_power, second_power)
  result = 0.0
  first_vect.each_index do |i|
    result += basic_fi(first_vect[i], first_power) * basic_fi(second_vect[i], second_power)
  end
  result
end