How to merge 2 or more expressions
example: 3x + 2y - 2 and 2x + 3y -1 (3x + 2y -2 = 2x + 3y -1)
which gives x-y-1
from sympy import *
# declare symbols
var("x, y")
# write the two expressions
expr1 = 3*x + 2*y - 2
expr2 = 2*x + 3*y - 1
# subtract them
result = expr1 - expr2
result
# out: x - y - 1
EDIT: Alternatively, we can also create an Equality object and later rewrite it as an addition:
eqaulity = Eq(expr1, expr2)
# out: 3*x + 2*y - 2 = 2*x + 3*y - 1
eqaulity.rewrite(Add)
# out: x - y - 1
Related
I have the following code
import sympy as sp
x, y = sp.symbols('x y', real=True, positive=True)
expr = sp.sqrt(1/(x-y))/sp.sqrt(x+y)
the expression created should be equal to sqrt(1/(x^2 - y^2)) and I would like to rewrite it in that form but I cannot figure out how. I tried .rewrite(sp.sqrt), .rewrite(x**2 - y**2) and similar stuff but it did not help.
Ty for any tips on this,
Franz
The expression that you want is not equivalent to the one that you have:
In [53]: expr1 = sqrt(1/x - y)/sqrt(x + y)
In [54]: expr2 = 1/sqrt(x**2 - y**2)
In [55]: expr1
Out[55]:
________
╱ 1
╱ -y + ─
╲╱ x
────────────
_______
╲╱ x + y
In [56]: expr2
Out[56]:
1
────────────
_________
╱ 2 2
╲╱ x - y
In [58]: expr1.subs({x:1, y:2})
Out[58]:
√3⋅ⅈ
────
3
In [59]: expr2.subs({x:1, y:2})
Out[59]:
-√3⋅ⅈ
──────
3
That being said it should still be possible to do what you want using powsimp(expr1, force=True) but that doesn't seem to work in this case. I recommend opening a bug report about powsimp:
https://github.com/sympy/sympy/issues
I have some expression expr which is subject for analysis
c, l = symbols('c l', real=True, positive=True)
w = symbols('w', real=True)
expr = (w ** 2) / ((2 * c * l * w**4) - 3 * w**2 + (2/(c*l)))
expr
Output:
2
w
─────────────────────
4 2 2
2⋅c⋅l⋅w - 3⋅w + ───
c⋅l
I want to analyze this function w.r.t w. More specifically, I want to find the maximum value for the expression which should occur when
1
w = ─────
√c⋅√l
I have tried solving it for when the derivative is zero:
solve(diff(expr, w))
Output:
⎡⎧ 1 ⎫ ⎤
⎢⎪c: ────⎪, {w: 0}⎥
⎢⎨ 2⎬ ⎥
⎢⎪ l⋅w ⎪ ⎥
⎣⎩ ⎭ ⎦
I am not sure how to use the output above and express this as a function of l and c. I would prefer
2 1
w = ───
c⋅l
Is there a more standard way of analyzing expr with respect to w ?
You need to tell solve which symbol to solve for:
In [144]: solve(expr.diff(w), w)
Out[144]:
⎡ -1 1 ⎤
⎢0, ─────, ─────⎥
⎣ √c⋅√l √c⋅√l⎦
I have a list of polynomial expressions, (in my case obtained as the output of a Groebner basis computation), that I would like to view. I am using Jupyter, and I have started off with
import sympy as sy
sy.init_printing()
This causes an individual expression to be given nicely typeset. For a non-Groebner example:
sy.var('x')
fs = sy.factor_list(x**99-1)
fs2 = [x[0] for x in fs[1]]
fs2
The result is a nice list of LaTeX-typeset expressions. But how do I print these expressions one at a time; or rather; one per line? I've tried:
for f in fs2:
sy.pprint(f)
but this produces ascii pretty printing, not LaTeX. In general the expressions I have tend to be long, and I really do want to look at them individually. I can of course do
fs2[0]
fs2[1]
fs2[2]
and so on, but this is tiresome, and hardly useful for a long list. Any ideas or advice? Thanks!
Jupyter (through IPython) has a convenience function called display which works well with SymPy:
import sympy as sy
sy.init_printing()
sy.var('x')
fs = sy.factor_list(x**99-1)
fs2 = [x[0] for x in fs[1]]
for f in fs2:
display(f)
Output:
You can also get the latex code for each of these polynomials by using the latex function:
import sympy as sy
from sympy.printing.latex import latex
sy.init_printing()
sy.var('x')
fs = sy.factor_list(x**99-1)
fs2 = [x[0] for x in fs[1]]
for f in fs2:
print(latex(f))
Output:
x - 1
x^{2} + x + 1
x^{6} + x^{3} + 1
x^{10} + x^{9} + x^{8} + x^{7} + x^{6} + x^{5} + x^{4} + x^{3} + x^{2} + x + 1
x^{20} - x^{19} + x^{17} - x^{16} + x^{14} - x^{13} + x^{11} - x^{10} + x^{9} - x^{7} + x^{6} - x^{4} + x^{3} - x + 1
x^{60} - x^{57} + x^{51} - x^{48} + x^{42} - x^{39} + x^{33} - x^{30} + x^{27} - x^{21} + x^{18} - x^{12} + x^{9} - x^{3} + 1
I want to achieve this kind of simplification:
sqrt(2)*sqrt(pi)*(p**2 + (-p**2 + 4*pi**2)*exp(2*I*p) - 4*pi**2)*exp(-I*p)/(p**2 - 4*pi**2)**2=-2*I*sqrt(2*pi)*sin(p)/(p**2 - 4*pi**2)
However, sympy.simplify can't simplify this expression:
f=sqrt(2)*sqrt(pi)*(p**2 + (-p**2 + 4*pi**2)*exp(2*I*p) -\
4*pi**2)*exp(-I*p)/(p**2 - 4*pi**2)**2
print(sympy.simplify(f))
#sqrt(2)*sqrt(pi)*(p**2 + (-p**2 + 4*pi**2)*exp(2*I*p) - 4*pi**2)*exp(-I*p)/(p**2 - 4*pi**2)**2
How to simplify this expression with SymPy?
Besides, I don't want to use Piecewise((sqrt(2)*I/(2*sqrt(pi)), Eq(p, -2*pi))...)
Just massage the expression a bit. You know that with fractions, you generally factorize them and then you cancel like-terms. Then you simplify after that:
from sympy import *
p = Symbol("p", real=True)
f = sqrt(2)*sqrt(pi)*(p**2 + (-p**2 + 4*pi**2)*exp(2*I*p) - 4*pi**2)*exp(-I*p)/(p**2 - 4*pi**2)**2
f = simplify(expand(cancel(factor(f))))
print(f)
Gives
-2*sqrt(2)*I*sqrt(pi)*sin(p)/(p**2 - 4*pi**2)
The following example shows a simple DFA with one accepting state q2 :
Based on the R(i,j,k) Algorithm shown above i want to convert this DFA to regular expression, unfortunately i can't find a good definition of the K. My question is what does the K mean ?
is it the number of states (in this case 3) or something else?
Then, we solve those equations to get the equation for qi in terms of α ij and that expression is the required solution, where qi is a final state. It is shown below −
q 1= q a + q3a + € ( € move is because q1is the initial state)
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these equations, as shown below−
. q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab) * (Applying Arden's Theorem)
. q1 = q1a + q3a + €
= q1a + q2aa + € (Substituting value of q3)
= q1a + q1b(b + ab*)aa + € (Substituting value of q2)
= q1(a + b(b + ab) *aa) + €
= € (a+ b(b + ab) *aa) *
= (a + b(b + ab) *aa) *
Hence, the regular expression is (a + b(b + ab) *aa) *.