Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/determinant-and-permanent/oracle.py # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / determinant-and-permanent / oracle.py` 40 lines ยท 1583 bytes Raw Blame History 1 # Independent reference for "Determinant and permanent" (Python 3, standard library only). 2 # expected.txt is this program's stdout; omc never produced it. 3 # Cross-check, WolframLanguageEvaluator: ms = {the four matrices}; {Map[Det, ms], Map[Permanent, ms]} 4 # -> {{-2, -360, 0, 0}, {10, 900, 29556, `6778800`}} 5 from fractions import Fraction 6 def det(m): 7 # Gaussian elimination over exact fractions 8 a = [[Fraction(v) for v in row] for row in m] 9 n, d = len(a), Fraction(1) 10 for c in range(n): 11 p = next((r for r in range(c, n) if a[r][c] != 0), None) 12 if p is None: 13 return 0 14 if p != c: 15 a[c], a[p] = a[p], a[c] 16 d = -d 17 d *= a[c][c] 18 for r in range(c + 1, n): 19 f = a[r][c] / a[c][c] 20 a[r] = [x - f * y for x, y in zip(a[r], a[c])] 21 return int(d) 22 def per(m): 23 # Ryser's formula: sum over column subsets S of (-1)^(n-|S|) * prod over rows of the sum over S 24 n, total = len(m), 0 25 for s in range(1, 1 << n): 26 cols = [j for j in range(n) if s >> j & 1] 27 prod = 1 28 for row in m: 29 prod *= sum(row[j] for j in cols) 30 total += (-1) ** (n - len(cols)) * prod 31 return total 32 cases = [[[1, 2], [3, 4]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], 33 [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10], [10, 11, 12, 13]], 34 [[r * 5 + c for c in range(5)] for r in range(5)]] 35 for m in cases: 36 print("matrix:") 37 for row in m: 38 print("".join(f"{v:4}" for v in row)) 39 print("determinant:", det(m)) 40 print("permanent:", per(m))