Skip to content
Merged
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
17 changes: 11 additions & 6 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,19 +1309,19 @@ class PossibleZeroQ(SympyFunction):
<dd>returns 'True' if basic symbolic and numerical methods suggest that expr has value zero, and 'False' otherwise.
</dl>

## Test whether a numeric expression is zero:
## >> PossibleZeroQ[E^(I Pi/4) - (-1)^(1/4)]
## = True
Test whether a numeric expression is zero:
>> PossibleZeroQ[E^(I Pi/4) - (-1)^(1/4)]
= True

## The determination is approximate.
The determination is approximate.

Test whether a symbolic expression is likely to be identically zero:
>> PossibleZeroQ[(x + 1) (x - 1) - x^2 + 1]
= True


## >> PossibleZeroQ[(E + Pi)^2 - E^2 - Pi^2 - 2 E Pi]
## = True
>> PossibleZeroQ[(E + Pi)^2 - E^2 - Pi^2 - 2 E Pi]
= True

Show that a numeric expression is nonzero:
>> PossibleZeroQ[E^Pi - Pi^E]
Expand All @@ -1346,6 +1346,11 @@ def apply(self, expr, evaluation):

sympy_expr = expr.to_sympy()
result = _iszero(sympy_expr)
if result is None:
# try expanding the expression
exprexp = Expression("ExpandAll", expr).evaluate(evaluation)
exprexp = exprexp.to_sympy()
result = _iszero(exprexp)
if result is None:
# Can't get exact answer, so try approximate equal
numeric_val = Expression("N", expr).evaluate(evaluation)
Expand Down
2 changes: 1 addition & 1 deletion mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def parse(self, query):

return self.parse_feeder(MathicsSingleLineFeeder(query))


def parse_evaluate(self, query, timeout=None):
expr = self.parse(query)
if expr is not None:
Expand All @@ -273,7 +274,6 @@ def parse_feeder(self, feeder):
def parse_feeder_returning_code(self, feeder):
"Parse a single expression from feeder and print the messages."
from mathics.core.parser.util import parse_returning_code

try:
result, source_code = parse_returning_code(self.definitions, feeder)
except TranslateError:
Expand Down
9 changes: 5 additions & 4 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ def __neg__(self) -> 'Rational':

@property
def is_zero(self) -> bool:
return self.numerator().is_zero
return self.numerator().is_zero and not self.denominator().is_zero()


class Real(Number):
Expand Down Expand Up @@ -2257,9 +2257,10 @@ def is_zero(self) -> bool:

@property
def is_approx_zero(self) -> bool:
# FIXME: figure out how to hook int $MachinePrecision and
# what the right definition here would be.
return abs(self.value) <= 10**-14
# In WMA, Chop[10.^(-10)] == 0,
# so, lets take it.
res = abs(self.value) <= 1e-10
return res


class PrecisionReal(Real):
Expand Down