Skip to main content
Tweeted twitter.com/#!/StackMma/status/290589688174112768
Added tag [functional-style]; removed "Thanks ..."
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial.

For example, if I have

a = {1,2,3}
b = {2,4,6}
c = {3,2,1}

I would like to get

poly = xy^2z^3 + x^2y^4z^6 + x^3y^2z

Unfortunately, I am not too familiar with the functional programming style Mathematica is intended to be used for, and I can only write something like

For[i = 1, i < Length[a] + 1, i++,
 term = x^Part[a, i]*y^Part[b, i]*z^Part[c, i];
poly = poly + term;]

As you may expect, this is taking quite a long time to evaluate. Would anybody have any thoughts as to how I would rewrite this to make it more efficient? Thanks in advance.

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial.

For example, if I have

a = {1,2,3}
b = {2,4,6}
c = {3,2,1}

I would like to get

poly = xy^2z^3 + x^2y^4z^6 + x^3y^2z

Unfortunately, I am not too familiar with the functional programming style Mathematica is intended to be used for, and I can only write something like

For[i = 1, i < Length[a] + 1, i++,
 term = x^Part[a, i]*y^Part[b, i]*z^Part[c, i];
poly = poly + term;]

As you may expect, this is taking quite a long time to evaluate. Would anybody have any thoughts as to how I would rewrite this to make it more efficient? Thanks in advance.

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial.

For example, if I have

a = {1,2,3}
b = {2,4,6}
c = {3,2,1}

I would like to get

poly = xy^2z^3 + x^2y^4z^6 + x^3y^2z

Unfortunately, I am not too familiar with the functional programming style Mathematica is intended to be used for, and I can only write something like

For[i = 1, i < Length[a] + 1, i++,
 term = x^Part[a, i]*y^Part[b, i]*z^Part[c, i];
poly = poly + term;]

As you may expect, this is taking quite a long time to evaluate. Would anybody have any thoughts as to how I would rewrite this to make it more efficient?

Source Link

How to write this without For loop

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial.

For example, if I have

a = {1,2,3}
b = {2,4,6}
c = {3,2,1}

I would like to get

poly = xy^2z^3 + x^2y^4z^6 + x^3y^2z

Unfortunately, I am not too familiar with the functional programming style Mathematica is intended to be used for, and I can only write something like

For[i = 1, i < Length[a] + 1, i++,
 term = x^Part[a, i]*y^Part[b, i]*z^Part[c, i];
poly = poly + term;]

As you may expect, this is taking quite a long time to evaluate. Would anybody have any thoughts as to how I would rewrite this to make it more efficient? Thanks in advance.