2
$\begingroup$

I have expression: $$f(x)=\sum_{i=0}^{K-1}\sum_{j=0}^{N-1}a_{i,j}e^{ix}x^j.$$ I want to extract coefficient matrix $A$ from expression $f(x)$ where $$A=\left( \begin{array}{ccccc} a_{0,0}& \cdots & a_{0,j} & \cdots & a_{0,N-1} \\ \vdots & \cdots & \vdots & \cdots & \vdots\\ a_{i,0} & \cdots & a_{i,j} & \cdots & a_{i,N-1} \\ \vdots & \vdots & \cdots & \vdots\\ a_{K-1,0} & \cdots & a_{K-1,j} & \cdots & a_{K-1,N-1} \\ \end{array} \right).$$ For example: $$f(x)=-\frac{1}{2} e^{-x} x^3+\frac{1}{2} e^{-2 x} x^2+\frac{1}{2} e^{-x} x^2+2 e^{-2 x} x-2 e^{-x} x+e^{-2 x}-2 e^{-x}+1$$ and its $A$ is $$A=\left( \begin{array}{cccc} 1 & 0 & 0 & 0 \\ -2 & -2 & \frac{1}{2} & -\frac{1}{2} \\ 1 & 2 & \frac{1}{2} & 0 \\ \end{array} \right).$$ Does Mathematica have specific function for this, or else can someone help me to develop a technique using existing functions in Mathematica?

$\endgroup$
3
$\begingroup$

A simple solution would be using Coefficient

p = Sum[c[i, j] x^i Exp[I j x], {i, 0, 3}, {j, 0, 3}];

Table[a[i, j] = Coefficient[Coefficient[p, x, i], Exp[I x], j]
 , {j, 0, 3}, {i, 0, 3}];

MatrixForm[%]    

$ \left( \begin{array}{cccc} c(0,0) & c(0,1) & c(0,2) & c(0,3) \\ c(1,0) & c(1,1) & c(1,2) & c(1,3) \\ c(2,0) & c(2,1) & c(2,2) & c(2,3) \\ c(3,0) & c(3,1) & c(3,2) & c(3,3) \\ \end{array} \right) $

General structure

The syntax is applicable for any arbitrary polynomial $\sum_{i,j} x^i y^j$

p = Sum[c[i, j] x^i y^j, {i, 0, 3}, {j, 0, 3}];

var1 = x;
var2 = y;
Table[a[i, j] = Coefficient[Coefficient[p, var1, i], var2, j]
 , {j, 0, 3}, {i, 0, 3}];

For your given example

p = -1/2 Exp[-x] x^3 + 1/2 Exp[-2 x] x^2 + 1/2 Exp[-x] x^2
 + 2 Exp[-2 x] x - 2 Exp[-x] x + Exp[-2 x] - 2 Exp[-x] + 1

var1 = x ;
var2 = Exp[-x] ;
Table[a[i, j] = Coefficient[Coefficient[p, var1, i], var2, j]
 , {j, 0, 3}, {i, 0, 3}];

MatrixForm[%]

$ \left( \begin{array}{cccc} 1 & 0 & 0 & 0 \\ -2 & -2 & \frac{1}{2} & -\frac{1}{2} \\ 1 & 2 & \frac{1}{2} & 0 \\ 0 & 0 & 0 & 0 \\ \end{array} \right) $

You can easily extend it for more variable. In that case you would not be able to see it as a matrix, but the information will be stored in a[i,j,...].

$\endgroup$
  • $\begingroup$ Thanks @Sumit, How can I change the second line if my p = 1 - E^(-3 x) + 3 E^(-2 x) - 3 E^-x + 3 E^(-2 x) x^2 - 3 E^-x x^2 + E^(-2 x) x^3 + E^-x x^3 + 1/4 E^(-2 x) x^4 - 1/4 E^-x x^4 $\endgroup$ – Frey May 15 '15 at 8:24
  • $\begingroup$ just replace Exp[I x] by Exp[-x] in Coefficient. The idea is if you have $\sum_{i,j} x^i y^j$ then a[i, j] = Coefficient[Coefficient[p, y, j], x, i]. In your case $y=e^{-x}$. $\endgroup$ – Sumit May 15 '15 at 16:45
  • $\begingroup$ Thanks @Sumit. This is what exactly I needed, and this saves my time :) $\endgroup$ – Frey May 15 '15 at 20:17
2
$\begingroup$

You can just use CoefficientList:

p = -1/2 Exp[-x] x^3 + 1/2 Exp[-2 x] x^2 + 1/2 Exp[-x] x^2 + 2 Exp[-2 x] x -
    2 Exp[-x] x + Exp[-2 x] - 2 Exp[-x] + 1;
CoefficientList[p, {Exp[-x], x}] //TeXForm

$\left( \begin{array}{cccc} 1 & 0 & 0 & 0 \\ -2 & -2 & \frac{1}{2} & -\frac{1}{2} \\ 1 & 2 & \frac{1}{2} & 0 \\ \end{array} \right)$

$\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.