On Tue, Jun 23, 2020 at 2:59 PM Wes Turner <wes.tur...@gmail.com> wrote:
> > ∆y(P, H,L, E, I) := H * L^4 * P / (384 * E * I) > > ```python > Δy = lambda P, H, L, E, I: H * L**4 * P / (384 * E * I) > Δy > ``` > <function __main__.<lambda>(P, H, L, E, I)> > > Is there a good way to redefine the '^' operator for {int, float, Decimal, > Fraction, numbers.Number}? Why would it be dangerous to monkey-patch global > types this way? Could a context manager redefine `float.__xor__ = > float.__pow_` for just that scope? Why or why not? > > https://docs.python.org/3.4/library/operator.html#operator.__pow__ > > As far as syntactical preferences, LaTeX may be considered to be the > canonical non-executable syntax for mathematical expressions and equations. > SymPy can parse LaTeX into symbolic expressions. > Jupyter can render LaTeX embedded in Markdown. > SymPy displays expressions as LaTeX in Jupyter notebooks. > > ```python > from sympy import symbols > P, H, L, E, I = symbols('P, H, L, E, I') > Δy = H * L**4 * P / (384 * E * I) > Δy > ``` > $\frac{H L^{4} P}{384 E I}$ > > ```python > import sympy > sympy.sympify("H*L^4*P/(384*E_*I_)") > ``` > $\frac{H L^{4} P}{384 E_{} I_{}}$ > > https://docs.sympy.org/latest/modules/core.html#sympy.core.sympify.sympify > calls parse_expr with `convert_xor=True`, by default. > > > https://docs.sympy.org/latest/modules/parsing.html#sympy.parsing.sympy_parser.convert_xor > : > > Treats XOR, ^, as exponentiation, **. > > ```python > from sympy.parsing.sympy_parser import parse_expr, > standard_transformations,\ convert_xor > trx = (standard_transformations + > (convert_xor,)) > parse_expr("H*L^4*P/(384*E_*I_)", transformations=trx) > ``` > $\frac{H L^{4} P}{384 E_{} I_{}}$ > > > https://docs.sympy.org/latest/modules/parsing.html#experimental-mathrm-latex-parsing > : > > LaTeX Parsing Caveats > > The current implementation is experimental. The behavior, parser backend > and API might change in the future. Unlike some of the other parsers, LATEX > is designed as a type-setting language, not a computer algebra system and > so can contain typographical conventions that might be interpreted multiple > ways > > ```python > from sympy.parsing.latex import parse_latex > #parse_latex(r'\frac{H L^{4} P}{384 E_{} I_{}}') > parse_latex(r'\frac{H L^{4} P}{384 E I}') > ``` > $\displaystyle \frac{H L^{4} P}{384 E I}$ > > > SageMath defines ^ as operator.pow. CoCalc supports Sage notebooks as well > as Jupyter notebooks which import a CAS like SymPy, SymEngine, Diofant, > SageMath. > > https://ask.sagemath.org/question/49127/what-is-sage-equivalent-to-pow-in-sympy/ > > ... > https://www.lidavidm.me/blog/posts/2013-09-15-implicit-parsing-in-sympy.html > > > https://stackoverflow.com/questions/49284583/how-to-use-unicode-characters-as-variable-in-python-3/49284653 > > Thanks for all of those suggestions! Some of those I've explored, others I haven't. I feel like I ought to relent from hijacking this thread now. Apologies to all.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/2JHIWUP2VHX3E6RLTUXWULP4TTKVXTJ5/ Code of Conduct: http://python.org/psf/codeofconduct/