Class: Polymath::Math
- Inherits:
-
Object
- Object
- Polymath::Math
- Defined in:
- lib/polymath/math/math.rb
Instance Method Summary collapse
-
#factor_rational_zeroes(polynomial:) ⇒ Object
An array of Rational numbers.
- #factor_zeroes(polynomial:) ⇒ Object
-
#factors_of(x) ⇒ Object
An array of integers.
-
#is_a_zero?(test_value:, polynomial:) ⇒ Boolean
True if a zero, False otherwise.
-
#rational_zeroes(polynomial:) ⇒ Object
An array of Rational numbers.
-
#synthetic_remainder(polynomial:, divisor:) ⇒ Object
A Rational number.
Instance Method Details
#factor_rational_zeroes(polynomial:) ⇒ Object
Returns an array of Rational numbers.
45 46 47 48 49 |
# 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:) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
Returns an array of integers.
87 88 89 |
# 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:) ⇒ Boolean
Returns True if a zero, False otherwise.
34 35 36 |
# 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:) ⇒ Object
Returns an array of Rational numbers.
16 17 18 19 20 21 22 23 |
# 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:) ⇒ Object
Returns a Rational number.
74 75 76 77 78 |
# File 'lib/polymath/math/math.rb', line 74 def synthetic_remainder(polynomial:, divisor:) polynomial.coefficients.reduce { |carry, next_cof| (carry * divisor) + next_cof } end |