I was thinking about this after viewing the following video by Conor Hoekstra:
Julia gets dinged around minute 15 since broadcasting does not equate directly with an outer product in all circumstances. This gets explained further around minute 32 where he takes issues with transpose
on Vector
. See also this prior post:
ThIs thread clearly shows that a true outer product is also accessible in Julia.
Hoekstra’s original version without using I
is as follows:
# check fo see if matrix is an X matrix
# where only the main diagonals are non-zero
function checkmatrix(grid)
n = size(grid, 1)
i = 1:n .== 1:n |> transpose
min.(grid, 1) == max.(i, reverse(i, dims=1))
end
Using Iterators.product
, map
, and allequal
we could do the following.
function checkmatrix2(grid)
n = size(grid, 1)
i = map(allequal, Iterators.product(1:n, 1:n))
min.(grid, 1) == max.(i, reverse(i, dims=1))
end