interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / kronecker-product / kronecker-product.om

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