Align the latex output of sympy? - python

The display of LaTeX equations in sympy appear as an unbroken line:
a = b + c + d(e - fg) + hij
I would like this to appear as something like
a = b
+ c
+ d(e
-
fg)
+ hij
or perhaps customizing how long the line can be before wrapping and aligning, like for example:
a = b + c
+ d(e - fg)
+ hij
Is such a thing possible?

Related

simplify (a + b*(c+d)) / (c+d) in sympy

Given an expression like
a + b⋅(c + d)
─────────────
c + d
I would like to use sympy to simplify it to:
a
───── + b
c + d
It works when I substitute (c+d) to e and back:
import sympy as sp
a,b,c,d,e = sp.symbols('a b c d e')
expr = (a + b*(c+d)) / (c+d)
expr = expr.subs({(c+d):e}).simplify().subs({e:c+d})
print( sp.pretty(expr) )
# prints
# a
# ───── + b
# c + d
Why is this? Is there a way to do it without substitution?
Using apart helps simplifying fractions :
expr = sp.apart((a + b*(c + d))/(c + d), a)
Output is:
a
───── + b
c + d

Setting terms of specific powers in a sympy expression to zero

I have a sympy expression of the following form:
a, b = symbols('a b')
expr = a + a/b + a/b**2 + a**2/b**2 + a/b**3
I want to set any terms where the exponent of b is larger than the exponent of a to zero, such that the result is like this:
newexpr = a + a/b + a**2/b**2
How can I achieve this?
I managed to solve this by doing:
import sympy as sp
a, b, c = sp.symbols('a b c')
expr = a + a/b + a/b**2 + a**2/b**2 + a/b**3
newexpr = sp.limit(expr.subs(a/b, c), b, sp.oo).subs(c, a/b)
newexpr
out[1]: a + a/b + a**2/b**2
Here is a short plan for this:
we make the substitution 1/b -> c
we rewrite the resulting expression as a multivariate polynomial in a,c
we decompose the poly into monomials and only keep those terms that have exp_a >= exp_c , and we finish by substituting back c -> 1/b
#!/usr/bin/python3
from sympy import *
a, b = symbols('a b')
expr = a + a/b + a/b**2 + a**2/b**2 + a/b**3
def truncate(e):
c = symbols('c')
e1 = e.subs(S(1)/b,c)
p = 0
for m in poly(e1,a,c).monoms():
exp_a,exp_c = m
if exp_c <= exp_a:
p += a**exp_a * (1/b)**exp_c
return p
truncated_expr = truncate(expr)
print(truncated_expr)
OUTPUT:
a**2/b**2 + a + a/b

How do you split a string after a symbol in python?

import sympy
equation = input('Enter an equation: ')
a = equation.split(('=') or ('<') or ('>') or ('<=') or ('>=') or ('==') or ('!='))[0:2]
b = sympify(a[0])
c = sympify(a[1])
d = simplify(b - c)
print('This is the equation simplified: ' + str(d))
I want to split the equation in two parts when one of the symbols (=,<,>,>=,<=,==,!=) appear, but in this code it only works when the '=' sign is the symbol.
I think your code should be like that:
import re #<--- add this
import sympy
equation = input('Enter an equation: ')
a = re.split(r'[=|<|>|<=|>=|==]', equation)[0:2] #<--- only change this
b = sympify(a[0])
c = sympify(a[1])
d = simplify(b - c)
print('This is the equation simplified: ' + str(d))

printing equation with variables in python

I am trying to print the equation with the variables
I have already tried to put all symbols in quotes
import random
import random
def ask():
a = raw_input("do you want the equation to be easy, medium, or hard: ")
b = int(raw_input("what is the number that you want to be the answer: "))
if(a == "easy"):
d = random.randint(1, 10)
e = random.randint(2, 5)
round(b)
print C = b - d + e - (e/2) + ((d - e) + e/2)
I wanted it to print out the equation with all the variables and symbols
when i type this in i get a syntax error
You cannot print out strings not in quotes. Put the bits you want to print out exactly as written in quotes, and print variables as is. For example:
print 'C =', b, '-', d, '+', e, '-', (e/2), '+', ((d - e/2)
Play around with that and see how you go. You'll want to think about how to do it differently if e.g. d-e/2 is negative.
Also round(b) will do nothing, it does not operate in-place.
try to put your equation in str() first,then print string
so that it will display equation before result.
then print out results
Here's what I think you want as a full solution. It accepts a single equation string as an input It then fills out that equation with the input variables, prints the resulting equation, and then evaluates it to provide a result:
import random
equation = "b - c + e - (e/2) + ((d- e) + e/2)"
b = 12
c = 24
d = random.randint(1, 10)
e = random.randint(2, 5)
# Expand the vlaues into the equation
equation = equation.replace('b', str(b)).replace('c', str(c)).replace('d', str(d)).replace('e', str(e))
# Print the equation
print "C = " + equation
# Evaluate the equation and print the result
C = eval(equation)
print "C = " + str(C)
Sample result:
C = 12 - 24 + 2 - (2/2) + ((6- 2) + 2/2)
C = -6
This code is just a demonstration of what can be done. You could take these ideas and generalize this to expand a map of variable names and values into an arbitrary expression without hard-coding the variable names. The map and equation could come, for example, from a file.

Regex in Python Equation Replacement

I'm somewhat new to regex and Python and am in the following situation. I'd like to take an equation string, like "A + B + C + 4D", and place the number 1 in front of all variables that have no number in front of them. So something like:
>>> foo_eqn = "A + B + C + 4D"
>>> bar_eqn = fill_in_ones(foo_eqn)
>>> bar_eqn
"1A + 1B + 1C + 4D"
After some research and asking, I came up with
def fill_in_ones(in_eqn):
out_eqn = re.sub(r"(\b[A-Z]\b)", "1"+ r"\1", in_eqn, re.I)
return(out_eqn)
However, it looks like this only works for the first two variables:
>>> fill_in_ones("A + B")
1A + 1B
>>> fill_in_ones("A + B + E")
1A + 1B + E
>>> fill_in_ones("2A + B + C + D")
2A + 1B + 1C + D
Anything really obvious I'm missing? Thanks!
Looks like the re.I (ignore case flag) is the culprit:
>>> def fill_in_ones(in_eqn):
... out_eqn = re.sub(r"(\b[A-Z]\b)", "1"+ r"\1", in_eqn)
... return(out_eqn)
...
>>>
>>> fill_in_ones("A + 3B + C + 2D + E")
'1A + 3B + 1C + 2D + 1E'
This is because the next positional argument to re.sub is count, not flags. You'll need:
def fill_in_ones(in_eqn):
out_eqn = re.sub(r"(\b[A-Z]\b)", "1"+ r"\1", in_eqn, flags=re.I)
return(out_eqn)
Unfortunately, the re.I flag happens to be 2:
>>> import re
>>> re.I
2

Categories

Resources