I have been using python to do algebraic calculations, but at many times when writing 1/2 Python writes 0.5, but I wanted you to write 1/2 and only when needed, convert it to a float.

I'm having a hard time imposing a simplification because Python doesn't recognize 32.0 as being 32, in short example situation where I have.

$$\frac{(32.0-64x)^2}{4096}$$

$$\frac{(1-2x)^2}{4}$$

• That's just how most programming languages work. If you want to do symbolic math, you need to use Maple or Mathematica. Feb 1, 2021 at 4:39

If you want to do symbolic math, there's a python-module called sympy.

In order the achive what you want here, you could do the following:

from sympy.abc import x
from sympy import simplify
f = (32 - 64 * x)**2 / 4096
print(f) # (32 - 64*x)**2/4096
print(simplify(f)) # (2*x - 1)**2/4

• I've been using the python-module sympy, but it doesn't have simplification. Feb 1, 2021 at 12:33
• @PEDROMORAIS, it does have simplification. I suggest that you check SymPy documentation. If you have similar questions you could use the mailing list or Gitter. Feb 1, 2021 at 16:37
• @nicoguaro Checking the SymPy documentation. I found the command nsimplify()which find a simple representation for a number or, if there are free symbols or if rational=True, then replace Floats with their Rational equivalents. If no change is made and rational is not False then Floats will at least be converted to Rationals. Feb 2, 2021 at 14:43
• @PEDROMORAIS, I suggest that you ask in Gitter. This is not the place to have this discussion. Feb 2, 2021 at 17:32
• @PEDROMORAIS, also. I suggest that you accept the answer. Feb 2, 2021 at 17:41