Ask Your Question
1

In a system of 5 nonlinear equations with 5 unknowns using the solve command, Sage gives 5 lists of 5 solutions. Is it possible to print only one list with the 5 third solutions?

asked 2022-07-16 02:14:04 +0100

jafrancor gravatar image

updated 2022-07-16 16:39:53 +0100

Emmanuel Charpentier gravatar image

For example:

var('a,b,c,d,e,f,g,h,i,j,k,l,x,y,p,q,r,s,t,u,v,A,B,C,D,E,F,G,H,T,K,L,M,N')
solve([A - E, A*E - B, B*E - C, C*E - D - 10, D*E + 2], A,B,C,D,E, solution_dict=true)

[{A: 1.724306472919419,
  B: 2.973232669869595,
  C: 5.126764621146644,
  D: -1.159886628876292,
  E: 1.724306472919419},
 {A: -0.04961250951920438 - 1.781741795058672*I,
  B: -3.172142423158106 + 0.1767933635362254*I,
  C: 0.4723780710666982 + 5.643167572788117*I,
  D: 0.03123165940893102 - 1.121627457212148*I,
  E: -0.04961250951919667 - 1.781741795058679*I},
 {A: -0.04961250951920438 + 1.781741795058672*I,
  B: -3.172142423158106 - 0.1767933635362254*I,
  C: 0.4723780710666982 - 5.643167572788117*I,
  D: 0.03123165940893102 + 1.121627457212148*I,
  E: -0.04961250951919667 + 1.781741795058679*I},
 {A: -1.825113562621674,
  B: 3.331039229181005,
  C: -6.079524680073126,
  D: 1.095822281167109,
  E: -1.825113562621674},
 {A: 0.2000320307495195,
  B: 0.04001281142046625,
  C: 0.008003843719970937,
  D: -9.99839871897518,
  E: 0.2000320307495195}]

But I only need to print the list of the C solutions. Say:

[ C: 5.126764621146644,
  C: 0.4723780710666982 + 5.643167572788117*I,
  C: 0.4723780710666982 - 5.643167572788117*I,
  C: -6.079524680073126,
  C: 0.008003843719970937]

Or something like that.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2022-07-16 11:27:04 +0100

rburing gravatar image

Yes it's possible like this:

sage: var('a,b,c,d,e,f,g,h,i,j,k,l,x,y,p,q,r,s,t,u,v,A,B,C,D,E,F,G,H,T,K,L,M,N')
sage: sols = solve([A - E, A*E - B, B*E - C, C*E - D - 10, D*E + 2], A,B,C,D,E, solution_dict=true)
sage: [{C: sol[C]} for sol in sols]
[{C: 5.126764621146644},
 {C: 0.4723780710666982 + 5.643167572788117*I},
 {C: 0.4723780710666982 - 5.643167572788117*I},
 {C: -6.079524680073126},
 {C: 0.008003843719970937}]

Or as a plain list:

sage: [sol[C] for sol in sols]
[5.126764621146644,
 0.4723780710666982 + 5.643167572788117*I,
 0.4723780710666982 - 5.643167572788117*I,
 -6.079524680073126,
 0.008003843719970937]
edit flag offensive delete link more

Comments

Thanks a lot!!

jafrancor gravatar imagejafrancor ( 2022-07-16 20:03:12 +0100 )edit
1

answered 2022-07-16 11:28:51 +0100

tolga gravatar image

A quick suggestion without a dictionary would be the following:

var('a,b,c,d,e,f,g,h,i,j,k,l,x,y,p,q,r,s,t,u,v,A,B,C,D,E,F,G,H,T,K,L,M,N')
solutions=solve([A - E, A*E - B, B*E - C, C*E - D - 10, D*E + 2], A,B,C,D,E)
justC=[]
for sol in solutions:
    justC.append(sol[2]) #2 is for C
show(justC)
edit flag offensive delete link more

Comments

Thank you too!!

jafrancor gravatar imagejafrancor ( 2022-07-16 20:11:04 +0100 )edit
0

answered 2022-07-16 16:53:18 +0100

Emmanuel Charpentier gravatar image

updated 2022-07-16 16:54:16 +0100

A possibly cleaner solution (avoiding to define a truckload of unused symbolic variables and needlessly scratch predefined Sage identifiers such as e, I and i...), expressed as a two-liner :

sage: R1.<A,B,C,D,E>=QQbar[]
sage: [u[C] for u in R1.ideal(A - E, A*E - B, B*E - C, C*E - D - 10, D*E + 2).variety()]
[-6.079524581813865?,
 0.00800384369086252?,
 5.126764595989604?,
 0.4723780710666998? + 5.643167572788120?*I,
 0.4723780710666998? - 5.643167572788120?*I]

Of course, it would be preferable to explicitly compute the idealJ1=R1.ideal(A - E, A*E - B, B*E - C, C*E - D - 10, D*E + 2) and check that its .dimension() is indeed 0 vbefore asking for its .variety()...

edit flag offensive delete link more

Comments

I don't understand what implies and what is the meaning of checking that the result of..dimension() is indeed 0 before asking for its .variety()

jafrancor gravatar imagejafrancor ( 2022-07-16 20:20:29 +0100 )edit

The .variety() method of ideals returns someting only if its .dimension() is 0 ; in other cases, you have to search the relevant .elimination_ideal().

RTFM...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-07-29 00:19:12 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-07-16 02:14:04 +0100

Seen: 255 times

Last updated: Jul 16 '22