+-----------++------+------+------+------+
| $x$ || 0 | 1 | 2 | 3 |
+-----------++------+------+------+------+
| $\sin(x)$ || 0.00 | 0.84 | 0.91 | 0.14 |
+-----------++------+------+------+------+
Help on class table in module sage.misc.table:
class table(sage.structure.sage_object.SageObject)
| Display a rectangular array as a table, either in plain text, LaTeX,
| or html.
|
| INPUTS:
|
| - ``rows`` (default ``None``) - a list of lists (or list of tuples,
| etc.), containing the data to be displayed.
| - ``columns`` (default ``None``) - a list of lists (etc.), containing
| the data to be displayed, but stored as columns. Set either ``rows``
| or ``columns``, but not both.
| - ``header_row`` (default ``False``) - if ``True``, first row is
| highlighted.
| - ``header_column`` (default ``False``) - if ``True``, first column is
| highlighted.
| - ``frame`` (default ``False``) - if ``True``, put a box around each
| cell.
| - ``align`` (default 'left') - the alignment of each entry: either
| 'left', 'center', or 'right'
|
| EXAMPLES::
|
| sage: rows = [['a', 'b', 'c'], [100,2,3], [4,5,60]]
| sage: table(rows)
| a b c
| 100 2 3
| 4 5 60
| sage: latex(table(rows))
| \begin{tabular}{lll}
| a & b & c \\
| $100$ & $2$ & $3$ \\
| $4$ & $5$ & $60$ \\
| \end{tabular}
|
| If ``header_row`` is ``True``, then the first row is highlighted. If
| ``header_column`` is ``True``, then the first column is
| highlighted. If ``frame`` is ``True``, then print a box around every
| "cell". ::
|
| sage: table(rows, header_row=True)
| a b c
| +-----+---+----+
| 100 2 3
| 4 5 60
| sage: latex(table(rows, header_row=True))
| \begin{tabular}{lll}
| a & b & c \\ \hline
| $100$ & $2$ & $3$ \\
| $4$ & $5$ & $60$ \\
| \end{tabular}
| sage: table(rows=rows, frame=True)
| +-----+---+----+
| | a | b | c |
| +-----+---+----+
| | 100 | 2 | 3 |
| +-----+---+----+
| | 4 | 5 | 60 |
| +-----+---+----+
| sage: latex(table(rows=rows, frame=True))
| \begin{tabular}{|l|l|l|} \hline
| a & b & c \\ \hline
| $100$ & $2$ & $3$ \\ \hline
| $4$ & $5$ & $60$ \\ \hline
| \end{tabular}
| sage: table(rows, header_column=True, frame=True)
| +-----++---+----+
| | a || b | c |
| +-----++---+----+
| | 100 || 2 | 3 |
| +-----++---+----+
| | 4 || 5 | 60 |
| +-----++---+----+
| sage: latex(table(rows, header_row=True, frame=True))
| \begin{tabular}{|l|l|l|} \hline
| a & b & c \\ \hline \hline
| $100$ & $2$ & $3$ \\ \hline
| $4$ & $5$ & $60$ \\ \hline
| \end{tabular}
| sage: table(rows, header_column=True)
| a | b c
| 100 | 2 3
| 4 | 5 60
|
| The argument ``header_row`` can, instead of being ``True`` or
| ``False``, be the contents of the header row, so that ``rows``
| consists of the data, while ``header_row`` is the header
| information. The same goes for ``header_column``. Passing lists
| for both arguments simultaneously is not supported. ::
|
| sage: table([(x,n(sin(x), digits=2)) for x in [0..3]], header_row=["$x$", "$\sin(x)$"], frame=True)
| +-----+-----------+
| | $x$ | $\sin(x)$ |
| +=====+===========+
| | 0 | 0.00 |
| +-----+-----------+
| | 1 | 0.84 |
| +-----+-----------+
| | 2 | 0.91 |
| +-----+-----------+
| | 3 | 0.14 |
| +-----+-----------+
|
| You can create the transpose of this table in several ways, for
| example, "by hand," that is, changing the data defining the table::
|
| sage: table(rows=[[x for x in [0..3]], [n(sin(x), digits=2) for x in [0..3]]], header_column=['$x$', '$\sin(x)$'], frame=True)
| +-----------++------+------+------+------+
| | $x$ || 0 | 1 | 2 | 3 |
| +-----------++------+------+------+------+
| | $\sin(x)$ || 0.00 | 0.84 | 0.91 | 0.14 |
| +-----------++------+------+------+------+
|
| or by passing the original data as the ``columns`` of the table
| and using ``header_column`` instead of ``header_row``::
|
| sage: table(columns=[(x,n(sin(x), digits=2)) for x in [0..3]], header_column=['$x$', '$\sin(x)$'], frame=True)
| +-----------++------+------+------+------+
| | $x$ || 0 | 1 | 2 | 3 |
| +-----------++------+------+------+------+
| | $\sin(x)$ || 0.00 | 0.84 | 0.91 | 0.14 |
| +-----------++------+------+------+------+
|
| or by taking the :meth:`transpose` of the original table::
|
| sage: table(rows=[(x,n(sin(x), digits=2)) for x in [0..3]], header_row=['$x$', '$\sin(x)$'], frame=True).transpose()
| +-----------++------+------+------+------+
| | $x$ || 0 | 1 | 2 | 3 |
| +-----------++------+------+------+------+
| | $\sin(x)$ || 0.00 | 0.84 | 0.91 | 0.14 |
| +-----------++------+------+------+------+
|
| In either plain text or LaTeX, entries in tables can be aligned to the
| left (default), center, or right::
|
| sage: table(rows, align='left')
| a b c
| 100 2 3
| 4 5 60
| sage: table(rows, align='center')
| a b c
| 100 2 3
| 4 5 60
| sage: table(rows, align='right', frame=True)
| +-----+---+----+
| | a | b | c |
| +-----+---+----+
| | 100 | 2 | 3 |
| +-----+---+----+
| | 4 | 5 | 60 |
| +-----+---+----+
|
| To print HTML, use either ``table(...)._html_()`` or ``html(table(...))``::
|
| sage: html(table([["$x$", "$\sin(x)$"]] + [(x,n(sin(x), digits=2)) for x in [0..3]], header_row=True, frame=True))
| <html>
| <div class="notruncate">
| <table border="1" class="table_form">
| <tbody>
| <tr>
| <th><script type="math/tex">x</script></th>
| <th><script type="math/tex">\sin(x)</script></th>
| </tr>
| <tr class ="row-a">
| <td><script type="math/tex">0</script></td>
| <td><script type="math/tex">0.00</script></td>
| </tr>
| <tr class ="row-b">
| <td><script type="math/tex">1</script></td>
| <td><script type="math/tex">0.84</script></td>
| </tr>
| <tr class ="row-a">
| <td><script type="math/tex">2</script></td>
| <td><script type="math/tex">0.91</script></td>
| </tr>
| <tr class ="row-b">
| <td><script type="math/tex">3</script></td>
| <td><script type="math/tex">0.14</script></td>
| </tr>
| </tbody>
| </table>
| </div>
| </html>
|
| It is an error to specify both ``rows`` and ``columns``::
|
| sage: table(rows=[[1,2,3], [4,5,6]], columns=[[0,0,0], [0,0,1024]])
| Traceback (most recent call last):
| ...
| ValueError: Don't set both 'rows' and 'columns' when defining a table.
|
| sage: table(columns=[[0,0,0], [0,0,1024]])
| 0 0
| 0 0
| 0 1024
|
| Note that if ``rows`` is just a list or tuple, not nested, then
| it is treated as a single row::
|
| sage: table([1,2,3])
| 1 2 3
|
| Also, if you pass a non-rectangular array, the longer rows or
| columns get truncated::
|
| sage: table([[1,2,3,7,12], [4,5]])
| 1 2
| 4 5
| sage: table(columns=[[1,2,3], [4,5,6,7]])
| 1 4
| 2 5
| 3 6
|
| TESTS::
|
| sage: TestSuite(table([["$x$", "$\sin(x)$"]] + [(x,n(sin(x), digits=2)) for x in [0..3]], header_row=True, frame=True)).run()
|
| Method resolution order:
| table
| sage.structure.sage_object.SageObject
| __builtin__.object
|
| Methods defined here:
|
| __eq__(self, other)
| Two tables are equal if and only if their data rowss and
| their options are the same.
|
| EXAMPLES::
|
| sage: rows = [['a', 'b', 'c'], [1,plot(sin(x)),3], [4,5,identity_matrix(2)]]
| sage: T = table(rows, header_row=True)
| sage: T2 = table(rows, header_row=True)
| sage: T is T2
| False
| sage: T == T2
| True
| sage: T2.options(frame=True)
| sage: T == T2
| False
|
| __init__(self, rows=None, columns=None, header_row=False, header_column=False, frame=False, align='left')
| EXAMPLES::
|
| sage: table([1,2,3], frame=True)
| +---+---+---+
| | 1 | 2 | 3 |
| +---+---+---+
|
| options(self, **kwds)
| With no arguments, return the dictionary of options for this
| table. With arguments, modify options.
|
| INPUTS:
|
| - ``header_row`` - if True, first row is highlighted.
| - ``header_column`` - if True, first column is highlighted.
| - ``frame`` - if True, put a box around each cell.
| - ``align`` - the alignment of each entry: either 'left',
| 'center', or 'right'
|
| EXAMPLES::
|
| sage: T = table([['a', 'b', 'c'], [1,2,3]])
| sage: T.options()['align'], T.options()['frame']
| ('left', False)
| sage: T.options(align='right', frame=True)
| sage: T.options()['align'], T.options()['frame']
| ('right', True)
|
| Note that when first initializing a table, ``header_row`` or
| ``header_column`` can be a list. In this case, during the
| initialization process, the header is merged with the rest of
| the data, so changing the header option later using
| ``table.options(...)`` doesn't affect the contents of the
| table, just whether the row or column is highlighed. When
| using this :meth:`options` method, no merging of data occurs,
| so here ``header_row`` and ``header_column`` should just be
| ``True`` or ``False``, not a list. ::
|
| sage: T = table([[1,2,3], [4,5,6]], header_row=['a', 'b', 'c'], frame=True)
| sage: T
| +---+---+---+
| | a | b | c |
| +===+===+===+
| | 1 | 2 | 3 |
| +---+---+---+
| | 4 | 5 | 6 |
| +---+---+---+
| sage: T.options(header_row=False)
| sage: T
| +---+---+---+
| | a | b | c |
| +---+---+---+
| | 1 | 2 | 3 |
| +---+---+---+
| | 4 | 5 | 6 |
| +---+---+---+
|
| If you do specify a list for ``header_row``, an error is raised::
|
| sage: T.options(header_row=['x', 'y', 'z'])
| Traceback (most recent call last):
| ...
| TypeError: header_row should be either True or False.
|
| transpose(self)
| Return a table which is the transpose of this one:
| rows and columns have been interchanged. Several of the
| properties of the original table are preserved: whether a
| frame is present and any alignment setting. On the other hand,
| header rows are converted to header columns, and vice versa.
|
| EXAMPLES::
|
| sage: T = table([[1,2,3], [4,5,6]])
| sage: T.transpose()
| 1 4
| 2 5
| 3 6
| sage: T = table([[1,2,3], [4,5,6]], header_row=['x', 'y', 'z'], frame=True)
| sage: T.transpose()
| +---++---+---+
| | x || 1 | 4 |
| +---++---+---+
| | y || 2 | 5 |
| +---++---+---+
| | z || 3 | 6 |
| +---++---+---+
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Methods inherited from sage.structure.sage_object.SageObject:
|
| __hash__(...)
| File: sage/structure/sage_object.pyx (starting at line 227)
|
| __repr__(...)
| File: sage/structure/sage_object.pyx (starting at line 121)
|
| Default method for string representation.
|
| .. NOTE::
|
| Do not overwrite this method. Instead, implement
| a ``_repr_`` (single underscore) method.
|
| EXAMPLES:
|
| By default, the string representation coincides with
| the output of the single underscore ``_repr_``::
|
| sage: P.<x> = QQ[]
| sage: repr(P) == P._repr_() #indirect doctest
| True
|
| Using :meth:`rename`, the string representation can
| be customized::
|
| sage: P.rename('A polynomial ring')
| sage: repr(P) == P._repr_()
| False
|
| The original behaviour is restored with :meth:`reset_name`.::
|
| sage: P.reset_name()
| sage: repr(P) == P._repr_()
| True
|
| category(...)
| File: sage/structure/sage_object.pyx (starting at line 397)
|
| db(...)
| File: sage/structure/sage_object.pyx (starting at line 368)
|
| Dumps self into the Sage database. Use db(name) by itself to
| reload.
|
| The database directory is ``$HOME/.sage/db``
|
| dump(...)
| File: sage/structure/sage_object.pyx (starting at line 340)
|
| Same as self.save(filename, compress)
|
| dumps(...)
| File: sage/structure/sage_object.pyx (starting at line 346)
|
| Dump ``self`` to a string ``s``, which can later be reconstituted
| as ``self`` using ``loads(s)``.
|
| There is an optional boolean argument ``compress`` which defaults to ``True``.
|
| EXAMPLES::
|
| sage: O=SageObject(); O.dumps()
| 'x\x9ck`J.NLO\xd5+.)*M.)-\x02\xb2\x80\xdc\xf8\xfc\xa4\xac\xd4\xe4\x12\xae` \xdb\x1f\xc2,d\xd4l,d\xd2\x03\x00\xb7X\x10\xf1'
| sage: O.dumps(compress=False)
| '\x80\x02csage.structure.sage_object\nSageObject\nq\x01)\x81q\x02.'
|
| rename(...)
| File: sage/structure/sage_object.pyx (starting at line 44)
|
| Change self so it prints as x, where x is a string.
|
| .. NOTE::
|
| This is *only* supported for Python classes that derive
| from SageObject.
|
| EXAMPLES::
|
| sage: x = PolynomialRing(QQ, 'x', sparse=True).gen()
| sage: g = x^3 + x - 5
| sage: g
| x^3 + x - 5
| sage: g.rename('a polynomial')
| sage: g
| a polynomial
| sage: g + x
| x^3 + 2*x - 5
| sage: h = g^100
| sage: str(h)[:20]
| 'x^300 + 100*x^298 - '
| sage: h.rename('x^300 + ...')
| sage: h
| x^300 + ...
|
| Real numbers are not Python classes, so rename is not supported::
|
| sage: a = 3.14
| sage: type(a)
| <type 'sage.rings.real_mpfr.RealLiteral'>
| sage: a.rename('pi')
| Traceback (most recent call last):
| ...
| NotImplementedError: object does not support renaming: 3.14000000000000
|
| .. NOTE::
|
| The reason C-extension types are not supported by default
| is if they were then every single one would have to carry
| around an extra attribute, which would be slower and waste
| a lot of memory.
|
| To support them for a specific class, add a
| ``cdef public __custom_name`` attribute.
|
| reset_name(...)
| File: sage/structure/sage_object.pyx (starting at line 101)
|
| Remove the custrom name of an object.
|
| EXAMPLES::
|
| sage: P.<x> = QQ[]
| sage: P
| Univariate Polynomial Ring in x over Rational Field
| sage: P.rename('A polynomial ring')
| sage: P
| A polynomial ring
| sage: P.reset_name()
| sage: P
| Univariate Polynomial Ring in x over Rational Field
|
| save(...)
| File: sage/structure/sage_object.pyx (starting at line 317)
|
| Save self to the given filename.
|
| EXAMPLES::
|
| sage: f = x^3 + 5
| sage: f.save(os.path.join(SAGE_TMP, 'file'))
| sage: load(os.path.join(SAGE_TMP, 'file.sobj'))
| x^3 + 5
|
| version(...)
| File: sage/structure/sage_object.pyx (starting at line 300)
|
| The version of Sage.
|
| Call this to save the version of Sage in this object.
| If you then save and load this object it will know in what
| version of Sage it was created.
|
| This only works on Python classes that derive from SageObject.
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from sage.structure.sage_object.SageObject:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T