How to program in Sagemath using the first root of an equation that needs to be entered in the coefficients of another equation to solve it

How to program in Sagemath using the first root of an equation that needs to be entered in the coefficients of another equation to solve it. Then, in a second iteration use the second root in the same way as the previous one, then in a third iteration use the third root in the same way as the previous roots, and so on, until all the roots of the first equation are used in the coefficients of the second equation. For example, the equation: -(4f^2 q + 5f^2 r - 4q^2 + 5fr - 3qr) = 0 has two roots, f_1 and f_2, where q and r are real numbers. and I want to solve the following another equation: -1/5(20f^2 hq + 20f^2 iq + 40fgq + 40fhq - 8fq^2 - 20hq^2 - 20iq^2 + 50fgr + 100fhr + 50fir - 4q^2 + 25gr + 25hr - 50qr - 25r^2) = 0 which is linearly dependent on variables g, h and i. But, it will give us one root g_1(h, i) if we enter f_1 in this 2nd equation, or g_2(h, i) if we enter f_2 in it. And although the process continues with other equations, let's leave it here for now.

edit retag close merge delete

Can you give a concrete example using math notation please? In code I guess you'll want a tower of (number) field extensions.

( 2021-12-19 17:53:49 +0200 )edit
1

Thanks, I hope this addition helps.

( 2021-12-20 16:14:23 +0200 )edit

Sort by » oldest newest most voted

Alternative :let's be lazy and let Sage solve it for you. Let :

var('f, q, r, h, i, g')
e1 = -(4*f^2*q + 5*f^2*r - 4*q^2 + 5*f*r - 3*q*r) == 0
e2 = -1/5*(20*f^2*h*q + 20*f^2*i*q + 40*f*g*q + 40*f*h*q - 8*f*q^2 -
20*h*q^2 - 20*i*q^2 + 50*f*g*r + 100*f*h*r + 50*f*i*r - 4*q^2 +
25*g*r + 25*h*r - 50*q*r - 25*r^2) == 0


Then :

sage: solve([e1, e2], [f, g], solution_dict=True)
[{f: -1/2*(5*r + sqrt(64*q^3 + 128*q^2*r + 5*(12*q + 5)*r^2))/(4*q + 5*r),
g: -1/5*(5120*h*q^5 - 1024*q^6 + 625*(12*(2*h + i)*q + 10*h + 5*i)*r^4 + 50*(10*(94*h + 38*i - 1)*q^2 - 24*q^3 + 25*(5*h + i)*q)*r^3 + 80*(5*(132*h + 36*i - 1)*q^3 - 44*q^4 + 25*h*q^2)*r^2 + 128*(5*(41*h + 5*i)*q^4 - 26*q^5)*r + (80*(2*h + 2*i + 11)*q^3*r + 64*q^4 + 125*(5*h + 5*i + 18*q)*r^3 + 625*r^4 + 50*(4*(h + i + 12)*q^2 + 5*(h + i)*q)*r^2)*sqrt(64*q^3 + 128*q^2*r + 5*(12*q + 5)*r^2))/(1024*q^5 + 4608*q^4*r + 125*(12*q + 5)*r^4 + 200*(28*q^2 + 5*q)*r^3 + 80*(96*q^3 + 5*q^2)*r^2)},
{f: -1/2*(5*r - sqrt(64*q^3 + 128*q^2*r + 5*(12*q + 5)*r^2))/(4*q + 5*r),
g: -1/5*(5120*h*q^5 - 1024*q^6 + 625*(12*(2*h + i)*q + 10*h + 5*i)*r^4 + 50*(10*(94*h + 38*i - 1)*q^2 - 24*q^3 + 25*(5*h + i)*q)*r^3 + 80*(5*(132*h + 36*i - 1)*q^3 - 44*q^4 + 25*h*q^2)*r^2 + 128*(5*(41*h + 5*i)*q^4 - 26*q^5)*r - (80*(2*h + 2*i + 11)*q^3*r + 64*q^4 + 125*(5*h + 5*i + 18*q)*r^3 + 625*r^4 + 50*(4*(h + i + 12)*q^2 + 5*(h + i)*q)*r^2)*sqrt(64*q^3 + 128*q^2*r + 5*(12*q + 5)*r^2))/(1024*q^5 + 4608*q^4*r + 125*(12*q + 5)*r^4 + 200*(28*q^2 + 5*q)*r^3 + 80*(96*q^3 + 5*q^2)*r^2)}]

more

I'm not completely sure what you ask for, but here is my best guess:

