Writing long mathematival equations in python

hi i m trying to calculate long mathematical equation. But there is a problem. Python acts weird on math operation sequence. it calculates uncorrectly. So i need to put lots of prantheses"()" to my equation. But that time it is hard to write. İs there any easy way to write long equations?
i try to calculate this.

√(l(R^2+2b^2))/(b^(2 ) g)

after making 10 or 15 error i finally calculate with this code:

t=math.sqrt((l*((R2)+2*(b2)))/(b**2*g))

it was hard because every time i forgot some paranteses :smiley: still i got hard times when i tried to change equation. for example in last part "(b**2g)" i am not sure if it is calculate b^2 then multiply with g or first multiply 2g than make calculations.

my question is; is there any convenient , user-friendly and trustable way to calculate long mathematical equations.

Thank you.

1 Like

How does Python act weird? Operator precedence for mathematical operations is pretty standard and Python uses the same rules as everybody else (including mathematicians) for common binary operators.

i mean that when u try to calculate equation above , u made lots of mistake because of missing parantheses or extra prantheses. i want to find a way or a tool that make calculations correctly and easily.

lets try to calculate √(l(R^2+2b^2))/(b^(2 ) g) this equation with l=2, R=3,b=4,g=5 . the result should be 1.01242. could you do it in first time and easily? that is what i mean. thank you

I’m not sure if this helps, but the latexify_py package will convert a simple python function into a nice-looking LaTeX equation: latexify_py/equation.ipynb at develop · google/latexify_py · GitHub Maybe that could help you make sure the function is what you think it is.

Other than that, my rule of thumb is that if operator precedence is unclear, then add parentheses until it’s clear – there is no harm in extra parentheses. But I think you’ll find that most people (writing either equations or code) will rely on the fact that exponentiation binds tighter than multiplication and division, which bind tighter than addition and subtraction.

2 Likes

thank you. it really helps.

Yes, actually. I just started up IPython, pasted in your formula, and added a * for all multiplications, replaced the ^ with **. IPython has feature which highlights matching brackets (as do most programmer’s text editors), which showed me that there’s a bracket missing, so I added one at the end, and hey presto

math.sqrt((l*(R**2+2*b**2))/(b**(2 )* g))

No additional brackets required other than the one at the end that was missing in the original.

My point is that if you have an equation with standard mathematical operators and the brackets in the right place, then all you have to do is replace a few symbols (like using ** for exponentiation and possibly renaming variables) because Python operator precedence is, as far as I can tell, entirely what you’d expect it to be coming from maths.

What this exercise did remind me of is that standard tools that highlight matching brackets are really useful when you have a lot of brackets. Basically all programmers’ text editors and IDEs have this feature, as do IPython, Jupyter, and most other tools designed for writing code.

Of course it does take some practice to learn to read equations in this kind of plain-text format (and it takes practice to learn to format them in a readable way!).

If you’re unsure what order Python expressions with different operators are evaluatted in, the precedence of all the operators is listed in the reference documentation.

1 Like

i mean that when u try to calculate equation above , u made lots of mistake because of missing parantheses or extra prantheses. i want to find a way or a tool that make calculations correctly and easily.

lets try to calculate √(l(R^2+2b^2))/(b^(2 ) g) this equation with

l=2, R=3,b=4,g=5 . the result should be 1.01242.

No it shouldn’t. Substitute the values:

√(l(R^2+2b^2))/(b^(2 ) g)

= √(2×(3^2 + 2×4^2))/(4^2 × 5)

= √(2×(9 + 2×16))/(16 × 5)

= √(2×(9 + 32))/80

= √(2×41)/80

= √82/80

= 9.05554/80

= 0.11319

Your answer 1.01242 is incorrect. The only way to get that answer is to

shift one of the closing brackets so that the square root covers the

entire expression, not just the denominator:

√(l(R^2+2b^2) /(b^(2 ) g) )

See how I moved one closing bracket ) next to the division and placed it

all the way to the end? Now you get 1.01242 as the answer.

could you do it in

first time and easily? that is what i mean. thank you

Yes.

Change the √ to math.sqrt:

math.sqrt(l(R^2+2b^2))/(b^(2 ) g)

Change the ^ exponents to **

math.sqrt(l(R**2+2b**2))/(b**(2 ) g)

Remove the unnecessary brackets (parentheses) around the constant 2:

math.sqrt(l(R**2+2b**2))/(b**2 g)

Insert multiply operator * where you have implied multiplication:

math.sqrt(l*(R**2+2*b**2))/(b**2*g)

and there you go:

>>> import math

>>> l=2; R=3; b=4; g=5

>>> math.sqrt(l*(R**2+2*b**2))/(b**2*g)

0.11319231422671772

Perfect in one go! If you want the other answer, move the closing

bracket:

>>> math.sqrt(l*(R**2+2*b**2))/(b**2*g)

0.11319231422671772

>>> math.sqrt(l*(R**2+2*b**2)/(b**2*g))

1.0124228365658292

The problem here is not Python, it is that you had the bracket in the

wrong place.

this is what i want to say. u also get confused either 0.11 correct or 1.012. the correct answer is 1.012 :smiley: lets see with calculator result.

image

as u see it is not a very hard problem. but when u try to solve it in python it is getting harder and open for mistake. of course python does what we write. but writing this equation without any error is very hard thing. i m still asking easy or convenient way of writing equations. for examle go to symbolab.com and you can solve this equation very easily. but for now and for me it is still hard in python . ty

No, Stephen’s correct.

This is what you told us:

√(l(R^2+2b^2))/(b^(2 ) g)

This is what you have in the image:

√(l(R^2+2b^2)/(b^(2 ) g))

The brackets are in different places. The first equation equals 0.11319.
The second equation equals 1.01242. The answers are different because
the equations are different.

If you type the wrong equation, of course you will get an answer
different to what you expected.

Just to remind you that in case of negative value of b you will get result which you are probably not expecting:

>>> -2**2
-4

So you need additional brackets or use math.pow:

>>> (-2)**2
4
>>> import math
>>> math.pow(-2, 2)
4.0

Yes, I can do it in first time and easily.

>>> l=2; R=3; b=4; g=5
>>> import math
>>> math.sqrt(l*(R**2 + 2*b**2)/(b**2*g))
1.0124228365658292

What is your problem?

One can argue that digitizing long equations is tedious in general, not just in Python. User error can be high when entering equations.

Approaches

Here are two approaches to help you reduce user error:

  1. Split it up - easier to read and debug
# Given: (l*(R**2 + 2*b**2)/(b**2*g))**(1/2)
# Goal:  (X * Y / Z)**(1/2)
X = l
Y = R**2 + 2*b**2
Z = b**2*g
result = (X * Y / Z)**(1/2)
  1. Space it out - easier to see, edit and contract
# Given: (l*(R**2 + 2*b**2)/(b**2*g))**(1/2)
result = (
  l * (
    R**2 + 2*b**2
  )
  /
  (
    b**2*g
  )
)**(1/2)

Details

The given equation isn’t particularly complex, so one line is good enough. If it was longer, I might use the first approach in general. I would also recommend enclosing equations in a function, to reuse and prevent name-clobbering. Also, avoid using l as a name, which looks like 1.

def your_eqn(L, R, B, G):
    X = L
    Y = R**2 + 2*B**2
    Z = B**2*G
    result = (X * Y / Z)**(1/2)
    return result

The second approach is useful in a language like LaTex, having many more “functions” and no real assignment operations to save expressions into. The indentation helps to track matching parentheses, brackets, etc. Note: in Python, the outer parens are required.

1 Like