我正在创建随机 Toeplitz 矩阵来估计它们可逆的概率。我目前的代码是
import random
from scipy.linalg import toeplitz
import numpy as np
for n in xrange(1,25):
rankzero = 0
for repeats in xrange(50000):
column = [random.choice([0,1]) for x in xrange(n)]
row = [column[0]]+[random.choice([0,1]) for x in xrange(n-1)]
matrix = toeplitz(column, row)
if (np.linalg.matrix_rank(matrix) < n):
rankzero += 1
print n, (rankzero*1.0)/50000
这可以加速吗?
我想增加值 50000 以获得更高的准确性,但目前这样做太慢了。
仅使用for n in xrange(10,14)
节目进行分析
400000 9.482 0.000 9.482 0.000 {numpy.linalg.lapack_lite.dgesdd}
4400000 7.591 0.000 11.089 0.000 random.py:272(choice)
200000 6.836 0.000 10.903 0.000 index_tricks.py:144(__getitem__)
1 5.473 5.473 62.668 62.668 toeplitz.py:3(<module>)
800065 4.333 0.000 4.333 0.000 {numpy.core.multiarray.array}
200000 3.513 0.000 19.949 0.000 special_matrices.py:128(toeplitz)
200000 3.484 0.000 20.250 0.000 linalg.py:1194(svd)
6401273/6401237 2.421 0.000 2.421 0.000 {len}
200000 2.252 0.000 26.047 0.000 linalg.py:1417(matrix_rank)
4400000 1.863 0.000 1.863 0.000 {method 'random' of '_random.Random' objects}
2201015 1.240 0.000 1.240 0.000 {isinstance}
[...]