Ask Your Question

Is there a SageMath version of Mathematica's '//' shorthand?

asked 2024-04-22 12:06:01 +0200

reynormalizer gravatar image

If I had some expression x^2 + x^3 - 31 and I want to apply a previously defined function f to it, is there a quicker way of doing f(x^2 + x^3 - 31)?

I'm thinking along the lines of Mathematica's x^2 + x^3 - 31 // f, where // applies the function to the expression that precedes it.

edit retag flag offensive close merge delete


Please provide a complete code example, including the definition of f.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-04-22 15:32:52 +0200 )edit


g(x)=x^2 + x^3 - 31

But I'm probably missing something here, maybe you could give the function f in order to check with your function as Max Alekseyev asked you ?.

ortollj gravatar imageortollj ( 2024-04-23 06:40:28 +0200 )edit

@ortollj@Max Alekseyev Writing x^2+x^3-31//Sin is equivalent to Sin[x^2 + x^3 - 31] in Mathematica. OP asks whether there is equivalent of this syntax // in Sagemath.

azerbajdzan gravatar imageazerbajdzan ( 2024-04-23 22:44:46 +0200 )edit

Let's wait clarification from OP. I read the question differently, and I do not understand why f(x^2 + x^3 - 31) is not considered quick enough. This may depend on how f is defined, hence I asked for clarification.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-04-23 23:32:46 +0200 )edit

Sorry for the delay -- @Max Alekseyev@ortollj, I was indeed thinking along the lines of @azerbajdzan , e.g. with f as something like sin or even N, so that I could quickly evaluate e.g. cos(23) via cos(23) // N. (But I guess in the latter case, the answer is to just use cos(23).n() .)

I've now forgotten what the context was (I didn't get any notifications for the comments added here), but I think it's based on using SageMath via an interactive shell. Sometimes I will have an expression defined as the input to one line, and then after executing it, I can access it via the up-arrow key. Rather than scroll across the line to wrap the expression in parentheses and f, it would be quicker to just type // f, especially as the cursor is already at the end of the expression.

reynormalizer gravatar imagereynormalizer ( 2024-05-09 23:22:54 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2024-05-03 11:52:50 +0200

Sébastien gravatar image

updated 2024-05-03 13:26:19 +0200

Sage is based on Interactive Python. Therefore, every feature in IPython works in SageMath. For example, one feature is automatic-parentheses-and-quotes.

So, if you are too lazy to write parenthesis and commas, then you can turn on the autocall magic and use it by starting the line by /:

sage: %autocall 1
Automatic calling is: Smart
sage: f(x,y,z) = x + y^2 + z^3
sage: /f x+1 y+2 z+3                  # no parenthesis, no comma!
(z + 3)^3 + (y + 2)^2 + x + 1

There is also the auto-quoting which can be used if you are too lazy to quote strings. It may be activated by starting the line with a comma:

sage: ,print aujourd'hui                # no parenthesis, no comma, no quotes "" !

For more information and options, see the documentation of IPython on the link above or as follows:

$ sage -ipython
Python 3.11.1 (main, Sep  2 2023, 13:40:58) [GCC 9.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: ?
edit flag offensive delete link more



@Sébastien Just to point out that it is more equivalent of Mathematica's f@x rather then x // f but anyway I upvoted, as it may be useful for OP in any case.

azerbajdzan gravatar imageazerbajdzan ( 2024-05-07 15:23:45 +0200 )edit

Thanks @Sébastien, this is useful to know if not quite what I'm looking for. I was hoping to have some feature that would have the function called at the end of the expression its acting on, just for my convenience when working in the interactive shell. But this does also appear to help, in the case that I want to quickly apply a function with multiple arguments without having to type in the parentheses and commas.

reynormalizer gravatar imagereynormalizer ( 2024-05-09 23:30:48 +0200 )edit

Indeed, my answer was not exactly answering the question. In fact, when I do not want to repeat the argument, I most often use the variable _ which always stores the previous output:

sage: f(x) = x^2+1
sage: x^2+x^3-31
x^3 + x^2 - 31
sage: f(_)
(x^3 + x^2 - 31)^2 + 1

Also __ and ___ stores the pre-previous and the pre-pre-previous outputs. Also, _8 stores the 8-th previous output, etc. All this is also documented in the same link I provided in my answer.

Sébastien gravatar imageSébastien ( 2024-05-10 20:26:23 +0200 )edit

Also, with multiple input arguments, notice that you may use * to unwrap the content of a tuple:

sage: def f(a,b,c):
....:     return a+b+c
sage: t = (1,2,3)
sage: f(*t)

Thus with the underscore storing the previous output you may do:

sage: (5,6,7)
(5, 6, 7)
sage: f(*_)
Sébastien gravatar imageSébastien ( 2024-05-10 20:29:57 +0200 )edit

Thanks again @Sébastien , I was aware of _ but your *_ is new to me and I'm sure will be useful in the future. Appreciate your time on this.

reynormalizer gravatar imagereynormalizer ( 2024-05-10 22:58:20 +0200 )edit

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: 2024-04-22 12:06:01 +0200

Seen: 301 times

Last updated: May 03