Write a program that will determine if a given matrix represents a quandle. A quandle is a set equipped with a single (non-commutative, non-associative) operation \$◃\$ which obeys the following axioms:
- The operation is closed, meaning that \$a◃b = c\$ is always an element of the set if \$a\$ and \$b\$ are elements of the set.
- The operation is right-self-distributive: \$(a◃b)◃c = (a◃c)◃(b◃c)\$.
- The operation is right-divisible: For any chosen pair of \$a\$ and \$b\$, there is a single unique \$c\$ such that \$c◃a = b\$
- The operation is idempotent: \$a◃a = a\$
A finite quandle can be represented as a square matrix. Below is an example of an order-\$5\$ quandle (source).
0 0 1 1 1
1 1 0 0 0
3 4 2 4 3
4 2 4 3 2
2 3 3 2 4
The value located at the \$n\$-th row and \$m\$-th column (0-indexed) is the value of \$n◃m\$. For example, in this quandle, \$4◃1 = 3\$. Some of the quandle properties are easy to see from this matrix:
- It is closed because only values \$0-4\$ appear in this \$5\times5\$ matrix.
- It is idempotent because the matrix diagonal is \$0, 1, 2, 3, 4\$
- It is right-divisible because no column contains any duplicate values. (The rows can, and usually will.)
The property of right-self-distributivity is harder to test. There might be a shortcut, but the simplest method is to iterate over each possible combination of three indexes to verify that m[m[a][b]][c] = m[m[a][c]][m[b][c]]
.
Input
Input will be the list of rows of a square matrix, using either 0-indexing or 1-index (your choice). Each entry will be a single digit number from 0
to 8
(or 1
through 9
). I will be flexible on the input format. Some acceptable formats include:
- Your language's most natural formatting for matrices or lists, such as
[[0 0 0][2 1 1][1 2 2]]
or(0,0,0,2,1,1,1,2,2)
. - The list of values delimited by whitespace, newlines, commas, etc.
- A single string consisting of all values concatenated together, such as
000211122
.
You are also allowed to take the transpose of the matrix as input (swapping rows with columns). Just be sure to state this in your answer.
Output
A single truthy/falsey value indicating the matrix's status as a quandle.
Examples of quandles
0
0 0
1 1
0 0 0
2 1 1
1 2 2
0 0 1 1
1 1 0 0
3 3 2 2
2 2 3 3
0 3 4 1 2
2 1 0 4 3
3 4 2 0 1
4 2 1 3 0
1 0 3 2 4
Examples of non-quandles
not closed
1
0 0 0
2 1 1
1 9 2
not right-self-distributive
0 0 1 0
1 1 0 1
2 3 2 2
3 2 3 3
(3◃1)◃2 = 2◃2 = 2
(3◃2)◃(1◃2) = 3◃0 = 3
not right-divisible
0 2 3 4 1
0 1 2 3 4
3 4 2 2 2
3 3 3 3 3
4 1 1 1 4
0 1 2 3
3 1 2 0
3 1 2 3
0 1 2 3
not idempotent
1 1 1 1
3 3 3 3
2 2 2 2
0 0 0 0
2 1 0 4 3
3 4 2 0 1
4 2 1 3 0
1 0 3 2 4
0 3 4 1 2