Is there a reason it doesn't need to be polynomial in x?

I think you can remove this if block from the for loop and just check if pden is a polynomial.