2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ @glS Not duplicate. The idea varies in matrix construction depending on the set. $\endgroup$ – Emad kareem Aug 13 '17 at 0:29
  • 1
    $\begingroup$ 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? $\endgroup$ – Henrik Schumacher Aug 13 '17 at 3:42
  • $\begingroup$ @HenrikSchumacher Thanks for trying to help me. Answer by @Mr.Wizard♦ $\endgroup$ – Emad kareem Aug 13 '17 at 5:17
5
$\begingroup$

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}}
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Try CoefficientRules[S][[All, 1, 1]].DiagonalMatrix[var]. $\endgroup$ – J. M.'s technical difficulties Aug 13 '17 at 1:52
  • 1
    $\begingroup$ @J.M. I actually considered that but you know how much I like brevity and var # & /@ was shorter. $\endgroup$ – Mr.Wizard Aug 13 '17 at 1:53
  • $\begingroup$ Thank you so much . This exact solution. $\endgroup$ – Emad kareem Aug 13 '17 at 5:21
5
$\begingroup$

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}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Emad kareem Aug 13 '17 at 0:48
1
$\begingroup$
rule = Thread[Variables[S] -> DiagonalMatrix[Variables[S]]];
Block[{Times = Plus}, S /. rule]

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

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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