升级/降级 APL 订单

发布于 2025-01-03 22:30:16 字数 247 浏览 1 评论 0原文

怎么会

⌽(⍒'Hello')

1 2 4 3 5

时候

⍋'Hello'

这个

1 2 3 4 5

我是 APL 的新手,偶然发现它。我只是想知道为什么第二个 l 出现在第一个之前。

How come that

⌽(⍒'Hello')

is

1 2 4 3 5

when

⍋'Hello'

is

1 2 3 4 5

?

I'm new to APL and stumbled on it by accident. I just wonderes why the second l comes before the first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2025-01-10 22:30:16

您正在使用升级 和降级 作为单子基元。

根据定义,升级返回一个索引整数数组,该数组指定其后面的表达式的排序顺序(按升序排列)。如果任何元素相等(在您的示例中是两个字母 l),它们将按照它们在输入表达式中出现的顺序出现在结果中。

因此,⍋'Hello' 返回 1 2 3 4 5。两个l 的顺序相同,即第3 个字符(第1 个字母l)在第4 个字符(第2 个字母l)之前。

根据定义,降级还返回一个索引整数数组,该数组指定其后面的表达式的排序顺序(按降序顺序)。如果任何元素相等(在您的示例中是两个字母 l),它们也将以与表达式中出现的顺序相同的顺序出现在结果中。

因此,⍒'Hello' 返回 5 3 4 2 1。两个 l 保持相同的顺序,因为它们相等。
当您应用旋转时,整数数组将反转为1 2 4 3 5,如您所见。

鉴于函数的定义方式以及它们如何处理相等值,您看到的结果正是预期的结果。

如果您想查看更极端的示例,请比较以下两个数组的输出。创建一个包含 10 个元素的数组,每个元素具有相同的值 1。10⍴1,然后尝试升级函数,然后尝试降级函数:

⍋10⍴1

它们

⍒10⍴1

都会产生相同的结果:

1 2 3 4 5 6 7 8 9 10

You are using both the grade up and grade down as monadic primitives.

By definition grade up returns an integer array of indices which specify the sorted order of the expression following it, in ascending order. If any elements are equal (in your example the two letter l's) , they will appear in the result in the same order that they appeared in the input expression.

So, ⍋'Hello' returns 1 2 3 4 5. The two l's are in the same order, i.e., the 3rd character (1st letter l) precedes the 4th character (2nd letter l).

By definition grade down also returns an integer array of indices which specify the sorted order of the expression following it, in descending order. If any elements are equal (in your example the two letter l's) , they will also appear in the result in the same order that they appeared in the expression.

So, ⍒'Hello' returns 5 3 4 2 1. The two l's remain in the same order because they are equal.
When you apply rotate the integer array gets reversed to 1 2 4 3 5 as you witnessed.

The outcome you are seeing is precisely what is expected given the way the functions are defined and how they deal with equal values.

If you want to see a more extreme example compare the output for the following two arrays. Create an array with 10 elements each having the same value of 1. 10⍴1 and then try the grade up function and then try the grade down function:

⍋10⍴1

and

⍒10⍴1

They will both yield the same result:

1 2 3 4 5 6 7 8 9 10
甜`诱少女 2025-01-10 22:30:16

Grade up ⍋ 和 Grade Down ⍒ 原语保留相等元素的顺序。正如其他人所说,必须有平等争论的规则。但这条规则的优点是它允许多键排序。
也就是说,如果您有一个包含多个关联键的数组,则通过对每个键从最不重要到最重要的顺序进行排序,您将获得按最重要的键排序的结果,其中 equals 按第二个最重要的项排序,第一个项相等两个按第三个排序,依此类推。为此,必须捕获索引向量并将其用于更新所有密钥和数据以保持它们同步。或者它们可以存储在嵌套结构中,在这种情况下,它们将自动保持正确的相对顺序。

The grade up ⍋ and grade down ⍒ primitives preserve the order of equal elements. As others have said, there must be a rule for equal arguments. But this rule has the virtue that it allows multi-key sorts.
That is, if you have an array with several associated keys, by sorting on each key from least significant to most significant, you obtain a result sorted by the most significant key, with equals sorted by the 2nd mot significant, items equal on the 1st two sorted by the 3rd, and so on. For this to work the index vector must be captured and used to update all keys and the data to keep them in sync. Or they could be stored in a nested structure, in which case they would automatically be kept in proper relative order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文