From 9c83b13ce1b08aed8181d284566d002086393a89 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Thu, 27 Feb 2020 16:30:54 -0800 Subject: ENH: Improved __str__, __format__ for polynomials Changes the printing style of instances of the convenience classes in the polynomial package to a more "human-readable" format. __str__ has been modified and __format__ added to ABCPolyBase, modifying the string representation of polynomial instances, e.g. when printed. __repr__ and the _repr_latex method (which is used in the Jupyter environment are unchanged. Two print formats have been added: 'unicode' and 'ascii'. 'unicode' is the default mode on *nix systems, and uses unicode values for numeric subscripts and superscripts in the polynomial expression. The 'ascii' format is the default on Windows (due to font considerations) and uses Python-style syntax to represent powers, e.g. x**2. The default printing style can be controlled at the package-level with the set_default_printstyle function. The ABCPolyBase.__str__ has also been made to respect the linewidth printoption. Other parameters from the printoptions dictionary are not used. Co-Authored-By: Warren Weckesser Co-authored-by: Eric Wieser --- numpy/polynomial/polynomial.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'numpy/polynomial/polynomial.py') diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py index 2fb032db3..97f1e7dc0 100644 --- a/numpy/polynomial/polynomial.py +++ b/numpy/polynomial/polynomial.py @@ -1495,6 +1495,14 @@ class Polynomial(ABCPolyBase): window = np.array(polydomain) basis_name = None + @classmethod + def _str_term_unicode(cls, i, arg_str): + return f"ยท{arg_str}{i.translate(cls._superscript_mapping)}" + + @staticmethod + def _str_term_ascii(i, arg_str): + return f" {arg_str}**{i}" + @staticmethod def _repr_latex_term(i, arg_str, needs_parens): if needs_parens: -- cgit v1.2.1