# Solving linear equations of two variables, where one variable is a list of numbers [closed]

For example:

 X + Y == 1


where

X = {1, 2, 3, 4, 5}


How can I solve for Y in Mathematica? The answer Y should also be a list.

• Sir show your code please and your question clearly ! – Alrubaie May 30 at 13:20
• BTW, a standard pro-tip is to avoid capitals (see pt. 4). – Michael E2 May 30 at 15:40
• Sorry，It's my first post on stackexchange, and my English is not well. Thanks for Alrubaie ,Michael E2 ,and others' help. – fayehoo May 31 at 2:49

## 2 Answers

Is this what you want?:

Clear[X];
Solve[X + Y == 1 , Y] /. X -> {1, 2, 3, 4, 5}

(*  {{Y -> {0, -1, -2, -3, -4}}}  *)


Explanation:

The first alternative code below is perhaps conceptually the right thing to do, but it's not as simple as the above code. Other alternatives deal with the shortcoming of Plus by modifying Plus, which is potentially dangerous in more complicated situations. The "shortcoming" here — and it's a shortcoming only in some limited contexts — is that Plus at basis represents scalar addition. It treats what the user sees as a vector as a list of scalars to be threaded with the other arguments (see Thread); it treats variables such as Y as representing a scalar. Mathematica does not have a VectorPlus or a MatrixPlus. The threading action of Plus may be disabled by clearing the attribute Listable, and this is shown below. In the OP's case, the variables X and Y are to be treated as vectors, but there's a scalar 1 on the right hand side that should be threaded. So we want X + Y, which is to be {1, 2, 3, 4, 5} + Y, not to be threaded until we have the solution Y -> 1 - X = Y -> 1 - {1, 2, 3, 4, 5}. At this point, addition/subtraction can be threaded. This is what the code above does.

Alternatives:

The first two alternatives below avoid changing Plus; the last two show the temporary disabling of the listability of Plus. If the Listable attribute of Plus is cleared or Plus is blocked, other code, including unknown internal code, that relies on Plus being normal may fail to work, even though the examples below seem fine. The second through fourth alternatives represent different ways to accomplish the approach above. Note that the second alternative uses Inactivate to disable Plus only for the equation to be solved; thus Plus will work as intended in internal code, unlike the last two examples. The first alternative threads the 1 before the equation is solved, but we need to post-process the result to get a list as a solution.

X = {1, 2, 3, 4, 5};
Solve[Or @@ Thread[X + Y == 1], Y] (* manually threading the scalar 1 *)
(* yields multiple solutions; *)
Normal@Merge[%, Join]      (* merges into a single solution list *)
Clear[X]

(*
{{Y -> 0}, {Y -> -1}, {Y -> -2}, {Y -> -3}, {Y -> -4}}
{Y -> {0, -1, -2, -3, -4}}
*)

Block[{X = {1, 2, 3, 4, 5}},
Quiet[Solve[Inactivate[X + Y == 1, Plus], Y], Solve::ifun] // Activate
]

(*  {{Y -> {0, -1, -2, -3, -4}}}  *)

(* potentially dangerous *)
Block[{X = {1, 2, 3, 4, 5}},
InternalInheritedBlock[{Plus},
ClearAttributes[Plus, Listable];
Solve[X + Y == 1, Y]
]]

(*  {{Y -> {0, -1, -2, -3, -4}}}  *)

(* simple and more dangerous *)
Block[{Plus},
X = {1, 2, 3, 4, 5};
Solve[X + Y == 1, Y]
]
Clear[X]

(*  {{Y -> {0, -1, -2, -3, -4}}}  *)


Another alternative is to call Solve for each scalar value in X. This is essentially the approach in @Alrubaie's answer.

Doing your Example sir.

x = {1, 2, 3, 4, 5}
eq[x_, y_] := x + y

sol = Table[{x, y /. Flatten[Solve[eq[x, y] == 1]]}, {x, 1, 5, 1}]

y -> Last /@ sol


or use Directly

sol = Table[{y /. Flatten[Solve[eq[x, y] == 1]]}, {x, 1, 5, 1}]

• sols = Table[Solve[x + Y == 1], {x, X}] would work no matter what the list/vector X of numbers was. Then one would merge the solutions in whatever way one prefers (e.g. Normal@Merge[sols, Join]`). (Not the downvoter, and I'm not sure why it was DV'ed. One might feel a better answer is possible. In that case, leaving a suggestion, posting your own answer, or just moving on seem better responses. The code does work as shown, so it's not like it's wrong or misleading.) – Michael E2 May 30 at 15:55
• appreciated your comment sir – Alrubaie May 30 at 15:58