Solving symbolic polynomials in terms of alternate variables (+ bug?)
I've been struggling for a few days to convert get sage making inferences like the following, where I convert a (multivariate polynomial) function in terms of one set of variables (roots) into a function in terms of another (coefficients). For my use cases the conversions are always "clean", producing simpler formulas than the original:
F == p^2+pq+q^2
a == -p-q
b == pq
>> F == a^2+b (which expands to p^2-2pq+q^2 + pq)
I'm hoping for a general solution which will work with considerably more complicated root formulas. The required math is simple algebra, so I shouldn't need anything so extravagant as solving for p and q in terms of a and b (which becomes prohibitively complex).
After some other failed experiments, I'm currently trying to use the solve() function to navigate the problem. This hasn't gone so well. My approach is to reformulate the problem in terms of an equation multiplying unknown variables by combinations of A and B which I know might show up in the particular permutation of the equation.
var("k j a x", domain='real')
#Solver can't handle these pseudo-overdetermined systems, and returns nothing
show(solve([a==-x, k*a^2==x^2], k)) # Expected k == 1
show(solve([a==-x, k*a ==x ], k)) # Expected k == -1
show(solve([a== x, k ==x ], k)) # Expected k == x
#If we substitute the known value of a, it works...
show(solve([k*(-x)^2==x^2], k)) # Yields k == 1
#...But not if solving for multiple variables --- is this a bug?
#show(solve([k*(-x)^2+j*(-x)==x^2-x], [k, j]))
#>> AttributeError: 'list' object has no attribute 'lhs'
#We can work around the bug by solving one variable at a time
show(solve([k*(-x)^2+j*(-x)==x^2-x], j)) #Yields j=(k-1)x+1 ... scalar part 1 is correct
show(solve([k*(-x)^2+j*(-x)==x^2-x], k)) #Yields (j+x-1)/x ... scalar part 1 is correct
With my "cheat" I may be able to repeatedly solve the formula and get this just barely working... But this requires some difficult equation mangling and promises to be super inefficient. All of this has me thinking that I'm using the wrong approach to solve this relatively simple algebraic problem. Help?
I am not an expert on this part of Sage, but just as a comment, note that you don't have the bug if you don't define your variables as real:
I have the same result as yours for your first three examples.