Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions and Exponents #14771

Open
bmccary opened this issue Jun 3, 2018 · 3 comments
Open

Functions and Exponents #14771

bmccary opened this issue Jun 3, 2018 · 3 comments
Labels

Comments

@bmccary
Copy link

bmccary commented Jun 3, 2018

To begin: I teach math at university and I find sympy and pythontex very useful for reasons I imagine are obvious to you.

Suppose I have the following python script.

from sympy import *
var('x')
latex((sin(x))**2) == r'\sin^{2} {\left(x\right)}` # Up to some insignificant whitespace
latex((ln(x))**2) == r'\log {\left(x\right)}^{2}` # Up to some insignificant whitespace

That is, (sin(x))**2 is rendered as sine squared x and (log(x))**2 is rendered as log x squared. Until recently, I believe that the logarithms would have followed the trigonometry functions in placing the exponent near the function. I imagine this change is related to the recent changes related to allowing log to be rendered as ln and the various rendering options for inverse trigonometric functions.

I'm familiar with the various notations, and I imagine I'll be able to distinguish whether or not \log^2 x means \log(\log(x)) or (\log(x))^2 by the context. I'm also comfortable with using operator precedence to disambiguate the competing interpretations of, say, \log(x)^2.

I also believe the non-stem students I teach are entirely capable of learning to navigate the intricacies of notation and operator precedence. But they come to first-year courses without that knowledge, and I haven't the time to give it to them. As a result, I take the strategy of always writing something that seems to have the least potential for ambiguity and/or requires the least consideration of operator precedence. So, in such first-year classes I write:

3 x % Never
3 \cdot x % Always

\ln x % Never
\ln (x) % Always

\ln^2 (x) % Never
{\ln (x)}^2 % Never
(\ln(x))^2 % Always

% Lots of other stuff

I immediately concede that the above may be highly opinionated and that opinions can differ. But is it possible for the printing to easily support a broader range of settings? I was glad to see the inclusion of the ln_notation option (for my students, but in my heart it will always be log). I'd like to be able to do something like:

latex((ln(x))**2, exponent_with_function_heresy=True) == r'\ln^2 (x)'
latex((ln(x))**2, know_operator_precedence_or_suffer=True) == r'{\ln(x)}^2'
latex((ln(x))**2, be_kind_to_the_first_year_students=True) == r'(\ln(x))^2'

Is there any technical/other objection to these possibilities?

@bmccary
Copy link
Author

bmccary commented Jun 3, 2018

I forked sympy and made a minimal edit to sympy/printing/latex.py which illustrates what I mean. Example use in an isympy session:

In [1]: latex((ln(x))**2)
Out[1]: '\\log{\\left (x \\right )}^{2}'

In [2]: latex((ln(x))**2, parenthesize_base_when_function=True)
Out[2]: '\\left(\\log{\\left (x \\right )}\\right)^{2}'

@asmeurer
Copy link
Member

asmeurer commented Jun 5, 2018

I agree we should change it or at least add an option.

By the way, it isn't necessary to fork SymPy to make the change. The printing system is designed to be extensible. You can just subclass LatexPrinter and define your own custom _print_Pow.

Since you have done so, however, you can send it as a pull request. It will need a test as well. The option name could be something simpler as well.

@oscarbenjamin
Copy link
Contributor

CC @oscargus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants