#24773 closed enhancement (fixed)

Delayed/Conditional Substitution

Reported by: cheuberg Owned by:
Priority: major Milestone: sage-8.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 12 months ago by cheuberg

  • Branch set to u/cheuberg/delayed-substitution

comment:2 Changed 12 months ago by cheuberg

  • Cc behackl rws added
  • Commit set to 55818942d0f68722ce228a465427131860a761b6
  • Status changed from new to needs_review

New commits:

0f55981Trac #24773: delayed substitution: first version
b801825Trac #24773: remove input argument self
6d0793aTrac #24773: Shorten references
5581894Trac #24773: Fix see also block

comment:3 follow-up: Changed 12 months ago by dkrenn

  • 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 substitute-methods 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 12 months ago by rws

Replying to dkrenn:

LGTM, but I want to raise the following to be discussed: the name delayed_substitution of the method. The other substitute-methods are called substitute*, so maybe an substitute_delayed would fit more into this scheme.

Agree, mostly because it makes it easier to be found by TAB completion.

comment:5 Changed 12 months ago by git

  • Commit changed from 55818942d0f68722ce228a465427131860a761b6 to d2b835c21e54e71a84cafcc5ca7702f99ccf29b3

Branch pushed to git repo; I updated commit sha1. New commits:

d2b835cTrac #24773: delayed_substitution -> substitution_delayed

comment:6 follow-up: Changed 12 months ago by cheuberg

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 12 months ago by cheuberg

  • Status changed from needs_info to needs_review

comment:8 in reply to: ↑ 6 Changed 12 months ago by dkrenn

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*x

But then, [...]

Ok, I understand. Then I suggest to skip this example completely to not get confused.

comment:9 Changed 12 months ago by dkrenn

  • Status changed from needs_review to positive_review

comment:10 Changed 12 months ago by jdemeyer

  • 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 12 months ago by git

  • Commit changed from d2b835c21e54e71a84cafcc5ca7702f99ccf29b3 to 224476ec9e346efb53c4dd60918c6483eae369b2

Branch pushed to git repo; I updated commit sha1. New commits:

224476eTrac #24773: additional test and explanation

comment:12 Changed 12 months ago by cheuberg

  • Status changed from needs_work to needs_review

I have added an example and some more explanation.

comment:13 Changed 12 months ago by rws

  • 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 12 months ago by vbraun

  • Branch changed from u/cheuberg/delayed-substitution to 224476ec9e346efb53c4dd60918c6483eae369b2
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.