
>> l = rand(1000,1);
>> l1 = l';
>> C = toeplitz([l1(1) fliplr(l1(2:end))], l1);
>> C(1:5, 1:5)
ans =
0.8147 0.9058 0.1270 0.9134 0.6324
0.8667 0.8147 0.9058 0.1270 0.9134
0.0600 0.8667 0.8147 0.9058 0.1270
0.4440 0.0600 0.8667 0.8147 0.9058
0.9509 0.4440 0.0600 0.8667 0.8147
>> x = rand(1000,1);
>> y=C *x;
>> for i = 1:100, y=C *x; end;
>> tic; y=C *x; toc;
Elapsed time is 0.000913 seconds.
>> tic; for i = 1:100, y=C *x; end; toc
Elapsed time is 0.074619 seconds.

>> y1 = ifft(fft(C(1:end,1)) .* fft(x));
>> norm(y-y1)
ans =
4.4646e-12
>> ll = C(1:end,1);
>> tic; for i = 1:100, y1 = ifft(fft(ll) .* fft(x)); end; toc
Elapsed time is 0.011608 seconds.
误差非常小,同时运算速度得到了几倍的提升,大约 0.1 ms/loop


>> l = rand(1000,1);
>> T = toeplitz(l, [l(1); zeros(999,1)]);
>> T(1:5, 1:5)
ans =
0.8147 0 0 0 0
0.9058 0.8147 0 0 0
0.1270 0.9058 0.8147 0 0
0.9134 0.1270 0.9058 0.8147 0
0.6324 0.9134 0.1270 0.9058 0.8147
>> x = rand(1000,1);
>> tic; y = T*x; toc
Elapsed time is 0.000593 seconds.
>> tic; for i = 1:100, y = T*x; end; toc
Elapsed time is 0.010883 seconds.
当然我们还可以把矩阵变成稀疏矩阵
>> T1 = sparse(T);
>> tic; y = T1*x; toc
Elapsed time is 0.826337 seconds.
>> tic; for i = 1:100, y = T1*x; end; toc
Elapsed time is 0.033503 seconds.
>> tic; y = T1*x; toc
Elapsed time is 0.000596 seconds.

>> y1 = T*x;
>> y2 = ifft(fft(l1).*fft([x;zeros(1000,1)]));
>> norm(y2(1:1000) - y1)
ans =
3.4828e-12
>> tic; y2 = ifft(fft(l1).*fft([x;zeros(1000,1)])); toc
Elapsed time is 0.000527 seconds.
>> tic; for i = 1:100, y2 = ifft(fft(l1).*fft([x;zeros(1000,1)])); end; toc
Elapsed time is 0.005841 seconds.



见知乎《https://zhuanlan.zhihu.com/p/27919464》
本文探讨了如何通过使用Toeplitz矩阵和快速傅立叶变换(FFT)技术提高矩阵乘法的效率,展示了用C++实现的示例,并比较了不同方法的时间性能。通过FFT,运算速度得到显著提升,尤其是在处理稀疏矩阵时效果明显。

2万+

被折叠的 条评论
为什么被折叠?



