# How I can generate a matrix from a set of terms?

I want to build a matrix, from the following set $S = \{a b , a c\}$

  S = {a b , a c}


where the terms of S represent the raw of the matrix m , and the element in S represent the column of the matrix m , then we get

Column of m such that c1={a,a},c2={b,0} and c3={0,c}

raw of m such that r1={a,b,0} and r2={a,0,c}

building m from columns m={c1,c2,c3} or building m from raw m={r1,r2}, then we get

m={{a, b, 0}, {a, 0, c}}


$m=\left( \begin{array}{ccc} a & b & 0 \\ a & 0 & c \\ \end{array} \right)$

Is this possible in practice?

In general, is this possible regardless of the terms of set?

Thanks for the help.

• @glS Not duplicate. The idea varies in matrix construction depending on the set. – Emad kareem Aug 13 '17 at 0:29
• I read the question several times but I do not get it. Maybe you mix up MATLAB's and Mathematicas notations for matrices? Have you ever read about Transpose? Do you know that a b means $a \cdot b$ in Mathematica? – Henrik Schumacher Aug 13 '17 at 3:42
• @HenrikSchumacher Thanks for trying to help me. Answer by @Mr.Wizard♦ – Emad kareem Aug 13 '17 at 5:17

### New method

Leveraging CoefficientRules.

S = {a b, a c};

var = Variables[S];

var # & /@ CoefficientRules[S][[All, 1, 1]]

{{a, b, 0}, {a, 0, c}}


### Old Method

S = {a b, a c};

rls = MapIndexed[# -> #2.{1} -> # &, Variables[S]];

m = PadRight[SparseArray /@ (List @@@ S /. rls)]

{{a, b, 0}, {a, 0, c}}


Automation of Carl Woll's method:

PowerExpand @ Log[S /. Thread[# -> Exp@DiagonalMatrix@#]] & @ Variables[S]

{{a, b, 0}, {a, 0, c}}

• Try CoefficientRules[S][[All, 1, 1]].DiagonalMatrix[var]. – J. M.'s ennui Aug 13 '17 at 1:52
• @J.M. I actually considered that but you know how much I like brevity and var # & /@ was shorter. – Mr.Wizard Aug 13 '17 at 1:53
• Thank you so much . This exact solution. – Emad kareem Aug 13 '17 at 5:21

One idea:

S = {a b, a c};

PowerExpand @ Log[S /. {a -> Exp[{a, 0, 0}], b -> Exp[{0, b, 0}], c -> Exp[{0, 0, c}]}]


{{a, b, 0}, {a, 0, c}}

• Thank you so much. But if $S=\{a,abc,ade,ad,ae\}$This will require many inputs. When the terms of s are increased are getting harder. – Emad kareem – Emad kareem Aug 13 '17 at 0:48
rule = Thread[Variables[S] -> DiagonalMatrix[Variables[S]]];
Block[{Times = Plus}, S /. rule]


{{a,b,0},{a,0,c}}