Search code examples
pythonnumpyscipytoeplitz

Toeplitz matrix using numpy/scipy


In Octave or Matlab there is a neat, compact way to create large Toeplitz matrices, for example:

T = toeplitz([1,-0.25,zeros(1,20)])

That saves a lot of time that would otherwise be spent to fill the matrix with dozens or hundreds of zeros by using extra lines of code.

Yet I don't seem to be able to do the same with neither scipy or numpy, although those two libraries have both toeplitz() and zeros() functions. Is there a similar way to do that or do I have to put together a routine of my own to do that (not a huge problem, but still a nuisance)?

Thanks,

F.


Solution

  • Currently I think the best you can do is:

    from numpy import concatenate, zeros
    from scipy.linalg import toeplitz
    
    toeplitz(concatenate([[1., -.25], zeros(20)]))
    

    As of python 3.5 however we'll have:

    toeplitz([1., -.25, *zeros(20)])
    

    So that's something to look forward to.