1
$\begingroup$

I want to split a list of sums into a sum of lists For example, I'd like to turn

{{x + y + z}, {x}, {y}, {z}} 

into

x{{1}, {1}, {0}, {0}} +y{{1}, {0}, {1}, {0}} + z{{1}, {0}, {0}, {1}}

as you would when turning a vector with a sum inside it into a sum of vectors.

I've looked all over for documentation for this kind of thing, but I couldn't find anything :(. It would be even further helpful if someone could tell me how to put these three vectors into a single list, I know I can append them, I'm just not sure how to do it when they're being multiplied by something.

$\endgroup$

1 Answer 1

3
$\begingroup$

You can do this using CoefficientArrays:

expr   = {{x + y + z}, {x}, {y}, {z}};
coeffs = Last@CoefficientArrays[expr, {x, y, z}] ~Flatten~ 1 // Transpose // Normal
(* {{1, 1, 0, 0}, {1, 0, 1, 0}, {1, 0, 0, 1}} *)

You can just Dot this with the variable list to get back the original list:

{x, y, z} . Map[List, coeffs, {2}]
(* {{x + y + z}, {x}, {y}, {z}} *)
$\endgroup$
1
  • $\begingroup$ You are a saint, thank you so much! $\endgroup$
    – Davenine
    Commented Apr 26, 2013 at 5:32

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.