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
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,144 @@ def f_slash_other_f(f: Expr, symbols_enumerated: set[Symbol]):
return f.xreplace(fs) / f > 1


# Values given to the symbols of f when looking for a witness in
# _has_witness_that_s_affects_comparison. Any positive integers would do, since a
# witness found at any values proves the result. Varied primes make it unlikely that
# terms cancel by accident at the chosen values, which would only make the search give
# up and leave the call to the symbolic check.
_WITNESS_PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61)
_WITNESS_PRIMES += (67, 71, 73, 79, 83, 89, 97)

# How many sets of values to try before leaving the call to the symbolic check.
_WITNESS_TRIES = 6


def _prime(index: int) -> sympy.Integer:
"""Returns the prime at index in _WITNESS_PRIMES, wrapping around at the end."""
return sympy.Integer(_WITNESS_PRIMES[index % len(_WITNESS_PRIMES)])


def _prime_values(symbols: list[Symbol], first: int) -> dict[Symbol, sympy.Integer]:
"""Gives the symbols consecutive primes from _WITNESS_PRIMES, starting at first."""
return {symbol: _prime(first + i) for i, symbol in enumerate(symbols)}


def _exact_value(f: Expr, values: dict[Symbol, sympy.Integer]) -> Expr | None:
"""Returns f at the given values if the result is an exact rational number.

Returns None when the result can't be compared exactly. That happens when f is
undefined at these values (sympy raises on Max(2, zoo), for example, when a pole is
inside a Max), or when the result is not rational (a square root, for example).
"""
try:
value = f.xreplace(values)
except (TypeError, ValueError):
return None
return value if value.is_Rational else None


def _has_witness_that_s_affects_comparison(
f: Expr, s: Symbol, symbols_enumerated: set[Symbol]
) -> bool:
"""Tries to show, without expanding, that affects_comparison returns True.

affects_comparison(f, s, symbols_enumerated) returns True when s is still a free
symbol after sympy.expand of both

f(E_2) - f(E) > 0 and f(E_2) / f(E) > 1,

where f(E_2) is f with every enumerated symbol replaced by a new copy of it.
Expanding is the slow part, so this function first looks for a witness: values of
the symbols where changing only s changes f(E_2) - f(E), and values where changing
only s changes f(E_2) / f(E).

A witness proves the result for every input:

1. expand() returns an expression with the same value, and an expression that does
not contain s has the same value for any two values of s. So if the value
changes when only s changes, s is a free symbol of the expanded expression.
2. Neither relational can simplify to True or False, which would leave it with no
free symbols. Both are False when E_2 equals E, so neither is always True.
Swapping E and E_2 negates the difference and inverts the ratio, so if either
one were always False, f(E_2) would equal f(E) at the witness values, and it
doesn't.

Values are computed exactly. Floats in f are first replaced by their exact rational
value, so rounding can't make two values look different. This is where the answer
can differ from the symbolic check: if expand() rounds a term in s to exactly
zero, the symbolic check drops s and this function keeps it. Keeping a symbol is
the safe direction, because it only means less pruning.

Only positive integers are tried, so this returns False right away unless every
symbol in f is positive. Otherwise a value might be outside a symbol's domain.

Args:
f: The formula being compared.
s: The symbol that might affect the comparison.
symbols_enumerated: The symbols that E and E_2 each have their own copy of.

Returns:
True if a witness was found, which means affects_comparison is True. False if
none was found. False says nothing about s, and the symbolic check decides.
"""
if not all(symbol.is_positive for symbol in f.free_symbols):
return False

floats = f.atoms(sympy.Float)
if floats:
f = f.xreplace({c: sympy.Rational(c) for c in floats})

symbols = sorted(f.free_symbols, key=str)
enumerated = [x for x in symbols if x in symbols_enumerated]
shared = [x for x in symbols if x not in symbols_enumerated and x != s]

difference_changes = False
ratio_changes = False
for attempt in range(_WITNESS_TRIES):
# Each attempt starts E, E_2 and the shared symbols at a different place in
# the list, and the offsets keep each enumerated symbol's values in E and E_2
# apart. s gets one small and one large value, so that a term in s that only
# wins a Max or Min (or changes a ceiling) at one end still shows up.
values_e = _prime_values(enumerated, 5 * attempt + 2)
values_e_2 = _prime_values(enumerated, 5 * attempt + 12)
values_shared = _prime_values(shared, 5 * attempt + 7)
s_1, s_2 = _prime(attempt), _prime(-1 - attempt)

if s in symbols_enumerated:
# E_2 has its own copy of s, so f(E_2) is the same for both values of s.
f_e_2 = _exact_value(f, {**values_e_2, **values_shared})
f_e_2_at_s_1 = f_e_2_at_s_2 = f_e_2
else:
f_e_2_at_s_1 = _exact_value(f, {**values_e_2, **values_shared, s: s_1})
f_e_2_at_s_2 = _exact_value(f, {**values_e_2, **values_shared, s: s_2})
f_e_at_s_1 = _exact_value(f, {**values_e, **values_shared, s: s_1})
f_e_at_s_2 = _exact_value(f, {**values_e, **values_shared, s: s_2})

