#! /usr/bin/env python
"""Perform some linear algebra and vector operations

Date: November 14, 2008

Author: Bob Wimmer

"""



def main():
    #To perform inversions, etc., it is more efficient to convert lil to another format:
    # lil: list of lists
    # csc: compressedsparse columns
    # csr: compressed sparse rows

    from time import time
    from numpy import allclose, arange, eye, linalg, random, ones
    from scipy import linsolve, sparse
    
    Asp = sparse.lil_matrix((50000,50000))
    Asp.setdiag(ones(50000))
    #add some random numbers to make this a little harder to solve
    Asp[20,100:250] = 10*random.rand(150)
    Asp[200:250,30] = 10*random.rand(50)
    Asp[2000:2500,30] = 10*random.rand(250)
    b = arange(0,50000)
    t=time(); xsp1 = linsolve.spsolve(Asp,b); t1 = time()-t; print t1
    t=time(); xsp2 = linsolve.spsolve(Asp.tocsc(),b); t2 = time()-t; print t2
    t=time(); xsp3 = linsolve.spsolve(Asp.tocsr(),b); t3 = time()-t; print t3
    print "ratios of times:"
    print "t(lil) / t(csr) = " + str(t1/t3)
    print "t(csc) / t(csr) = " + str(t2/t3)
    print "and if calculations were ok, the we should see two True statements"
    print allclose(xsp1,xsp2) # Should be True
    print allclose(xsp2,xsp3) # Should be True

    



main()



