omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / identity-matrix.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 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 10 | fn emit a view uchar * to void |
| 11 | o = ulong 0 |
| 12 | while lt o len a bound 4096 |
| 13 | k = write 1 a o sub len a o |
| 14 | if le k 0 |
| 15 | exit 1 |
| 16 | end |
| 17 | o = add o ulong k |
| 18 | end |
| 19 | end |
| 20 | note emitu writes n in decimal; 20 digits hold every ulong |
| 21 | fn emitu n ulong to void |
| 22 | d = new uchar 20 0 |
| 23 | i = ulong 20 |
| 24 | v = n |
| 25 | while true bound 20 |
| 26 | i = sub i 1 |
| 27 | put d i uchar add rem v 10 48 |
| 28 | v = div v 10 |
| 29 | if eq v 0 |
| 30 | break |
| 31 | end |
| 32 | end |
| 33 | emit part d i sub 20 i |
| 34 | end |
| 35 | note entry (i, j) of the n x n identity is 1 on the diagonal i = j and 0 elsewhere |
| 36 | fn ident n ulong to void |
| 37 | for i ulong n |
| 38 | for j ulong n |
| 39 | if gt j 0 |
| 40 | emit sp |
| 41 | end |
| 42 | if eq i j |
| 43 | emitu 1 |
| 44 | else |
| 45 | emitu 0 |
| 46 | end |
| 47 | end |
| 48 | emit nl |
| 49 | end |
| 50 | end |
| 51 | fn main to int |
| 52 | ident 1 |
| 53 | emit nl |
| 54 | ident 3 |
| 55 | emit nl |
| 56 | ident 6 |
| 57 | ret 0 |
| 58 | end |
| 59 | </syntaxhighlight> |
| 60 | {{out}} |
| 61 | <pre> |
| 62 | 1 |
| 63 | |
| 64 | 1 0 0 |
| 65 | 0 1 0 |
| 66 | 0 0 1 |
| 67 | |
| 68 | 1 0 0 0 0 0 |
| 69 | 0 1 0 0 0 0 |
| 70 | 0 0 1 0 0 0 |
| 71 | 0 0 0 1 0 0 |
| 72 | 0 0 0 0 1 0 |
| 73 | 0 0 0 0 0 1 |
| 74 | </pre> |