Ask Your Question

Mathematica-like transformations rules

asked 2018-03-24 14:39:14 +0200

Peter Wildemann gravatar image

Hey everyone!

I am currently exploring Sage's features and was wondering if there is something similar to Mathematica's replacement/transformation rules, which are (in my opinion) the strongest feature of Mathematica itself.

Thanks in advance for any answers!

PS: I would have liked to link on Wolfram's documentation page on Transformation rules but my Karma is insufficient for posting links, so in case you wonder what I mean by transformation rules, just give Google a hit.

edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2018-03-24 15:21:07 +0200 )edit

New users cannot post hyperlinks, which helps avoid a lot of spam.

A workaround is to introduce spaces in the url of links.

For example: https:// instead of

Or put more spaces if necessary, eg https:// wiki .sagemath .org.

slelievre gravatar imageslelievre ( 2018-03-24 15:21:56 +0200 )edit

I think the question is more general than just substitutions in symbolics expression as I remember a course on Mathematica I saw some years ago. Can you update the question by giving an example of such typical transformation rules?

Sébastien gravatar imageSébastien ( 2018-03-24 16:42:51 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-03-24 15:38:00 +0200

slelievre gravatar image

You can use the substitute or subsmethods:

sage: a, b, x, y = SR.var('a b x y')
sage: expr = x^2 + sin(x) - x*y + exp(sin(y)-x)
sage: expr
x^2 - x*y + e^(-x + sin(y)) + sin(x)
sage: expr.subs({x: a, y: b})
a^2 - a*b + e^(-a + sin(b)) + sin(a)

See also:

  • expr.subs
  • expr.substitute_function
  • expr.substitute
  • expr.substitution_delayed

To get the documentation for those, define expr as above and type for example one of:

  • expr.substitute_function?
  • browse_sage_doc(expr.substitute_function)

One will give you text-mode help, and the other will render in html in a browser.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-03-24 14:39:14 +0200

Seen: 594 times

Last updated: Mar 24 '18