values = (f_e_at_s_1, f_e_at_s_2, f_e_2_at_s_1, f_e_2_at_s_2)
if any(value is None for value in values):
continue

if f_e_2_at_s_1 - f_e_at_s_1 != f_e_2_at_s_2 - f_e_at_s_2:
difference_changes = True
# The ratio needs every value to be positive. A zero would divide by zero, and
# step 2 of the argument above only holds for positive values.
if all(value > 0 for value in values):
if f_e_2_at_s_1 / f_e_at_s_1 != f_e_2_at_s_2 / f_e_at_s_2:
ratio_changes = True

if difference_changes and ratio_changes:
return True
return False


@lru_cache(maxsize=10000)
def affects_comparison(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]):
if not isinstance(f, sympy.Expr):
return False
# A witness settles the common True case without expanding. Without one, the
# symbolic check below decides.
if _has_witness_that_s_affects_comparison(f, s, symbols_enumerated):
return True
delta = simplify(f_minus_other_f(f, symbols_enumerated))
if s not in getattr(delta, "free_symbols", set()):
return False
Expand Down
98 changes: 98 additions & 0 deletions tests/test_affects_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Tests for the witness search in affects_comparison.

affects_comparison first looks for a witness that s affects the comparison
(_has_witness_that_s_affects_comparison), and only expands symbolically when it
finds none. These tests check that the answer is the same as the symbolic check's
alone, and that the witness search never claims a symbol that cancels out.
"""

import unittest
from unittest import mock

import sympy

from accelforge.mapper.FFM._make_pmappings.make_pmappings_from_templates import (
make_tile_shapes as mts,
)

# Positive like the mapper's symbols, with no integer=True. Ctrl-F SYMPY_BUG.
x, y, z, n = sympy.symbols("x y z n", positive=True)


def symbolic_check_only(f, s, symbols_enumerated):
"""Runs affects_comparison with the witness search turned off."""
with mock.patch.object(
mts, "_has_witness_that_s_affects_comparison", return_value=False
):
return mts.affects_comparison.__wrapped__(f, s, symbols_enumerated)


class TestAffectsComparisonWitness(unittest.TestCase):
formulas = [
# A plain polynomial.
x * y + 3 * z,
# Max, and a Float coefficient.
sympy.Max(x * n, y) + sympy.Float(0.37) * z * x,
# ceiling and Min, and a very small Float coefficient.
sympy.ceiling(n / x) * y + sympy.Min(x, z) * sympy.Float(1.5e-12),
# A quotient.
(x + y) ** 2 / z + n,
# With x enumerated, y cancels out of f(E_2) - f(E).
x + y,
# With x enumerated, y cancels out of f(E_2) / f(E).
x * y,
# A Max with a constant.
sympy.Max(x, 4) * n + y,
]

def test_same_answer_as_the_symbolic_check(self):
for enumerated in (frozenset(), frozenset({x, z}), frozenset({x, y, z, n})):
for f in self.formulas:
# n is not in every formula, which covers an s that f doesn't use.
for s in sorted(f.free_symbols | {n}, key=str):
with self.subTest(f=f, s=s, enumerated=enumerated):
self.assertEqual(
mts.affects_comparison.__wrapped__(f, s, enumerated),
symbolic_check_only(f, s, enumerated),
)

def test_no_witness_for_a_symbol_that_cancels(self):
witness = mts._has_witness_that_s_affects_comparison
self.assertFalse(witness(x + y, y, frozenset({x})))
self.assertFalse(witness(x * y, y, frozenset({x})))

def test_witness_for_a_shared_symbol(self):
f = sympy.Max(x, y) * y + x
self.assertTrue(
mts._has_witness_that_s_affects_comparison(f, y, frozenset({x}))
)

def test_witness_for_an_enumerated_symbol(self):
# E_2 has its own copy of x, so only f(E) changes with x.
f = x * y + y
self.assertTrue(
mts._has_witness_that_s_affects_comparison(f, x, frozenset({x}))
)

def test_exact_value_rejects_undefined_and_irrational_values(self):
three, two = sympy.Integer(3), sympy.Integer(2)
# y equal to x puts a pole inside the Max, and sympy raises.
self.assertIsNone(
mts._exact_value(sympy.Max(2, 1 / (y - x)), {x: three, y: three})
)
self.assertIsNone(mts._exact_value(sympy.sqrt(x), {x: two}))
self.assertEqual(mts._exact_value(x / 3, {x: two}), sympy.Rational(2, 3))

def test_undefined_values_fall_back_to_the_symbolic_check(self):
f = sympy.Max(2, 1 / (y - x))
self.assertTrue(mts.affects_comparison.__wrapped__(f, y, frozenset({x})))

def test_no_search_without_positive_assumptions(self):
w = sympy.Symbol("w")
self.assertFalse(
mts._has_witness_that_s_affects_comparison(x * w + w**2, w, frozenset({x}))
)


if __name__ == "__main__":
unittest.main()
Loading