Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/kronecker-product.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / kronecker-product.wiki`
168 lines ยท 3341 bytes Raw Blame History
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 (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text 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"
10 global vals long 32
11 global ka long 16
12 global kb long 16
13 global dims ulong 4
14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15 fn emit a view uchar * to void
16 o = ulong 0
17 while lt o len a bound 4096
18 k = write 1 a o sub len a o
19 if le k 0
20 exit 1
21 end
22 o = add o ulong k
23 end
24 end
25 note emitu writes n in decimal; 20 digits hold every ulong
26 fn emitu n ulong to void
27 d = new uchar 20 0
28 i = ulong 20
29 v = n
30 while true bound 20
31 i = sub i 1
32 put d i uchar add rem v 10 48
33 v = div v 10
34 if eq v 0
35 break
36 end
37 end
38 emit part d i sub 20 i
39 end
40 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41 fn emiti n long to void
42 if lt n 0
43 emit minus
44 emitu add bitcast ulong not n 1
45 else
46 emitu ulong n
47 end
48 end
49 note emitwi writes a signed n right-aligned in w columns (w at most 20)
50 fn emitwi n long w ulong to void
51 d = new uchar 21 32
52 i = ulong 21
53 v = n
54 if lt n 0
55 v = neg n
56 end
57 while true bound 20
58 i = sub i 1
59 put d i uchar add long 48 rem v 10
60 v = div v 10
61 if eq v 0
62 break
63 end
64 end
65 if lt n 0
66 i = sub i 1
67 put d i 45
68 end
69 if gt w sub 21 i
70 i = sub 21 w
71 end
72 emit part d i sub 21 i
73 end
74 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
75 note returns how many; one pass over s, so the loop is bounded by len s
76 fn parse s view uchar * to ulong
77 n = ulong 0
78 v = long 0
79 sg = long 1
80 dig = false
81 for i ulong len s
82 c = get s i
83 if eq c 45
84 sg = long -1
85 else
86 if and ge c 48 le c 57
87 v = add mul v 10 long sub c 48
88 dig = true
89 else
90 if dig
91 put vals n mul sg v
92 n = add n 1
93 end
94 v = long 0
95 sg = long 1
96 dig = false
97 end
98 end
99 end
100 if dig
101 put vals n mul sg v
102 n = add n 1
103 end
104 ret n
105 end
106 note the matrices come from tdata: 2x2 (a), 2x2 (b), then 3x3 (a), 3x4 (b); dims holds ra ca rb cb
107 note entry (i, j) of a (x) b is a[i / rb][j / cb] * b[i mod rb][j mod cb]
108 fn entry i ulong j ulong to long
109 rb = get dims 2
110 cb = get dims 3
111 x = get ka add mul div i rb get dims 1 div j cb
112 y = get kb add mul rem i rb cb rem j cb
113 ret mul x y
114 end
115 fn kron to void
116 rows = mul get dims 0 get dims 2
117 cols = mul get dims 1 get dims 3
118 for i ulong rows
119 for j ulong cols
120 emitwi entry i j 3
121 end
122 emit nl
123 end
124 end
125 note load copies na entries of vals from offset o into ka, then nb entries into kb
126 fn load o ulong na ulong nb ulong to void
127 for i ulong na
128 put ka i get vals add o i
129 end
130 for i ulong nb
131 put kb i get vals add add o na i
132 end
133 end
134 fn main to int
135 n = parse tdata
136 put dims 0 2
137 put dims 1 2
138 put dims 2 2
139 put dims 3 2
140 load 0 4 4
141 kron
142 emit nl
143 put dims 0 3
144 put dims 1 3
145 put dims 2 3
146 put dims 3 4
147 load 8 9 12
148 kron
149 ret 0
150 end
151
152 {{out}}
153
154 0 5 0 10
155 6 7 12 14
156 0 15 0 20
157 18 21 24 28
158
159 0 0 0 0 1 1 1 1 0 0 0 0
160 0 0 0 0 1 0 0 1 0 0 0 0
161 0 0 0 0 1 1 1 1 0 0 0 0
162 1 1 1 1 1 1 1 1 1 1 1 1
163 1 0 0 1 1 0 0 1 1 0 0 1
164 1 1 1 1 1 1 1 1 1 1 1 1
165 0 0 0 0 1 1 1 1 0 0 0 0
166 0 0 0 0 1 0 0 1 0 0 0 0
167 0 0 0 0 1 1 1 1 0 0 0 0
168