Prove that a Number is Algebraic
SageMath, 108 bytes
def f(s):p=map('{:+} '.format,numerator(minpoly(sage_eval(s)/1)));return'*'+p[-1][1:]+'*x '.join(p[-2::-1])
Try it on SageMathCell.
Explanation:
Evaluate the string symbolically as an algebraic number (sage_eval()
). Every algebraic number is a zero of some polynomial a[0] + a[1] x^1 + a[2] x^2 + ⋯ + a[n] x^n with rational coefficients a[0], …, a[n] (minpoly()
). Multiply all the coefficients by their common denominator to turn them into integers (numerator()
), then write this polynomial in the desired output format,
*a[n] +a[n-1] *x +a[n-2] *x … *x +a[1] *x +a[0]
SageMath, 102 bytes, almost
lambda s:(lambda a,*p:'*%d'%a+'*x'.join(map(' {:+} '.format,p)))(*numerator(minpoly(1/sage_eval(s))))
This works for all inputs except 0, because a polynomial for 1/α is a polynomial for α with the coefficients reversed. :-(