Opened 15 months ago
Closed 15 months ago
#24773 closed enhancement (fixed)
Delayed/Conditional Substitution
Reported by:  cheuberg  Owned by:  

Priority:  major  Milestone:  sage8.2 
Component:  symbolics  Keywords:  
Cc:  dkrenn, behackl, rws  Merged in:  
Authors:  Clemens Heuberger  Reviewers:  Daniel Krenn, Ralf Stephan 
Report Upstream:  N/A  Work issues:  
Branch:  224476e (Commits)  Commit:  224476ec9e346efb53c4dd60918c6483eae369b2 
Dependencies:  Stopgaps: 
Description
Mathematica (see http://reference.wolfram.com/language/ref/RuleDelayed.html) has a very useful feature called delayed rule. Instead of a static rule, a function is invoked which decides looking at the argument how the actual substitution should be carried out. This is a useful feature when simplifying some symbolic expressions.
This ticket shall implement such a delayed substitution in SageMath.
Change History (14)
comment:1 Changed 15 months ago by
 Branch set to u/cheuberg/delayedsubstitution
comment:2 Changed 15 months ago by
 Cc behackl rws added
 Commit set to 55818942d0f68722ce228a465427131860a761b6
 Status changed from new to needs_review
comment:3 followup: ↓ 4 Changed 15 months ago by
 Reviewers set to Daniel Krenn
 Status changed from needs_review to needs_info
LGTM, but I want to raise the following to be discussed: the name delayed_substitution
of the method. The other substitutemethods are called substitute*
, so maybe an substitute_delayed
would fit more into this scheme. However, I do not have a strong opinion on this topic.
The first basic Mathematica example is
{x, x, x} /. x :> RandomReal[]
so maybe adding this example as well could be a benefit.
comment:4 in reply to: ↑ 3 Changed 15 months ago by
Replying to dkrenn:
LGTM, but I want to raise the following to be discussed: the name
delayed_substitution
of the method. The other substitutemethods are calledsubstitute*
, so maybe ansubstitute_delayed
would fit more into this scheme.
Agree, mostly because it makes it easier to be found by TAB completion.
comment:5 Changed 15 months ago by
 Commit changed from 55818942d0f68722ce228a465427131860a761b6 to d2b835c21e54e71a84cafcc5ca7702f99ccf29b3
Branch pushed to git repo; I updated commit sha1. New commits:
d2b835c  Trac #24773: delayed_substitution > substitution_delayed

comment:6 followup: ↓ 8 Changed 15 months ago by
I renamed the method as suggested.
Concerning the Mathematica example: I have two problems with that example. The first being that a list is not a symbolic expression. So we would need something like
sage: var('a b c') sage: e1 = a*x + b*x + c*x
But then, it does not work as expected:
sage: e1.substitution_delayed(x, lambda d: random()) 0.7859203112304044*a + 0.7859203112304044*b + 0.7859203112304044*c
The reason is that the code proposed here tries to do the job with as little effort as possible. This means that first, I look for all matches of the pattern:
sage: e1.find(x) [x]
And then, I simply replace all occurrences.
One possibility would be:
sage: var('a b c x y z') sage: w = SR.wild(0) sage: e2 = a*sin(x) + b*sin(y) + c*sin(z) sage: e2.substitution_delayed(sin(w), lambda d: random()) 0.9299473549632423*a + 0.1776245598330548*b + 0.5870609216512541*c
comment:7 Changed 15 months ago by
 Status changed from needs_info to needs_review
comment:8 in reply to: ↑ 6 Changed 15 months ago by
Replying to cheuberg:
Concerning the Mathematica example: I have two problems with that example. The first being that a list is not a symbolic expression. So we would need something like
sage: var('a b c') sage: e1 = a*x + b*x + c*xBut then, [...]
Ok, I understand. Then I suggest to skip this example completely to not get confused.
comment:9 Changed 15 months ago by
 Status changed from needs_review to positive_review
comment:10 Changed 15 months ago by
 Status changed from positive_review to needs_work
I think this is badly missing documentation. I just read the docstring and have absolutely no clue what this is about.
What does the "delayed" in the name refer to? How does this differ from subs
or subs_expr
? Can you add an example which is actually meant to explain what this method does?
comment:11 Changed 15 months ago by
 Commit changed from d2b835c21e54e71a84cafcc5ca7702f99ccf29b3 to 224476ec9e346efb53c4dd60918c6483eae369b2
Branch pushed to git repo; I updated commit sha1. New commits:
224476e  Trac #24773: additional test and explanation

comment:12 Changed 15 months ago by
 Status changed from needs_work to needs_review
I have added an example and some more explanation.
comment:13 Changed 15 months ago by
 Reviewers changed from Daniel Krenn to Daniel Krenn, Ralf Stephan
 Status changed from needs_review to positive_review
I think it's worth to have. LGTM too.
comment:14 Changed 15 months ago by
 Branch changed from u/cheuberg/delayedsubstitution to 224476ec9e346efb53c4dd60918c6483eae369b2
 Resolution set to fixed
 Status changed from positive_review to closed
New commits:
Trac #24773: delayed substitution: first version
Trac #24773: remove input argument self
Trac #24773: Shorten references
Trac #24773: Fix see also block