scambio.represent
Representation of objects as human-readable strings.
1r"""Representation of objects as human-readable strings.""" 2 3import numpy as np 4from collections.abc import Sequence 5from numbers import Real 6 7 8def repr_num_seq( 9 num_seq: Sequence[Real, ...], 10 *, 11 margin: int = 0, 12 width: int = 40, 13 precision: int = 2, 14) -> str: 15 r"""Represent numerical sequence as human-readable string. 16 17 Args: 18 num_seq: Numerical sequence. 19 margin: Line margin. 20 width: Line width. 21 precision: Decimal places. 22 23 Returns: 24 Representation. 25 """ 26 num_seq = np.asarray(num_seq, dtype=float).flatten() 27 text = ", ".join([f"{value:.{precision}f}" for value in num_seq]) 28 rows = [] 29 while len(text) > width: 30 i = text[:width].rfind(" ") 31 row = text[:i] 32 rows.append(row + "\n") 33 text = text[i + 1 :] 34 last_row = text 35 rows.append(last_row) 36 return (" " * margin).join(rows) 37 38 39def repr_poly(pcoeff: Sequence[Real, ...], *, precision: int = 2) -> str: 40 r"""Represent polynomial as human-readable string. 41 42 Args: 43 pcoeff: Coefficients in order of increasing degree. 44 precision: Decimal places. 45 46 Returns: 47 Representation. 48 """ 49 to_superscript = { 50 "0": "⁰", 51 "1": "¹", 52 "2": "²", 53 "3": "³", 54 "4": "⁴", 55 "5": "⁵", 56 "6": "⁶", 57 "7": "⁷", 58 "8": "⁸", 59 "9": "⁹", 60 } 61 pcoeff = np.asarray(pcoeff, dtype=float).flatten() 62 monomials = [] 63 for deg, coeff in enumerate(pcoeff): 64 monomial = f"{coeff:.{precision}f}" 65 if deg == 1: 66 monomial += "·e" 67 elif deg > 1: 68 deg = "".join([to_superscript[digit] for digit in str(deg)]) 69 monomial += f"·e{deg}" 70 monomials.append(monomial) 71 return " + ".join(monomials)
def
repr_num_seq( num_seq: collections.abc.Sequence[numbers.Real, ...], *, margin: int = 0, width: int = 40, precision: int = 2) -> str:
9def repr_num_seq( 10 num_seq: Sequence[Real, ...], 11 *, 12 margin: int = 0, 13 width: int = 40, 14 precision: int = 2, 15) -> str: 16 r"""Represent numerical sequence as human-readable string. 17 18 Args: 19 num_seq: Numerical sequence. 20 margin: Line margin. 21 width: Line width. 22 precision: Decimal places. 23 24 Returns: 25 Representation. 26 """ 27 num_seq = np.asarray(num_seq, dtype=float).flatten() 28 text = ", ".join([f"{value:.{precision}f}" for value in num_seq]) 29 rows = [] 30 while len(text) > width: 31 i = text[:width].rfind(" ") 32 row = text[:i] 33 rows.append(row + "\n") 34 text = text[i + 1 :] 35 last_row = text 36 rows.append(last_row) 37 return (" " * margin).join(rows)
Represent numerical sequence as human-readable string.
Arguments:
- num_seq: Numerical sequence.
- margin: Line margin.
- width: Line width.
- precision: Decimal places.
Returns:
Representation.
def
repr_poly( pcoeff: collections.abc.Sequence[numbers.Real, ...], *, precision: int = 2) -> str:
40def repr_poly(pcoeff: Sequence[Real, ...], *, precision: int = 2) -> str: 41 r"""Represent polynomial as human-readable string. 42 43 Args: 44 pcoeff: Coefficients in order of increasing degree. 45 precision: Decimal places. 46 47 Returns: 48 Representation. 49 """ 50 to_superscript = { 51 "0": "⁰", 52 "1": "¹", 53 "2": "²", 54 "3": "³", 55 "4": "⁴", 56 "5": "⁵", 57 "6": "⁶", 58 "7": "⁷", 59 "8": "⁸", 60 "9": "⁹", 61 } 62 pcoeff = np.asarray(pcoeff, dtype=float).flatten() 63 monomials = [] 64 for deg, coeff in enumerate(pcoeff): 65 monomial = f"{coeff:.{precision}f}" 66 if deg == 1: 67 monomial += "·e" 68 elif deg > 1: 69 deg = "".join([to_superscript[digit] for digit in str(deg)]) 70 monomial += f"·e{deg}" 71 monomials.append(monomial) 72 return " + ".join(monomials)
Represent polynomial as human-readable string.
Arguments:
- pcoeff: Coefficients in order of increasing degree.
- precision: Decimal places.
Returns:
Representation.