Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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}} *)
share|improve this answer
    
You are a saint, thank you so much! –  Davenine Apr 26 '13 at 5:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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