interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / matrix-multiplication.wiki

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