In [62]:
import os, sys
lib_path = os.path.abspath('../')
sys.path.append(lib_path)
from interfaces import *
from utilities import *
import numpy as np
#This command is for a prettier print of numpy arrays
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})

from scipy.sparse import *
import time
import matplotlib.pyplot as plt

In [63]:
#Vector spaceya
vs=IteratedVectorSpace(UniformLagrangeVectorSpace(3), np.linspace(0,1,5))
vs.print_info()


============================================================
Name: IteratedVectorSpace
N dofs: 9, N cells: 4, 
Cell boundaries: [ 0.000  0.250  0.500  0.750  1.000]
Shared dofs on cell boundaries: 1
------------------------------------------------------------
Cell 0: [0.0,0.25]
Nonzero basis: [0, 1, 2]
------------------------------------------------------------
Cell 1: [0.25,0.5]
Nonzero basis: [2, 3, 4]
------------------------------------------------------------
Cell 2: [0.5,0.75]
Nonzero basis: [4, 5, 6]
------------------------------------------------------------
Cell 3: [0.75,1.0]
Nonzero basis: [6, 7, 8]
------------------------------------------------------------

In [64]:
cells=vs.n_cells
cells


Out[64]:
4

In [65]:
nbasis=vs.n_dofs
nbasis


Out[65]:
9

In [66]:
#test using sparce functions.

cum=dok_matrix((nbasis,nbasis))
(qst,wst)=np.polynomial.legendre.leggauss(5)      
for lcell in range(vs.n_cells):
    a,b=vs.cells[lcell],vs.cells[lcell+1]
    q=((a+b)+(b-a)*qst)/2
    w=(b-a)*wst/2
    fnts=vs.cell_span(lcell)
    nfnts=len(fnts)
    fq=np.zeros((nfnts,5))
    it=range(nfnts)
    for i in it:
        fq[i]=vs.basis(fnts[i])(q)
    out=np.dot(fq*w,np.transpose(fq))
    cont=-1
    for rout, rcum in zip(out, fnts):
        for vout, ccum in zip(rout, fnts):
            cum[rcum,ccum]+=vout
            
#print using 3 formats
tmp=cum.todense()
print (type(tmp), "\n", tmp)
tmp=cum.toarray()
print (type(tmp), "\n", tmp)
print (type(cum), "\n", cum)


<class 'numpy.matrixlib.defmatrix.matrix'> 
[[ 0.033  0.017 -0.008  0.000  0.000  0.000  0.000  0.000  0.000]
 [ 0.017  0.133  0.017  0.000  0.000  0.000  0.000  0.000  0.000]
 [-0.008  0.017  0.067  0.017 -0.008  0.000  0.000  0.000  0.000]
 [ 0.000  0.000  0.017  0.133  0.017  0.000  0.000  0.000  0.000]
 [ 0.000  0.000 -0.008  0.017  0.067  0.017 -0.008  0.000  0.000]
 [ 0.000  0.000  0.000  0.000  0.017  0.133  0.017  0.000  0.000]
 [ 0.000  0.000  0.000  0.000 -0.008  0.017  0.067  0.017 -0.008]
 [ 0.000  0.000  0.000  0.000  0.000  0.000  0.017  0.133  0.017]
 [ 0.000  0.000  0.000  0.000  0.000  0.000 -0.008  0.017  0.033]]
<type 'numpy.ndarray'> 
[[ 0.033  0.017 -0.008  0.000  0.000  0.000  0.000  0.000  0.000]
 [ 0.017  0.133  0.017  0.000  0.000  0.000  0.000  0.000  0.000]
 [-0.008  0.017  0.067  0.017 -0.008  0.000  0.000  0.000  0.000]
 [ 0.000  0.000  0.017  0.133  0.017  0.000  0.000  0.000  0.000]
 [ 0.000  0.000 -0.008  0.017  0.067  0.017 -0.008  0.000  0.000]
 [ 0.000  0.000  0.000  0.000  0.017  0.133  0.017  0.000  0.000]
 [ 0.000  0.000  0.000  0.000 -0.008  0.017  0.067  0.017 -0.008]
 [ 0.000  0.000  0.000  0.000  0.000  0.000  0.017  0.133  0.017]
 [ 0.000  0.000  0.000  0.000  0.000  0.000 -0.008  0.017  0.033]]
<class 'scipy.sparse.dok.dok_matrix'> 
  (6, 6)	0.0666666666667
  (5, 6)	0.0166666666667
  (5, 4)	0.0166666666667
  (2, 1)	0.0166666666667
  (1, 2)	0.0166666666667
  (6, 7)	0.0166666666667
  (3, 3)	0.133333333333
  (7, 6)	0.0166666666667
  (4, 4)	0.0666666666667
  (2, 2)	0.0666666666667
  (8, 6)	-0.00833333333333
  (1, 1)	0.133333333333
  (6, 4)	-0.00833333333333
  (3, 2)	0.0166666666667
  (0, 0)	0.0333333333333
  (4, 5)	0.0166666666667
  (7, 7)	0.133333333333
  (2, 3)	0.0166666666667
  (8, 7)	0.0166666666667
  (6, 8)	-0.00833333333333
  (4, 2)	-0.00833333333333
  (1, 0)	0.0166666666667
  (6, 5)	0.0166666666667
  (5, 5)	0.133333333333
  (0, 1)	0.0166666666667
  (4, 6)	-0.00833333333333
  (7, 8)	0.0166666666667
  (0, 2)	-0.00833333333333
  (2, 0)	-0.00833333333333
  (8, 8)	0.0333333333333
  (4, 3)	0.0166666666667
  (3, 4)	0.0166666666667
  (2, 4)	-0.00833333333333

In [ ]:
#Using the default function that returns a numpy array no sparced.

t=MassMatrix(vs,5)
print (t)


[[ 0.033  0.017 -0.008  0.000  0.000  0.000  0.000  0.000  0.000]
 [ 0.017  0.133  0.017  0.000  0.000  0.000  0.000  0.000  0.000]
 [-0.008  0.017  0.067  0.017 -0.008  0.000  0.000  0.000  0.000]
 [ 0.000  0.000  0.017  0.133  0.017  0.000  0.000  0.000  0.000]
 [ 0.000  0.000 -0.008  0.017  0.067  0.017 -0.008  0.000  0.000]
 [ 0.000  0.000  0.000  0.000  0.017  0.133  0.017  0.000  0.000]
 [ 0.000  0.000  0.000  0.000 -0.008  0.017  0.067  0.017 -0.008]
 [ 0.000  0.000  0.000  0.000  0.000  0.000  0.017  0.133  0.017]
 [ 0.000  0.000  0.000  0.000  0.000  0.000 -0.008  0.017  0.033]]

In [68]:
#Using the CSC function

t2=MassMatrix(vs,5,format="CSC")
print (t2)


  (0, 0)	0.0333333333333
  (1, 0)	0.0166666666667
  (2, 0)	-0.00833333333333
  (0, 1)	0.0166666666667
  (1, 1)	0.133333333333
  (2, 1)	0.0166666666667
  (0, 2)	-0.00833333333333
  (1, 2)	0.0166666666667
  (2, 2)	0.0666666666667
  (3, 2)	0.0166666666667
  (4, 2)	-0.00833333333333
  (2, 3)	0.0166666666667
  (3, 3)	0.133333333333
  (4, 3)	0.0166666666667
  (2, 4)	-0.00833333333333
  (3, 4)	0.0166666666667
  (4, 4)	0.0666666666667
  (5, 4)	0.0166666666667
  (6, 4)	-0.00833333333333
  (4, 5)	0.0166666666667
  (5, 5)	0.133333333333
  (6, 5)	0.0166666666667
  (4, 6)	-0.00833333333333
  (5, 6)	0.0166666666667
  (6, 6)	0.0666666666667
  (7, 6)	0.0166666666667
  (8, 6)	-0.00833333333333
  (6, 7)	0.0166666666667
  (7, 7)	0.133333333333
  (8, 7)	0.0166666666667
  (6, 8)	-0.00833333333333
  (7, 8)	0.0166666666667
  (8, 8)	0.0333333333333

