I am doing some nasty integrals trying to fill in a matrix, and to save computation time, I set my hbar to be a constant of undefined value, so the answer it spits out consists of a real number plus some number times the square of hbar, which means I should be able to ignore it and chop it off since hbar squared is very small. Essentially I want to tell mathematica to apply to the answer the value of hbar. To clarify, I don't want to define hbar and have it do the integration again because that would make the computation time too long. I want the value of hbar to be defined only on the answer I get from the integration, not during the integration.

Thank you!

  • $\begingroup$ I took at shot at answering this. If you mean something other than how I interpreted it please clarify and I'll try again. $\endgroup$ – Mr.Wizard Feb 20 '13 at 1:21
  • 2
    $\begingroup$ why not use dimensionless units? this is usually the way to go with numerical calculations. $\endgroup$ – acl Feb 20 '13 at 2:22
  • $\begingroup$ @acl We were told not to use all physical constants equal to one. I tried it too and the integration takes a long time surprisingly. $\endgroup$ – user17338 Feb 20 '13 at 5:02

I'm not sure what you are describing is well advised but it sounds like you just want the function of $PrePrint:

$PrePrint = # /. {HoldPattern[\[HBar]] -> 0} &;


17 a + b \[HBar]^2
17 a

If you don't want to apply this every time you can just do the replacement manually:

17 a + b \[HBar]^2 /. HoldPattern[\[HBar]] -> 0
17 a
  • $\begingroup$ I just ran across some of your code on Project Euler that greatly intrigues me. Love to have you explain it some time. $\endgroup$ – kale Feb 20 '13 at 4:04
  • $\begingroup$ Is HoldPattern really necessary in the second example? $\endgroup$ – Ajasja Feb 20 '13 at 10:15
  • 1
    $\begingroup$ @Ajasja I was waiting for someone to ask that. Normally I would not use it, but it occurred to me that it might be useful to a beginner. You see if Hbar was also assigned a value then the LHS of the pattern would evaluate to this value, and you might replace more than just Hbar in an expression. For example, if you do a^(1/3) + h^2 /. h -> 0 you get a^(1/3) as expected, but if you first set h = 1/3 you get 10/9 which might be quite unexpected to a new user. Of course this situation would be avoided by a more experienced user but I thought it best to include this safeguard. $\endgroup$ – Mr.Wizard Feb 20 '13 at 21:02
  • $\begingroup$ Aha, good point. $\endgroup$ – Ajasja Feb 21 '13 at 8:27

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.