本文整理汇总了Python中Matrix.Matrix.new方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.new方法的具体用法?Python Matrix.new怎么用?Python Matrix.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix.Matrix
的用法示例。
在下文中一共展示了Matrix.new方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QRalg
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
def QRalg(m):
from copy import deepcopy as copy
from math import cos, sin, atan, pi
def get_rotate_matrix(n, i, j, angle):
m = Matrix.new(n, n)
for k in range(n):
if k not in [i, j]:
m[k][k] = 1
s = sin(angle)
c = cos(angle)
m[i][j] = -s
m[j][i] = s
m[i][i] = c
m[j][j] = c
return m
m = copy(m)
n = len(m)
u = Matrix.new(n, n)
for i in range(n):
u[i][i] = 1
iter_n = 1
f = True
while f and iter_n < 1000:
q = 0
qi, qj = 0, 0
for i in range(n):
for j in range(n):
if i != j and abs(m[i][j]) > q:
q, qi, qj = abs(m[i][j]), i, j
angle = pi / 4
if abs(m[qi][qi] - m[qj][qj]) > 0.000001:
angle = atan(2 * m[qi][qj] / (m[qi][qi] - m[qj][qj])) / 2
rm = get_rotate_matrix(n, qi, qj, angle)
rmt = rm.transpose()
nm = rmt * m * rm
u = u * rm
#l.write('{}: '.format(iter_n))
#l.write(nm)
s = 0
for i in range(n):
for j in range(n):
if i != j:
s += nm[i][j] ** 2
f = abs(s) > (0.1 ** 5)
m = copy(nm)
iter_n += 1
values = Vector([m[i][i] for i in range(n)])
vectors = u.transpose()
for i in range(len(vectors)):
if abs(vectors[i][-1]) > 0.001:
vectors[i] *= 1/vectors[i][-1]
return (values, vectors, )
示例2: get_rotate_matrix
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
def get_rotate_matrix(n, i, j, angle):
m = Matrix.new(n, n)
for k in range(n):
if k not in [i, j]:
m[k][k] = 1
s = sin(angle)
c = cos(angle)
m[i][j] = -s
m[j][i] = s
m[i][i] = c
m[j][j] = c
return m
示例3: hh
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
def hh(step, a):
def sign(a):
if a >= 0:
return 1
else:
return -1
n = len(a)
v = Matrix.new(n, 1)
t = 0
for i in range(step, n):
t += a[i][0] ** 2
t = sqrt(t)
v[step][0] = a[step][0] + sign(a[step][0]) * t
for i in range(step + 1, n):
v[i][0] = a[i][0]
#l.write('v: ')
#l.write(v.list())
vt = v.transpose()
tmp = vt * v
tmp = tmp[0][0]
h = Matrix.E(n) - (v * vt) * (2/tmp)
return h
示例4: open
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
from Matrix import Matrix
from Vector import Vector
from copy import deepcopy as copy
log_file = open('log13', 'w')
verb = True
def log(s):
s = str(s)
if verb:
print(s)
log_file.write(s + '\n')
m = Matrix.new(4, 5)
fin = open('input13', 'r')
for i, s in enumerate(fin.readlines()):
t = list(map(float, s.split()))
for j, n in enumerate(t):
m[i][j] = n
log('Input matrix: ')
log(m)
log('\n')
n = 4
a = Matrix.new(4, 4)
for i in range(n):
for j in range(n):
a[i][j] = -m[i][j] / m[i][i]
a[i][i] = 0
b = Matrix.new(4, 1)
for i in range(4):
示例5: open
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
from Matrix import Matrix
from Vector import Vector
from copy import deepcopy as copy
log_file = open('log12', 'w')
verb = True
def log(s):
s = str(s)
if verb:
print(s)
log_file.write(s + '\n')
m = Matrix.new(5, 6)
fin = open('input12', 'r')
for i, s in enumerate(fin.readlines()):
t = list(map(float, s.split()))
for j, n in enumerate(t):
m[i][j] = n
log('Input matrix: ')
log(m)
log('\n')
n = 5
p = [0 for i in range(n)]
q = copy(p)
p[0] = -m[0][1] / m[0][0]
q[0] = m[0][-1] / m[0][0]
for i in range(1, n):
示例6: range
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
x[-1] = q[-1]
for i in range(n-2, -1, -1):
x[i] = x[i+1] * p[i] + q[i]
return x
#x = [-3, -1, 1, 3, 5]
#f = [-4.2490, -1.7854, 1.7854, 4.2490, 6.3734]
x = [0, 1, 2, 3, 4]
f = [0, 1.8415, 2.9093, 3.1411, 3.2432]
n = len(x)
h = [0] + [x[i]-x[i-1] for i in range(1, n)]
print(f[-3])
a = Matrix.new(n-2, n-2)
b = Matrix.new(n-2, 1)
a[0][0] = 2 * (h[1] + h[2])
a[0][1] = h[2]
b[0][0] = 3 * ((f[2] - f[1]) / h[2] - (f[1] - f[0]) / h[1])
for i in range(3, n-1):
a[i-2][i-3] = h[i-1]
a[i-2][i-2] = 2 * (h[i-1] + h[1])
a[i-2][i-1] = h[i]
b[i-2][0] = 3 * ((f[i] - f[i-1]) / h[i] - (f[i-1] - f[i-2]) / h[i-1])
a[-1][-2] = h[-2]
a[-1][-1] = 2 * (h[-1] + h[-2])
b[-1][0] = 3 * ((f[-1] - f[-2]) / h[-1] - (f[-2] - f[-3]) / h[-2])
示例7: log
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
from Matrix import Matrix
from Vector import Vector
from logging import log
from copy import deepcopy as copy
l = log('log14', False)
m = Matrix.new(3, 3)
fin = open('input14', 'r')
for i, s in enumerate(fin.readlines()):
t = list(map(float, s.split()))
for j, n in enumerate(t):
m[i][j] = n
l.fwrite('Input matrix: ')
l.fwrite(m)
l.fwrite('\n')
n = 3
from math import cos, sin, atan, pi
def get_rotate_matrix(n, i, j, angle):
m = Matrix.new(n, n)
for k in range(n):
if k not in [i, j]:
m[k][k] = 1
s = sin(angle)
c = cos(angle)
m[i][j] = -s
m[j][i] = s
m[i][i] = c
m[j][j] = c
示例8: open
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
from Matrix import Matrix
from Vector import Vector
from copy import deepcopy as copy
log_file = open('log11', 'w')
verb = True
def log(s):
s = str(s)
if verb:
print(s)
log_file.write(s + '\n')
m = Matrix.new(4, 5)
fin = open('input11', 'r')
for i, s in enumerate(fin.readlines()):
t = list(map(float, s.split()))
for j, n in enumerate(t):
m[i][j] = n
log('Input matrix: ')
log(m)
log('\n')
n = 4
a = Matrix.new(n, n)
for i in range(n):
for j in range(n):
a[i][j] = m[i][j]
u = copy(a)
l = Matrix.new(n, n)
示例9: log
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import new [as 别名]
from Matrix import Matrix
from Vector import Vector
from logging import log
from math import sqrt
from copy import deepcopy as copy
l = log('log15')
m = Matrix.new(3, 3)
fin = open('input15', 'r')
for i, s in enumerate(fin.readlines()):
t = list(map(float, s.split()))
for j, n in enumerate(t):
m[i][j] = n
l.fwrite('Input matrix: ')
l.fwrite(m)
l.fwrite('\n')
n = 3
def hh(step, a):
def sign(a):
if a >= 0:
return 1
else:
return -1
n = len(a)
v = Matrix.new(n, 1)
t = 0
for i in range(step, n):
t += a[i][0] ** 2
t = sqrt(t)