f,q,r,h,i,g = var('f q r h i g')
for t in solve( 4 * f^2* q + 5 * f^2 * r - 4 * q^2 + 5*f*r - 3*q*r == 0, f ):
eq = (-1/5*(20*f^2*h*q + 20*f^2*i*q + 40*f*g*q + 40*f*h*q - 8*f*q^2 - 20*h*q^2 - 20*i*q^2 + 50*f*g*r + 100*f*h*r + 50*f*i*r - 4*q^2 + 25*g*r + 25*h*r - 50*q*r - 25*r^2)).subs({f:t.rhs()})
print( solve(eq==0, g) )


more

1

To expand a bit on Max's answer, you might be looking at :

var('f, q, r, h, i, g')
e1 = -(4*f^2*q + 5*f^2*r - 4*q^2 + 5*f*r - 3*q*r) == 0
e2 = -1/5*(20*f^2*h*q + 20*f^2*i*q + 40*f*g*q + 40*f*h*q - 8*f*q^2 -
20*h*q^2 - 20*i*q^2 + 50*f*g*r + 100*f*h*r + 50*f*i*r - 4*q^2 +
25*g*r + 25*h*r - 50*q*r - 25*r^2) == 0
Sol = flatten([[u|v
for v in e2.subs(u).solve(g, algorithm="sympy",
solution_dict=True)]
for u in e1.solve(f, algorithm="sympy", solution_dict=True)])

( 2021-12-21 10:46:26 +0200 )edit

For now Thanks a lot. I think that I will use both. I'll try first that of Max and secondly that of Emmanuel.

( 2021-12-21 22:51:46 +0200 )edit

How can I enter the previous obtained value of f in the e3 to know the h?

f,q,r,h,i,g = var('f q r h i g')
e1 = 4 * f^2* q + 5 * f^2 * r - 4 * q^2 + 5*f*r - 3*q*r
e2 = -1/5*(20*f^2*h*q + 20*f^2*i*q + 40*f*g*q + 40*f*h*q - 8*f*q^2 - 20*h*q^2 - 20*i*q^2 + 50*f*g*r + 100*f*h*r + 50*f*i*r - 4*q^2 + 25*g*r + 25*h*r - 50*q*r - 25*r^2)
e3 = - 1/5*(40*f*g*h*q + 40*f*h^2*q +  40*f*g*i*q + 40*f*h*i*q + 25*f*h^2*r + 50*f*h*i*r + 25*f*i^2*r + 20*g^2*q + 40*g*h*q + 20*h^2*q - 8*g*q^2  - 16*h*q^2  - 8*i*q^2  + 25*g^2*r + 100*g*h*r + 75*h^2*r + 50*g*i*r + 50*h*i*r - 5*f*q*r - 50*h*q*r - 50*i*q*r - 25*r^2)

for u in solve( e2 == 0, g ):
eq3 = (e3).subs({g:u.rhs()})

print( solve(eq3==0, h) )


Similarly, in a fourth equation e4, how can I enter the previous obtained values of f and g in order to know i?

more

This question (in my answer) is also valid for Emmanuel

( 2021-12-24 02:14:35 +0200 )edit

Use eq3 = e3.subs( { f:t.rsh(), g:u.rhs() } ) or alike.

( 2021-12-24 04:11:42 +0200 )edit

The extended problem can be solved as follows :

sage: S1 = flatten(e1.solve(f, solution_dict=True))
sage: S12=flatten([[u|v for v in e2.subs(u).solve(g, solution_dict=True)] for u in S1])
sage: S123=flatten([[u|v for v in e3.subs(u).solve(i, solution_dict=True)] for u in S12])


The scheme should be pretty clear by now...

Look up the subs documentation. Using solution_dict=True} hejps getting clearer (= easier to read) solutions.

Unfortunately, SS=solve([e1, e2, e3], [f, g, i], solution_dict=True), which is a general solution, is extremely slow (> 30 m). The stepwise substitution you use limits the space of solutions Sage has to explore...

( 2021-12-24 12:32:13 +0200 )edit

Yes, unfortunately the general solution requires too much memory. I have been using letters substituting long constant expressions in order to avoid slowing Sage. I will study and try the use of the command subs as you indicated. Thanks a lot you all for your quick responses..

( 2021-12-24 14:06:54 +0200 )edit

Depending on your goal, it may be worth to work directly with the ideal generated by your polynomial equations rather than to compute their zeros.

( 2021-12-24 17:19:33 +0200 )edit