# How to obtain independent variables from a set of linear constraints

I have a question, how to obtain independent variables from a set of linear constraints. Let me illuminate this by a simple example, we have 5 variables and 3 linear constraints,

$$x1+x2+2*(x3+x4)+x5=0$$;

$$4*(x1+x2)+3*(x3+x4)+x5=0$$;

$$x1+x2+x3+x4+x5=0$$.

If we calculate the equations by hand, we obtain

$$x1+x2=0; x3+x4=0; x5=0;$$

so we have two independent variables, we can choose $$x1$$ and $$x3$$ as one possibility. My question is how to obtain independent variables by Mathematica, suppose we have more variables and constraints, so that hand calculation is impossible.

Thanks very much for your help.

• Welcome to the Mathematica Stack Exchange. Could you please load your equations as Mathematica code? You can copy directly from the input cell and paste it in the Edit window. There is a { } icon there to format it.
– Syed
Jun 13 at 14:22
• Look at the documentation for Solve. Jun 13 at 14:29
• Thanks for your reply, I do not have a Mathematica code. I just do not know how to solve this problem by Mathematica. Jun 13 at 14:29
• Yes, if I try {Solve[ x1 + x2 + 2*(x3 + x4) + x5 == 0 && 4*(x1 + x2) + 3*(x3 + x4) + 2*x5 == 0 && 4*(x1 + x2) + (x3 + x4) + x5 == 0, {x2, x4, x5}]} by Mathematica, it give solutions for x2, x4 and x5, if we consider x1 and x3 as independent variables. However, my question is how do I know x1 and x3 are independent variables? Could you please show me more hints? Jun 13 at 14:35

First one can set up and solve the system.

linpolys = {x1 + x2 + 2*(x3 + x4) + x5,
4*(x1 + x2) + 3*(x3 + x4) + x5, x1 + x2 + x3 + x4 + x5};
solns = Solve[linpolys == 0]

(* Out[83]= {{x2 -> -x1, x4 -> -x3, x5 -> 0}} *)


Now find which variables were used as parameters to describe the solution set.

indepvars = Variables[Variables[linpolys] /. solns]

(* Out[84]= {x1, x3} *)

Clear["Global*"]

eqns = x1 + x2 + 2*(x3 + x4) + x5 == 0 &&
4*(x1 + x2) + 3*(x3 + x4) + 2*x5 == 0 &&
4*(x1 + x2) + (x3 + x4) + x5 == 0;

vars = Variables[Level[eqns, {-1}]];

solVar = Subsets[vars, {3}];

{#, sol = Solve[eqns, #],
If[sol === {}, "-", Complement[vars, #]]} & /@ solVar //
Prepend[#, {"  Solve\nvariables", "Solutions",
"Independent\n variables"}] & //
Grid[#, Frame -> All, Alignment -> {Center, Center}] &
`