Declaring multiple arrays in Julia

Suppose I need to declare (but not initialize the values) five 10x10 arrays named, say, A1 - A5 . Fortran has good syntax for declaring several types of arrays:

 REAL(8), DIMENSION(10,10) :: A1, A2, A3, A4, A5 

However, the only method in Julia that I know of is much uglier:

 A1 = Array(Float64, 10, 10) A2 = Array(Float64, 10, 10) A3 = Array(Float64, 10, 10) A4 = Array(Float64, 10, 10) A5 = Array(Float64, 10, 10) 

Is there a more concise way to declare multiple arrays of the same dimension in Julia?

+2
arrays julia-lang
source share
2 answers

Thanks to some help from @simonster on another question, you can briefly declare your variables without any overhead of runtime using metaprogramming,

 for x = [:A1,:A2,:A3,:A4,:A5] @eval $x = Array(Float64,10,10) end 

However, we can now take one step better than Fortran, allowing you to generate names dynamically:

 for x in [symbol("A"*string(i)) for i=1:100] @eval $x = Array(Float64,10,10) end 

This will allow you to allocate 100 arrays of A1-A100. Thanks to @ rickhf12hs comment for this idea / implementation.

+8
source share

Assuming this is normal for creating a single temporary array containing five arrays, you can use an understanding of the array:

 A1, A2, A3, A4, A5 = [Array(Float64, 10, 10) for i = 1:5] 
+1
source share

All Articles