-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.py
More file actions
54 lines (47 loc) · 1.62 KB
/
helper.py
File metadata and controls
54 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def trimesh(vertices, indices, labels=False):
"""
Plot a 2D triangle mesh
"""
from scipy import asarray
from matplotlib import collections
from pylab import gca, axis, text
from numpy import average
vertices,indices = asarray(vertices),asarray(indices)
#3d tensor [triangle index][vertex index][x/y value]
triangles = vertices[indices.ravel(),:].reshape((indices.shape[0],indices.shape[1],2))
col = collections.PolyCollection(triangles)
col.set_facecolor('grey')
col.set_alpha(0.5)
col.set_linewidth(1)
sub = gca()
sub.add_collection(col,autolim=True)
axis('off')
sub.autoscale_view()
if labels:
barycenters = average(triangles,axis=1)
for n,bc in enumerate(barycenters):
text(bc[0], bc[1], str(n), {'color' : 'k', 'fontsize' : 8,
'horizontalalignment' : 'center',
'verticalalignment' : 'center'})
def graph_laplacian(V,E):
# build graph Laplacian
from numpy import kron, ones, zeros, array
from scipy.sparse import coo_matrix
Nel = E.shape[0]
Npts = E.max()+1
Elps = E.shape[1]
row = kron(range(0,Nel),ones((Elps,)))
col = E.ravel()
data = ones((col.size,),dtype=float)
A = coo_matrix((data,(row,col)), shape=(Nel,Npts)).tocsr()
A = A.T * A
A.data = -1*ones((A.nnz,),dtype=float)
A.setdiag(zeros((Npts,),dtype=float))
A.setdiag(-1*array(A.sum(axis=1)).ravel())
return A.tocsr()
def run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False