# Extracting Reduce results

I'm solving a Diophantine equation inside of a function using Reduce but I'm having trouble extracting the necessary parts of the answer.

For example, if my input equation is linear, I get an output from Reduce like:

C[1] ∈ Integers && x == 8 + 49 C[1] && k == 1 + 5 C[1]


if the equation is a quadratic, though, I get:

(C[1] ∈ Integers && x == 20 - 49 C[1] &&
k == 41 - 200 C[1] + 245 C[1]^2) || (C[1] ∈ Integers &&
x == 29 - 49 C[1] && k == 86 - 290 C[1] + 245 C[1]^2)


if the equation is cubic, I get:

C[1] ∈
Integers && (x == 2 + 49 C[1] || x == 11 + 49 C[1] ||
x == 36 + 49 C[1]) && k == 1/49 (9 + 5 x^3)


How do I identify and/or extract, say, the resulting condition on x? I originally used Part when I just had the linear case. So for example I would do something like:

Ans=Reduce[SomeEquality[x,k],{x,k},Integers];
X=x/.ToRules[Ans[[2]]/.{C[1]->0}]


But the outputs in the quadratic and cubic cases are in wildly different places...

• The conditions you're getting at depend not only on x, but on the existence of another constants C[i], which must be integers in your examples. You can't get rid of them if you want the full result set. There isn't "a condition on x" to extract. Sorry. – Dr. belisarius Mar 29 '13 at 6:00
• It's hard to tell what you want in general, Aeryk, but for these particular cases would it be the output of Last@Reap[(output of Reduce)/. {Equal[x, y_] :> Sow[y]}]? This gives a list of all the "somethings" where the form x==(something) appears in the solution. – whuber Mar 29 '13 at 22:49

Cannot test this in Mathematica right now, but pattern matching should work to get the bits that appear next to x. So for instance you could use

Cases[Ans,(x == z_)->(z/.C[1]->0),Infinity]


That will capture all the terms where x appears on the left of == and will give return what on the right side of that equality with C[1] replaced by 0.

• Close, but to get to set C[1] to 0, it needs to be this way: Cases[ans, (x == z_) -> (z), Infinity] /. C[1] -> 0 And that's just what I needed. Thanks! – Aeryk Mar 31 '13 at 18:46
• Your welcome. I forgot that the syntax for -> in ´Cases´ is rather rigid. Nesting ´/.´ and ´->´ will work in replacements though. – Eduardo Serna Apr 1 '13 at 19:45

It's a bit difficult to answer here because you cannot simply throw the condition of Element[C[1], Integers] away. At least you have to remember this and when your goal is to transform this result into a set of rules, then just throw the Element parts away and apply ToRules

(C[1] \[Element] Integers && x == 20 - 49 C[1] && k == 41 - 200 C[1] + 245 C[1]^2) ||
(C[1] \[Element] Integers && x == 29 - 49 C[1] && k == 86 - 290 C[1] + 245 C[1]^2)
/. HoldPattern[Element[__]] :> Sequence[]

{ToRules[%]}

(*
{{x -> 20 - 49 C[1], k -> 41 - 200 C[1] + 245 C[1]^2},
{x -> 29 - 49 C[1], k -> 86 - 290 C[1] + 245 C[1]^2}}
*)