Make bool method in expression and check_maxima_relation consistent

60 views
Skip to first unread message

cxzhong

unread,
Nov 24, 2025, 9:38:50 AM (9 days ago) Nov 24
to sage-devel
In the document of expression.pyx
bool method is described as 
Return ``True`` unless this symbolic expression can be shown by Sage
to be zero.  Note that deciding if an expression is zero is
undecidable in general.
In this definition, because we define eq as this function and we do not have _ne_. so python will  translate a!=b is not a==b.

In check_maxima_relation method in relation.py
Return ``True`` if this (in)equality is definitely true. Return ``False``
if it is false or the algorithm for testing (in)equality is inconclusive.

But in check_relation_maxima, maxima will actually check a!=b is really unequal at any time, will return true if a!=b at any time. will return False if a==b, otherwise return unknown, but in check_relation_maxima we consider it as False, they have different semantic or we can say they are contradict in mathematics in bool system(two state: True and False). It will cause some strange behavior like

sage: y = SR.var("y")
sage: bool(y != 0)
True    --do not fall back into check_relation_maxima, other method just use != is not == so it is True
sage: y = SR.var("y", domain="real")
sage: bool(y != 0)
False   --fall back into check_relation_maxima because of assumptions maxima said y can be zero, so it is False

I think True is needed in the bool method to make the bool method semantic in the document is consistent.

Proposal solution:

I will try to create a function that check_relation_maxima_neq_as_not_eq as the PR  Fix inconsistency of != with is_zero() and matrix symmetry by cxzhong · Pull Request #41212 · sagemath/sage
Then we use this function in bool method in expression.pyx


Nils Bruin

unread,
Nov 24, 2025, 3:47:37 PM (8 days ago) Nov 24
to sage-devel
Perhaps as a bit of explanation of why you may care about this and why our hand is forced:

Given symbolic expression A and B, what should

bool(A == B) and bool(A != B) be?

Python forces us that these are either True or False and since equality testing happens all over in Python, throwing an error isn't really an option either.

It would seem the reasonable equality notion here is: "as functions in whatever variables are present". That's a notion of equality that is not always practically decidable and in fact is even theoretically undecidable in full generality (see Richardson's Theorem). It then seems reasonable to have bool(A==B) return True if equality can be established and False otherwise. That's generally what is attempted in sage.

The question now arises for bool(A != B): what should it do for undecided cases? At least I was originally under the impression that a similar approach might be prudent to also return "False" for undecided, in which case bool(A==B) and bool(A!=B) aren't necesarily complementary: they could both be false.

This is problematic in python, but even more important: the primitives in Maxima that we rely on don't even do this!
We use "is(equal(A,B))" in maxima. In maxima, "is(equal(x,x^2))" returns "unknown" because there are values for x that make it True as well as values that make it False. So "unknown" doesn't mean "unable to determine if the two expressions describe the same function".

With what I understand presently, I don't think we have a reasonable other option than to just make "bool(A!=B) == not bool(A==B)". Now would be a good time to voice concerns and help with finding alternatives, should they exist.

Reply all
Reply to author
Forward
0 new messages