I need your help to make my code faster.
I have a formula for computing a value from any monomial $x^i y^j$, say
formular1[i_,j_]:=(Gamma[i]/Gamm[j] ). It is just an example, anyway.
My task is to extend this function to all polynomials
f[x,y], as fast as possible.
One approach is to use
CoefficientList to get a matrix of the coefficients of
f[x,y]. Next, using
Table[Table[...]...] to construct a matrix whose entries is the values of
formular1[i,j]; Then multiply (entry by entry, not matrix multiplication) these 2 matrices and find the total of its entries.
This approach is slow when my polynomial is of high degree but sparse.
I once tried another approach:
MonomialValue[c_ x^(i_) y^(j_)]:= c * formular1[i,j] MonomialValue[x^(i_) y^(j_)]:= 1 * formular1[i,j] MonomialValue[c_ x^(i_)]:= c * formular1[i,0] MonomialValue[x^(i_)]:= 1 * formular1[i,0] .... ....
PolynomialValue[f] := Total[MonomialValue /@ MonomialList[ f]]
This will avoid the unnecessary computing of
formula[i,j] when the coefficient of
f[x,y] is zero, also avoid the unnecessary entries in a huge matrix of coefficient.
The only disadvantage of this second approach is that I have to define
MonomialValue for all kind of them. I can't not just define
MonomialValue[c_ x^(i_) y^(j_)]. If I did so, then
MonomialValue[x^2 y^3] (with coefficient 1) would not be evaluated. In order to make this works for all , I have to defined a total of 16 function.
I need this for just polynomials of 2 variables. But a generalization to several variables is appreciated.