本文整理汇总了Python中matrix.Matrix.transpose方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.transpose方法的具体用法?Python Matrix.transpose怎么用?Python Matrix.transpose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matrix.Matrix
的用法示例。
在下文中一共展示了Matrix.transpose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMatrix
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
class TestMatrix(TestCase):
def setUp(self):
self.m1 = Matrix([[1, 2],
[3, 4]])
self.m2 = Matrix([[-1, 5],
[0, 9]])
self.m3 = Matrix([[-1, 5, 4],
[0, 9, 1],
[4, 5, 2]])
self.m4 = Matrix([[-1, 5],
[0, 9],
[3, 2]])
def test_bad_matrix(self):
"""Make sure the input matrix is valid"""
# should raise an exception for an empty matrix
self.assertRaises(TypeError, Matrix, [])
# should raise an exception for an invalid matrix
self.assertRaises(TypeError, Matrix, ['a'])
# should raise an exception for an incorrect matrix
# must be 2-dimensional
self.assertRaises(TypeError, Matrix, [1, 2])
def test_row_col_getters(self):
# get row with index 1
self.assertEqual(self.m1.row(1), [3, 4])
# get col with index 1
self.assertEqual(self.m1.col(1), [2, 4])
def test_transposal(self):
self.m1.transpose()
self.assertEqual(self.m1.matrix, [[1, 3], [2, 4]])
def test_addition(self):
# should raise an exception for addition of matrices with
# different lengths
self.assertRaises(IndexError, self.m1.__add__, self.m3)
# test simple addition
self.assertEqual((self.m1 + self.m2).matrix, [[0, 7], [3, 13]])
def test_multiplication(self):
# test whether number of columns in multiplicand
# is equal to number of rows in multiplier
self.assertRaises(IndexError, self.m1.__mul__, self.m3)
# test multiplication
self.assertEqual((self.m3 * self.m4).matrix, [[13, 48],
[3, 83],
[2, 69]])
示例2: testTranspose
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def testTranspose(self):
matrix = Matrix([1, 2], [3, 4])
self.assertEquals(
Matrix([1, 3], [2, 4]),
matrix.transpose())
matrix2 = Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9])
self.assertEquals(
Matrix([1, 4, 7], [2, 5, 8], [3, 6, 9]),
matrix2.transpose())
示例3: test_should_contain_1337_in_last_row_after_transpose
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def test_should_contain_1337_in_last_row_after_transpose(self):
cols = 100
rows = 1
matrix = Matrix(cols, rows, 1)
val = 1337
matrix.set_item(cols - 1, 0, val)
matrix.transpose()
self.assertEquals(val, matrix.get_item(0, cols - 1))
示例4: rotationMethod
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def rotationMethod(m,A,a,epsilon,p):
#print("Start Rotation.")
VECTOR = 1
X = Matrix(m,VECTOR)
oldA = Matrix(m,m)
r = Matrix(m,VECTOR)
iterationNumber = 0
condition = True
#print("Entering while.")
while condition:
#print("OldA copy.")
oldA = copy.deepcopy(A)
#print("0")
r = copy.deepcopy(a.substractMatrix(A.matrixMultiplication(X,a)))
#print("1")
(xpq,p,q) = getMax(A.lowerTriangularMatrix())
#print("2")
theta = 0
if A.lowerTriangularMatrix().at(p,p) == A.lowerTriangularMatrix().at(q,q):
#print("3")
theta = math.pi/4
else:
#print("4")
theta = ( 2 * xpq )/(A.upperTriangularMatrix().at(p,p) - A.upperTriangularMatrix().at(q,q) + (10**(-5)))
#print("Determined theta.")
c = math.cos(theta)
s = math.sin(theta)
#print("5")
T = Matrix(m,m)
for i in range(0,T.numberOfColumns):
T.insert(i,i,1)
T.insert(p,p,c)
T.insert(p,q,s)
T.insert(q,p,-s)
T.insert(q,q,c)
#print("Determined T.")
A = copy.deepcopy((T.transpose()).multiplyMatrix(oldA.multiplyMatrix(T)))
#condition = not r.isAlmostZero()
if iterationNumber%100 == 0:
print("-----")
print("Situation at iteration:",iterationNumber)
print("Condition:",condition)
print("A matrix:")
A.display()
print("X matrix:")
X.display()
print("-----")
X = copy.deepcopy(oldA)
iterationNumber += 1
condition = iterationNumber > 10**(15)
#condition = checkX(X)
print("===== Rotation Method =====")
print("X:")
X.display()
print("A:")
A.display()
#print("Test:")
#r.display()
print("====================")
示例5: train
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
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
示例6: test_transpose_2_by_2_matrix
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def test_transpose_2_by_2_matrix(self):
m = Matrix(2, 2)
m[0][0] = 1
m[0][1] = 2
m[1][0] = 3
m[1][1] = 4
mt = m.transpose()
self.assertEqual(mt[0][0], 1)
self.assertEqual(mt[0][1], 3)
self.assertEqual(mt[1][0], 2)
self.assertEqual(mt[1][1], 4)
示例7: Newton
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
class Newton():
def __init__(self, bases, lmd=0, err=1):
self.bases = bases
self.lmd = lmd
self.err = err
def fit(self, x, y):
A = []
for i in range(self.bases):
A.append([float(xi)**i for xi in x])
A.reverse()
self.A = Matrix(A).transpose()
self.b = Matrix([y]).transpose()
w0 = Matrix([[0 for i in range(self.bases)]]).transpose()
while True:
w1 = w0 - self.hession(w0).inverse() * self.gradient(w0)
if abs(self.error(w1).data[0][0] - self.error(w0).data[0][0]) < self.err:
break
w0 = w1
self.w = w1
return self.w.transpose().data[0]
def show(self):
w = self.w.transpose().data[0]
for i in range(self.bases-1):
print("(%.3f x^%d) + " % (w[i], self.bases-i-1), end='')
print("(%.3f) = y" % w[-1])
def score(self):
return round(self.error(self.w).data[0][0], 3)
def error(self, x):
return (self.A * x - self.b).transpose() * (self.A * x - self.b)
def gradient(self, x):
return self.A.transpose() * self.A * 2 * x - self.A.transpose() * self.b * 2
def hession(self, x):
return self.A.transpose() * self.A * 2
示例8: filter
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def filter(x, P):
for n in range(len(self.measurements)):
# prediction
x = (self.F * x) + self.u
P = self.F * P * self.F.transpose()
# measurement update
Z = Matrix([self.measurements[n]])
y = Z.transpose() - (self.H * x)
S = self.H * P * self.H.transpose() + self.R
K = P * self.H.transpose() * S.inverse()
x = x + (K * y)
P = (self.I - (K * self.H)) * P
return x,P
示例9: get_householder_transform
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def get_householder_transform(mtrx, col):
sum_pow_2 = 0
for i in range(col, mtrx.size[0]):
sum_pow_2 += mtrx[i][col] ** 2
v = Matrix((mtrx.size[0], 1))
v[col][0] = mtrx[col][col] + math.copysign(math.sqrt(sum_pow_2), mtrx[col][col])
for i in range(col + 1, mtrx.size[0]):
v[i][0] = mtrx[i][col]
h_mtrx = Matrix.identity(mtrx.size)
transpose_v = v.transpose()
tmp_mtrx = v * transpose_v
tmp_mtrx *= 2 / (transpose_v * v)[0][0]
h_mtrx -= tmp_mtrx
return h_mtrx
示例10: rotationMethod
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def rotationMethod(m,A,a,epsilon,p):
VECTOR = 1
X = Matrix(m,VECTOR)
oldA = Matrix(m,m)
r = Matrix(m,VECTOR)
iterationNumber = 0
condition = True
while condition:
oldA = copy.deepcopy(A)
r = copy.deepcopy(a.substractMatrix(A.matrixMultiplication(X,a)))
(xpq,p,q) = getMax(A.lowerTriangularMatrix())
theta = 0
if A.lowerTriangularMatrix().at(p,p) == A.lowerTriangularMatrix().at(q,q):
theta = math.pi/4
else:
theta = (1/2)*math.arct(( 2 * xpq )/(A.upperTriangularMatrix().at(p,p) - A.upperTriangularMatrix().at(q,q)))
c = math.cos(theta)
s = math.sin(theta)
T = Matrix(m,m)
for i in range(0,T.numberOfColumns):
T.insert(i,i,1)
T.insert(p,p,c)
T.insert(p,q,s)
T.insert(q,p,-s)
T.insert(q,q,c)
A = copy.deepcopy((T.transpose()).multiplyMatrix(oldA.multiplyMatrix(T)))
'''
if iterationNumber%100 == 0:
print("-----")
print("Situation at iteration:",iterationNumber)
print("Condition:",condition)
print("A matrix:")
A.display()
print("X matrix:")
X.display()
print("-----")
'''
X = copy.deepcopy(oldA)
iterationNumber += 1
condition = (iterationNumber < ITERATIONS)
print("===== Rotation Method =====")
print("X:")
X.display()
print("A:")
A.display()
print("Test:")
r.display()
print("====================")
示例11: test_transpose_non_quadratic_matrix
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def test_transpose_non_quadratic_matrix(self):
m = Matrix(2, 3)
m[0][0] = 1
m[0][1] = 2
m[0][2] = 3
m[1][0] = 4
m[1][1] = 5
m[1][2] = 6
mt = m.transpose()
self.assertEqual(mt[0][0], 1)
self.assertEqual(mt[1][0], 2)
self.assertEqual(mt[2][0], 3)
self.assertEqual(mt[0][1], 4)
self.assertEqual(mt[1][1], 5)
self.assertEqual(mt[2][1], 6)
示例12: matrix_trans_mult
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def matrix_trans_mult(m, v):
'''Takes an openGL matrix and a 2 Vector and return
the transpose of teh matrix applied to the vector'''
if _use_numpy:
mat = numpy.matrix(m)
vec = numpy.matrix(v)
result = vec*mat.T
return Vector(result[0,0],result[0,1])
else:
mat = Matrix([
RowVector(list(m[0])),
RowVector(list(m[1])),
RowVector(list(m[2])),
RowVector(list(m[3]))] )
vec = RowVector(v)
result = vec*mat.transpose()
return Vector(result[1], result[2])
示例13: mlr
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def mlr(self, order=2):
sname = self.listSamples()
X = self.sample[sname[0]]
Y = self.sample[sname[1]]
if X.rowcount == Y.rowcount: slen = X.rowcount
elif X.rowcount > Y.rowcount: slen = Y.rowcount
else: slen = X.rowcount
X_data = X.data[:slen]
Y_data = Y.data[:slen]
data_array = [[1]*slen]
for x in range(1, order+1):
data_array.append([element**x for element in X_data])
beta_matrix = Matrix(data_array)
b1 = beta_matrix * beta_matrix.transpose()
b1 = b1.inverse()
b2 = Matrix(data_array) * Matrix([Y_data]).transpose()
result = b1 * b2
return [x[0] for x in result.m]
示例14: testTranspose
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
def testTranspose(self):
"""Траспонирование матрицы"""
# выполняем транспонирование матрицы и проверяем результат
a = Matrix(3, 2)
a.set(0, 0, 1)
a.set(0, 1, 2)
a.set(1, 0, 3)
a.set(1, 1, 4)
a.set(2, 0, 5)
a.set(2, 1, 6)
b = Matrix(2, 3)
b.set(0, 0, 1)
b.set(1, 0, 2)
b.set(0, 1, 3)
b.set(1, 1, 4)
b.set(0, 2, 5)
b.set(1, 2, 6)
self.assertEqual(a.transpose(), b)
示例15: LSE
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import transpose [as 别名]
class LSE():
def __init__(self, bases, lmd=0):
self.bases = bases
self.lmd = lmd
def fit(self, x, y):
A = []
for i in range(self.bases):
A.append([float(xi)**i for xi in x])
A.reverse()
self.A = Matrix(A).transpose()
self.b = Matrix([y]).transpose()
self.w = (self.A.transpose() * self.A + Matrix(self.bases) * self.lmd).inverse() * (self.A.transpose() * self.b)
return self.w.transpose().data[0]
def show(self):
w = self.w.transpose().data[0]
for i in range(self.bases-1):
print("(%.3f x^%d) + " % (w[i], self.bases-i-1), end='')
print("(%.3f) = y" % w[-1])
def score(self):
return round(((self.A * self.w - self.b).transpose() * (self.A * self.w - self.b)).data[0][0], 3)