I have a sympy expression like so:
exp_str = '3 * x**2*y + 4*a**2 * x*y + 9*b * x'
my_expr = sp.parsing.sympy_parser.parse_expr(exp_str)
and I want to get the coefficient of x*y, which should be 4*a**2.
Is there a function that I can pass my_expr to along with a list of variables I want my polynomial to be over? For example, I would need to pass this function x and y so that it knows x and y are variables and that a and b are coefficients.
If there is no such function, and recommendations on how to write code to do this would be appreciated. Thanks
There is a coeff method of sympy expressions:
In [28]: x, y, a, b = symbols('x, y, a, b')
In [29]: expr = 3 * x**2*y + 4*a**2 * x*y + 9*b * x
In [30]: expr.coeff(x*y)
Out[30]:
2
4⋅a
https://docs.sympy.org/latest/modules/core.html?highlight=coeff#sympy.core.expr.Expr.coeff
You might find it useful though to work with expressions as structured polynomials e.g.:
In [31]: p = Poly(expr, [x, y])
In [32]: p
Out[32]: Poly(3*x**2*y + 4*a**2*x*y + 9*b*x, x, y, domain='ZZ[a,b]')
In [33]: p.coeff_monomial(x**2 * y)
Out[33]: 3
In [34]: p.coeff_monomial(x * y)
Out[34]:
2
4⋅a
https://docs.sympy.org/latest/modules/polys/basics.html
Related
I have the following code where I define a composite function f(r(x,y),theta(x,y)), and I'm trying to simplify f.diff(x) assuming that I know an expression for f.diff(r):
x, y = sp.symbols("x y", real=True)
r = sp.Function('r')(x, y)
phi = sp.Function('phi')(x, y)
f = sp.Function('f')(r, phi)
df_dr = sp.Function('df_dr')(r, phi)
df_phi = sp.Function('df_phi')(r, phi)
Next, I compute the derivatives of f(r,phi) with respect to x:
f.diff(x)
And I get the following:
Derivative(phi(x, y), x)*Subs(Derivative(f(r(x, y), _xi_2), _xi_2), _xi_2, phi(x, y)) + Derivative(r(x, y), x)*Subs(Derivative(f(_xi_1, phi(x, y)), _xi_1), _xi_1, r(x, y))
My question is: how can I replace Subs(Derivative(f(_xi_1, phi(x, y)), _xi_1) by df_dr?
from sympy import *
x = Symbol('x')
c, k = symbols('c, k', real=True, nonnegative=True)
integrate(exp(-c * k * x**2), (x, -oo, oo))
Produces
(sqrt(pi)*sqrt(polar_lift(c))*sqrt(polar_lift(k))/(c*k)
I don't want polar_lift in the output, is there an assumption I can add, or something else I can do to get a cleaner result?
You've set the symbols to be nonnegative which implies the possibility that they are both zero in which case the integral does not converge. If the symbols are both positive (i.e. not zero) then you get a simple result:
In [85]: x = Symbol('x')
In [86]: c, k = symbols('c, k', positive=True)
In [87]: integrate(exp(-c * k * x**2), (x, -oo, oo))
Out[87]:
√π
─────
√c⋅√k
I find it strange that sympy cannot evaluate integrate(sec(x+1)**2, x) when it can evaluate integrate(sec(x)**2, x). I've restricted the domain of x just in case and I still can't evaluate the integral of `sec(x+1)**2.
x, y, z = symbols('x, y, z', real=True, positive=True)
Why does sympy struggle with this?
This is arguably a bug in SymPy but you can work around by rewriting sec:
>>> integrate(sec(x+1)**2, x)
Integral(sec(x + 1)**2, x)
>>> _.rewrite(cos) # sin or tan works too
Integral(cos(x + 1)**(-2), x)
>>> _.doit()
-2*tan(x/2 + 1/2)/(tan(x/2 + 1/2)**2 - 1)
>>> simplify(_)
tan(x + 1)
I am trying to solve something which may not actually be too complicated (mathematically), but I'm a bit lost and want to do this wich SymPy's aid. I
Given are two variables X, Y (these are symbolic, I do not have access to any values) and known constants a, b, ..., g. In a piece of existing software that I cannot change, from these
X' = a * X + b
Y' = c * Y + d
Z' = e * X * Y + f
are calculated. In another different piece of existing software that I cannot change,
I have different values, a similar calculation is done. I am greatly simplifying this for purposes of this question, but to get the idea:
X' and Y' like above
Z_ = (a * x + b) * ( c * y + d) - g
To bridge between Z' and Z_, I want to express z_ as a linear combination of X', Y', Z', and 1:
Z_ == 𝛂 * X' + 𝛃 * Y' + 𝛄 * Z' + 𝛅.
How can I solve this for 𝛂, 𝛃, 𝛄, 𝛅 with Sympy?
I know I could probably avoid Sympy, expressing each of the variables X', Y', Z', Z_ as linear combinations of X, Y, X*Y, and 1, and doing matrix algebra But since my problem actually is more complicated and its details likely to change in the future, I would really appreciate some software assistance.
I knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D
Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system 𝐪 ≥ 𝐚 + H 𝐪, where 𝐪 is the vector of all the variables, 𝐚 the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to 𝐪 ≥ 𝐚 + H 𝐪, in matrix form is 𝐪 = H* 𝐚.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system 𝐪 ≥ 𝐚 + H 𝐪.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
a: q → q, b: q → r, c: q → s,
d: r → q, e: r → r, f: r → s,
g: s → q, h: s → r, i: s → s.
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
q ≥ x + a q + b r + c s,
r ≥ y + d q + e r + f s,
s ≥ z + g q + h r + i s.
Solve for s, first, to obtain
s = i* (z + g q + h r) = i* z + i* g q + i* h r.
Substitute in the other inequalities to get:
q ≥ x + c i* z + (a + c i* g) q + (b + c i* h) r,
r ≥ y + f i* z + (d + f i* g) q + (e + f i* h) r.
Rewrite this as
q ≥ x' + a' q + b' r,
r ≥ y' + d' q + e' r,
where
x' = x + c i* z, a' = a + c i* g, b' = b + c i* h,
y' = y + f i* z, d' = d + f i* g, e' = e + f i* h.
Solve for r to get
r = e'* (y' + d' q) = e'* y' + e'* d' q.
Substitute into the inequality for q to get
q ≥ (x' + b' e'* y') + (a' + b e'* d') q
and rewrite this as
q ≥ x" + a" q
where
x" = x' + b' e'* y', a" = a' + b e'* d'.
Finally, solve this for q to get
q = a"* x".
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
a: q → r, a: q → q, b: q → q, b: q → s, a: s → s, b: s → s
The corresponding right-affine system is
q ≥ a r + a q + b q
r ≥ b s
s ≥ 1 + a s + b s
Solve for s first:
s ≥ 1 + a s + b s = 1 + (a + b) s ⇒ s = (a + b)*.
Substitute into the inequality for r and solve to find the least fixed point:
r ≥ b (a + b)* ⇒ r = b (a + b)*.
Finally, substitute into the inequality for q and solve to find the least fixed point:
q ≥ a b (a + b)* + (a + b) q ⇒ q = (a + b)* a b (a + b)*.
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.