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.
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.
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]}
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 ) };