Suppose I have an expressions, i.e.,
a*x + b*y + c*z
If the coefficient $a,b,c \in \{-1,0,1\}$, I want to make them as a list., i.e. ,
$\{ \{-x+y\},\{-x-y-z\}, \cdots\}$
Is there any Mathematica command for this?
t = Tuples[{-1, 0, 1}, 3]
# . {x, y, z} & /@ t
{-x - y - z, -x - y, -x - y + z, -x - z, -x, -x + z, -x + y - z, -x + y, -x + y + z, -y - z, -y, -y + z, -z, 0, z, y - z, y, y + z, x - y - z, x - y, x - y + z, x - z, x, x + z, x + y - z, x + y, x + y + z}
t . {x, y, z}
to be more concise
$\endgroup$
Use a loop
Table[
a + b + c, {a, -x, x, x}, {b, -y, y, y}, {c, -z, z, z}
] // Flatten
For this particular example:
Mod[IntegerDigits[Range[0, 26], 3, 3], 3, -1] . {x, y, z}
yields:
{0, z, -z, y, y + z, y - z, -y, -y + z, -y - z, x, x + z, x - z,
x + y, x + y + z, x + y - z, x - y, x - y + z,
x - y - z, -x, -x + z, -x - z, -x + y, -x + y + z, -x + y - z, -x -
y, -x - y + z, -x - y - z}
Table[a*x + b*y + c*z, {a, Range[-1, 1, 1]}, {b, Range[-1, 1, 1]}, {c,
Range[-1, 1, 1]}] // Flatten
If you like the idea of Metaprogramming, here is one example:
n = 3;
vars = Array[Symbol["x" <> ToString[#]] &, n];
coeffs = Array[Symbol["coeff" <> ToString[#]] &, n];
(* Just modify here *)
coeffSel[n_Integer] := {-1, 0, 1};
exprTerm[n_Integer] := vars[[n]]*coeffs[[n]];
expr = Total[Array[exprTerm@# &, n]]
(* Just modify here *)
functionInput = ({expr}~Join~
Array[{Symbol["coeff" <> ToString[#]], coeffSel@#} &, n])
Flatten@*Table @@ functionInput
Outer[#1 x + #2 y + #3 z &, {-1, 0, 1}, {-1, 0, 1}, {-1, 0,
1}] // Flatten