interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / determinant-and-permanent / determinant-and-permanent.om

207 lines · 3747 bytes Raw Blame History
1note Rosetta Code task "Determinant and permanent": https://rosettacode.org/wiki/Determinant_and_permanent
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tm "matrix:\n"
8text tdet "determinant: "
9text tper "permanent: "
10text tdata "1 2 3 4 2 9 4 7 5 3 6 1 8 1 2 3 4 4 5 6 7 7 8 9 10 10 11 12 13"
11global vals long 64
12global mat long 25
13global perm ulong 8
14global res long 2
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
42fn emiti n long to void
43if lt n 0
44emit minus
45emitu add bitcast ulong not n 1
46else
47emitu ulong n
48end
49end
50note emitwi writes a signed n right-aligned in w columns (w at most 20)
51fn emitwi n long w ulong to void
52d = new uchar 21 32
53i = ulong 21
54v = n
55if lt n 0
56v = neg n
57end
58while true bound 20
59i = sub i 1
60put d i uchar add long 48 rem v 10
61v = div v 10
62if eq v 0
63break
64end
65end
66if lt n 0
67i = sub i 1
68put d i 45
69end
70if gt w sub 21 i
71i = sub 21 w
72end
73emit part d i sub 21 i
74end
75note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
76note returns how many; one pass over s, so the loop is bounded by len s
77fn parse s view uchar * to ulong
78n = ulong 0
79v = long 0
80sg = long 1
81dig = false
82for i ulong len s
83c = get s i
84if eq c 45
85sg = long -1
86else
87if and ge c 48 le c 57
88v = add mul v 10 long sub c 48
89dig = true
90else
91if dig
92put vals n mul sg v
93n = add n 1
94end
95v = long 0
96sg = long 1
97dig = false
98end
99end
100end
101if dig
102put vals n mul sg v
103n = add n 1
104end
105ret n
106end
107note both are sums over all n! permutations p of the products m[0][p0] * .. * m[n-1][p(n-1)]; the
108note determinant gives each the sign of p (its inversion count parity), the permanent adds them all
109note nextp: the next permutation in lexicographic order, false after the last
110fn nextp n ulong to bool
111if lt n 2
112ret false
113end
114i = sub n 1
115while gt i 0 bound 8
116if lt get perm sub i 1 get perm i
117break
118end
119i = sub i 1
120end
121if eq i 0
122ret false
123end
124j = sub n 1
125while le get perm j get perm sub i 1 bound 8
126j = sub j 1
127end
128t = get perm sub i 1
129put perm sub i 1 get perm j
130put perm j t
131k = sub n 1
132while lt i k bound 8
133t2 = get perm i
134put perm i get perm k
135put perm k t2
136i = add i 1
137k = sub k 1
138end
139ret true
140end
141fn odd n ulong to bool
142c = ulong 0
143for i ulong n
144for j ulong n
145if and lt i j gt get perm i get perm j
146c = add c 1
147end
148end
149end
150ret eq rem c 2 1
151end
152fn term n ulong to long
153p = long 1
154for i ulong n
155p = mul p get mat add mul i n get perm i
156end
157ret p
158end
159note detper fills res[0] with the determinant and res[1] with the permanent; 5! = 120 bounds the loop
160fn detper n ulong to void
161for i ulong n
162put perm i i
163end
164put res 0 0
165put res 1 0
166while true bound 120
167t = term n
168put res 1 add get res 1 t
169if odd n
170put res 0 sub get res 0 t
171else
172put res 0 add get res 0 t
173end
174if not nextp n
175break
176end
177end
178end
179fn case o ulong n ulong to void
180emit tm
181for i ulong n
182for j ulong n
183put mat add mul i n j get vals add o add mul i n j
184emitwi get mat add mul i n j 4
185end
186emit nl
187end
188detper n
189emit tdet
190emiti get res 0
191emit nl
192emit tper
193emiti get res 1
194emit nl
195end
196note the 5 x 5 case is 0 .. 24, filled in place rather than parsed
197fn main to int
198n = parse tdata
199case 0 2
200case 4 3
201case 13 4
202for i ulong 25
203put vals add 29 i long i
204end
205case 29 5
206ret 0
207end