Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @Cyrille! I believe that @slelievre has already given you a perfect solution for your problem, so i am just going to complement it with a few comments and alternatives for you, or any reader looking for the way to work with maps. However, I highly recommend using @slelievre's answer, which is way more efficient and elegant.

You cannot define f(x) = round(x, 2), because this is a symbolic definition, but round() is not a symbolic function. See the answer to this question for more details (I also made a similar mistake.) Remember that syntax like f(x) = x^2 is actually syntactic sugar defined by Sage for comfort. Instead, when you can't use that type of definitions, you should use a Python function definition:

def f(x):
    return round(x, 2)

Now, a similar approach to yours would be to use the map() function; this acts on the elements of a list. In your particular case, A is a list of lists, so you should define a function that takes a list (a row of your table) and returns a list with its elements rounded off (a new row of your table):

def f(row): # row is a row of your table "A" (automatically chosen by "map()")
    result = [] # this will be the new rounded off row
    for x in row:
        result.append(round(x, 2)) # this applies the "round()" function to every element of "row" and appends it to the new row
    return result

Now, you can apply this to your table A with this code:

A=[[10,10,10,10,10,10,10,10,10,10],[100,0,0,0,0,0,0,0,0,0],
     [11.1,11.1,11.1,11.1,11.1,11.1,11.1,11.1,11.1,0],
     [20,20,20,20,20,0,0,0,0,0],[50,25,12.5,6.25,3.125,1.56,0.78,0.39,0.19,0.09]]
temp = map(f, A)

Now temp is a Python map that will round your table elements. In order to trigger the process and obtain a new table (list of lists), you must write list(A), like in the following code:

t=table(list(A),header_row="$1$","$2$","$3$","$4$","$5$","$6$","$7$","$8$","$9$","$10$"],
            header_column["","ÉquiRep","Tout pour un","Un déshérité","Injuste pour 1/2","$5$",
            "Injuste croissante"])
show(t)

Following @slelievre's answer, you can also use the following function:

 def f(row):
    result = []
    for x in row:
        result.append(RDF(x))
    return result

Once again, this syntax is necessary, since RDF() is not a symbolic function.

I hope this helps!

Hello, @Cyrille! I believe that @slelievre has already given you a perfect solution for your problem, so i am just going to complement it with a few comments and alternatives for you, or any reader looking for the way to work with maps. However, I highly recommend using @slelievre's answer, which is way more efficient and elegant.

You cannot define f(x) = round(x, 2), because this is a symbolic definition, but round() is not a symbolic function. See the answer to this question for more details (I also made a similar mistake.) Remember that syntax like f(x) = x^2 is actually syntactic sugar defined by Sage for comfort. Instead, when you can't use that type of definitions, you should use a Python function definition:

def f(x):
    return round(x, 2)

Now, a similar approach to yours would be to use the map() function; this acts on the elements of a list. In your particular case, A is a list of lists, so you should define a function that takes a list (a row of your table) and returns a list with its elements rounded off (a new row of your table):

def f(row): # row is a row of your table "A" (automatically chosen by "map()")
    result = [] # this will be the new rounded off row
    for x in row:
        result.append(round(x, 2)) # this applies the "round()" function to every element of "row" and appends it to the new row
    return result

Now, you can apply this to your table A with this code:

A=[[10,10,10,10,10,10,10,10,10,10],[100,0,0,0,0,0,0,0,0,0],
     [11.1,11.1,11.1,11.1,11.1,11.1,11.1,11.1,11.1,0],
     [20,20,20,20,20,0,0,0,0,0],[50,25,12.5,6.25,3.125,1.56,0.78,0.39,0.19,0.09]]
temp = map(f, A)

Now temp is a Python map that will round your table elements. In order to trigger the process and obtain a new table (list of lists), you must write list(A), like in the following code:

t=table(list(A),header_row="$1$","$2$","$3$","$4$","$5$","$6$","$7$","$8$","$9$","$10$"],
t=table(list(temp),header_row="$1$","$2$","$3$","$4$","$5$","$6$","$7$","$8$","$9$","$10$"],
            header_column["","ÉquiRep","Tout pour un","Un déshérité","Injuste pour 1/2","$5$",
            "Injuste croissante"])
show(t)

Following @slelievre's answer, you can also use the following function:

 def f(row):
    result = []
    for x in row:
        result.append(RDF(x))
    return result

Once again, this syntax is necessary, since RDF() is not a symbolic function.

I hope this helps!