如何用索引上的条件定义序列?

发布于 2025-01-16 20:20:42 字数 115 浏览 5 评论 0原文

我需要处理 i

我尝试按照下面的屏幕截图进行操作,但它仍然保留 j

在此处输入图像描述

I need to deal with the sequence a[i][j] with i<j, but I do not know if there exists a command to do this.

I am trying to manipulate as in the screenshot below, but it is still keeping the cases j<i.

enter image description here

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

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

发布评论

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

评论(2

各自安好 2025-01-23 20:20:42

生成完整集合(i=1..nj=1..n)然后删除不需要的部分的方法效率低下。

这里有两种更有效的方法(以及使用 minus 的方法,但固定为使用 double-seq)。

n:=4:

{seq( seq( d[i][j], j=i..n ), i=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}

{seq( seq( d[i][j], i=1..j ), j=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}

# The next is less efficient
{seq( seq( d[i][j], j=1..n ), i=1..n )}
minus {seq( seq( d[i][j], j=1..i-1 ), i=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}

The approach of generating the full set (i=1..n and j=1..n) and then removing the unwanted portion is unnecessarily inefficient.

Here are two more efficient ways (as well as that way using minus, but fixed to use the double-seq).

n:=4:

{seq( seq( d[i][j], j=i..n ), i=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}

{seq( seq( d[i][j], i=1..j ), j=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}

# The next is less efficient
{seq( seq( d[i][j], j=1..n ), i=1..n )}
minus {seq( seq( d[i][j], j=1..i-1 ), i=1..n )};

    {d[1][1], d[1][2], d[1][3], d[1][4],
     d[2][2], d[2][3], d[2][4], 
     d[3][3], d[3][4], d[4][4]}
救赎№ 2025-01-23 20:20:42

对于这个问题,我会使用@acer的答案中的第一种方法。但以防万一,如果索引的条件有点复杂,以至于您无法快速想出一个简单的公式,可以用来在该答案的第一种方法中创建索引,或者写为集合减去二索引设置等,然后您可以使用 Maple 中的 select 命令以及作为布尔函数的原始条件。以下是它在您的情况下的工作原理。

n := 4:
idxs := select( pair -> pair[1] < pair[2], [ seq( seq( [i, j], j = 1..n ), i = 1..n ) ] ); 

您知道如何无限制地生成所有 [i, j],即上面 select 命令的第二个参数。你对他们的条件是;如果你拿起一对,它的第一个元素小于它的第二个元素。所以我们在上面写了select的第一个参数,它是一个布尔函数,对于每一对,如果条件成立,则返回true,否则返回falseselect 命令选取其第二个参数的元素,这些元素在第一个参数的函数下给出 true 。现在您已经有了索引列表,您可以使用单个 seq 来使用它们。

{ seq( d[ pair[1] ][ pair[2] ], pair in idxs ) };

这是输出的屏幕截图。
输入图片此处描述

For this question, I would go with the first method in @acer's answer. But just in case, if the condition on the indices were a bit complicated such that you could not quickly come up with an easy formulation that can be used to create your indices in the 1st method in that answer or to write as set minus of two indices set etc. then you can use the select command in Maple and the original condition that you have as a boolean function. Here is how it would work in your case.

n := 4:
idxs := select( pair -> pair[1] < pair[2], [ seq( seq( [i, j], j = 1..n ), i = 1..n ) ] ); 

You knew how to generate all [i, j]'s with no restriction, the second argument of the select command above. And your condition on them is; if you pick up a pair, its 1st element being less than its 2nd element. So we wrote the 1st argument of select above which is a boolean function, for each pair, if the condition holds, it returns true otherwise returns false. The select command picks up the elements of its 2nd arguments that give true under the function in its 1st argument. Now that you have the list of indices, you can use a single seq to use them.

{ seq( d[ pair[1] ][ pair[2] ], pair in idxs ) };

Here is the screenshot of the outputs.
enter image description here

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