omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / matrix-multiplication.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text ttimes "times\n" |
| 10 | text tequals "equals\n" |
| 11 | global mat long 27 |
| 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 emitwi writes a signed n right-aligned in w columns (w at most 20) |
| 39 | fn emitwi n long w ulong to void |
| 40 | d = new uchar 21 32 |
| 41 | i = ulong 21 |
| 42 | v = n |
| 43 | if lt n 0 |
| 44 | v = neg n |
| 45 | end |
| 46 | while true bound 20 |
| 47 | i = sub i 1 |
| 48 | put d i uchar add long 48 rem v 10 |
| 49 | v = div v 10 |
| 50 | if eq v 0 |
| 51 | break |
| 52 | end |
| 53 | end |
| 54 | if lt n 0 |
| 55 | i = sub i 1 |
| 56 | put d i 45 |
| 57 | end |
| 58 | if gt w sub 21 i |
| 59 | i = sub 21 w |
| 60 | end |
| 61 | emit part d i sub 21 i |
| 62 | end |
| 63 | note mat holds the three matrices row by row: a at offset 0, b at 9 and their product at 18 |
| 64 | note showm writes the r x c matrix at offset o of mat, each entry right-aligned in w columns |
| 65 | fn showm o ulong r ulong c ulong w ulong to void |
| 66 | for i ulong r |
| 67 | for j ulong c |
| 68 | emitwi get mat add o add mul i c j w |
| 69 | end |
| 70 | emit nl |
| 71 | end |
| 72 | end |
| 73 | note dot is row i of the m-column matrix a times column j of the p-column matrix b |
| 74 | fn dot i ulong j ulong m ulong p ulong to long |
| 75 | s = long 0 |
| 76 | for k ulong m |
| 77 | x = get mat add mul i m k |
| 78 | y = get mat add 9 add mul k p j |
| 79 | s = add s mul x y |
| 80 | end |
| 81 | ret s |
| 82 | end |
| 83 | note matmul computes the n x p product of the n x m matrix a and the m x p matrix b |
| 84 | fn matmul n ulong m ulong p ulong to void |
| 85 | for i ulong n |
| 86 | for j ulong p |
| 87 | put mat add 18 add mul i p j dot i j m p |
| 88 | end |
| 89 | end |
| 90 | end |
| 91 | fn main to int |
| 92 | for i ulong 6 |
| 93 | put mat i long add i 1 |
| 94 | put mat add 9 i long add i 7 |
| 95 | end |
| 96 | showm 0 2 3 5 |
| 97 | emit ttimes |
| 98 | showm 9 3 2 5 |
| 99 | emit tequals |
| 100 | matmul 2 3 2 |
| 101 | showm 18 2 2 5 |
| 102 | emit nl |
| 103 | for i ulong 9 |
| 104 | put mat i sub long mul i 3 10 |
| 105 | put mat add 9 i sub 4 long rem mul i 5 7 |
| 106 | end |
| 107 | showm 0 3 3 5 |
| 108 | emit ttimes |
| 109 | showm 9 3 3 5 |
| 110 | emit tequals |
| 111 | matmul 3 3 3 |
| 112 | showm 18 3 3 5 |
| 113 | ret 0 |
| 114 | end |
| 115 | </syntaxhighlight> |
| 116 | {{out}} |
| 117 | <pre> |
| 118 | 1 2 3 |
| 119 | 4 5 6 |
| 120 | times |
| 121 | 7 8 |
| 122 | 9 10 |
| 123 | 11 12 |
| 124 | equals |
| 125 | 58 64 |
| 126 | 139 154 |
| 127 | |
| 128 | -10 -7 -4 |
| 129 | -1 2 5 |
| 130 | 8 11 14 |
| 131 | times |
| 132 | 4 -1 1 |
| 133 | 3 -2 0 |
| 134 | 2 4 -1 |
| 135 | equals |
| 136 | -69 8 -6 |
| 137 | 12 17 -6 |
| 138 | 93 26 -6 |
| 139 | </pre> |