William Stein wrote: > On Thu, Mar 26, 2009 at 8:00 AM, Drini <[email protected]> wrote: >> >> >> Jason Grout wrote: >> >>> Here it is using linear algebra: >>> >>> sage: var('a,b,c,d,x,y') >>> (a, b, c, d, x, y) >>> sage: A=matrix(2,[a,b,c,d]); A >>> [a b] >>> [c d] >>> sage: result=vector([3,5]); result >>> (3, 5) >>> sage: soln=A.solve_right(result) # you could also do soln=A\result >>> sage: soln >>> (3/a - b*(5 - 3*c/a)/(a*(d - b*c/a)), (5 - 3*c/a)/(d - b*c/a)) >>> >>> >>> Now, checking our answers: >>> >>> >>> sage: (a*x+b*y).subs(x=soln[0],y=soln[1]).simplify_full() >>> 3 >>> sage: (c*x+d*y).subs(x=soln[0],y=soln[1]).simplify_full() >>> 5 >>> >>> >>> Or just checking it with matrix multiplication: >>> >>> sage: A*soln >>> (a*(3/a - b*(5 - 3*c/a)/(a*(d - b*c/a))) + b*(5 - 3*c/a)/(d - b*c/a), >>> c*(3/a - b*(5 - 3*c/a)/(a*(d - b*c/a))) + (5 - 3*c/a)*d/(d - b*c/a)) >>> >>> Let's simplify each entry by applying the "simplify_full" function to >>> each entry: >>> >>> sage: (A*soln).apply_map(lambda x: x.simplify_full()) >>> (3, 5) >>> >>> >>> This example probably ought to go into some documentation somewhere... >>> >>> Jason >> Well :) it was almost too nice to be true: >> >> n=var('n') >> T=vector([3,n]) >> A=matrix([[6,2],[7,1]]) >> A.solve_right(T) >> >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> File "/home/drini/.sage/sage_notebook/worksheets/admin/6/code/ >> 34.py", line 6, in <module> >> (......) >> TypeError: unable to convert n to a rational >> >> Likewise with A\T >> >> it's kinda strange that such elemental system of equations can't be >> done symbollically by matrix way: >> 6x + 2y = 3 >> 7x + y = n >> >> of course, changing n for some number works. > > Do this: > > sage: n=var('n') > sage: T=vector([3,n]) > sage: A=matrix(SR,[[6,2],[7,1]]) > sage: A.solve_right(T) > ((n - 7/2)/4 + 1/2, -3*(n - 7/2)/4) >
(as an explanation) matrix(SR, ...) makes the matrix a "symbolic" matrix, rather than just an "integer" matrix. As it was, the strictly integer matrix A doesn't know how to deal with the symbolic vector T. Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
