-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Mathematical expressions become unreadable #1984
Comments
Given that the most elegant presentation of a mathematical expression is highly variable, I think it would be best if So, the following code samples should all be stable, i.e., result = alpha * beta**zeta + gamma * delta + epsilon * sigma / 2
result = alpha*beta**zeta + gamma*delta + epsilon*sigma/2
result = (
alpha * beta ** zeta
+ gamma * delta
+ epsilon * sigma / 2
)
result = (
alpha*beta**zeta
+ gamma*delta
+ epsilon*sigma/2
) ...One might claim that the second sample above is the least readable, but readability of math expressions is often determined by the variable lengths. For instance, with shorter variable lengths: result = a * b ** z + g * d + e * s / 2
result = a*b**z + g*d + e*s/2
result = (
a * b ** z
+ g * d
+ e * s / 2
)
result = (
a*b**z
+ g*d
+ e*s/2
) I think the second sample is actually the most readable, and also the most succinct.
Also slightly related: #148 |
Just want to add some thoughts to this. A long expression like this need to be wrapped over several line area_ratio = 1 / mach_number * ((1 + 1/2 * (boltzmann_constant - 1) * mach_number ** 2) / (1/2 * (boltzmann_constant + 1))) ** (1/2 * (boltzmann_constant+1) / (boltzmann_constant-1)) black does this, but it's rather ugly. area_ratio = (
1
/ mach_number
* (
(1 + 1 / 2 * (boltzmann_constant - 1) * mach_number ** 2)
/ (1 / 2 * (boltzmann_constant + 1))
)
** (1 / 2 * (boltzmann_constant + 1) / (boltzmann_constant - 1))
) Specifically, The exponent is tabbed to the same level as the other lines, but it doesn't apply at the same level as the other operators, indeed it only applies to the previous term. So I propose that the exponent be tabbed to the level of the first parenthesis of the term that it applies to. So this would be a more desirable format. area_ratio = (
1 / mach_number
* (
(1 + 1 / 2 * (boltzmann_constant - 1) * mach_number ** 2)
/ (1 / 2 * (boltzmann_constant + 1))
)
** (1 / 2 * (boltzmann_constant + 1) / (boltzmann_constant - 1))
) Essentially, you should be able to visually parse the order of operations. You can clearly see in the above that you've got the inverse of a number multiplying a fraction, and that fraction is raised to an exponent |
Request: minimal support for reasonable formatting of mathematical expressions
Readability of mathematical expressions is critical for numerical code, and this is vitally dependent on reasonable code formatting.
Black
appears to have no sense of this issue, and produces code formatting that makes mathematical expressions not just obscure, but unreadable. I (developer ofscqubits
) love the idea of uniform code formatting. I also love the idea of enforcing uniformity by not providing options for formatting variations. The latter, however, can only work if the choicesblack
makes fulfill minimal requirements of readability. If there is any interest within theblack
development team to address this issue, I would be happy to contribute and advise on reasonable formatting of mathematical expressions.Evidence of the above
Reasonable style
The latter fits into the default 88 character line width just fine.
The text was updated successfully, but these errors were encountered: