Skip to content

Commit

Permalink
adding documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Nov 15, 2022
1 parent b74729f commit ed98034
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Bugs

# ``0`` with a given precision (like in ```0`3```) is now parsed as ``0``, an integer number.
#. ``RandomSample`` with one list argument now returns a random ordering of the list items. Previously it would return just one item.
#. Rules of the form ``pat->Condition[expr, cond]`` are handled as in WL. The same works for nested `Condition` expressions.
#. Rules of the form ``pat->Condition[expr, cond]`` are handled as in WL. The same also works for nested `Condition` expressions. In particular, the comparison between two Rules with the same pattern but an iterated ``Condition`` expressionare considered equal if the conditions are the same.


Enhancements
++++++++++++
Expand Down
14 changes: 12 additions & 2 deletions mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,21 @@ class SetDelayed(Set):
'Condition' ('/;') can be used with 'SetDelayed' to make an
assignment that only holds if a condition is satisfied:
>> f[x_] := p[x] /; x>0
>> f[x_] := p[-x]/; x<-2
>> f[3]
= p[3]
>> f[-3]
= f[-3]
It also works if the condition is set in the LHS:
= p[3]
>> f[-1]
= f[-1]
Notice that the LHS is the same in both definitions, but the second
does not overwrite the first one.
To overwrite one of these definitions, we have to assign using the same condition:
>> f[x_] := Sin[x] /; x>0
>> f[3]
= Sin[3]
In a similar way, the condition can be set in the LHS:
>> F[x_, y_] /; x < y /; x>0 := x / y;
>> F[x_, y_] := y / x;
>> F[2, 3]
Expand Down
9 changes: 7 additions & 2 deletions mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,15 @@ def get_tag_position(pattern, name) -> Optional[str]:


def insert_rule(values, rule) -> None:
rhs_cond = getattr(rule, "rhs_conditions", [])
for index, existing in enumerate(values):
if existing.pattern.sameQ(rule.pattern):
del values[index]
break
# Check for coincidences in the rhs conditions,
# it they are there.
existing_rhs_cond = getattr(existing, "rhs_conditions", [])
if existing_rhs_cond == rhs_cond:
del values[index]
break
# use insort_left to guarantee that if equal rules exist, newer rules will
# get higher precedence by being inserted before them. see DownValues[].
bisect.insort_left(values, rule)
Expand Down
5 changes: 3 additions & 2 deletions mathics/core/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ def __init__(self, pattern, replace, delayed=True, system=False) -> None:
while replace.has_form("System`Condition", 2):
replace, cond = replace.elements
conds.append(cond)
self.rhs_conditions = sorted(conds)
self.strip_replace = replace

self.rhs_conditions = sorted(conds)
self.strip_replace = replace

def do_replace(self, expression, vars, options, evaluation):
replace = self.replace if self.rhs_conditions == [] else self.strip_replace
Expand Down

0 comments on commit ed98034

Please sign in to comment.