Ask Your Question

Revision history [back]

Elements of RDF have no trailing zeros in their decimal representation.

Here is how I would go about displaying the table.

It's a bit convoluted, and there must be ways to simplify, but hopefully that helps.

sage: A = [[10.] * 10, [100.] + [0.] * 9, [11.1] * 9 + [0.], [20.] * 5 + [0.] * 5, [50./2**n for n in (1 .. 10)]]
sage: B = matrix(RDF, A)
sage: C = B.round(3)
sage: D = [list(row) for row in C]

sage: hr = [f"${n}$" for n in (1 .. 10)]
sage: hc = ["", "Équi-Rep", "Tout pour un", "Un déshérité"]
sage: hc += ["Injuste pour 1/2", "$5$", "Injuste croissante"]
sage: t = table(D, header_row=hr, header_column=hc)

sage: t
                   | $1$     $2$    $3$    $4$     $5$     $6$     $7$     $8$     $9$     $10$
+------------------+-------+------+------+-------+-------+-------+-------+-------+-------+-------+
  Équi-Rep         | 10.0    10.0   10.0   10.0    10.0    10.0    10.0    10.0    10.0    10.0
  Tout pour un     | 100.0   0.0    0.0    0.0     0.0     0.0     0.0     0.0     0.0     0.0
  Un déshérité     | 11.1    11.1   11.1   11.1    11.1    11.1    11.1    11.1    11.1    0.0
  Injuste pour 1/2 | 20.0    20.0   20.0   20.0    20.0    0.0     0.0     0.0     0.0     0.0
  $5$              | 25.0    12.5   6.25   3.125   1.562   0.781   0.391   0.195   0.098   0.049

sage: view(t)

Table

Elements Here is a solution taking advantage of the following:

  • elements of RDF have are represented with no trailing zeros in their decimal representation.

    Here is how I would go about displaying the table.

    It's zeros

  • matrices over RDF have a bit convoluted, and there must round method that can round to n decimal digits

There might be ways to simplify, simpler ways, but hopefully that this helps.

Define A as a list of lists:

sage: A = [[10.] [[10] * 10, [100.] [100] + [0.] [0] * 9, [11.1] * 9 + [0.], [20.] [0]]
sage: A.extend([[20] * 5 + [0.] [0] * 5, [50./2**n [RDF(50)/2**n for n in (1 .. 10)]]
sage: B = matrix(RDF, A)
sage: C = B.round(3)
range(10)]])

Make a deep copy, which we can change without changing A:

sage: D = [list(row) for row in C]

deepcopy(A)

Round the rows with non-integer values:

sage: D[2] = list(matrix(RDF, A[2]).round(3)[0])
sage: D[4] = list(matrix(RDF, A[4]).round(3)[0])

Set table headers:

sage: hr = [f"${n}$" for n in (1 .. 10)]
sage: hc = ["", "Équi-Rep", "Équirép", "Tout pour un", "Un déshérité"]
sage: hc += ["Injuste ["Inj pour 1/2", "$5$", "Injuste "Inj croissante"]

Build the table:

sage: t = table(D, header_row=hr, header_column=hc)
 

The table in text mode:

sage: t
                   | $1$     $2$    $3$    $4$     $5$     $6$     $7$     $8$     $9$     $10$
+------------------+-------+------+------+-------+-------+-------+-------+-------+-------+-------+
  Équi-Rep  +----------------+------+------+------+------+-------+-------+-------+-------+-------+-------+
  Équirép        | 10.0    10.0   10.0   10.0    10.0    10.0    10.0    10.0    10.0    10.0
10     10     10     10     10      10      10      10      10      10
  Tout pour un    | 100.0   0.0    0.0    0.0     0.0     0.0     0.0     0.0     0.0     0.0
