0
$\begingroup$

I looked around and didn't see this question on the forum, so my apologies in advance in case this has been answered here.

My question can be concisely described by the following example:

a = b + c;
x = a + b + 2 c

The output for "x" will never contain "a", but will substitute "b+c" instead - which is obvious given my assignment of "a". How do I output "x" in terms of "a"?

Is there a built-in function that allows me to output "x" in terms of "a", or should I assign a relation for "a" differently?

My apologies for the simplicity of the question (might sound stupid to many).

Thanks a lot in advance for any help!

$\endgroup$
2
$\begingroup$

The replacement is done automatically. If you make assignment to x delayed, then you see it only in the definition

ClearAll[a,b,c,x]
a=b+c;
x:=a+b+2 c
??x

Mathematica graphics

But once you evaluate x it will replace a by b+c.

An option is to put a HoldForm around a

ClearAll[a,b,c,x]
a=b+c;
x=HoldForm[a]+b+2 c;
x

Mathematica graphics

But you will have to eventually remove the HoldForm to do something useful with the expression, and once you do that, Mathematica will replace a again.

You can't have your cake and eat it too. If you want a not to be replaced by its value, do not assign a value to it before.

I think automatic replacement is build-into Mathematica evaluation loop and I do not know other ways to prevent it other than Hold and its friends.

You could also save a temporarily

ClearAll[a,b,c,x]
a=b+c;
z=a; Clear[a];
x=a+b+2 c
a=z;

Mathematica graphics

Or you could use different context for a

ClearAll[a,b,c,x]
a=b+c;
Module[{a},
   x=a+b+2 c
]

If you explain what is it you are trying to accomplish, may be there is a better way.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.