Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions blueprint/src/python/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions blueprint/src/python/tests/test_function_degree_regression.py
Original file line number Diff line number Diff line change
@@ -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()
Loading