Hello! I’ve been eyeing Julia for a while as a J/APL fan and really want to move some programming I would otherwise do in J into Julia, given they are both array-centric programming languages.
But one of the things that keeps me sticking to J is the ability to easily “broadcast” over arbitrary rank. As an example, if I have a 2 x 10 array a
:
a =. 2 10 $ 1 + i. 20
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
And a 10 x 10 array b
:
b =. 10 10 $ 100 + i. 100
100 101 102 103 104 105 106 107 108 109
110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129
130 131 132 133 134 135 136 137 138 139
140 141 142 143 144 145 146 147 148 149
150 151 152 153 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169
170 171 172 173 174 175 176 177 178 179
180 181 182 183 184 185 186 187 188 189
190 191 192 193 194 195 196 197 198 199
Then in J, I can quickly add each row of a
to each row of b
to produce a 2x10x10 array using J’s rank operator (adding vectors ("1
), for each vector in a
over b
("1 2
)):
a +"1"1 2 b
101 103 105 107 109 111 113 115 117 119
111 113 115 117 119 121 123 125 127 129
121 123 125 127 129 131 133 135 137 139
131 133 135 137 139 141 143 145 147 149
141 143 145 147 149 151 153 155 157 159
151 153 155 157 159 161 163 165 167 169
161 163 165 167 169 171 173 175 177 179
171 173 175 177 179 181 183 185 187 189
181 183 185 187 189 191 193 195 197 199
191 193 195 197 199 201 203 205 207 209
111 113 115 117 119 121 123 125 127 129
121 123 125 127 129 131 133 135 137 139
131 133 135 137 139 141 143 145 147 149
141 143 145 147 149 151 153 155 157 159
151 153 155 157 159 161 163 165 167 169
161 163 165 167 169 171 173 175 177 179
171 173 175 177 179 181 183 185 187 189
181 183 185 187 189 191 193 195 197 199
191 193 195 197 199 201 203 205 207 209
201 203 205 207 209 211 213 215 217 219
This is a contrived example, but this comes in extremely handy in array programming with J.
In Julia, does a similarly expressive way to do that operation exist, i.e. to broadcast a function over arbitrary specified ranks? I can make the arrays easily enough for this example, but I can’t find a way to add them like above in Julia without writing an explicit for loop. Is there something expressive that would accomplish this, without writing explicit for loops?
Dot syntax works for me for the simple case in which only one dimension needs to be broadcasted, but I’m looking for something as flexible as J’s rank operator.
For reference, these are the same arrays a
and b
in Julia - I just don’t know how to add them up to produce a 3 dimensional array as above in an easily expressible way, without explicitly looping.
a = collect(1:20) |> x -> reshape(x, (10, 2)) |> transpose |> copy;
b = collect(100:199) |> x -> reshape(x, (10,10)) |> transpose |> copy;
Thank you in advance for any help! I really like Julia so far, and I really want to use it. But I’m having trouble letting go of some of the indulgences that J/APL give users in manipulating arrays easily and expressively.