Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在julia中将数组元素用作迭代器?_Loops_Math_Vector_Julia_Readability - Fatal编程技术网

Loops 如何在julia中将数组元素用作迭代器?

Loops 如何在julia中将数组元素用作迭代器?,loops,math,vector,julia,readability,Loops,Math,Vector,Julia,Readability,我试图让我的Julia代码更容易理解(对物理学家来说),并认为如果我能使用某种向量类型的迭代器,那就太好了。我试图将每个元素用作迭代器。到目前为止,我的解决方案是: kcut=2 You can maybe iterate directly on M, for example like this: julia> using LinearAlgebra: norm julia> kcut=2; julia> Ø = [0, 0, 0]; julia> for You c

我试图让我的Julia代码更容易理解(对物理学家来说),并认为如果我能使用某种向量类型的迭代器,那就太好了。我试图将每个元素用作迭代器。到目前为止,我的解决方案是:

kcut=2
You can maybe iterate directly on M, for example like this:

julia> using LinearAlgebra: norm
julia> kcut=2;
julia> Ø = [0, 0, 0];

julia> for You can do the following with 
StaticArrays
to get "tuples that can do linear algebra":

julia> using StaticArrays

julia> ⊗(u, v) = (i ⊗ j for i in u for j in v)
⊗ (generic function with 1 method)

julia> ⊗(x::Number, y::Number) = SVector(x, y)
⊗ (generic function with 2 methods)

julia> ⊗(x::SVector{N}, y::Number) where {N} = SVector(x..., y)
⊗ (generic function with 3 methods)

julia> collect((1:3) ⊗ (10:12) ⊗ (100:101))
18-element Array{SArray{Tuple{3},Int64,1,3},1}:
 [1, 10, 100]
 [1, 10, 101]
 [1, 11, 100]
 [1, 11, 101]
 [1, 12, 100]
 [1, 12, 101]
 [2, 10, 100]
 [2, 10, 101]
 [2, 11, 100]
 [2, 11, 101]
 [2, 12, 100]
 [2, 12, 101]
 [3, 10, 100]
 [3, 10, 101]
 [3, 11, 100]
 [3, 11, 101]
 [3, 12, 100]
 [3, 12, 101]

julia> using LinearAlgebra: norm

julia> for M in (1:3) ⊗ (10:12) ⊗ (100:101)
           println(norm(M))
       end
100.50373127401788
101.49876846543509
100.60815076324582
101.6021653312566
100.72239075796404
101.7152889196113
100.5186549850325
101.51354589413178
100.62305898749054
101.61692772368194
100.73728207570423
101.73003489628813
100.54352291420865
101.5381701627521
100.64790112068906
101.64152694642087
100.7620960480676
101.75460677532
kcut=2

您可以直接在M上迭代,例如:

julia> ⨂(spaces::NTuple{N}) where {N} = (SVector{N}(t) for t in Iterators.product(spaces...))
⨂ (generic function with 1 method)

julia> ⨂(spaces...) = ⨂(spaces)
⨂ (generic function with 2 methods)

julia> collect(⨂(1:3, 10:11))
3×2 Array{SArray{Tuple{2},Int64,1,2},2}:
 [1, 10]  [1, 11]
 [2, 10]  [2, 11]
 [3, 10]  [3, 11]

julia> collect(⨂(1:3, 10:11, 100:101))
3×2×2 Array{SArray{Tuple{3},Int64,1,3},3}:
[:, :, 1] =
 [1, 10, 100]  [1, 11, 100]
 [2, 10, 100]  [2, 11, 100]
 [3, 10, 100]  [3, 11, 100]

[:, :, 2] =
 [1, 10, 101]  [1, 11, 101]
 [2, 10, 101]  [2, 11, 101]
 [3, 10, 101]  [3, 11, 101]
julia>使用线性代数:norm
julia>kcut=2;
朱莉娅>Ø=[0,0,0];

julia>对于您可以使用
staticArray
执行以下操作,以获得“可以执行线性代数的元组”:

但我不确定这是否值得。理想情况下,用于
SVector
s和
Number
s的
\otimes
将构造一个惰性数据结构,在迭代时只创建大小适当的
SVector
s(而不是像这里那样展开)。我现在懒得写那个

一个更好的变体(但稍微少一些mathy语法)是重载
⨂(空格…
要立即执行所有操作:


收集到一个不同的形状,尽管我认为更合适。

将<代码>为(m)₁, M₂, M₃) 输入(-kcut:kcut)⊗ (-kcut:kcut)⊗ (-kcut:kctu)
be“物理”够了吗?因为这样你就可以定义
\otimes
迭代器的同义词。product
。这很酷。不过我应该澄清一下。在循环中,我实际上需要对M做线性代数,所以如果它作为数组输出就好了。有没有办法通过类似的方法实现这一点?因为在这种情况下,M总是g如果使用3个元素的元组,那么(性能方面)使用3个元素的元组会更好。尽管如此,我编辑了我的答案,以便在需要时包含转换。这是最“朱利安”回答。特别是当
kcut
的大小很大时,简单地生成整个
数组而不是使用
迭代器将导致内存不足错误。