一个交替的符号矩阵是n
由n
组成的数字矩阵-1,0,1,使得:
- 每行和每一列的总和为1
- 每行和每列中的非零条目以符号交替
这些矩阵概括了置换矩阵,并且对于给定n
的时间,此类矩阵的数量令人关注。它们在计算矩阵行列式的Dodgson凝聚方法(以Charles Dodgson命名,更名为Lewis Carroll)中自然发生。
以下是4 x 4交替符号矩阵的一些示例:
0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0 0 1 -1 1 1 0 -1 1
1 0 0 0 0 1 -1 1 1 -1 1 0 0 1 0 0
0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0
以下是4 x 4矩阵的一些示例,这些矩阵不是交替的符号矩阵:
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 -1 (last row and last column don't add to 1)
0 0 0 1
1 0 0 0
-1 1 1 0
1 0 0 0 (third row does not alternate correctly)
您的程序或函数将由-1s,0s和1s 的n
by n
矩阵(n >= 1
)给出- 如果给定矩阵是交替符号矩阵,则输出真实值,否则输出伪值。
这是code-golf,因此目标是最大程度地减少使用的字节数。
测试用例
以下测试案例以类似Python的2D列表格式给出。
真相:
[[1]]
[[1,0],[0,1]]
[[0,1],[1,0]]
[[0,1,0],[0,0,1],[1,0,0]]
[[0,1,0],[1,-1,1],[0,1,0]]
[[0,1,0,0],[0,0,1,0],[1,0,0,0],[0,0,0,1]]
[[1,0,0,0],[0,0,1,0],[0,1,-1,1],[0,0,1,0]]
[[0,0,1,0],[0,1,-1,1],[1,-1,1,0],[0,1,0,0]]
[[0,0,1,0],[1,0,-1,1],[0,1,0,0],[0,0,1,0]]
[[0,0,1,0,0],[0,1,-1,1,0],[1,-1,1,0,0],[0,1,0,-1,1],[0,0,0,1,0]]
[[0,0,1,0,0,0,0,0],[1,0,-1,0,1,0,0,0],[0,0,0,1,-1,0,0,1],[0,0,1,-1,1,0,0,0],[0,0,0,0,0,0,1,0],[0,0,0,0,0,1,0,0],[0,1,-1,1,0,0,0,0],[0,0,1,0,0,0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