omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / matrix-multiplication / matrix-multiplication.om
| 1 | note Rosetta Code task "Matrix multiplication": https://rosettacode.org/wiki/Matrix_multiplication |
| 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 ttimes "times\n" |
| 8 | text tequals "equals\n" |
| 9 | global mat long 27 |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note emitwi writes a signed n right-aligned in w columns (w at most 20) |
| 37 | fn emitwi n long w ulong to void |
| 38 | d = new uchar 21 32 |
| 39 | i = ulong 21 |
| 40 | v = n |
| 41 | if lt n 0 |
| 42 | v = neg n |
| 43 | end |
| 44 | while true bound 20 |
| 45 | i = sub i 1 |
| 46 | put d i uchar add long 48 rem v 10 |
| 47 | v = div v 10 |
| 48 | if eq v 0 |
| 49 | break |
| 50 | end |
| 51 | end |
| 52 | if lt n 0 |
| 53 | i = sub i 1 |
| 54 | put d i 45 |
| 55 | end |
| 56 | if gt w sub 21 i |
| 57 | i = sub 21 w |
| 58 | end |
| 59 | emit part d i sub 21 i |
| 60 | end |
| 61 | note mat holds the three matrices row by row: a at offset 0, b at 9 and their product at 18 |
| 62 | note showm writes the r x c matrix at offset o of mat, each entry right-aligned in w columns |
| 63 | fn showm o ulong r ulong c ulong w ulong to void |
| 64 | for i ulong r |
| 65 | for j ulong c |
| 66 | emitwi get mat add o add mul i c j w |
| 67 | end |
| 68 | emit nl |
| 69 | end |
| 70 | end |
| 71 | note dot is row i of the m-column matrix a times column j of the p-column matrix b |
| 72 | fn dot i ulong j ulong m ulong p ulong to long |
| 73 | s = long 0 |
| 74 | for k ulong m |
| 75 | x = get mat add mul i m k |
| 76 | y = get mat add 9 add mul k p j |
| 77 | s = add s mul x y |
| 78 | end |
| 79 | ret s |
| 80 | end |
| 81 | note matmul computes the n x p product of the n x m matrix a and the m x p matrix b |
| 82 | fn matmul n ulong m ulong p ulong to void |
| 83 | for i ulong n |
| 84 | for j ulong p |
| 85 | put mat add 18 add mul i p j dot i j m p |
| 86 | end |
| 87 | end |
| 88 | end |
| 89 | fn main to int |
| 90 | for i ulong 6 |
| 91 | put mat i long add i 1 |
| 92 | put mat add 9 i long add i 7 |
| 93 | end |
| 94 | showm 0 2 3 5 |
| 95 | emit ttimes |
| 96 | showm 9 3 2 5 |
| 97 | emit tequals |
| 98 | matmul 2 3 2 |
| 99 | showm 18 2 2 5 |
| 100 | emit nl |
| 101 | for i ulong 9 |
| 102 | put mat i sub long mul i 3 10 |
| 103 | put mat add 9 i sub 4 long rem mul i 5 7 |
| 104 | end |
| 105 | showm 0 3 3 5 |
| 106 | emit ttimes |
| 107 | showm 9 3 3 5 |
| 108 | emit tequals |
| 109 | matmul 3 3 3 |
| 110 | showm 18 3 3 5 |
| 111 | ret 0 |
| 112 | end |