Fast Matrix-Vector-Multiplication By FFT

在这里插入图片描述

>> 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》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值