0
$\begingroup$

I would like to remove one variable in the output of the "Solve" command without changing the rest. For instance, in the following code

p = 6;
A = Solve[{p == x + y + z + t && x > 0 && y >= x && z >= y && 
 t > 0}, {x, y, z, t}, Integers]

we have the output

{x -> 1, y -> 1, z -> 1, t -> 3},{x -> 1, y -> 1, z -> 2, t -> 2},{x -> 1, y -> 1, z -> 3, t -> 1},{x -> 1, y -> 2, z -> 2, t -> 1}

and I need to remove for example the last column for "t" and have:

{x -> 1, y -> 1, z -> 1},{x -> 1, y -> 1, z -> 2},{x -> 1, y -> 1, z -> 3},{x -> 1, y -> 2, z -> 2}

without changing the code in the input, I mean, I only want this change in the output section keeping the original output there.

$\endgroup$
  • 3
    $\begingroup$ You can just do {x,y,z} /. A, no need to hack the solutions list. The reason why Solve returns lists of rules instead of lists of values is that you can use these rules to instantiate any expression you wish, not just a list of the solved variables in the original order. $\endgroup$ – Roman Aug 17 '19 at 13:27
  • 1
    $\begingroup$ After you've set A=Solve[...]. Just try it out. $\endgroup$ – Roman Aug 17 '19 at 13:33
  • 1
    $\begingroup$ Yes, you can do B=Most/@A. But it's a hack. $\endgroup$ – Roman Aug 17 '19 at 13:36
  • 1
    $\begingroup$ I mean what I already referred to: whatever you will be doing with the list of solutions can be done without editing the solutions list. For example, if in the next step you want to find out what the value of $x^3$ is, then there is no need to first extract the first elements of A; rather, you can directly do x^3 /. A, because this replacement (/.) uses only those elements in A that are actually needed. Further, if you edit A then you need to make sure you get the right order of the elements, which is risky and causes errors when you guess it wrong. $\endgroup$ – Roman Aug 17 '19 at 13:43
  • 1
    $\begingroup$ Although @Roman's answer is optimal, if you must remove solutions for t, you can use A /. (t -> _) :> Nothing $\endgroup$ – Bob Hanlon Aug 17 '19 at 19:55
3
$\begingroup$

Split the variables into {x, y, z}, {t} instead of {c, y, z, t}:

Solve[{p == x + y + z + t && x > 0 && y >= x && z >= y && t > 0}, {x, y, z}, {t}, Integers]

(* {{x -> 1, y -> 1, z -> 1}, {x -> 1, y -> 1, z -> 2}, {x -> 1, y -> 1, z -> 3}, {x -> 1, y -> 2, z -> 2}} *)
$\endgroup$
  • $\begingroup$ although it changes the input code a little bit, but I like your answer. $\endgroup$ – asad Aug 17 '19 at 19:02

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.