首页 > 解决方案 > 如何有效地构造对称矩阵?

问题描述

我想构造一个对称矩阵。

r=np.arange(0,11)
k=np.zeros((11,11))

for i in r:
    k[i]=np.arange(i,i-11,-1)

如何摆脱for循环以更有效地构造矩阵?

标签: pythonarraysloopsnumpymatrix

解决方案


你可以这样做:

k = np.arange(0, 11)[:, np.newaxis] + np.arange(0, -11, -1)
print(k)

输出:

[[  0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10]
 [  1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9]
 [  2   1   0  -1  -2  -3  -4  -5  -6  -7  -8]
 [  3   2   1   0  -1  -2  -3  -4  -5  -6  -7]
 [  4   3   2   1   0  -1  -2  -3  -4  -5  -6]
 [  5   4   3   2   1   0  -1  -2  -3  -4  -5]
 [  6   5   4   3   2   1   0  -1  -2  -3  -4]
 [  7   6   5   4   3   2   1   0  -1  -2  -3]
 [  8   7   6   5   4   3   2   1   0  -1  -2]
 [  9   8   7   6   5   4   3   2   1   0  -1]
 [ 10   9   8   7   6   5   4   3   2   1   0]]

但是请注意,这个矩阵不是对称的,而是反对称的。

获得相同结果但使用更少内存的另一个更高级的选项是创建一个数字从 10 到 -10 的数组,并在每一行上“滚动”它:

import numpy as np

def make_matrix(n):
    r = np.arange(n, -(n + 1), -1)
    s, = r.strides
    m = np.ndarray(shape=(n + 1, n + 1),
                   dtype=r.dtype,
                   buffer=r.data,
                   offset=s * n,
                   strides=(-s, s),
                   order='C')
    # Avoid writing since it is not a contiguous array
    m.flags.writeable = False
    return m

print(make_matrix(10))
# Same output

这仅占用第一个数组的内存,而不是连续矩阵的二次大小。

编辑:

如果要创建对称矩阵,可以取绝对值:

k = np.abs(np.arange(0, 11)[:, np.newaxis] + np.arange(0, -11, -1))

或者您可以像这样稍微修改上面的函数:

import numpy as np

def make_matrix(n):
    a = np.arange(n + 1)
    r = np.concatenate([a[::-1], a[1:]])
    s, = r.strides
    m = np.ndarray(shape=(n + 1, n + 1),
                   dtype=r.dtype,
                   buffer=r.data,
                   offset=s * n,
                   strides=(-s, s),
                   order='C')
    m.flags.writeable = False
    return m

print(make_matrix(10))

输出:

[[ 0  1  2  3  4  5  6  7  8  9 10]
 [ 1  0  1  2  3  4  5  6  7  8  9]
 [ 2  1  0  1  2  3  4  5  6  7  8]
 [ 3  2  1  0  1  2  3  4  5  6  7]
 [ 4  3  2  1  0  1  2  3  4  5  6]
 [ 5  4  3  2  1  0  1  2  3  4  5]
 [ 6  5  4  3  2  1  0  1  2  3  4]
 [ 7  6  5  4  3  2  1  0  1  2  3]
 [ 8  7  6  5  4  3  2  1  0  1  2]
 [ 9  8  7  6  5  4  3  2  1  0  1]
 [10  9  8  7  6  5  4  3  2  1  0]]

关于性能,在这种情况下,您有几个连续与非连续的测试:

import numpy as np

def make_matrix_cont(n):
    return np.abs(np.arange(0, n + 1)[:, np.newaxis] + np.arange(0, -(n + 1), -1))

def make_matrix_noncont(n):
    a = np.arange(n + 1)
    r = np.concatenate([a[::-1], a[1:]])
    s, = r.strides
    m = np.ndarray(shape=(n + 1, n + 1), dtype=r.dtype, buffer=r.data, offset=s * n, strides=(-s, s), order='C')
    m.flags.writeable = False
    return m

n = 1000
k_cont = make_matrix_cont(n)
k_noncont = make_matrix_noncont(n)
print(np.all(k_cont == k_noncont))
# True

%timeit make_matrix_cont(n)
# 3.48 ms ± 42.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit make_matrix_noncont(n)
# 5.2 µs ± 11.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit k_cont.sum()
# 317 µs ± 4.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit k_noncont.sum()
# 370 µs ± 1.59 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit k_cont @ k_cont
# 313 ms ± 3.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit k_noncont @ k_noncont
# 417 ms ± 1.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

因此,除了占用更少的空间之外,非连续矩阵的创建速度要快得多,但对其元素求和要慢一些,而矩阵乘法要多一些。


推荐阅读