Skip to content

Instantly share code, notes, and snippets.

@halfhope
Created April 26, 2022 02:30
Show Gist options
  • Save halfhope/8589f5f97f76e066480dcfc7c0ac88da to your computer and use it in GitHub Desktop.
Save halfhope/8589f5f97f76e066480dcfc7c0ac88da to your computer and use it in GitHub Desktop.
// generate sample of symmetry set (matrix)
$samlpes = [];
$items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
foreach ($items as $k => $v) {
foreach ($items as $k2 => $v2) {
$samples[$k][$k2] = abs($v - $v2);
}
}
// init symmetric set storage
function getMatrixUniqID($x, $y) {
if ($x >= $y) {
return ($x * ($x + 1) / 2) + $y;
} else {
return ($y * ($y + 1) / 2) + $x;
}
}
$items_count = count($samples);
$matrix_size = ($items_count * ($items_count + 1) / 2) + 1;
$computed = new \SplFixedArray($matrix_size);
// test
$x = 5;
$y = 6;
$xy = $matrix[getMatrixUniqID($x, $y)];
$yx = $matrix[getMatrixUniqID($y, $x)];
if ($xy == $yx) {
echo 'hit';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment