Minh Nguyen wrote: > Hi Mani, > > On Thu, Aug 13, 2009 at 6:08 PM, Mani chandra<mchan...@iitk.ac.in> wrote: > >> Hi, >> >> I have a set of variables which I defined in an array and I'm trying >> to use "solve" to solve for these variables, but I get the following error: >> >> x = [] >> N = 3 >> for i in range(N): >> string = 'x' + str(i) >> temp_var = var(string) >> x.append(temp_var) >> Eqn = [] >> Eqn.append(x[0] + x[1] + x[2]) >> Eqn.append(x[0] - x[1] + x[2]) >> >> solve([Eqn[1]==0], x[1], solution_dict=True) >> >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> File "/home/mc/.sage/sage_notebook/worksheets/admin/7/code/47.py", line 17, >> in <module> >> solve([Eqn[_sage_const_1 ]==_sage_const_0 ], x[_sage_const_1 ], >> solution_dict=True) >> File "", line 1, in <module> >> >> File >> "/opt/sage/sage/local/lib/python2.6/site-packages/sage/symbolic/relation.py", >> line 509, in solve >> sol_dict=[dict([[eq.left(),eq.right()] for eq in solution]) for solution >> in sol_list] >> >> >> TypeError: 'sage.symbolic.expression.Expression' object is not iterable >> >> >> However the following seems to work: >> >> solve([Eqn[1]==0], x[1]) >> >> >> [x1 == x0 + x2] >> >> The reason I need solution_dict=True is that I need to put the solution >> in x[1], which solve does not do. Could someone please help me out? >> > > Is the following what you're trying to do? > > ---------------------------------------------------------------------- > | Sage Version 4.1, Release Date: 2009-07-09 | > | Type notebook() for the GUI, and license() for information. | > ---------------------------------------------------------------------- > sage: x = [] > sage: N = 3 > sage: for i in xrange(N): > ....: string = "x" + str(i) > ....: temp_var = var(string) > ....: x.append(temp_var) > ....: > sage: Eqn = [] > sage: Eqn.append(x[0] + x[1] + x[2]) > sage: Eqn.append(x[0] - x[1] + x[2]) > sage: solve(Eqn[1] == 0, x[1], solution_dict=True) > [{x1: x0 + x2}] > > You only had one equation to solve and you wanted to get the solution > dictionary for the solution of that one equation. In that case, you > don't need to put that equation in a list. So instead of > > [Eqn[1] == 0] > > I changed it to > > Eqn[1] == 0 > > If you have more than one equation to solve, then you can put those > equations in a list. The solve() command would then interpret the list > of equations as a system of simultaneous equations. For example: > > sage: solve([Eqn[1] == 0, Eqn[0] == 0], [x[1], x[0]], solution_dict=True) > [{x1: 0, x0: -x2}] > > Thank you for that, solved my problem!
Mani chandra --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---