0
eng3 = [solve(eng==(k-1)*md*Vm^2/(2*Patm*Vc*(Pc-Pc^(1/k))),Vc,solution_dict=1) for Pc in  xrange(2,10)]

I'm trying to automate the process of some efficiency graphs in sage mathematics, and I've got this formula. Matplotlib.pyplot wants a list of values. I either get a dictionary in a list as described below, or a list in the form of:

List:
[[Vc == (5231100000/35166511904977)], [Vc == (111907000000/1692075843832947)], [Vc == (364112000000/8911538005781703)], [Vc == (753818000000/25992803547821049)], [Vc == (138358000000/6222694018119437)], [Vc == (659020000/36808310575981)], [Vc == (25902500000/1736640592770261)], [Vc == (59144000000/4642920082445667)]]

Dictionary:    
[[{Vc: 1743700000/105499535714931}], [{Vc: 319775000/43516062236099}], [{Vc:  34670000000/7636845816518169}], [{Vc: 41083500000/12749600170083701}], [{Vc: 1703720000/689628024927753}], [{Vc: 25247000000/12691109152994019}], [{Vc: 44767000000/27012747987604137}], [{Vc: 5993300000/4234372317920577}]]

I want to take those fractions, convert them in to their numerical approximations, and create a list with those values in sage math[python]. The n() function will convert fractions to decimals, but I'm not sure how to isolate the values to feed it to n().

Sicco
  • 6,167
  • 5
  • 45
  • 61
cii
  • 155
  • 2
  • 12

1 Answers1

1

The list comes from solve, but the question is really a pure Python one. To get each of these, if the list is called Vlist, you could do

[v[0].rhs().n() for v in Vlist]

and for the dictionary the somewhat more cumbersome

[v[0].values()[0].n() for v in Vlist]

This construction is known as a "list comprehension". These assume that you really have single solutions to each! If the format is different (e.g., if you get something like Vc == 2*Vc^2) then you'd have to be more clever.

Background - apparently this ask.sagemath.org question is related.

kcrisman
  • 4,374
  • 20
  • 41
  • I posted that other one on sagemath. Ashamed to say I forgot about the difference between = and ==. I'm kinda new to python, but not to math. The formulas relate to optimum efficiency values for a pneumatic air cannon. It'll always give fractions in my current arrangement. It's a side engineering project I've been working on. Thanks again – cii Aug 29 '12 at 17:52