Skip to content

Commit a8317b9

Browse files
committed
Merge branch 'fixcomparisons' into fixcomparison-modularize
2 parents e9db081 + 8050ad9 commit a8317b9

5 files changed

Lines changed: 111 additions & 4 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
Symbol,
3434
SymbolComplexInfinity,
3535
SymbolDirectedInfinity,
36-
SymbolFalse,
3736
SymbolInfinity,
3837
SymbolN,
3938
SymbolNull,
4039
SymbolSequence,
4140
SymbolTrue,
41+
SymbolFalse,
42+
SymbolUndefined,
4243
from_mpmath,
4344
from_python,
4445
)
@@ -675,7 +676,9 @@ def apply(self, items, evaluation):
675676
Expression("Plus", item.leaves[1], leaves[-1].leaves[1]),
676677
)
677678
elif (
678-
leaves and item.has_form("Power", 2) and item.leaves[0].sameQ(leaves[-1])
679+
leaves
680+
and item.has_form("Power", 2)
681+
and item.leaves[0].sameQ(leaves[-1])
679682
):
680683
leaves[-1] = Expression(
681684
"Power", leaves[-1], Expression("Plus", item.leaves[1], Integer(1))
@@ -2209,3 +2212,106 @@ def apply(self, expr, evaluation):
22092212
elif expr == SymbolFalse:
22102213
return Integer(0)
22112214
return None
2215+
2216+
2217+
class Assumptions(Predefined):
2218+
"""
2219+
<dl>
2220+
<dt>'$Assumptions'
2221+
<dd>is the default setting for the Assumptions option used in such
2222+
functions as Simplify, Refine, and Integrate.
2223+
</dl>
2224+
"""
2225+
2226+
name = "$Assumptions"
2227+
attributes = ("Unprotected",)
2228+
rules = {
2229+
"$Assumptions": "True",
2230+
}
2231+
2232+
2233+
class Assuming(Builtin):
2234+
"""
2235+
<dl>
2236+
<dt>'Assuming[$cond$, $expr$]'
2237+
<dd>Evaluates $expr$ assuming the conditions $cond$
2238+
</dl>
2239+
>> $Assumptions = { x > 0 }
2240+
= {x > 0}
2241+
>> Assuming[y>0, $Assumptions]
2242+
= {x > 0, y > 0}
2243+
"""
2244+
2245+
attributes = ("HoldRest",)
2246+
2247+
def apply_assuming(self, cond, expr, evaluation):
2248+
"Assuming[cond_, expr_]"
2249+
cond = cond.evaluate(evaluation)
2250+
if cond.is_true():
2251+
cond = []
2252+
elif cond.is_symbol() or not cond.has_form("List", None):
2253+
cond = [cond]
2254+
else:
2255+
cond = cond.leaves
2256+
assumptions = evaluation.definitions.get_definition(
2257+
"System`$Assumptions", only_if_exists=True
2258+
)
2259+
2260+
if assumptions:
2261+
assumptions = assumptions.ownvalues
2262+
if len(assumptions) > 0:
2263+
assumptions = assumptions[0].replace
2264+
else:
2265+
assumptions = None
2266+
if assumptions:
2267+
if assumptions.is_symbol() or not assumptions.has_form("List", None):
2268+
assumptions = [assumptions]
2269+
else:
2270+
assumptions = assumptions.leaves
2271+
cond = assumptions + tuple(cond)
2272+
Expression(
2273+
"Set", Symbol("System`$Assumptions"), Expression("List", *cond)
2274+
).evaluate(evaluation)
2275+
ret = expr.evaluate(evaluation)
2276+
if assumptions:
2277+
Expression(
2278+
"Set", Symbol("System`$Assumptions"), Expression("List", *assumptions)
2279+
).evaluate(evaluation)
2280+
else:
2281+
Expression(
2282+
"Set", Symbol("System`$Assumptions"), Expression("List", SymbolTrue)
2283+
).evaluate(evaluation)
2284+
return ret
2285+
2286+
2287+
class ConditionalExpression(Builtin):
2288+
"""
2289+
<dl>
2290+
<dt>'ConditionalExpression[$expr$, $cond$]'
2291+
<dd>returns $expr$ if $cond$ evaluates to $True$, $Undefined$ if
2292+
$cond$ evaluates to $False$.
2293+
</dl>
2294+
2295+
>> f = ConditionalExpression[x^2, x>0]
2296+
= ConditionalExpression[x ^ 2, x > 0]
2297+
>> f /. x -> 2
2298+
= 4
2299+
>> f /. x -> -2
2300+
= Undefined
2301+
"""
2302+
2303+
rules = {
2304+
"ConditionalExpression[expr_, True]": "expr",
2305+
"ConditionalExpression[expr_, False]": "Undefined",
2306+
}
2307+
2308+
def apply_generic(self, expr, cond, evaluation):
2309+
"ConditionalExpression[expr_, cond_]"
2310+
cond = cond.evaluate(evaluation)
2311+
if cond is None:
2312+
return
2313+
if cond.is_true():
2314+
return expr
2315+
if cond == SymbolFalse:
2316+
return SymbolUndefined
2317+
return

mathics/builtin/comparison.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ class Equal(_EqualityOperator, SympyComparison):
534534
<dd>is 'True' if $x$ and $y$ are known to be equal, or
535535
'False' if $x$ and $y$ are known to be unequal, in which case
536536
case, 'Not[$x$ == $y$]' will be 'True'.
537-
Commutative properties apply, so if $x$ === $y$ then $y$ === $x$.
537+
Commutative properties apply, so if $x$ == $y$ then $y$ == $x$.
538538
</dl>
539539
540540
>> a==a

mathics/builtin/logic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mathics.core.expression import Expression, Symbol
77

88

9+
910
class Or(BinaryOperator):
1011
"""
1112
<dl>

mathics/core/expression.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,7 @@ def __getnewargs__(self):
20982098
SymbolMakeBoxes = Symbol("MakeBoxes")
20992099
SymbolN = Symbol("N")
21002100
SymbolNull = Symbol("Null")
2101+
SymbolUndefined = Symbol("Undefined")
21012102
SymbolRule = Symbol("Rule")
21022103
SymbolSequence = Symbol("Sequence")
21032104
SymbolTrue = Symbol("True")

test/test_compare.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def test_cmp1_no_pass(str_lhs, str_rhs, str_expected):
251251

252252

253253

254-
255254
@pytest.mark.parametrize(
256255
("str_lhs", "str_rhs", "str_expected"),
257256
tests2,

0 commit comments

Comments
 (0)