toeplitz matrix

#1

Hello,

I’m trying to write Toeplitz matrix function. I have something like this

function toeplitz(x::Array)
    n = length(x)
    A = zeros(T, n, n)
    for i in 1:n
        A[i,:] = x[1:n - i + 1]
    end
end

and I got information “toeplitz (generic function with 1 method)”

But when I’m trying to run it via julia there is info that “T is not defined”. I’m not sure where I should add it into my code, and how?

#2

replace T by eltype(x) and it should work

Btw. you might be interested in https://github.com/JuliaMatrices/ToeplitzMatrices.jl

2 Likes
#3

Alternatively, you can declare the function as:

function toeplitz(x::AbstractVector{T}) where T

Note that by using AbstractVector you accept any type of 1d array, not just the built-in Vector{T} = Array{T,1}.

(You could also just declare it as function toeplitz(x) and use eltype. There is no performance advantage to declaring the argument type here, but AbstractVector is probably useful to communicate the intent and to give clearer errors.)

However, you also have another problem: A[i,:] = x[1:n - i + 1] will give an error because the right-hand side has only n-i+1 elements whereas the left-hand side expects n elements. Probably the simplest thing here is to just write out the loop:

for i = 1:n, j = 1:n-i+1
    A[i,j] = x[j]
end

which has the added benefit of eliminating the x[1:n - i + 1] temporary array (or view object if you added a @views decorator).

However, this is still not a Toeplitz matrix according to the usual definition. The usual definition would correspond to

for i = 1:n
    for j = 1:n-i+1
        A[i,i+j-1] = x[j]
    end
    # delete this second loop if you want zeros under the diagonal
    # rather than a circulant matrix:
    for j = n-i+2:n
        A[i, j-(n-i+1)] = x[j]
    end
end

Finally, you need return A at the end in order to return the array. You then get e.g.

julia> function toeplitz(x::AbstractVector{T}) where T
           n = length(x)
           A = zeros(T, n, n)
           for i = 1:n
               for j = 1:n-i+1
                   A[i,i+j-1] = x[j]
               end
               for j = n-i+2:n
                   A[i, j-(n-i+1)] = x[j]
               end
           end
           return A
       end

julia> toeplitz(1:10)
10×10 Array{Int64,2}:
  1   2   3   4   5   6   7   8   9  10
 10   1   2   3   4   5   6   7   8   9
  9  10   1   2   3   4   5   6   7   8
  8   9  10   1   2   3   4   5   6   7
  7   8   9  10   1   2   3   4   5   6
  6   7   8   9  10   1   2   3   4   5
  5   6   7   8   9  10   1   2   3   4
  4   5   6   7   8   9  10   1   2   3
  3   4   5   6   7   8   9  10   1   2
  2   3   4   5   6   7   8   9  10   1

which I think is what you want?

4 Likes
#4

Hi

using ToeplitzMatrices
r=zeros(1,5)
r[1:2]=[2.0,-1.0]
Toeplitz(r,r)

ERROR: MethodError: no method matching Toeplitz(::Array{Float64,2}, ::Array{Float64,2})

However, in Matlab:

 r=zeros(1,5);
r(1:2)=[2,-1];
T=toeplitz(r);
T =

     2    -1     0     0     0
    -1     2    -1     0     0
     0    -1     2    -1     0
     0     0    -1     2    -1
     0     0     0    -1     2

how can we solve this bug in Julia?

#5

This is not what would normally qualify as a bug - Julia is a different language from MATLAB, so it is expected that you cannot simply run the same code and expect it to work.

A few points on your question:

  • what is the uppercase Toeplitz you are calling? The function you defined in your previous post is lowercase, so is this something different?

  • why are you calling this function with two arguments (r, r) instead of one as before (and indeed as you are doing in MATLAB?

  • note that a one dimensional vector and a two dimensional array with a singleton dimension are not the same thing in Julia - if you want to pass a vector to your function construct it with zeros(5)

1 Like
#6

Hi,

sure, it is hard to say that called a bug in Julia!

to your questions:

  1. “using ToeplitzMatrices” using ToeplitzMatrices.jl

  2. Toeplitz(r,r) due to ToeplitzMatrices.jl

  3. I have modified the lastest question.

#7

In your code, r should be a vector, not a 2d array.

using ToeplitzMatrices
r=zeros(5)
r[1:2]=[2.0,-1.0]
Toeplitz(r,r)
#8

On the phone so can’t confirm but from looking at the repo it seems that indeed Toeplitz expects a 'Vector, not a two dimensionalArray`

#9

As others have suggested, you have to input a vector not a matrix to Toeplitz, this should work:

julia> r = [2; -1; zeros(3)]
5-element Array{Float64,1}:
  2.0
 -1.0
  0.0
  0.0
  0.0

julia> Toeplitz(r,r)
5×5 Toeplitz{Float64,Complex{Float64}}:
  2.0  -1.0   0.0   0.0   0.0
 -1.0   2.0  -1.0   0.0   0.0
  0.0  -1.0   2.0  -1.0   0.0
  0.0   0.0  -1.0   2.0  -1.0
  0.0   0.0   0.0  -1.0   2.0
#10

How can I change it to function? I want to write function with it (n x n), but when I call it in Julia I want to write n-number. So, I don’t want create 5x5, just n x n.

#11

Do you mean like this?

function toeplitz(n)
    r = [2; -1; zeros(n-2)]
    Toeplitz(r, r)
end
1 Like
#12

something like this, but how can I call it in Julia to create matrix 5x5, or 9x9, etc ?

#13

I may be missing some wrinkle that makes this question non-obvious, but what about

toeplitz(5)
toeplitz(9)

?

#14

Unfortunately it gave me an error :frowning:
I don’t have access now, so I’ll check later what was about it.

#15

Just to expand on Tamas_Papp’s answer:

julia> using ToeplitzMatrices

julia> function toeplitz(n)
           r = [2; -1; zeros(n-2)]
           Toeplitz(r, r)
       end
toeplitz (generic function with 1 method)

julia> toeplitz(5)
5×5 Toeplitz{Float64,Complex{Float64}}:
  2.0  -1.0   0.0   0.0   0.0
 -1.0   2.0  -1.0   0.0   0.0
  0.0  -1.0   2.0  -1.0   0.0
  0.0   0.0  -1.0   2.0  -1.0
  0.0   0.0   0.0  -1.0   2.0
1 Like
#16
toeplitz(5)

It is a command in Matlab not in Julia

#17

Nope, this is Julia code.

Generally, please do try to pay some attention to the replies to questions. People invest time and effort to help others, and seeing it completely ignored is kind of demotivating, and just injects noise into the discussion in the best case.

7 Likes
#18

Ok, I know where I made mistake.
Just to clarify, and just wanna to be sure: the first toeplitz(n) is name of my function with n-variable, and Toeplitz(r,r) is like assigned function on which I can create my function and it comes from ToeplitzMatrices, isn’t it ?

P.S. Sorry, probably there is a lot of stupid questions, but I just have some basics of Python, I don’t know R or Matlab and Julia is also like s-f for me :smiley:

#19

No worries. You are right: toeplitz(n) is the function we wrote and it uses the function Toeplitz(r, r) which comes from the package ToeplitzMatrices. Most probably my choice for the name of the function was confusing.

2 Likes