Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Hopefully a simple problem for the experts here.

I have a need many times to build a symbolic expression for an equation (say a PDE) to use for plot labels in a low level function inside Manipulate, and so I make a function that I call to build the pde in symbolic form, so I can use it for a plot label. I pass the PDE parameters to this function, and the function uses HoldForm to build it.

But when one of the parameters is 1 (or even zero), and because of HoldForm, these remain stuck there and do not simplify away, and I'd like to remove them.

Currently I use If statement to check for these cases. But I have a feeling there is a better way.

Here is the function (example)

f[c_] := Module[{},
  HoldForm[c* D["u"["x", "t"], {"x", 2}] == "f"["x", "t"]] 
  ]

now, when I call it like this

f[1] 

it returns

enter image description here

So now I do

f[c_] := Module[{},
  If[c == 1,
   HoldForm[ D["u"["x", "t"], {"x", 2}] == "f"["x", "t"] ],
   HoldForm[c* D["u"["x", "t"], {"x", 2}] == "f"["x", "t"] ],
   ]
  ]

and now f[1] returns

enter image description here

I tried many things, (Defer, Simplify, Evaluate, etc...) and nothing works, other than by doing this manual check before using HoldForm.

And about the use of strings there, I had to do it this way :). Too many problems if I use actual symbols. Long story. May be for another topic I can explain why, but for the above works ok for me now other than those extra checks I have to do manually.

question is: is there a trick to do what I am doing about without the If check (I have few parameters to check, and have to do many checks for 1's and 0's as well). Not a big problem for me to do the checks, just wondering if there is a trick I am overlooking.

thanks

share|improve this question
1  
Nasser, in passing, why do you keep using Module[{}, ...]? –  Mr.Wizard Jan 19 '12 at 16:13
2  
Nasser, I fear I have received that answer before; my apologies for not remembering. In a compiled language I believe the empty module would not affect speed, but in Mathematica it does. The hit is small, but in cases of fast and simple functions it is significant. I would personally prefer that you leave the extraneous Module out of your questions as IMHO it obscures the function, and also results in propagation of the empty Module to answers. –  Mr.Wizard Jan 20 '12 at 4:11

1 Answer 1

up vote 7 down vote accepted

Why don't you use HoldForm only on the part that actually needs to be held? For example:

f[c_] := Module[{}, 
  c HoldForm[D["u"["x", "t"], {"x", 2}]] == "f"["x", "t"]]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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