Ask Your Question

Sets depending on a variable

asked 2021-02-19 18:18:40 +0200

Alain Ngalani gravatar image

updated 2021-02-19 18:20:39 +0200

I'm working on an algorithm that takes as impit a set and works accordingly to the set cardinality. In it I would like to define several sets with different properties in a way similar to this

For n in len(S) : Kn=stuff

However we all know the above will create a single set called Kn and work on it whilst I would like to have a set for each n, how can I do it?

This sets will be the base for several other sub algorithm so I need them to extually be defined, it's not enough to have them exist inside a given cycle as I think I should be able to do

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2021-02-19 19:37:17 +0200

updated 2021-02-19 22:00:02 +0200

First, for n in len(S) won't work, because len(S) is an integer:

TypeError: 'int' object is not iterable

Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.

Next, to create an object for each element of S, you can use list comprehension:

sage: [i for i in S]  # or [(stuff depending on i)  for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)]  # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]

Edit: you can also do this without list comprehension:

K = []
for i in S:
    (create stuff)

Then K will be a list with one entry for each element of S.

edit flag offensive delete link more


This looks nice for solving some kind of problem but it's not what I'm looking for I want to create a set for each element though, not an object. It may even be empty but I need to have it and being able to recall it just by knowing the element it comes form. The point is that I need to write stuff like

for a in range(len(S)):
        Ka=[set with certain properties depending on the given element of S]

So that later I can go

for b in Ka:
      do stuff
Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-19 21:15:22 +0200 )edit

Does my edit help?

John Palmieri gravatar imageJohn Palmieri ( 2021-02-19 22:01:24 +0200 )edit

It did, it made me realize that I can append sets to sets

Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-20 14:16:45 +0200 )edit

answered 2021-02-20 14:18:47 +0200

Alain Ngalani gravatar image

At the end I chose to do it in the following way

for a in range(len(S)):
     J.append(a^2+a)                                  #do what I need to do on the set J, this is a trivial example
edit flag offensive delete link more

answered 2021-02-20 18:08:20 +0200

Emmanuel Charpentier gravatar image

Unless you have a very specific reason to have each of your sets designated by a unique global symbol, the same effect can be achieved by storing your sets in a list. E. g. :

sage: K=[GF(u) for u in (2..100) if u.is_prime()]

K is now a list of all finite fields of size less than or equal to 100 (and set(K) is the set of such fields). The filtering can be done on the set itself rather u. For example, the same list can be obtained by :

[v for v in [Integers(u) for u in (2..100)] if v.is_field()]

As pointed out, you can work on such sets by using K[j] to denote them. Here $j$ would denote the $j$th finite field, not the finite field of size $j$, which may not exist (e. g. one would have K[5] being $\mathbf{F}_{13}$).

You may also store them in a dictionary whose keys are their sizes :

sage: D={u:GF(u) for u in (2..100) if u.is_prime()}

D's elements are accessed by their size, not by their rank in the list :

sage: D.keys()
dict_keys([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
sage: D[13]
Finite Field of size 13

If you still need a unique, distinct symbol to denote each of them, lookup sage.misc.misc.inject_variable?. But I fail to see the point...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-02-19 18:18:40 +0200

Seen: 211 times

Last updated: Feb 20 '21