From 0d4edbd301769d02efeb621f38afef57b63cea29 Mon Sep 17 00:00:00 2001 From: Taksh Date: Mon, 5 Oct 2026 07:09:08 +0530 Subject: [PATCH] Classify RationalFunction2 degrees in descending coefficient order --- blueprint/src/python/functions.py | 10 +++---- .../tests/test_function_degree_regression.py | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 blueprint/src/python/tests/test_function_degree_regression.py diff --git a/blueprint/src/python/functions.py b/blueprint/src/python/functions.py index d369b50..8b1d35d 100644 --- a/blueprint/src/python/functions.py +++ b/blueprint/src/python/functions.py @@ -686,7 +686,7 @@ def __repr__(self): if self.is_zero(): return f"0 on {self.domain}" if self.is_constant(): - return f"{self._num_coeffs[0] / self._den_coeffs[0]} on {self.domain}" + return f"{self._num_coeffs[-1] / self._den_coeffs[-1]} on {self.domain}" num = RationalFunction2._poly_str(self._num_coeffs, "x") den = RationalFunction2._poly_str(self._den_coeffs, "x") @@ -732,12 +732,12 @@ def is_zero(self): return all(c == 0 for c in self._num_coeffs) def is_constant(self): - return all(c == 0 for c in self._num_coeffs[1:]) and \ - all(c == 0 for c in self._den_coeffs[1:]) + return all(c == 0 for c in self._num_coeffs[:-1]) and \ + all(c == 0 for c in self._den_coeffs[:-1]) def is_affine(self): - return all(c == 0 for c in self._num_coeffs[2:]) and \ - all(c == 0 for c in self._den_coeffs[1:]) + return all(c == 0 for c in self._num_coeffs[:-2]) and \ + all(c == 0 for c in self._den_coeffs[:-1]) # Represents the univariate function P(x)/Q(x) where P, Q are polynomials. Currently diff --git a/blueprint/src/python/tests/test_function_degree_regression.py b/blueprint/src/python/tests/test_function_degree_regression.py new file mode 100644 index 0000000..51c59ec --- /dev/null +++ b/blueprint/src/python/tests/test_function_degree_regression.py @@ -0,0 +1,28 @@ +from functions import Interval, RationalFunction2 + + +def function(numerator, denominator=(1,)): + return RationalFunction2((list(numerator), list(denominator)), Interval(0, 1)) + + +def test_monomials_have_their_actual_degree(): + linear = function((1, 0)) + quadratic = function((1, 0, 0)) + assert not linear.is_constant() + assert linear.is_affine() + assert not quadratic.is_constant() + assert not quadratic.is_affine() + + +def test_leading_zeros_do_not_raise_degree(): + constant = function((0, 0, 2), (0, 1)) + assert constant.is_constant() + assert constant.is_affine() + assert str(constant) == "2.0 on [0,1)" + assert function((0, 1, 2)).is_affine() + + +def test_nonconstant_denominator_is_not_affine(): + reciprocal = function((1,), (1, 0)) + assert not reciprocal.is_constant() + assert not reciprocal.is_affine()