How to construct simultaneous equations from two lists?
coefs = {{2, 3, 5}, {1, -1, 0}};
Two additional approaches:
1. Using the fourth argument of Array
to do all in a single step:
ClearAll[slv]
slv[a_] := Array[x, Length @ a, 1, Solve[Thread[a. {##, -1} == 0], {##}] &]
slv @ coefs
{{x[1] -> 1, x[2] -> 1}}
2. Process coefs
directly into input for LinearProgramming
:
ClearAll[lp]
lp[a_] := LinearProgramming[ConstantArray[0, Length@a], Most /@ a, Thread[{Last /@ a, 0}]]
lp @ coefs
{1, 1}
One way might be
eqs = And @@ Thread[coefs[[1 ;; 2, 1 ;; 2]].vars == coefs[[All, 3]]]
Applying it
coefs = {{2, 3, 5}, {1, -1, 0}};
vars = Array[x, 2];
equ = And @@ Thread[coefs[[1 ;; 2, 1 ;; 2]].vars == coefs[[All, 3]]];
Solve[equ, vars]
If you need to see the equations, use
equ = Thread[coefs.Append[vars, -1] == 0]
Or, just go straight to the solution
Solve[Thread[coefs.Append[vars, -1] == 0], vars]