In [69]:
#Using the CSR option.

t2=MassMatrix(vs,5,format="CSR")
print (t2)


  (0, 0)	0.0333333333333
  (0, 1)	0.0166666666667
  (0, 2)	-0.00833333333333
  (1, 0)	0.0166666666667
  (1, 1)	0.133333333333
  (1, 2)	0.0166666666667
  (2, 0)	-0.00833333333333
  (2, 1)	0.0166666666667
  (2, 2)	0.0666666666667
  (2, 3)	0.0166666666667
  (2, 4)	-0.00833333333333
  (3, 2)	0.0166666666667
  (3, 3)	0.133333333333
  (3, 4)	0.0166666666667
  (4, 2)	-0.00833333333333
  (4, 3)	0.0166666666667
  (4, 4)	0.0666666666667
  (4, 5)	0.0166666666667
  (4, 6)	-0.00833333333333
  (5, 4)	0.0166666666667
  (5, 5)	0.133333333333
  (5, 6)	0.0166666666667
  (6, 4)	-0.00833333333333
  (6, 5)	0.0166666666667
  (6, 6)	0.0666666666667
  (6, 7)	0.0166666666667
  (6, 8)	-0.00833333333333
  (7, 6)	0.0166666666667
  (7, 7)	0.133333333333
  (7, 8)	0.0166666666667
  (8, 6)	-0.00833333333333
  (8, 7)	0.0166666666667
  (8, 8)	0.0333333333333

In [70]:
for i in range(2):
    for j in range(2,i):
        vs=IteratedVectorSpace(UniformLagrangeVectorSpace(j), np.linspace(0,1,i))
        v=np.ones(vs.n_dofs)
        for k in range(2,10):
            tem=MassMatrix(vs,k)
            assert abs(v.dot(tem.dot(v))-1.0)<10**(-10)

In [72]:
#test for to decide between using LIL or DOK
results=[]
print ("cels","nbasis","CSR","LIL")
for i in np.arange(2,5):
    for j in np.arange(2,10):
        vs=IteratedVectorSpace(UniformLagrangeVectorSpace(j), np.linspace(0,1,4**i))
        v=np.ones(vs.n_dofs)
        t1 = int(round(time.time() * 1000))
        tdok=MassMatrix(vs,3)
        t2 = int(round(time.time() * 1000))
        tlil=MassMatrix(vs,3,internal="LIL")
        t3 = int(round(time.time() * 1000))
        lres=[4**i, j, t2-t1,t3-t2]
        results.append(lres)
        print (lres)
print ("done")


cels nbasis CSR LIL
[16, 2, 24, 36]
[16, 3, 56, 63]
[16, 4, 110, 92]
[16, 5, 137, 161]
[16, 6, 238, 231]
[16, 7, 330, 422]
[16, 8, 861, 1178]
[16, 9, 608, 1057]
[64, 2, 71, 69]
[64, 3, 135, 166]
[64, 4, 269, 322]
[64, 5, 657, 820]
[64, 6, 1110, 1296]
[64, 7, 1235, 1392]
[64, 8, 2424, 2709]
[64, 9, 2464, 2981]
[256, 2, 319, 390]
[256, 3, 832, 961]
[256, 4, 1136, 2423]
[256, 5, 2028, 2374]
[256, 6, 4160, 5482]
[256, 7, 5973, 7215]
[256, 8, 9414, 8595]
[256, 9, 10149, 11687]
done