Julia中的数组排序 [英] Array ordering in Julia

查看:122
本文介绍了Julia中的数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在Julia本地处理C顺序或非连续数组? 例如,当使用NumPy时,C顺序数组是默认数组,但是我可以初始化Fortran顺序数组并使用该数组进行计算. 一种简单的方法是对矩阵进行转置. 我也可以使用通过切片制作的非连续数组. 我浏览了文档等,找不到在Julia中创建,声明或使用C排序数组的方法. 转置似乎会返回一个副本.

Is there a way to work with C-ordered or non-contiguous arrays natively in Julia? For example, when using NumPy, C-ordered arrays are the default, but I can initialize a Fortran ordered array and do computations with that as well. One easy way to do this was to take the Transpose of a matrix. I can also work with non-contiguous arrays that are made via slicing. I have looked through the documentation, etc. and can't find a way to make, declare, or work with a C-ordered array in Julia. The transpose appears to return a copy.

Julia是否允许用户使用C顺序和非连续数组? 当前是否有任何方法可以在不复制的情况下获得转置或切片?

Does Julia allow a user to work with C-ordered and non-contiguous arrays? Is there currently any way to get a transpose or a slice without taking a copy?

我已经找到了切片方法. 当前,它可以作为称为SubArray的另一种类型使用. 例如,我可以执行以下操作以获取100x100数组A

I have found how to do slicing. Currently it is available as a different type called a SubArray. As an example, I could do the following to get the first row of a 100x100 array A

sub(A, 1, 1:100)

似乎有计划对此进行改进,如 https://github上所示.com/JuliaLang/julia/issues/5513

It looks like there are plans to improve this, as can be seen in https://github.com/JuliaLang/julia/issues/5513

这仍然没有解决C顺序数组的问题. 是否存在C顺序数组的接口? 有没有办法通过视图而不是副本进行转置?

This still leaves open the question of C-ordered arrays. Is there an interface for C-ordered arrays? Is there a way to do a transpose via a view instead of a copy?

推荐答案

自然地,没有什么可以阻止您将行主要数组作为内存块和某些程序包(例如Images)一起使用. .jl)支持任意维数组的任意排序.

Naturally, there's nothing that prevents you from working with row-major arrays as a chunk of memory, and certain packages (like Images.jl) support arbitrary ordering of arbitrary-dimensional arrays.

大概您想知道的主要问题是线性代数.目前,我尚不了解任何现成的东西,但请注意,Julia中的矩阵乘法是通过一系列具有A_mul_BAt_mul_BAc_mul_Bc等名称的函数实现的,其中表示转置,而c表示共轭.解析器将A'*b之类的表达式替换为Ac_mul_B(A, b)而不实际进行转置.

Presumably the main issue you're wondering about is linear algebra. Currently I don't know of anything out-of-the-box, but note that matrix multiplication in Julia is implemented through a series of functions with names like A_mul_B, At_mul_B, Ac_mul_Bc, etc, where t means transpose and c means conjugate. The parser replaces expressions like A'*b with Ac_mul_B(A, b) without actually taking the transpose.

因此,您可以自己实现RowMajorMatrix <: AbstractArray类型,并设置特殊的乘法规则:

Consequently, you could implement a RowMajorMatrix <: AbstractArray type yourself, and set up special multiplication rules:

A_mul_B(A::RowMajorMatrix, B::RowMajorMatrix) = At_mul_Bt(A, B)
A_mul_B(A::RowMajorMatrix, B::AbstractArray) = At_mul_B(A, B)
A_mul_B(A::AbstractArray, B::RowMajorMatrix) = A_mul_Bt(A, B)

等除了这两个参数的版本外,还有3个参数的版本(如A_mul_B!)将结果存储在预分配的输出中.您也需要实现这些.最后,您还必须设置适当的show方法(以正确显示它们),size方法等.

etc. In addition to these two-argument versions, there are 3-argument versions (like A_mul_B!) that store the result in a pre-allocated output; you'd need to implement those, too. Finally, you'd also have to set up appropriate show methods (to display them appropriately), size methods, etc.

最后,茱莉亚(Julia)的transpose函数已经以缓存友好的方式实现,因此它比幼稚的速度要快很多

Finally, Julia's transpose function has been implemented in a cache-friendly manner, so it's quite a lot faster than the naive

for j = 1:n, i = 1:m
    At[j,i] = A[i,j]
end

因此,有时不必为创建算法的自定义实现而担心,您只需致电transpose.

Consequently there are occasions where it's not worth worrying about creating custom implementations of algorithms, and you can just call transpose.

如果您实现这样的事情,我建议您将其作为一个包来提供,因为其他人可能会对此感兴趣.

If you implement something like this, I'd encourage you to contribute it as a package, as it's likely that others may be interested.

这篇关于Julia中的数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