Una matrice di segni alternati è una matrice n
per n
matrice costituita dai numeri -1, 0, 1, in modo tale che:
- La somma di ogni riga e colonna è 1
- Le voci diverse da zero in ogni riga e colonna si alternano nel segno
Queste matrici generalizzano le matrici di permutazione e il numero di tali matrici per un dato n
era di interesse per qualche tempo. Si verificano naturalmente durante il metodo di condensazione di Dodgson per il calcolo dei determinanti della matrice (dal nome di Charles Dodgson, meglio noto come Lewis Carroll).
Ecco alcuni esempi di matrici di segni alternati 4 per 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
Ed ecco alcuni esempi di matrici 4 per 4 che non alternano matrici di segni:
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)
Il programma o funzione sarà data una n
da n
matrice ( n >= 1
) di -1S, 0 e 1 - uscita un valore truthy se la matrice è una matrice proposta alternato segno, altrimenti uscita un valore falsy.
Questo è code-golf , quindi l'obiettivo è ridurre al minimo il numero di byte utilizzati.
Casi test
I seguenti casi di test sono riportati in un formato elenco 2D simile a Python.
Truthy:
[[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,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