I posted a message about a month ago regarding an alternative function for finding coefficients. Someone was nice enough to reply to my first message, but it didn't quite solve my problem. So this is a ''new'' message relating to an old message.
The built in function Coefficient[exp,x] does not work for all cases that I would like it to work for. I would like to create a function coef[exp,x] that would, for example, give me these results:
coef[x^2,x] = x
coef[x^3*Sin[x]*y,x] = x^2*Sin[x]*y
coef[x^3,x^4] = 0
coef[2*x^4,x^2] = 2*x^2
The built in function returns 0 in all of these cases. I believe I know how the built in function Coefficient[exp,x] works - it looks for the head Times, and checks to see if the symbol, x, matches the symbol in expression, exp, and if they are the same returns what ever is left over. Ex:
FullForm[3*x] = Times[3,x]
Coefficient[3*x,x] = 3, because the head is Times and x = x, therefore it returns 3.
Coefficient[3*x,y] = 0, because x does not equal y, and it returns 0.
FullForm[x^2] = Power[x,2], and Coefficient[x^2,x] = 0, because there is no head of Times.
I was wondering if anyone knows an easy way of doing this or has already done this. I have started to write a function that does this, but it is very complicated, and I know there has got to be an easier way to do this.