Message Boards Message Boards

0
|
5211 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do you pass an unevaluated variable?

Suppose you have 3x^3 + 6x^3 + a 6x^3 and you want to replace all the terms with 6x^3 by zero. This works:

In[1]:= HoldForm[3 x^3 + 6 x^3 + a 6 x^3]/. 6x^3->0//ReleaseHold
Out[1]= 3 x^3

But if you have:

In[2]:= var=3 x^3 + 6 x^3 + a 6 x^3;
HoldForm[var]/. 6x^3->0//ReleaseHold
Out[2]= 9 x^3 + 6 a x^3

the var gets evaluated before it is even held.

I've tried using With and Defer and Module and Unevaluated every kind of Hold, even HoldForm-ing the var.

How do you do this?

POSTED BY: Eric Johnstone
4 Replies

Thanks,Nasser.

I agree, strings are not the answer. I should have been more explicit: I want to pass a variable in a function.

Here is something that might work:

In[77]:= Remove[test1];
SetAttributes[test1,HoldAllComplete];
test1[var_]:=HoldForm@var/. 6 x^3->0//ReleaseHold
test1[3x^3+6x^3+6 a x^3]
Out[80]= 3 x^3
POSTED BY: Eric Johnstone

As was mentioned above, once you write var=... then the expression has been evaluated and too late to do anything about it. If you still want to have the form of the expression be there without the Hold showing up. You can use Defer like this

   var = Defer[3 x^3 + 6 x^3 + a 6 x^3]
   (*3 x^3 + 6 x^3 + a 6 x^3*)

Now you can do var /. (6 x^3 -> 0) then to remove the defer, replace it with Identity

enter image description here

Maybe there is a way to pass it as characters.

I would think that using strings to Manipulate algebraic expressions is not the right way to go about these things and defeats the purpose of having symbolic computation.

POSTED BY: Nasser M. Abbasi

Thanks, David.

That sort of defeats the purpose of Hold, no? Maybe there is a way to pass it as characters.

Eric

POSTED BY: Eric Johnstone

In your second example var was evaluated in the first line. So, short of wrapping the definition of var with something like Hold as in

var = Hold[3 x^3 + 6 x^3 + a 6 x^3];

there is not much you can do since by the time it gets to your HoldForm in your second line is has already been evaluated to

9 x^3 + 6 a x^3
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract