Module: Mathpack::Approximation
- Defined in:
- lib/mathpack/approximation.rb
Class Method Summary collapse
- .approximate_by_polynom(params = {}, &function) ⇒ Object
- .basic_fi(x, power) ⇒ Object
- .count_mnk_values(x, f, polynom_power) ⇒ Object
- .fill_f(x, &function) ⇒ Object
- .generate_nodes(params = {}) ⇒ Object
- .print_polynom(coefficients) ⇒ Object
- .scalar_function_composition(first_vect, second_vect, first_power, second_power) ⇒ Object
Class Method Details
.approximate_by_polynom(params = {}, &function) ⇒ Object
9 10 11 12 13 |
# File 'lib/mathpack/approximation.rb', line 9 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
5 6 7 |
# File 'lib/mathpack/approximation.rb', line 5 def self.basic_fi(x, power) x**power end |
.count_mnk_values(x, f, polynom_power) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mathpack/approximation.rb', line 23 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
69 70 71 |
# File 'lib/mathpack/approximation.rb', line 69 def self.fill_f(x, &function) x.map { |val| function.call(val) } end |
.generate_nodes(params = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mathpack/approximation.rb', line 58 def self.generate_nodes(params = {}) nodes = [] x = params[:start] loop do nodes << x x += params[:step] break if x > params[:end] end nodes end |
.print_polynom(coefficients) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mathpack/approximation.rb', line 35 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
15 16 17 18 19 20 21 |
# File 'lib/mathpack/approximation.rb', line 15 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 |