Fausto Arinos Barbuto
Fausto Arinos Barbuto

Reputation: 398

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.

Upvotes: 3

Views: 6522

Answers (2)

DerWeh
DerWeh

Reputation: 1821

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)

Upvotes: 0

U2EF1
U2EF1

Reputation: 13259

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.

Upvotes: 4

Related Questions