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 ∈ Integers && x == 8 + 49 C && k == 1 + 5 C


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

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


if the equation is cubic, I get:

C ∈
Integers && (x == 2 + 49 C || x == 11 + 49 C ||
x == 36 + 49 C) && 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[]/.{C->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->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 replaced by 0.

• Close, but to get to set C to 0, it needs to be this way: Cases[ans, (x == z_) -> (z), Infinity] /. C -> 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, 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 \[Element] Integers && x == 20 - 49 C && k == 41 - 200 C + 245 C^2) ||
(C \[Element] Integers && x == 29 - 49 C && k == 86 - 290 C + 245 C^2)
/. HoldPattern[Element[__]] :> Sequence[]

{ToRules[%]}

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