Advertisement
jckuri

ToeplitzMatrix.py

Oct 30th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. from numpy.fft import fft,ifft
  2.  
  3. def multiplyVectors(v0,v1):
  4.  n=len(v0)
  5.  return [v0[i]*v1[i] for i in range(n)]
  6.  
  7. def pointMultiply(v0,v1):
  8.  return sum(multiplyVectors(v0,v1))
  9.  
  10. def multiply(m,v):
  11.  n=len(v)
  12.  if n!=len(m) or n!=len(m[0]): return None
  13.  return [pointMultiply(row,v) for row in m]
  14.  
  15. def convolve(tr,v):
  16.  ttr=fft(tr)
  17.  zeroPadding=[0 for i in range(len(tr)-len(v))]
  18.  tv=fft(vector+zeroPadding)
  19.  return ifft(multiplyVectors(ttr,tv))
  20.  
  21. toeplitz=[[1,0,3],[2,1,0],[-1,2,1]]
  22. t=[3,0,1,2,-1]
  23. vector=[10,20,30]
  24.  
  25. m=multiply(toeplitz,vector)
  26. print('Matrix multiplication in O(n^2): ',m)
  27.  
  28. c=convolve(t,vector)
  29. print('Convolution in O(n*log(n)): ',c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement