Compute elementary symmetric functions via MATLAB

218 Views Asked by At

For any given numbers $\{x_1,...,x_n\}$, the k-th elementary symmetric function is $$ E_k=\sum_{1\le i_1<...<i_k\le n} x_{i_1}x_{i_2}...x_{i_k},~~~~~~~k=1,...,n. $$

Given a specified n-tuple of numbers, how do you compute $E_k$ via Matlab?

I wrote the following code. I am not sure whether this can be improved (from computational cost/time point of view).


x=rand(1,n) % randomly generate a vector of n entries 

syms t;

p=expand(prod(t+x)); % p is a monic polynomial

c=sym2poly(p); % coefficient vector 

One finds that $E_k=c(k+1)$, $k=1, \ldots, n$.

Thank you.

1

There are 1 best solutions below

3
On BEST ANSWER

Here is an alternative.

Do you know that the "nchoosek" function used for having plain binomial coefficients nchoosek(n,k)$ \ = \ \binom{n}{k}$ possesses an extension where the first parameter instead of being a number $n$ is a set with $n$ elements giving the following alternative to your program using only numerical functions (see explanation below):

n=4;
x=rand(1,n);
for k=1:n
   T(k)=sum(prod(nchoosek(x,k),2));
end;
T

Explanation on an example:

nchoosek([a b c],k) gives the different sets with $k=2$ elements chosen among the set $\{a,b,c\}$ under the form :

$$\begin{matrix}a&b\\a&c\\b&c\end{matrix}$$

Therefore, in order to get the elementary symmetric polynomial: $ab+ac+bc$, the program upwards

  • in a first step (function "prod"), produces products $ab, \ \ ac, \ \ bc$ of entries line by line because we have indicated that we want the product done this way : this is why the second parameter $2$ has been added. Otherwise, "prod(x)" would have produced $a^2b \ \ \ bc^2$ which is not what we want... Explanation: by default, with Matlab, the application of an operator (such as multiplication, max, etc.) to a 2 dimensional array is vertical.

  • in a second step (function "sum"), one sums $ab, \ \ ac, \ \ bc$ to obtain $ab+ac+bc$. This time, no added parameter because we work vertically...