interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / matrix-transposition / matrix-transposition.om

91 lines · 1606 bytes Raw Blame History
1note Rosetta Code task "Matrix transposition": https://rosettacode.org/wiki/Matrix_transposition
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tt "transposed:\n"
8global ma long 15
9global mt long 15
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note emitwi writes a signed n right-aligned in w columns (w at most 20)
37fn emitwi n long w ulong to void
38d = new uchar 21 32
39i = ulong 21
40v = n
41if lt n 0
42v = neg n
43end
44while true bound 20
45i = sub i 1
46put d i uchar add long 48 rem v 10
47v = div v 10
48if eq v 0
49break
50end
51end
52if lt n 0
53i = sub i 1
54put d i 45
55end
56if gt w sub 21 i
57i = sub 21 w
58end
59emit part d i sub 21 i
60end
61note ma is a 3 x 5 matrix stored row by row; its transpose mt is 5 x 3, mt[j][i] = ma[i][j]
62fn showt r ulong c ulong to void
63for i ulong r
64for j ulong c
65emitwi get mt add mul i c j 4
66end
67emit nl
68end
69end
70fn showa r ulong c ulong to void
71for i ulong r
72for j ulong c
73emitwi get ma add mul i c j 4
74end
75emit nl
76end
77end
78fn main to int
79for i ulong 15
80put ma i sub long mul i i 50
81end
82showa 3 5
83for i ulong 3
84for j ulong 5
85put mt add mul j 3 i get ma add mul i 5 j
86end
87end
88emit tt
89showt 5 3
90ret 0
91end