# Using the output of Solve

I had a math problem I solved like this:

``````In[1]:= Solve[2x(a-x)==0, x]
Out[1]= {{x->0}, {x->a}}

In[2]:= Integrate[2x(a-x), {x,0,a}]
Out[2]= (a^3)/3

In[3]:= Solve[(a^3)/3==a, a]
Out[3]= {{a->0}, {a->-Sqrt[3]}, {a->Sqrt[3]}}
``````

My question is if I could rewrite this to compute it in one step, rather than having to manually input the result from the previous line. I could easily replace the integral used in step three with the `Integrate` command from step two. But what I can't figure out is how I would use the result from step 1 as the limits of integration in the integral.

-

You could combine step 1 and 2 by doing something like

``````Integrate[2 x (a - x), {x, ##}] & @@ (x /. Solve[2 x (a - x) == 0, x]);
``````
-
`Integrate[2 x (a - x), {x, ##}] & @@ (x /. Solve[2 x (a - x) == 0, x])//Solve[#==a,a]&` gets all three steps together. –  kguler Dec 2 '11 at 11:09
@kguler, post it as an answer, alongside an explanation of what it is doing. I'd vote for it. –  rcollyer Dec 2 '11 at 14:05
@rcollyer, Thank you! I think Heike's answer adresses the specific question asked. What I added is just piping the expression produced by Heike's answer into the pure function `Solve[#==a,a]`. I think some would enjoy to play with the following pure function that combines all three steps: ```solveIntegrateSolve := (Integrate[#1, {#2, ## & @@ (#2 /. Solve[#1 == 0, #2])}] // (Solve[# == t, t] & /. t -> #3)) &```. Use it as `solveIntegrateSolve[2x(a-x),x,a]` or `solveIntegrateSolve@@{2x(a-x),x,a}`. –  kguler Dec 3 '11 at 0:22
Nice, ReplaceAll elegantly gets the values out of the result `Solve` produces. Now why didn't I think of that? –  Mr Alpha Dec 3 '11 at 9:12

If you agree to delegate the choice of the (positive oriented) domain to `Integrate`, by means of using `Clip` or `Boole`:

``````In[77]:= Solve[
Integrate[
Clip[2 x (a - x), {0, Infinity}], {x, -Infinity, Infinity}] == a, a]

Out[77]= {{a -> 0}, {a -> Sqrt[3]}}
``````

or

``````In[81]:= Solve[
Integrate[
2 x (a - x) Boole[2 x (a - x) > 0], {x, -Infinity, Infinity}] ==
a, a]

Out[81]= {{a -> 0}, {a -> Sqrt[3]}}
``````

The reason only non-negative roots are found, is that `Integrate` will integrate from the smallest root to the largest root, i.e. from `{x,0,a}` for positive `a` and `{x,a,0}` for negative `a`.

-
The text passage contains a mathematical question about how to combine multiple steps of a computation into a single step using Mathematica. It also provides several answers or suggestions to the question.
single_question_with_answer
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Using the output of Solve

I had a math problem I solved like this:

``````In[1]:= Solve[2x(a-x)==0, x]
Out[1]= {{x->0}, {x->a}}

In[2]:= Integrate[2x(a-x), {x,0,a}]
Out[2]= (a^3)/3

In[3]:= Solve[(a^3)/3==a, a]
Out[3]= {{a->0}, {a->-Sqrt[3]}, {a->Sqrt[3]}}
``````

My question is if I could rewrite this to compute it in one step, rather than having to manually input the result from the previous line. I could easily replace the integral used in step three with the `Integrate` command from step two. But what I can't figure out is how I would use the result from step 1 as the limits of integration in the integral.

-

You could combine step 1 and 2 by doing something like

``````Integrate[2 x (a - x), {x, ##}] & @@ (x /. Solve[2 x (a - x) == 0, x]);
``````
-
`Integrate[2 x (a - x), {x, ##}] & @@ (x /. Solve[2 x (a - x) == 0, x])//Solve[#==a,a]&` gets all three steps together. –  kguler Dec 2 '11 at 11:09
@kguler, post it as an answer, alongside an explanation of what it is doing. I'd vote for it. –  rcollyer Dec 2 '11 at 14:05
@rcollyer, Thank you! I think Heike's answer adresses the specific question asked. What I added is just piping the expression produced by Heike's answer into the pure function `Solve[#==a,a]`. I think some would enjoy to play with the following pure function that combines all three steps: ```solveIntegrateSolve := (Integrate[#1, {#2, ## & @@ (#2 /. Solve[#1 == 0, #2])}] // (Solve[# == t, t] & /. t -> #3)) &```. Use it as `solveIntegrateSolve[2x(a-x),x,a]` or `solveIntegrateSolve@@{2x(a-x),x,a}`. –  kguler Dec 3 '11 at 0:22
Nice, ReplaceAll elegantly gets the values out of the result `Solve` produces. Now why didn't I think of that? –  Mr Alpha Dec 3 '11 at 9:12

If you agree to delegate the choice of the (positive oriented) domain to `Integrate`, by means of using `Clip` or `Boole`:

``````In[77]:= Solve[
Integrate[
Clip[2 x (a - x), {0, Infinity}], {x, -Infinity, Infinity}] == a, a]

Out[77]= {{a -> 0}, {a -> Sqrt[3]}}
``````

or

``````In[81]:= Solve[
Integrate[
2 x (a - x) Boole[2 x (a - x) > 0], {x, -Infinity, Infinity}] ==
a, a]

Out[81]= {{a -> 0}, {a -> Sqrt[3]}}
``````

The reason only non-negative roots are found, is that `Integrate` will integrate from the smallest root to the largest root, i.e. from `{x,0,a}` for positive `a` and `{x,a,0}` for negative `a`.

-
user
The text passage contains a mathematical question about how to combine multiple steps of a computation into a single step using Mathematica. It also provides several answers or suggestions to the question.

TYPE: SINGLE_QUESTION_WITH_ANSWER