'How to define a sequence with condtions on the index?
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
but it is still keeping the cases j<i.
Solution 1:[1]
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]}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | acer |
