Ask Your Question

Revision history [back]

Your question is very unclear but still let me try to speculate on what you are interested in. If you have a function f and a set of elements X and want to compute the elements {f(x) : x in X} as well as their preimages you can use dictionaries. Let me take the stupid example of the square function on Z/10Z.

sage: f = lambda x: x^2
sage: preimages = {}
sage: for x in Zmod(10):
....:     y = f(x)
....:     if y in preimages: preimages[y].append(x)
....:     else: preimages[y] = {x]
sage: preimages
{0: [0], 1: [1, 9], 4: [2, 8], 5: [5], 6: [4, 6], 9: [3, 7]}
sage: preimages[4]
[2, 8]

Your question is very unclear but still let me try to speculate on what you are interested in. If you have a function f and a set of elements X and want to compute the elements set of images {f(x) : x in X} as well as their preimages you can use dictionaries. Let me take the stupid example of the square function on Z/10Z.

sage: f = lambda x: x^2
sage: preimages = {}
sage: for x in Zmod(10):
....:     y = f(x)
....:     if y in preimages: preimages[y].append(x)
....:     else: preimages[y] = {x]
sage: preimages
{0: [0], 1: [1, 9], 4: [2, 8], 5: [5], 6: [4, 6], 9: [3, 7]}
sage: preimages[4]
[2, 8]

Your question is very unclear but still let me try to speculate on what you are interested in. If you have a function f and a set of elements X and want to compute the set of images {f(x) : x in X} as well as their preimages you can use dictionaries. Let me take the stupid example of the square function on Z/10Z.

sage: f = lambda x: x^2
sage: preimages = {}
sage: for x in Zmod(10):
....:     y = f(x)
....:     if y in preimages: preimages[y].append(x)
....:     else: preimages[y] = {x]
[x]
sage: preimages
{0: [0], 1: [1, 9], 4: [2, 8], 5: [5], 6: [4, 6], 9: [3, 7]}
sage: preimages[4]
[2, 8]

Your question is very unclear but still let me try to speculate on what you are interested in. If you have a function f and a set of elements X and want to compute the set of images {f(x) : x in X} as well as their preimages you can use dictionaries. Let me take the stupid example of the square function on Z/10Z.

sage: f = lambda x: x^2
sage: preimages = {}
sage: for x in Zmod(10):
....:     y = f(x)
....:     if y in preimages: preimages[y].append(x)
....:     else: preimages[y] = [x]
sage: preimages
{0: [0], 1: [1, 9], 4: [2, 8], 5: [5], 6: [4, 6], 9: [3, 7]}
sage: preimages[4]
[2, 8]

And if you want to access the set of images (which is in my example the squares in Z/10Z)

sage: preimages.keys()
[0, 1, 4, 5, 6, 9]

Your question Some comments on your code.

1) There is very unclear but still let me try to speculate on what no need to import numpy that you are interested in. not using.

2) You can simplify

 x = vector(x)
 x = x.row()
 v = x.transpose()
 b = x * A * v

into

 x = vector(x)
 b = x * A * x

Now to answer your question. If you have a function f want to gather the images and a set of elements X and want to compute the set of images {f(x) : x in X} as well as their the preimages you can use dictionaries. Let me take the stupid consider the simpler example of the square function on Z/10Z.

sage: f = lambda x: x^2
sage: preimages = {}
sage: for x in Zmod(10):
....:     y = f(x)
....:     if y in preimages: preimages:
....:         preimages[y].append(x)
....:     else: else:
....:         preimages[y] = [x]
sage: preimages
{0: [0], 1: [1, 9], 4: [2, 8], 5: [5], 6: [4, 6], 9: [3, 7]}
sage: preimages[4]
[2, 8]

And if you want to access the set of images (which is in my example the squares in Z/10Z)

sage: preimages.keys()
[0, 1, 4, 5, 6, 9]