interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / determinant-and-permanent.wiki

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