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

查看:17
本文介绍了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?

推荐答案

当然,没有什么可以阻止您将行主要数组作为一块内存,以及某些包(如图像.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_B 的函数实现的, Ac_mul_Bc 等,其中 t 表示转置,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 函数已经以缓存友好的方式实现了,所以它比 naive 快很多

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天全站免登陆