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

213 matrix:

214 1 2

215 3 4

216 determinant: -2

217 permanent: 10

218 matrix:

219 2 9 4

220 7 5 3

221 6 1 8

222 determinant: -360

223 permanent: 900

224 matrix:

225 1 2 3 4

226 4 5 6 7

227 7 8 9 10

228 ...

229 matrix:

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

235 determinant: 0

236 permanent: `6778800`

237