I am trying to write a function that creates a d
-dimensional grid (d
can be higher than 3) with n
points in each direction
function grid(d, n)
return coordinates of n^d points
end
So far I have the following:
julia> using Base.Iterators
julia> n = 10
10
# 1D
julia> a = range(0, 1, length=n)
0.0:0.1111111111111111:1.0
# 2D
julia> b = collect(product(a, a))
10×10 Array{Tuple{Float64,Float64},2}:
(0.0, 0.0) (0.0, 0.111111) … (0.0, 1.0)
(0.111111, 0.0) (0.111111, 0.111111) (0.111111, 1.0)
(0.222222, 0.0) (0.222222, 0.111111) (0.222222, 1.0)
(0.333333, 0.0) (0.333333, 0.111111) (0.333333, 1.0)
(0.444444, 0.0) (0.444444, 0.111111) (0.444444, 1.0)
(0.555556, 0.0) (0.555556, 0.111111) … (0.555556, 1.0)
(0.666667, 0.0) (0.666667, 0.111111) (0.666667, 1.0)
(0.777778, 0.0) (0.777778, 0.111111) (0.777778, 1.0)
(0.888889, 0.0) (0.888889, 0.111111) (0.888889, 1.0)
(1.0, 0.0) (1.0, 0.111111) (1.0, 1.0)
julia> f(x) = sum(xi^2 for xi in x)
f (generic function with 1 method)
julia> f.(a) # 1D
10-element Array{Float64,1}:
0.0
0.012345679012345678
0.04938271604938271
0.1111111111111111
0.19753086419753085
0.308641975308642
0.4444444444444444
0.6049382716049383
0.7901234567901234
1.0
julia> f.(b) # 2D
10×10 Array{Float64,2}:
0.0 0.0123457 0.0493827 0.111111 … 0.604938 0.790123 1.0
0.0123457 0.0246914 0.0617284 0.123457 0.617284 0.802469 1.01235
0.0493827 0.0617284 0.0987654 0.160494 0.654321 0.839506 1.04938
0.111111 0.123457 0.160494 0.222222 0.716049 0.901235 1.11111
0.197531 0.209877 0.246914 0.308642 0.802469 0.987654 1.19753
0.308642 0.320988 0.358025 0.419753 … 0.91358 1.09877 1.30864
0.444444 0.45679 0.493827 0.555556 1.04938 1.23457 1.44444
0.604938 0.617284 0.654321 0.716049 1.20988 1.39506 1.60494
0.790123 0.802469 0.839506 0.901235 1.39506 1.58025 1.79012
1.0 1.01235 1.04938 1.11111 1.60494 1.79012 2.0
julia> f.(c) # 3D???
Is this the right approach? And if so, how can I (efficiently) extend this to higher dimensions (3 and above)?