William Stein wrote:
> On Thu, Jun 18, 2009 at 8:31 PM, Ethan Van Andel<[email protected]> wrote:
>> Does sage have a way to use a numeric method (such as Jacobi, or Gauss-
>> Sidel) to solve matrix equations of the form A*x = b? (CDF matrices by
>> the way). the A.solve_right(b) method is too slow.
>>
>> Thanks,
>> Ethan
> 
> Here is an example of how to convert back/forth to numpy to solve A*x
> = b very quickly.  Below we do this with A a 1000x1000 matrix, and it
> takes about a half second.   It would be *great* if somebody made it
> so this would work in Sage automatically without having to explicitly
> use numpy like I've done before.  But for you, I bet this is a minor
> inconvenience.
> 
> sage: n = 1000
> sage: a = random_matrix(CDF,n); v = random_matrix(CDF,n,1)
> sage: aa = a.numpy(); vv = v.numpy()
> sage: import numpy
> sage: time ww = numpy.linalg.solve(aa, vv)
> Time: CPU 0.57 s, Wall: 0.41 s
> sage: w = matrix(CDF, ww)
> sage: max([abs(z) for z in a*w - v])
> 5.46740430707e-12 + 8.431033649e-13*I


Gosh, this is embarrassing...

I just looked at the code for the CDF matrices.  The *solve_left* 
function uses almost the exact same code as you did, William, and takes 
the same amount of time.  Now why is it solve_left?  I have no idea (but 
it's no doubt my fault!).  solve_right, of course, takes a lot more time 
because it copies the entire matrix to transpose it before solving the 
system (but then it solves the wrong system, since solve_left should be 
solve_right!)

I guess one thing is the saving grace---a CDF matrix's solve_left 
function is documented as solving the system Ax=b.

So, right now things are messed up.  I'll detail the fix on the ticket 
(and maybe even just post a patch).

To answer the original poster, here is William's session and comparable 
code (that is just as fast!):

Williams session on my computer:

sage: n=1000
sage: a=random_matrix(CDF,n); v=random_matrix(CDF,1,n)[0]
sage: aa=a.numpy(); vv=v.numpy()
sage: import numpy
sage: time www=numpy.linalg.solve(aa,vv)
CPU times: user 1.42 s, sys: 0.08 s, total: 1.50 s
Wall time: 2.89 s
sage: w=vector(CDF,www)
sage: max(abs(z) for z in a*w-v)
1.95733512586e-12

Using the current (wrongly-named!) functions:

sage: time w=a.solve_left(v)
CPU times: user 2.08 s, sys: 0.22 s, total: 2.30 s
Wall time: 7.59 s
sage: time w=a.solve_left(v)
CPU times: user 1.54 s, sys: 0.07 s, total: 1.61 s
Wall time: 2.32 s
sage: max(abs(z) for z in a*w-v)
2.67581211819e-12


Jason

P.S. The solve_left_lu function is still probably messed up too...


> 
> > 
> 


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to