7
$\begingroup$

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?

$\endgroup$

5 Answers 5

11
$\begingroup$
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}
$\endgroup$
1
  • 13
    $\begingroup$ Or t . {x, y, z} to be more concise $\endgroup$
    – Roman
    Commented Jul 12, 2023 at 7:47
5
$\begingroup$

Use a loop

Table[
    a + b + c, {a, -x, x, x}, {b, -y, y, y}, {c, -z, z, z}
] // Flatten
$\endgroup$
3
$\begingroup$

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}
$\endgroup$
1
$\begingroup$
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
$\endgroup$
0
$\begingroup$
Outer[#1 x + #2 y + #3 z &, {-1, 0, 1}, {-1, 0, 1}, {-1, 0,
   1}] // Flatten
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.