2

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.

2 Answers2

3

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.

U2EF1
  • 12,907
  • 3
  • 35
  • 37
  • 1
    Thanks for the tip, that worked perfectly! I forgot to mention though that I don't use Python 3.x yet. I also made a mistake: as far as I know, numpy does not have toeplitz(). That one is part of scipy. – Fausto Arinos Barbuto Mar 01 '15 at 18:17
0

Another option is using r_:

import numpy as np
from scipy.linalg import toeplitz

toeplitz(np.r_[1, -0.25, np.zeros(20)])

You can also work with lists:

toeplitz([1, -0.25] + [0]*20)
DerWeh
  • 1,721
  • 1
  • 15
  • 26