How can I tell if a sympy expression is a vector? See the following example:
from sympy import *
from sympy.vector import *
N = CoordSys3D('N')
x = symbols('x')
v = x * N.i + x**2 * N.j + x**3 * N.k
type(v)
# sympy.vector.vector.VectorAdd
vf=factor(v)
vfs = vf.as_ordered_factors()
vfs
#[x, N.i + N.j*x + N.k*x**2]
type(vfs[1])
# sympy.core.add.Add
After v was factored, none of its factors have a sympy.vector... type. How can I tell which one of its factors is a vector? Is there a test for that?
SymPy has a variety of ways to search/parse expressions. Something that may work for you is the as_independent method:
>>> vf.as_independent(Vector)
(x, N.i + N.j*x + N.k*x**2)
You Vector-dependent part of vf will be the rightmost element.
Related
I am having trouble getting the sympy.py solve function to produce an answer to this problem. The answer should be Q/q = 2*sqrt(2).
import sympy as sy
q,Q,a, K = sy.symbols('q Q a K')
F21 = K * q * Q / a**2 * (-sy.I)
F41i = K * Q * Q / (2*a**2) * 1/sy.sqrt(2) * (sy.I)
Eqn = (F21 + F41i)
print(Eqn)
ans = sy.solve(Eqn,Q/q)
print(ans)
If the ratio does not appear as a literal expression, you must make it do so before attempting to solve for it. A common way to do this is to replace the numerator in the ratio with a dummy variable multiplied by the denominator, e.g.
>>> solve(Eq(x, Q/q), Q) # x = Q/q -> Q = q*x
[q*x]
>>> solve(Eqn.subs(Q, _[0]), x)
[0, 2*sqrt(2)]
x∂y/∂x=y+2x^3sin^2(y/x)
This is what I tried:
from math import *
from sympy import *
x = symbols('x')
y = symbols('y', cls=Function)
eq = Eq(Derivative(y(x),x), 2*x**2 * sin(y(x)/x)**(2) + y(x)/x)
display(eq)
an_sol = dsolve(eq, y(x))
display(an_sol)
But it shows problem:
NotImplementedError: The given ODE -2*x**2*sin(y(x)/x)**2 + Derivative(y(x), x) - y(x)/x cannot be solved by the lie group method
I have one exponential equation with two unknowns, say:
y*exp(ix) = sqrt(2) + i * sqrt(2)
Manually, I can transform it to system of trigonometric equations:
y * cos x = sqrt(2)
y * sin x = sqrt(2)
How can I do it automatically in sympy?
I tried this:
from sympy import *
x = Symbol('x', real=True)
y = Symbol('y', real=True)
eq = Eq(y * cos(I * x), sqrt(2) + I * sqrt(2))
print([e.trigsimp() for e in eq.as_real_imag()])
but only got two identical equations except one had "re" before it and another one "im".
You can call the method .rewrite(sin) or .rewrite(cos) to obtain the desired form of your equation. Unfortunately, as_real_imag cannot be called on an Equation directly but you could do something like this:
from sympy import *
def eq_as_real_imag(eq):
lhs_ri = eq.lhs.as_real_imag()
rhs_ri = eq.rhs.as_real_imag()
return Eq(lhs_ri[0], rhs_ri[0]), Eq(lhs_ri[1], rhs_ri[1])
x = Symbol('x', real=True)
y = Symbol('y', real=True)
original_eq = Eq(y*exp(I*x), sqrt(2) + I*sqrt(2))
trig_eq = original_eq.rewrite(sin) # Eq(y*(I*sin(x) + cos(x)), sqrt(2) + sqrt(2)*I)
eq_real, eq_imag = eq_as_real_imag(trig_eq)
print(eq_real) # Eq(y*cos(x), sqrt(2))
print(eq_imag) # Eq(y*sin(x), sqrt(2))
(You might also have more luck just working with expressions (implicitly understood to be 0) instead of an Equation e.g. eq.lhs - eq.rhs in order to call the method as_real_imag directly)
I need to calculate the norm of a vector using sympy and Symbol from sympy. An extract of the code is below:
from sympy import *
x = Symbol('x')
sb = [2,1]
func = sympy.exp(-(sympy.sqrt((x.norm() + (x-sb).norm())**2 - sb**2)/(2)))
func_prime = func.diff(x)
ff = lambdify(x, func_prime, 'numpy')
f = -1*ff(np.array(r))
The above implementation gives me the error: AttributeError: 'Symbol' object has no attribute 'norm'.
If I use func = V0 * sympy.exp(-(sympy.sqrt((sympy.sqrt(sum(x**2)) + sympy.sqrt(sum((x-sb*e)**2)))**2 - sb**2)/(2*sig))) I get the following error: TypeError: 'Pow' object is not iterable.
numpy.linalg.norm() is not accepted by sympy.
How can I find the norm of a vector in this situation?
Thanks
Sympy has a vector module you can use.
from sympy.vector import CoordSys3D
C = CoordSys3D('C')
v = 3*C.i + 4*C.j + 5*C.k
v.dot(v)
Output: 50
You can also use it with symbols:
from sympy import Symbol
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
v = x*C.i + y*C.j + z*C.k
sympy.sqrt(v.dot(v))
Output: sqrt(x**2 + y**2 + z**2)
Hello :) i suggest you the following:
import sympy as sp
import sympy.physics.vector as spv
M = spv.ReferenceFrame("M")
vector=V1*M.x+V2*M.y+V3*M.z
Where M.x, M.y, M.z are the unitary vectors i, j, k in physics. As we know the norm is the square root of the dot product of the vector with itself, so
norm=sp.sqrt(spv.dot(vector, vector))
print(norm)
If you want to print the result in LaTeX format
print(sp.latex(norm))
If you want to simplify the expresion,
print(norm.simplify())
x, y, z = symbols('x y z')
vec = Matrix([x, y, z])
vec_norm = sqrt(sum(sympy.matrices.dense.matrix_multiply_elementwise(vec, vec)))
How about this?
from sympy import *
x = Symbol('x')
y = Symbol('y')
a = Matrix([x,y])
anorm = sqrt(a.T # a)
I am trying to solve expression in SymPy.
x,c,m=symbols('x,c,m')
y = x**4 + sym.Rational(11/4)*(c/m)*(x**2)+(sym.Rational(3/2)*(c/m)**2)
solve(y,x)
Is there a way in Sympy where sqrt(-1) will automatically get formatted as i in the output expression instead of containing as -1 in the square root?
The equation results in [-sqrt(2)*sqrt(-c/m), sqrt(2)*sqrt(-c/m), -sqrt(3)*sqrt(-c/m)/2, sqrt(3)*sqrt(-c/m)/2]. Without any hints, all variable can be complex, and c/m could be a positive real. There isn't a reason to bring I out of the square root. (Note that sympy represents I with a capital.)
If, however, c and m are declared as real and positive, the output changes:
import sympy as sym
c, m = sym.symbols('c,m', real=True, positive=True)
x = sym.symbols('x')
y = x ** 4 + sym.Rational(11 / 4) * (c / m) * (x ** 2) + (sym.Rational(3 / 2) * (c / m) ** 2)
sym.solve(y, x)
Result:
[-sqrt(2)*I*sqrt(c)/sqrt(m),
sqrt(2)*I*sqrt(c)/sqrt(m),
-sqrt(3)*I*sqrt(c)/(2*sqrt(m)),
sqrt(3)*I*sqrt(c)/(2*sqrt(m))]