Skip to content

Commit 2c1dc75

Browse files
authored
Merge pull request #1219 from mathics/iteratedconditionset
Handle successive conditions in SetDelayed (mistranslation fixed)
2 parents 365774a + 4e59303 commit 2c1dc75

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Enhancements
2626
``Compile[] and CompiledFunction[]`` every expression can have a compiled form,
2727
as a Python function.
2828
* ``Equal[]`` now compares complex against other numbers properly.
29-
* Improvements in handling products with infinite factors: ``0 Infinity``-> ``Indeterminate``, and ``expr Infinity``-> ``DirectedInfinite[expr]``
29+
* Improvements in handling products with infinite factors: ``0 Infinity``-> ``Indeterminate``, and ``expr Infinity``-> ``DirectedInfinite[expr]`.
30+
* ``SetDelayed`` now accept several conditions impossed both at LHS as well as RHS.
3031

3132
Bug fixes
3233
+++++++++

mathics/builtin/assignment.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,24 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
7171
name = lhs.get_head_name()
7272
lhs._format_cache = None
7373
condition = None
74+
7475
if name == "System`Condition" and len(lhs.leaves) == 2:
75-
condition = lhs
76-
lhs = condition._leaves[0]
76+
# This handle the case of many sucesive conditions:
77+
# f[x_]/; cond1 /; cond2 ...
78+
# is summarized to a single condition
79+
# f[x_]/; And[cond1, cond2, ...]
80+
condition = [lhs._leaves[1]]
81+
lhs = lhs._leaves[0]
82+
name = lhs.get_head_name()
83+
while name == "System`Condition" and len(lhs.leaves) == 2:
84+
condition.append(lhs._leaves[1])
85+
lhs = lhs._leaves[0]
86+
name = lhs.get_head_name()
87+
if len(condition) > 1:
88+
condition = Expression("System`And", *condition)
89+
else:
90+
condition = condition[0]
91+
condition = Expression("System`Condition", lhs, condition)
7792
name = lhs.get_head_name()
7893
lhs._format_cache = None
7994
if name == "System`Pattern":
@@ -334,15 +349,19 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
334349
return False
335350

336351
rhs_name = rhs.get_head_name()
337-
if rhs_name == "System`Condition":
352+
while rhs_name == "System`Condition":
338353
if len(rhs.leaves) != 2:
339354
evaluation.message_args("Condition", len(rhs.leaves), 2)
340355
return False
341356
else:
342357
lhs = Expression("Condition", lhs, rhs.leaves[1])
343358
rhs = rhs.leaves[0]
359+
rhs_name = rhs.get_head_name()
360+
361+
# Now, let's add the conditions on the LHS
344362
if condition:
345363
lhs = Expression("Condition", lhs, condition.leaves[1])
364+
346365
rule = Rule(lhs, rhs)
347366
count = 0
348367
defs = evaluation.definitions
@@ -532,12 +551,14 @@ class SetDelayed(Set):
532551
>> f[-3]
533552
= f[-3]
534553
It also works if the condition is set in the LHS
535-
>> F[x_, y_] /; x < y := x / y;
554+
>> F[x_, y_] /; x < y /; x>0 := x / y;
536555
>> F[x_, y_] := y / x;
537556
>> F[2, 3]
538557
= 2 / 3
539558
>> F[3, 2]
540559
= 2 / 3
560+
>> F[-3, 2]
561+
= -2 / 3
541562
"""
542563

543564
operator = ":="

0 commit comments

Comments
 (0)