omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / kronecker-product / kronecker-product.om
| 1 | note Rosetta Code task "Kronecker product": https://rosettacode.org/wiki/Kronecker_product |
| 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 tdata "1 2 3 4 0 5 6 7 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 1 1 1 1 1" |
| 8 | global vals long 32 |
| 9 | global ka long 16 |
| 10 | global kb long 16 |
| 11 | global dims ulong 4 |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow |
| 39 | fn emiti n long to void |
| 40 | if lt n 0 |
| 41 | emit minus |
| 42 | emitu add bitcast ulong not n 1 |
| 43 | else |
| 44 | emitu ulong n |
| 45 | end |
| 46 | end |
| 47 | note emitwi writes a signed n right-aligned in w columns (w at most 20) |
| 48 | fn emitwi n long w ulong to void |
| 49 | d = new uchar 21 32 |
| 50 | i = ulong 21 |
| 51 | v = n |
| 52 | if lt n 0 |
| 53 | v = neg n |
| 54 | end |
| 55 | while true bound 20 |
| 56 | i = sub i 1 |
| 57 | put d i uchar add long 48 rem v 10 |
| 58 | v = div v 10 |
| 59 | if eq v 0 |
| 60 | break |
| 61 | end |
| 62 | end |
| 63 | if lt n 0 |
| 64 | i = sub i 1 |
| 65 | put d i 45 |
| 66 | end |
| 67 | if gt w sub 21 i |
| 68 | i = sub 21 w |
| 69 | end |
| 70 | emit part d i sub 21 i |
| 71 | end |
| 72 | note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and |
| 73 | note returns how many; one pass over s, so the loop is bounded by len s |
| 74 | fn parse s view uchar * to ulong |
| 75 | n = ulong 0 |
| 76 | v = long 0 |
| 77 | sg = long 1 |
| 78 | dig = false |
| 79 | for i ulong len s |
| 80 | c = get s i |
| 81 | if eq c 45 |
| 82 | sg = long -1 |
| 83 | else |
| 84 | if and ge c 48 le c 57 |
| 85 | v = add mul v 10 long sub c 48 |
| 86 | dig = true |
| 87 | else |
| 88 | if dig |
| 89 | put vals n mul sg v |
| 90 | n = add n 1 |
| 91 | end |
| 92 | v = long 0 |
| 93 | sg = long 1 |
| 94 | dig = false |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | if dig |
| 99 | put vals n mul sg v |
| 100 | n = add n 1 |
| 101 | end |
| 102 | ret n |
| 103 | end |
| 104 | note the matrices come from tdata: 2x2 (a), 2x2 (b), then 3x3 (a), 3x4 (b); dims holds ra ca rb cb |
| 105 | note entry (i, j) of a (x) b is a[i / rb][j / cb] * b[i mod rb][j mod cb] |
| 106 | fn entry i ulong j ulong to long |
| 107 | rb = get dims 2 |
| 108 | cb = get dims 3 |
| 109 | x = get ka add mul div i rb get dims 1 div j cb |
| 110 | y = get kb add mul rem i rb cb rem j cb |
| 111 | ret mul x y |
| 112 | end |
| 113 | fn kron to void |
| 114 | rows = mul get dims 0 get dims 2 |
| 115 | cols = mul get dims 1 get dims 3 |
| 116 | for i ulong rows |
| 117 | for j ulong cols |
| 118 | emitwi entry i j 3 |
| 119 | end |
| 120 | emit nl |
| 121 | end |
| 122 | end |
| 123 | note load copies na entries of vals from offset o into ka, then nb entries into kb |
| 124 | fn load o ulong na ulong nb ulong to void |
| 125 | for i ulong na |
| 126 | put ka i get vals add o i |
| 127 | end |
| 128 | for i ulong nb |
| 129 | put kb i get vals add add o na i |
| 130 | end |
| 131 | end |
| 132 | fn main to int |
| 133 | n = parse tdata |
| 134 | put dims 0 2 |
| 135 | put dims 1 2 |
| 136 | put dims 2 2 |
| 137 | put dims 3 2 |
| 138 | load 0 4 4 |
| 139 | kron |
| 140 | emit nl |
| 141 | put dims 0 3 |
| 142 | put dims 1 3 |
| 143 | put dims 2 3 |
| 144 | put dims 3 4 |
| 145 | load 8 9 12 |
| 146 | kron |
| 147 | ret 0 |
| 148 | end |