3
$\begingroup$

Here's an example to illustrate the issue I'm having.

right = 8 - y;
left = y^2 + 6;

integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]

enter image description here

(I realize this is a strange form for integrand but it is the form I need to work with.) Here, HoldForm allows its argument to be evaluated, and then holds the form of the resulting output. This is what I wanted.

Then

simpleintegrand = ReleaseHold[integrand]

enter image description here

which is fine.

However,

HoldForm[Integrate[Evaluate[simpleintegrand], {y, 0, 1}]]

yields

enter image description here

(removing the Evaluate doesn't help) when I was after $$\int_0^1 (2-y-y^2)\,dy.$$

(In another direction, I'd actually prefer to get $$\int_0^1 (-y^2-y+2)\,dy$$ and am happy to hear how, but the crux of my question is how to use HoldForm when its argument involves nested commands like above.)

$\endgroup$
6
  • $\begingroup$ Integrate[HoldForm[Evaluate@simpleintegrand], {y, 0, 1}] but I've failed to apply PolynomialForm[#,TraditionalOrder->True]& to get the second form. $\endgroup$
    – Kuba
    Aug 15, 2013 at 23:56
  • 1
    $\begingroup$ The simple answer is that Evaluate only works when it is at the first level of a held expression. Consider: Hold[1, Evaluate[2 + 2], 3] versus Hold[{1, Evaluate[2 + 2], 3}] -- in the second case nothing evaluates. But instead of telling us that this "is the form I need to work with" why don't you tell us what you are trying to accomplish and we may be able to give you a different approach. $\endgroup$
    – Mr.Wizard
    Aug 16, 2013 at 0:06
  • $\begingroup$ Almost the exact same question was already asked. There are million ways to do this and the keyword is non-standard evaluation. This is not restricted to HoldForm! One possible solution to your first question is HoldForm[Integrate[#, {y, 0, 1}]]&[Evaluate[simpleintegrand]]. I'll mark it as duplicate so you'll see the original Q&A. $\endgroup$
    – halirutan
    Aug 16, 2013 at 0:32
  • $\begingroup$ possible duplicate of Question about evaluation control $\endgroup$
    – halirutan
    Aug 16, 2013 at 0:32
  • $\begingroup$ @halirutan: Before posting, I studied that example. However, it seemed to me that with my example it was the combination of the Integrate and the evaluation of the integrand, whereas there was only the former in the linked question. $\endgroup$
    – JohnD
    Aug 16, 2013 at 0:41

3 Answers 3

4
$\begingroup$

Looking at comments such as this:

For example, right = 9-y^2; left = 5; HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] doesn't preserve the "right minus left" ordering, yet HoldForm[HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]] fails. Ideas?

I think you didn't understand what I said about Evaluate so I'll try again.

  • Evaluate only works when it is the explicit head of an expression at level one.

  • You cannot use Evaluate in the way you keep attempting: it won't work.

Instead you need to use a different method to control evaluation in your expressions.
Let me direct you to some useful posts on the subject:

Replacement inside held expression

Injecting a sequence of expressions into a held expression

But here you don't even need these advanced methods, you can do a simple replacement:

right = 9 - y^2; left = 5;
HoldForm[r - l] /. {r -> right, l -> left}
(9-y^2)-5

Or a Function:

right = 9 - y^2; left = 5;
HoldForm[# - #2] &[right, left]
(9-y^2)-5

Kuba criticizes this code if e.g. left = -5 is used saying that "in general it is not useful." I disagree. It outputs exactly what you instructed it to.

right = 9 - y^2; left = -5;
HoldForm[# - #2] &[right, left]
(9-y^2)--5

You introduce two separate Times[-1, _] expressions here; look at the FullForm:

HoldForm[# - #2] &[right, left] // FullForm
HoldForm[Plus[Plus[9,Times[-1,Power[y,2]]],Times[-1,-5]]]

You should use a form that does not if you want proper formatting of Plus by `MakeBoxes:

HoldForm[+##] &[right, 5]
HoldForm[+##] &[right, -5]
(9 - y^2) + 5

(9 - y^2) - 5

See this answer and the ones linked within for more examples:

How can I reorder the factors in the terms of a polynomial?

$\endgroup$
5
  • 1
    $\begingroup$ The problem is, that in general it is not useful, try: left = -5; $\endgroup$
    – Kuba
    Aug 17, 2013 at 9:02
  • $\begingroup$ This is helpful, especially the links. Several other methods I tried kept running into the issue Kuba points out here. The $--5$ isn't a problem (for my application) if it would write it as $-(-5)$. $\endgroup$
    – JohnD
    Aug 17, 2013 at 14:16
  • $\begingroup$ @Kuba Please see update. $\endgroup$
    – Mr.Wizard
    Aug 17, 2013 at 14:42
  • $\begingroup$ @Mr.Wizard Thanks for update. Yes, it looks like I criticized the code but I should have added that that answer was good (+1ed earlier). I know we are getting what we are asking for :). I just wanted to point out this issue which would appear for everyone who was going to play with this code. p.s. - I like those tiny details in your codes, here the + ##. $\endgroup$
    – Kuba
    Aug 17, 2013 at 14:50
  • $\begingroup$ Very helpful indeed. Thus far, this seems to be doing best what I need across the widest variety of cases: HoldForm[Subtract[#1, #2]] &[right, left]. $\endgroup$
    – JohnD
    Aug 17, 2013 at 16:31
3
$\begingroup$

How about this:

right = 8 - y; left = y^2 + 6;
integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]

integrand.png

With[{f = ReleaseHold@integrand}, HoldForm@Integrate[f, {y, 0, 1}]]

integral.png

% // ReleaseHold
7/6
$\endgroup$
2
  • $\begingroup$ This is helpful. I'm still trying to better understand HoldForm especially when I want to pass arguments some of which I want evaluated. For example, right = 9-y^2; left = 5; HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] doesn't preserve the "right minus left" ordering, yet HoldForm[HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]] fails. Ideas? $\endgroup$
    – JohnD
    Aug 16, 2013 at 14:13
  • $\begingroup$ @JohnD. At this point, I think the best thing I can do is recommend David Wagner's book, which has a good discussion of working with held forms. Go to this link for info on how to get for free. Also, what you bring up with the ordering of right = 9 - y^2; left = 5; has more to do with the Orderless attribute of Plus than it does with held forms. $\endgroup$
    – m_goldberg
    Aug 16, 2013 at 14:33
2
$\begingroup$

As suggested in my comment, for your first question please look at the explanations here

Question about evaluation control

For your second question about the polynomial ordering, you can use the same approach to create and fix the presentation

(* your original code *)
right = 8 - y;
left = y^2 + 6;
integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]

(* tweaked presentation *)
Integrate[#, {y, 0, 1}] &[
 HoldForm[Plus[##]] & @@ MonomialList[ReleaseHold[integrand]]]

and you get

$$\int_0^1 \left(-y^2-y+2\right) \, dy$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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