如何在SymPy中访问obained线性方程组解?

时间:2016-10-31 19:16:44

标签: python sympy

我使用sympy解决了一些线性方程组:

t=linsolve([f(0.25), f(0.5)], (a1,a2)) #f is function of x, a1, a2

Output: t={(-0.145903479236813, -0.152637485970819)}

如何从t?

访问a1和a2的值

2 个答案:

答案 0 :(得分:2)

tset FiniteSet tuple tuple} for a1, a2 in t: print(a1, a2) function calc() { //sets all the variables to blank gpa109 = "" gpa115 = "" note = "" //The value of the GPA radio button input is set in the GPA variable (first button=1, etc...) GPA = document.data.GPA.value //The value of the ACT radio button input is set in the ACT variable (first button=1, etc...) ACT = document.data.ACT.value //Use if-then statements to assign gpa output values to variables based on GPA and ACT inputs //gpa output variables are gpa109, gpa115, gpa109120, etc... //the note in the text box at the end is in variable "note" if (GPA == 1) { if (ACT == 1) { gpa109 = "0.91" } } if (GPA == 1) { if (ACT == 1) { gpa115 = "No Data" } } if (GPA == 1) { if (ACT == 2) { gpa109 = "1.50" } } if (GPA == 1) { if (ACT == 2) { gpa115 = "No Data" } } if (GPA == 2) { if (ACT == 1) { gpa109 = "1.68" } } if (GPA == 2) { if (ACT == 1) { gpa115 = "No Data" } } if (GPA == 2) { if (ACT == 2) { gpa109 = "1.98" } } if (GPA == 2) { if (ACT == 2) { gpa115 = "1.27" } } if (GPA == 1) { if (ACT == 1) { note = "we are worried about you." } } if (GPA == 1) { if (ACT == 2) { note = "slacked off a bit in high school, did you not?" } } if (GPA == 2) { if (ACT == 1) { note = "you are a classic bad standardized test taker." } } if (GPA == 2) { if (ACT == 2) { note = "you should probably apply to a better college." } } //These statements put all the values determined by the if-then statements into the document document.data.gpa109.value = gpa109 document.data.gpa115.value = gpa115 document.data.note.value = note }解决方案<form name="data"> <table COLS=4 cellpadding=2 border=1 align=center> <tr> <td COLSPAN="4"> <center><b><font size=+2> <p> Advising Tool </p></font></b> </center> </td> </tr> <tr> <td COLSPAN="2"> <center><font size=+1> <p> What was your High School GPA? </p> </font> </center> </td> <td COLSPAN="2"> <center><font size=+1> <p> What was your ACT Math subscore? <p> </font> </center> </td> </tr> <tr> <td COLSPAN="2"> <center> <font size=+1> <P> <input type="radio" name="GPA" value="1" onclick="calc();"> Less than 3.5<br> <input type="radio" name="GPA" value="2" onclick="calc();"> 3.5 or greater<br> </P></font> </center> </td> <td COLSPAN="2"> <center><font size=+1> <P> <input type="radio" name="ACT" value="1" onclick="calc();"> Less than 26<br> <input type="radio" name="ACT" value="2" onclick="calc();"> 26 or greater<br> </P> </font> </center> </td> </tr> <tr> <td COLSPAN="4" align="center"> <font size=+1> <br> Over the past 5 years, students with similar high school GPAs and ACT Math<br>scores had the following average GPA in their introductory CHM courses. <br> <br> </font> </td> </tr> <tr> <td align="right"> Classes Taken </td> <td> Average GPA </td> </tr> <tr> <td align="right"> CHM 109 Only </td> <td> <input name="gpa109" size="10" type="text"> </td> <tr> <td align="right"> CHM 115 Only </td> <td> <input type="text" name="gpa115" size="10"> </td> <tr> <td COLSPAN="4" align="center"> <textarea rows="2" cols="100" name="note"> </textarea> </td> </tr> </table> </form> ,所以你需要迭代它的解决方案。类似的东西:

@ManagedBean Student()

答案 1 :(得分:0)

您可以通过将其转换为 tuplelist 来访问解决方案集的元素。

t=list(t)[0]
t[0]:-0.145903479236813
t[1]:-0.152637485970819