With ToeplitzMatrix
we can create a Toeplitz matrix from a list of values. I have a question going in the opposite direction, namely, how to efficiently check if a numerical matrix possesses this property.
Alternatives faster than OP's ToeplitzMatrixQ
:
ClearAll[toeplitzQ1, toeplitzQ2, toeplitzQ3]
toeplitzQ1 = # == ToeplitzMatrix[#[[All, 1]], #[[1]]] &;
toeplitzQ2[m_?SquareMatrixQ] := AllTrue[Diagonal[m, #] & /@
Range[-# + 1, # - 1]&[Length@m], Statistics`Library`ConstantVectorQ]
toeplitzQ3[m_?SquareMatrixQ] := Max[Length[Union[Diagonal[m, #]]] & /@
Range[-# + 1, # - 1] &[Length @ m]] == 1;
Examples:
tm1 = ToeplitzMatrix[{a, b, c, d}, {a, x, y, z}];
RepeatedTiming[#[tm1]] & /@ {toeplitzQ1, toeplitzQ2, toeplitzQ3, ToeplitzMatrixQ}
{{4.6*10^-6, True}, {0.00002, True}, {0.000026, True}, {0.00012, True}}
tm2 = ToeplitzMatrix[15];
RepeatedTiming[#[tm2]] & /@ {toeplitzQ1, toeplitzQ2, toeplitzQ3, ToeplitzMatrixQ}
{{3.2*10^-6, True}, {0.000043, True}, {0.000072, True}, {0.00037, True}}
tm3 = ToeplitzMatrix[10^3];
RepeatedTiming[#[tm3]] & /@ {toeplitzQ1, toeplitzQ2, toeplitzQ3, ToeplitzMatrixQ}
{{0.0045, True}, {0.013, True}, {0.012, True}, {32., True}}
That was a simple question. Anyway, providing an answer, maybe someone will find it useful
ToeplitzMatrixQ[A_] := Module[{c, r, a},
c = A[[All, 1]]; r = A[[1, All]];
a = ToeplitzMatrix[c, r];
Norm[a - A] == 0
]