Set sympy output for 2 ** 3 * 2 ** 4 = 2 ** 7 instead of 128

I would like to set the output to sympy for expression 2**3 * 2**4 =in 2**7instead 128. Is there an easy way?

+4
source share
2 answers

This "works", maybe this is not what you really need

>>> from sympy import *
>>> x = Symbol('2')

>>> x
2

>>> x**3 * x**7
2**10


>>> z = x**3 * x**7

>>> z
2**10

>>> str(z)
'2**10'
>>> eval(str(z))
1024

note added (using @if clause ....)

>>> two = Symbol('2', positive=True, integer=True)
>>> z = two**3 * two**7
>>> z
2**10

# a little cleaner perhaps than eval(str(z))
# but requiring you to remember the name `two`
>>> z.subs(two, 2)
1024

addition also 'works'

>>> two**3 + two**7
2**7 + 2**3

>>> ((two**3 + two**7)).subs(two,two)
2**7 + 2**3

>>> ((two**3 + two**7)).subs(two, 2)
136
+5
source

Not really; you are asking to change the default display of integers as something outside the standard set of options. Regardless of the implementation details, it comes down to writing a function that takes an integer and returns the form of the exponent that you want to see as a character string.

+1
source

Source: https://habr.com/ru/post/1691797/


All Articles