// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements
function isToeplitz($matrix) {
$rows = count($matrix);
$cols = count($matrix[0]);
for ($i = 1; $i < $rows; $i++) {
for ($j = 1; $j < $cols; $j++) {
echo $i . " " . $j . " - " . $matrix[$i][$j] . " : " . $matrix[$i - 1][$j - 1] . "\n";
if ($matrix[$i][$j] != $matrix[$i - 1][$j - 1]) {
return false;
}
}
echo "-----------","\n";
}
return true;
}
$matrix = array(
array(2, 7, 9, 8),
array(4, 2, 7, 9),
array(3, 4, 2, 7),
array(0, 3, 4, 2),
array(6, 0, 3, 4)
);
if (isToeplitz($matrix)) {
echo "Matrix is a Toeplitz";
}
else {
echo "Matrix is not a Toeplitz";
}
/*
run:
1 1 - 2 : 2
1 2 - 7 : 7
1 3 - 9 : 9
-----------
2 1 - 4 : 4
2 2 - 2 : 2
2 3 - 7 : 7
-----------
3 1 - 3 : 3
3 2 - 4 : 4
3 3 - 2 : 2
-----------
4 1 - 0 : 0
4 2 - 3 : 3
4 3 - 4 : 4
-----------
Matrix is a Toeplitz
*/