class Polymath::Math

Constants

ZeroRoot

Public Instance Methods

factor_rational_zeroes(polynomial:) click to toggle source

@brief determines the zeroes of a polynomial

@param polynomial The polynomial

@return an array of Rational numbers

# File lib/polymath/math/math.rb, line 45
def factor_rational_zeroes(polynomial:)
  rational_zeroes(polynomial: polynomial).select { |test_value|
    is_a_zero?(test_value: test_value, polynomial: polynomial)
  }
end
factor_zeroes(polynomial:) click to toggle source
# File lib/polymath/math/math.rb, line 51
def factor_zeroes(polynomial:)
  case polynomial.classification[:len]
  when :monomial
    case polynomial.classification[:special]
    when :zero
      [Float::INFINITY]
    else
      [::ZeroRoot]
    end
  else
    factor_rational_zeroes(polynomial: polynomial)
  end
end
factors_of(x) click to toggle source

@brief determine the prime factors of x

@param x the integer to factor

@return an array of integers

# File lib/polymath/math/math.rb, line 87
def factors_of(x)
  (x.prime_division.map { |f| f[0] } + [1, x]).uniq.sort
end
is_a_zero?(test_value:, polynomial:) click to toggle source

@brief Determines if a rational number is a zero for a given

polynomial

@param polynomial The polynomial @param test_value The test value

@return True if a zero, False otherwise.

# File lib/polymath/math/math.rb, line 34
def is_a_zero?(test_value:, polynomial:)
  synthetic_remainder(polynomial: polynomial, divisor: test_value) == 0
end
rational_zeroes(polynomial:) click to toggle source

@brief calculates possible rational zeroes for a given polynomial

@param polynomial The polynomial

@return an array of Rational numbers

# File lib/polymath/math/math.rb, line 16
def rational_zeroes(polynomial:)
  cnf = factors_of(polynomial.constant)
  lcf = factors_of(polynomial.leading_coefficient)

  cnf.map { |x|
    lcf.map { |y| [ Rational(x, y), -Rational(x, y) ] }
  }.flatten.uniq
end
synthetic_remainder(polynomial:, divisor:) click to toggle source

@brief calculates the remainder of the synthetic quotient of a

polynomial and a test value

@param polynomial The polynomial @param value The value

@return a Rational number

# File lib/polymath/math/math.rb, line 74
def synthetic_remainder(polynomial:, divisor:)
  polynomial.coefficients.reduce { |carry, next_cof|
    (carry * divisor) + next_cof
  }
end