Element-wise operations
The built-in metaoperators `~W<op>`, `~S<op>` and `~RS<op>` are defined for arbitrary nested arrays.
1
var m1 = [[3,1,4],[1,5,9]]
2
var m2 = [[2,7,1],[8,2,2]]
3
4
say ":: Matrix-matrix operations"
5
say (m1 ~W+ m2)
6
say (m1 ~W- m2)
7
say (m1 ~W* m2)
8
say (m1 ~W/ m2)
9
say (m1 ~W// m2)
10
say (m1 ~W** m2)
11
say (m1 ~W% m2)
12
13
say "\n:: Matrix-scalar operations"
14
say (m1 ~S+ 42)
15
say (m1 ~S- 42)
16
say (m1 ~S/ 42)
17
say (m1 ~S** 10)
18
# ...
19
20
say "\n:: Scalar-matrix operations"
21
say (m1 ~RS+ 42)
22
say (m1 ~RS- 42)
23
say (m1 ~RS/ 42)
24
say (m1 ~RS** 10)
25
# ...
Copied!

Output:

1
:: Matrix-matrix operations
2
[[5, 8, 5], [9, 7, 11]]
3
[[1, -6, 3], [-7, 3, 7]]
4
[[6, 7, 4], [8, 10, 18]]
5
[[3/2, 1/7, 4], [1/8, 5/2, 9/2]]
6
[[1, 0, 4], [0, 2, 4]]
7
[[9, 1, 4], [1, 25, 81]]
8
[[1, 1, 0], [1, 1, 1]]
9
10
:: Matrix-scalar operations
11
[[45, 43, 46], [43, 47, 51]]
12
[[-39, -41, -38], [-41, -37, -33]]
13
[[1/14, 1/42, 2/21], [1/42, 5/42, 3/14]]
14
[[59049, 1, 1048576], [1, 9765625, 3486784401]]
15
16
:: Scalar-matrix operations
17
[[45, 43, 46], [43, 47, 51]]
18
[[39, 41, 38], [41, 37, 33]]
19
[[14, 42, 21/2], [42, 42/5, 14/3]]
20
[[1000, 10, 10000], [10, 100000, 1000000000]]
Copied!
Last modified 1yr ago
Copy link
Contents
Output: