本文整理汇总了Python中matrix.Matrix类的典型用法代码示例。如果您正苦于以下问题:Python Matrix类的具体用法?Python Matrix怎么用?Python Matrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Matrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_should_multiply_itself_by_1x1_matrix
def test_should_multiply_itself_by_1x1_matrix(self):
matrix = Matrix(1, 1, 2)
other_matrix = Matrix(1, 1, 1337)
matrix.multiply(other_matrix)
self.assertEquals(2 * 1337, matrix.get_item(0, 0))
示例2: test_should_substract_other_10x10_matrix
def test_should_substract_other_10x10_matrix(self):
matrix = Matrix(10, 10, 1338)
other_matrix = Matrix(10, 10, 1)
matrix.subtract(other_matrix)
self.assertEquals(1337, matrix.get_item(9, 9))
示例3: test_should_substract_other_1x1_matrix
def test_should_substract_other_1x1_matrix(self):
matrix = Matrix(1, 1, 1338)
other_matrix = Matrix(1, 1, 1)
matrix.subtract(other_matrix)
self.assertEquals(1337, matrix.get_item(0, 0))
示例4: test_should_add_itself_to_other_10x10_matrix
def test_should_add_itself_to_other_10x10_matrix(self):
matrix = Matrix(10, 10, 1)
other_matrix = Matrix(10, 10, 1336)
matrix.add(other_matrix)
self.assertEquals(1337, matrix.get_item(9, 9))
示例5: test_should_multiply_itself_by_10x10_matrix
def test_should_multiply_itself_by_10x10_matrix(self):
matrix = Matrix(10, 10, 2)
other_matrix = Matrix(10, 10, 1337)
matrix.multiply(other_matrix)
self.assertEquals(2 * 1337, matrix.get_item(9, 9))
示例6: makeGraphFromEdges1
def makeGraphFromEdges1(self, edges):
"""
Constructs a directional graph from edges (a list of tuple).
Each tuple contains 2 vertices.
For example, P -> Q is written as ('P', 'Q').
@param edges: edges
@type edges: list of 2-element tuple
@status: Tested method
@since: version 0.1
"""
if type(edges) != list: raise GraphParameterError('Edges must be a \
list of tuples')
from Set import Set
from Matrix import Matrix
vertices = list(Set([x[0] for x in edges] + [x[1] for x in edges]))
adj = Matrix(len(vertices))
adj = adj.m
for e in edges:
row = vertices.index(e[0])
col = vertices.index(e[1])
# fill values into lower triangular matrix
adj[row][col] = adj[row][col] + 1
adj.insert(0, vertices)
self.makeGraphFromAdjacency(adj)
示例7: test_iter_over_one_row
def test_iter_over_one_row(self):
m = Matrix(3, 3)
m[1][0] = 1
m[1][1] = 2
m[1][2] = 3
self.assertEqual([x for x in m.row(1)], [1, 2, 3])
self.assertEqual([x for x in m[1]], [1, 2, 3])
示例8: train
def train(self, vector):
matrix_2 = Matrix(vector)
matrix_1 = Matrix(matrix_2.transpose())
matrix_3 = matrix_2 * matrix_1
identity = Matrix.identity(len(vector))
matrix_4 = matrix_3 - identity
self.weight = self.weight + matrix_4
示例9: main
def main():
r = 66
c = 66
s = 7
test = Matrix(rows=r, columns=c, side=s)
test.random_matrix()
test.run()
示例10: test_should_add_itself_to_other_1x1_matrix
def test_should_add_itself_to_other_1x1_matrix(self):
matrix = Matrix(1, 1, 1)
other_matrix = Matrix(1, 1, 1336)
matrix.add(other_matrix)
self.assertEquals(1337, matrix.get_item(0, 0))
示例11: parse_Matrix
def parse_Matrix(testset):
print "Parsing text file "+ testset + " into Matrix instance"
f=open(testset, "r")
mat_list = [] # this will be returned
res = [] # contains list of list that will be changed to a matrix
res_len = -1
for line in f.readlines():
if line.startswith("test"):
res.append([])
res_len += 1
else:
res[res_len].append(line.rstrip(" \r\n").split(" "))
for elem in res:
if(type(elem) == int):
mat = Matrix(1,1)
mat.matrix = elem
mat_list.append(mat)
else:
print elem
mat = Matrix(len(elem), len(elem[0])) # Matrix(2,10)
for r in range(mat.row): # 2
for c in range(mat.col): # 10
mat.matrix[r][c] = int(elem[r][c])
mat_list.append(mat)
return mat_list
示例12: main
def main():
if len(argv) < 4:
print "Usage: <num eigenvectors> <tolerance> <text file>"
return
data = load_matrix(argv[3])
k = int(argv[1])
epsilon = float(argv[2])
rows = len(data)
cols = len(data[0])
A = Matrix(rows, cols, data)
res, vals = power_method(k, epsilon, A)
Vecs = Matrix(rows, k)
for i in range(rows):
for j in range(k):
Vecs.data[i][j] = res[j].data[i][0]
f1 = open('vecs.txt','w')
f2 = open('vals.txt','w')
# TODO fix formatting
for i in range(rows):
for j in range(k):
f1.write(str(Vecs.data[i][j]) + ' ')
f1.write('\n')
for i in range(k):
f2.write(str(vals[i]) + '\n')
f1.close()
f2.close()
示例13: compute
def compute(self):
filename = self.getInputFromPort("filename")
f = open(filename.name, 'r')
# read metadata
_type = f.readline()
rows = int(f.readline().strip())
cols = int(f.readline().strip())
# read attributes
attributes = f.readline().split(';')
# read id, values and class
values = np.zeros((rows, cols))
ids = []
labels = []
for r in range(rows):
line = f.readline().split(';')
ids.append(r)
labels.append(line[0])
values[r,] = np.array([float(val) for val in line[1:-1]])
#klass = float(line[-1])
matrix = Matrix()
matrix.values = values
matrix.ids = ids
matrix.labels = labels
matrix.attributes = attributes
self.setResult('matrix', matrix)
示例14: makeGraphFromEdges2
def makeGraphFromEdges2(self, edges):
"""
Constructs an un-directional graph from edges (a list of tuple).
Each tuple contains 2 vertices.
An un-directional graph is implemented as a directional graph where
each edges runs both directions.
@param edges: list of edges
@type edges: list of 2-element tuples"""
if type(edges) != list: raise GraphParameterError('Edges must be a \
list of tuples')
from Set import Set
from Matrix import Matrix
vertices = list(Set([x[0] for x in edges] + [x[1] for x in edges]))
adj = Matrix(len(vertices))
adj = adj.m
for e in edges:
row = vertices.index(e[0])
col = vertices.index(e[1])
# fill values into lower triangular matrix
adj[row][col] = adj[row][col] + 1
# repeat on the upper triangular matrix for undirectional graph
adj[col][row] = adj[col][row] + 1
adj.insert(0, vertices)
self.makeGraphFromAdjacency(adj)
示例15: setUp
def setUp(self):
self.ecm = [[1, 1],[1, 0]]
self.ec = Matrix(["Bearing", "Belt"], ["Import Mechanical", "Export Mechanical"]
, lol_to_dict(self.ecm))
self.cfm = [[0,3],[1,0]]
self.cf = Matrix(["Creep", "Fatigue"], ["Bearing", "Belt"], lol_to_dict(self.cfm))
self.cfpm = [[1,2],[5,0]]
self.cfp = Matrix(["Creep", "Fatigue"], ["Bearing", "Belt"], lol_to_dict(self.cfpm))
self.efm = [[1,3],[0,3]]
self.ef = Matrix(["Creep", "Fatigue"], ["Import Mechanical", "Export Mechanical"],
lol_to_dict(self.efm))
self.l1m = [[2,5],[0,5]]
self.l1 = Matrix(["Creep", "Fatigue"], ["Import Mechanical", "Export Mechanical"],
lol_to_dict(self.l1m))
self.l2m = [[1,1],[0,1]]
self.l2 = Matrix(["Creep", "Fatigue"], ["Import Mechanical", "Export Mechanical"],
lol_to_dict(self.l2m))
self.c1m = [[5,2],[1,2]]
self.c1 = Matrix(["Creep", "Fatigue"], ["Import Mechanical", "Export Mechanical"],
lol_to_dict(self.c1m))
self.c2m = [[3,2],[1,2]]
self.c2 = Matrix(["Creep", "Fatigue"], ["Import Mechanical", "Export Mechanical"],
lol_to_dict(self.c2m))