# Toeplitz Matrix (diagonal-constant matrix)

**URL:** <https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969>\
**Category:** Newbie\
**Tags:** root, math\
**Created:** [May 13, 2022, 10:22am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969 "2022-05-13T10:22:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [May 13, 2022, 10:22am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/1 "2022-05-13T10:22:09Z")

</div>

Hello all,

I want to create a Toeplitz matrix which I need to invert. Do anyone know a good way to create one? The way I have take way too many steps.

```auto
Int_t n = 3; // n x n matrix
Double_t a[] = {1,2,3};
TVectorD aa;
aa.Use(n,a); 

TMatrixD A(n,n);
TMatrixDRow(A,0) = aa;

//here is a for to reallocate the elements and fill the matrix 
for(Int_t i = 1; i<n; i++){ // this for to fill the matrix
  Double_t temp = aa[0];
  aa[0] = aa[n-1];
  for(Int_t j = 1; j<n;j++){
    Double_t temp2 = aa[j];
    aa[j] = temp; 
    temp = temp2; 
  }
  TMatrixDRow(A,i) = aa;
 }
for(Int_t i = 0; i<n; i++){
  for(Int_t j = 0; j<n; j++){
    cout << A[i][j] << " ";
  }
  cout << "\n";
}

```

OBS.: The application is for Wiener Filter 🙂

Cheers,  
Henrique

* * *

_ROOT Version:_ 6.26/02  
_Platform:_ Ubuntu 21.10 impish (x86-64)  
_Compiler:_ (gcc/g++ 11.2.0)

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 13, 2022, 11:02am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/2 "2022-05-13T11:02:47Z")

</div>

> [@hvsouza](#):
>
> I want to create a Toeplitz matrix

I guess you should just follow the [definition of such matrix](https://en.wikipedia.org/wiki/Toeplitz_matrix).

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [May 13, 2022, 11:12am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/3 "2022-05-13T11:12:46Z")

</div>

Sorry, I didn’t follow you. I am using the definition of the matrix there. It is just that root does not accept negative values, so I though it would be easy this way.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 13, 2022, 11:13am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/4 "2022-05-13T11:13:57Z")

</div>

Ak ok… sorry. @moneta oneta may have an idea in that case.

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [May 14, 2022, 1:40am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/5 "2022-05-14T01:40:37Z")

</div>

Hi Henrique,

Not sure what you expect because what you wrote is already quite compact.  
The swapping could be done in one line :

```auto
void toeplitz()
{
  Int_t n = 3; // n x n matrix
  Double_t a[] = {1,-2,3};
  TVectorD aa;
  aa.Use(n,a);

  TMatrixD A(n,n);
  TMatrixDRow(A,0) = aa;

  //here is a for to reallocate the elements and fill the matrix 
  for(Int_t i = 1; i<n; i++){ // this for to fill the matrix
    Double_t temp = aa[0];
    aa[0] = aa[n-1];
    for(Int_t j = 1; j<n;j++)
      std::swap(aa[j],temp);
    TMatrixDRow(A,i) = aa;
  }

  A.Print();
}

```

or are you looking for something more “elegant” ? It could be done with TMatrixDDiag and TMatrixDSub but it will not be shorter .

-Eddy

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [May 15, 2022, 10:55pm UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/6 "2022-05-15T22:55:48Z")

</div>

Hi @Eddy_Offermann,

Thanks a lot for the reply. I liked the `std::swap` method there.  
I was wondering if there was any function member of TMatrixD that could do it 🙂  
I’ve stepped into a solution for M x M matrices:

```auto
Int_t M = 3; // M x M matrix
Double_t a[] = {1,-2,3};
TMatrixD A(M,M);

for (int i=0; i<M; i++)
  {
  	for (int j=0; j<M; j++)
  	{
  		A[i][j] = aa[abs(i-j)];
  	}
  }

```

Thanks again.

Cheers,  
Henrique

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [May 16, 2022, 2:03am UTC](https://root-forum.cern.ch/t/toeplitz-matrix-diagonal-constant-matrix/49969/7 "2022-05-16T02:03:05Z")

</div>

Just for the record, your code examples do create Toeplitz matrices but very specific ones, where the last one is symmetric and the first one permutates the off-diagonal values.

Any Toeplitz can be written by the following code (Notice the upper and lower boundary of the TVectorD:

```auto
 Int_t M = 3; // M x M matrix
 TMatrixD A(M,M);
 Double_t a[] = {1,2,3,4,5};

 TVectorD aa(-M+1,M-1);
 aa.Use(-M+1,M-1,a);

 for (int i=0; i<M; i++)
 {
   for (int j=0; j<M; j++)
   {
     A[i][j] = aa[j-i];
   }
 }

```
