Module: Silicium::Algebra

Defined in:
lib/algebra.rb,
lib/algebra_diff.rb,
lib/polynomial_division.rb,
lib/polynomial_interpolation.rb

Overview

Algebra module helps to perform calculations with polynoms

Defined Under Namespace

Modules: PolynomRootsReal Classes: Differentiation, PolynomialDivision, PolynomialInterpolation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



9
10
11
# File 'lib/algebra.rb', line 9

def str
  @str
end

Instance Method Details

#another_variable?(old_variable, new_variable) ⇒ Boolean

Checks if new variable is present and is not the same as last known variable

Returns:

  • (Boolean)


85
86
87
# File 'lib/algebra.rb', line 85

def another_variable?(old_variable, new_variable)
  !new_variable.nil? && old_variable != new_variable
end

#eratosthen_primes_to(n) ⇒ Object

eratosthen_primes_to(n) finds all primes up to n with the sieve of eratosthenes

eratosthen_primes_to(1) # => [] eratosthen_primes_to(15) # => [2, 3, 5, 7, 11, 13] eratosthen_primes_to(50) # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Raises:

  • (ArgumentError)


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

def eratosthen_primes_to(n)
  raise ArgumentError unless valid_n?(n)

  array = (2..n).to_a
  array.each do |prime|
    square = prime**2
    break if square > n

    array -= square.step(n, prime).to_a
  end
  array
end

#evaluate(val) ⇒ Object

evaluate(val) counts the result using a given value



111
112
113
114
# File 'lib/algebra.rb', line 111

def evaluate(val)
  res = to_ruby_s(val)
  eval(res)
end

#extract_variable(expr) ⇒ Array

retuns pair, the first value indicates if parsing succeeded, the second is variable

Parameters:

  • expr (String)
    • part of analytical function that has one independent variable

Returns:

  • (Array)


78
79
80
81
# File 'lib/algebra.rb', line 78

def extract_variable(expr)
  expr[/(\s?\d*\s?\*\s?)?([a-z])(\^\d*)?|\s?\d+$/]
  [!Regexp.last_match.nil?, Regexp.last_match(2)]
end

#initializer(str) ⇒ Object

initializer(str) creates a correct ruby str from given one

Raises:

  • (PolynomError)


13
14
15
16
# File 'lib/algebra.rb', line 13

def initializer(str)
  raise PolynomError, 'Invalid string for polynom ' unless polycop(str)
  @str = str
end

#letter_controller(term) ⇒ Object

check for extra letters in term



90
91
92
93
94
95
# File 'lib/algebra.rb', line 90

def letter_controller(term)
  allowed_w = %w[ln lg log cos sin]
  letters = term.scan(/[a-z]{2,}/)
  letters = letters.join
  letters.empty? || allowed_w.include?(letters)
end

#polycop(str) ⇒ Object

polycop(str) determines whether the str is an appropriate function which only has one variable

polycop(‘x^2 + 2 * x + 7’) # => True polycop(‘x^2 2nbbbbb * x 7’) # => False



53
54
55
56
57
58
59
# File 'lib/algebra.rb', line 53

def polycop(str)
  @letter_var = nil
  parsed = str.split(/[-+]/)
  parsed.each { |term| return false unless valid_term?(term) }

  true
end

#to_ruby_s(val) ⇒ Object

to_ruby_s(val) transforms @str into a correct ruby str works for logarithms, trigonometry and misspelled power

to_ruby_s(”) # =>



102
103
104
105
106
107
108
# File 'lib/algebra.rb', line 102

def to_ruby_s(val)
  temp_str = @str
  temp_str.gsub!('^', '**')
  temp_str.gsub!(/lg|log|ln/, 'Math::\1')
  temp_str.gsub!(@letter_var, val)
  temp_str
end

#valid_n?(n) ⇒ Boolean

Checks if the number n is correct

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/algebra.rb', line 40

def valid_n?(n)
  return false if n <= 0
  return false unless n.class == Integer
  
  true
end

#valid_term?(term) ⇒ Boolean

Parses single polynomial term and returns false if term is incorrect on has different independent variable It updated current independent variable if it wasn’t set before

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/algebra.rb', line 65

def valid_term?(term)
  correct, cur_var = extract_variable(term)
  return false unless correct

  @letter_var ||= cur_var
  !(another_variable?(@letter_var, cur_var) || !letter_controller(term))
end