-
Notifications
You must be signed in to change notification settings - Fork 1
/
20091109a.py
48 lines (41 loc) · 1.34 KB
/
20091109a.py
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
"""Create a matrix representation of a path graph with equal weights.
"""
from StringIO import StringIO
import numpy as np
from SnippetUtil import HandlingError
import MatrixUtil
import Euclid
import iterutils
import Form
import FormOut
def get_form():
"""
@return: a list of form objects
"""
form_objects = [
Form.Float('edge_affinity', 'edge affinities',
1.0, low_exclusive=0),
Form.Integer('nvertices', 'number of vertices',
5, low=0, high=100),
Form.RadioGroup('format', 'output options', [
Form.RadioItem('adjacency', 'adjacency matrix'),
Form.RadioItem('laplacian', 'laplacian matrix', True)])]
return form_objects
def get_form_out():
return FormOut.Matrix()
def create_adjacency_matrix(affinity, nvertices):
"""
@param affinity: affinity between adjacent vertices
@param nvertices: the number of vertices in the graph
@return: a numpy matrix
"""
A = np.zeros((nvertices, nvertices))
for i, j in iterutils.pairwise(range(nvertices)):
A[i,j] = affinity
A[j,i] = affinity
return A
def get_response_content(fs):
M = create_adjacency_matrix(fs.edge_affinity, fs.nvertices)
if fs.laplacian:
M = Euclid.adjacency_to_laplacian(M)
return MatrixUtil.m_to_string(M) + '\n'