TransWikia.com

Questions about Python

Computational Science Asked by mhpedro on April 29, 2021

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}$

Since the answer could be

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

One Answer

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

Correct answer by Dagon on April 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP