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