Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+1 vote

When one wants to copy expression, (s)he needs to do something like

ex:=a b;
ex1:=@(ex).
substitute(_,$a->c$);

but it's not direct, is there a more direct way to do this? For example,

ex:=a b;
substitute(@(ex),$a->c$);
in Feature requests by (2.1k points)

3 Answers

0 votes
 
Best answer

I would only like to add that a possible variant is

substitute($@[ex]$, $a -> c$)

Using square brackets also creates a copy and does not affect the original expression.

by (1.7k points)
selected by

Thanks, this is more natural!

In fact, ( ) also works.

+1 vote

Cadabra's philosophy has always been that algorithms act on expressions in-place, for reasons that have been discussed elsewhere. That's not everyone's preferred mode of operation, but changing it now would not be a good idea.

The notation you propose is an option to get closer to 'act on a copy. I would personally probably prefer something slightly more explicit, e.g.

substitute( copy(ex), $a->c$ );
by (82.0k points)
0 votes

Hi Eureka and Kasper.

I just wanted to point out that your suggested way to substitute on a copy has the disadvantage that it is not possible to retreive the result, buecause it lacks a name!

I'd suggest the following modification to Kasper's solution:

ex:=a b;
ex1 = substitute(ex.copy(), $a->c$);

In whose case the result of the substitution can be recalled by ex1;.

Best wishes, Dox.

by (14.3k points)

Yes, you of course need to assign to a new expression if you want to continue with it. Furthermore, we indeed already have ex.copy() which is preferable over my copy(ex). Thanks.

And to be precise: this already works now, without any changes, whereas the @ notation would require internal changes.

Thanks. This is what I want. It's direct enough.

...