100    0      0      0      0       0       0       0       0       0
  Un déshérité    | 11.1    11.1   11.1   11.1  11.1   11.1   11.1   11.1    11.1    11.1    11.1    11.1    0.0
  Injuste Inj pour 1/2  | 20.0    20.0   20.0   20.0    20.0    0.0     0.0     0.0     0.0     0.0
  $5$              20     20     20     20     20      0       0       0       0       0
  Inj croissante | 50.0   25.0    12.5   6.25   3.125   1.562   0.781   0.391   0.195   0.098   0.049

0.098

The table, rendered using LaTeX:

sage: view(t)

Table

Comparison table

Here is a solution taking advantage of the following:

  • elements of RDF are represented with no trailing decimal zeros
  • matrices over RDF have a round method that can round to n decimal digits

There might be simpler ways, but hopefully this helps.

Define A as a list of lists:

sage: A = [[10] * 10, [100] + [0] * 9, [11.1] [100/9] * 9 + [0]]
sage: A.extend([[20] * 5 + [0] * 5, [RDF(50)/2**n [50/2**n for n in range(10)]])

Make a deep copy, which so we can change it without changing A:

sage: D = deepcopy(A)

Round the rows with non-integer values:

sage: D[2] = list(matrix(RDF, A[2]).round(3)[0])
A[2]).round(1)[0])
sage: D[4] = list(matrix(RDF, A[4]).round(3)[0])
A[4]).round(2)[0])

Set table headers:

sage: hr = [f"${n}$" for n in (1 .. 10)]
sage: hc = ["", "Équirép", "Tout pour un", "Un déshérité"]
sage: hc += ["Inj pour 1/2", "Inj croissante"]
["Injus moitié", "Injus croiss"]

Build the table:

sage: t = table(D, header_row=hr, header_column=hc)

The table in text mode:

sage: t
                | $1$    $2$    $3$    $4$    $5$     $6$     $7$     $8$     $9$     $10$
+----------------+------+------+------+------+-------+-------+-------+-------+-------+-------+
+--------------+------+------+------+------+------+------+------+------+------+------+
  Équirép        | 10     10     10     10     10      10      10      10      10  | 10     10     10     10     10     10     10     10     10     10
  Tout pour un  | 100    0      0      0      0       0       0       0       0  0      0      0      0      0
  Un déshérité   | 11.1   11.1   11.1   11.1   11.1    11.1    11.1    11.1    11.1  | 11.1   11.1   11.1   11.1   11.1   11.1   11.1   11.1   11.1   0.0
  Inj Injus moitié | 20     20     20     20     20     0      0      0      0      0
  Injus croiss | 50.0   25.0   12.5   6.25   3.12   1.56   0.78   0.39   0.2    0.1

The latexed table:

sage: view(t)

Tableau de comparaison, version a

Simpler solution, inspired by the answer given by @dsejas.

Here A, hr, hc are as above.

Round only non-integer entries; adapt rounding precision to get 3 significant digits:

sage: f = lambda a: a if a in ZZ else round(a, 2 - floor(log(a, 10)))
sage: D = [[f(a) for a in r] for r in A]

Build the table:

sage: t = table(D, header_row=hr, header_column=hc)

The table in text mode:

sage: t
               | $1$    $2$    $3$    $4$    $5$    $6$    $7$     $8$     $9$     $10$
+--------------+------+------+------+------+------+------+-------+-------+-------+--------+
  Équirép      | 10     10     10     10     10     10     10      10      10      10
  Tout pour 1/2   un | 100    0      0      0      0      0      0       0       0       0
  Un déshérité | 11.1   11.1   11.1   11.1   11.1   11.1   11.1    11.1    11.1    0
  Injus moitié | 20     20     20     20     20      0  0      0       0       0       0
  Inj croissante | 50.0   25.0 Injus croiss | 50     25     12.5   6.25   3.125   1.562 3.12   1.56   0.781   0.391   0.195   0.098
0.0977

The table, rendered using LaTeX:latexed table:

sage: view(t)

Comparison table

Tableau de comparaison, version b