def test_derivative_numerically(f, z, tol=1.0e-6, a=2, b=-1, c=3, d=1): """ Test numerically that the symbolically computed derivative of f with respect to z is correct. Examples ======== >>> from sympy import sin, cos >>> from sympy.abc import x >>> from sympy.utilities.randtest import test_derivative_numerically as td >>> td(sin(x), x) True """ from sympy.core.function import Derivative z0 = random_complex_number(a, b, c, d) f1 = f.diff(z).subs(z, z0) f2 = Derivative(f, z).doit_numerically(z0) return comp(f1.n(), f2.n(), tol)
def test_derivative_numerically(f, z, tol=1.0e-6, a=2, b=-1, c=3, d=1): """ Test numerically that the symbolically computed derivative of f with respect to z is correct. This routine does not test whether there are Floats present with precision higher than 15 digits so if there are, your results may not be what you expect due to round-off errors. Examples ======== >>> from sympy import sin, cos >>> from sympy.abc import x >>> from sympy.utilities.randtest import test_derivative_numerically as td >>> td(sin(x), x) True """ from sympy.core.function import Derivative z0 = random_complex_number(a, b, c, d) f1 = f.diff(z).subs(z, z0) f2 = Derivative(f, z).doit_numerically(z0) return comp(f1.n(), f2.n(), tol)
def _eval_derivative(self, x): if x.is_real or self.args[0].is_real: return re(Derivative(self.args[0], x, evaluate=True)) if x.is_imaginary or self.args[0].is_imaginary: return -S.ImaginaryUnit \ * im(Derivative(self.args[0], x, evaluate=True))
def _eval_derivative(self, t): x, y = re(self.args[0]), im(self.args[0]) return (x * Derivative(y, t, evaluate=True) - y * Derivative(x, t, evaluate=True)) / (x**2 + y**2)
def test_del_operator(): # Tests for curl assert delop ^ Vector.zero == Vector.zero assert (delop ^ Vector.zero).doit() == Vector.zero == curl(Vector.zero) assert delop.cross(Vector.zero) == delop ^ Vector.zero assert (delop ^ i).doit() == Vector.zero assert delop.cross(2 * y ** 2 * j, doit=True) == Vector.zero assert delop.cross(2 * y ** 2 * j) == delop ^ 2 * y ** 2 * j v = x * y * z * (i + j + k) assert ( (delop ^ v).doit() == (-x * y + x * z) * i + (x * y - y * z) * j + (-x * z + y * z) * k == curl(v) ) assert delop ^ v == delop.cross(v) assert ( delop.cross(2 * x ** 2 * j) == (Derivative(0, C.y) - Derivative(2 * C.x ** 2, C.z)) * C.i + (-Derivative(0, C.x) + Derivative(0, C.z)) * C.j + (-Derivative(0, C.y) + Derivative(2 * C.x ** 2, C.x)) * C.k ) assert delop.cross(2 * x ** 2 * j, doit=True) == 4 * x * k == curl(2 * x ** 2 * j) # Tests for divergence assert delop & Vector.zero is S.Zero == divergence(Vector.zero) assert (delop & Vector.zero).doit() is S.Zero assert delop.dot(Vector.zero) == delop & Vector.zero assert (delop & i).doit() is S.Zero assert (delop & x ** 2 * i).doit() == 2 * x == divergence(x ** 2 * i) assert delop.dot(v, doit=True) == x * y + y * z + z * x == divergence(v) assert delop & v == delop.dot(v) assert delop.dot(1 / (x * y * z) * (i + j + k), doit=True) == -1 / ( x * y * z ** 2 ) - 1 / (x * y ** 2 * z) - 1 / (x ** 2 * y * z) v = x * i + y * j + z * k assert delop & v == Derivative(C.x, C.x) + Derivative(C.y, C.y) + Derivative( C.z, C.z ) assert delop.dot(v, doit=True) == 3 == divergence(v) assert delop & v == delop.dot(v) assert simplify((delop & v).doit()) == 3 # Tests for gradient assert delop.gradient(0, doit=True) == Vector.zero == gradient(0) assert delop.gradient(0) == delop(0) assert (delop(S.Zero)).doit() == Vector.zero assert ( delop(x) == (Derivative(C.x, C.x)) * C.i + (Derivative(C.x, C.y)) * C.j + (Derivative(C.x, C.z)) * C.k ) assert (delop(x)).doit() == i == gradient(x) assert ( delop(x * y * z) == (Derivative(C.x * C.y * C.z, C.x)) * C.i + (Derivative(C.x * C.y * C.z, C.y)) * C.j + (Derivative(C.x * C.y * C.z, C.z)) * C.k ) assert ( delop.gradient(x * y * z, doit=True) == y * z * i + z * x * j + x * y * k == gradient(x * y * z) ) assert delop(x * y * z) == delop.gradient(x * y * z) assert (delop(2 * x ** 2)).doit() == 4 * x * i assert (delop(a * sin(y) / x)).doit() == -a * sin(y) / x ** 2 * i + a * cos( y ) / x * j # Tests for directional derivative assert (Vector.zero & delop)(a) is S.Zero assert ((Vector.zero & delop)(a)).doit() is S.Zero assert ((v & delop)(Vector.zero)).doit() == Vector.zero assert ((v & delop)(S.Zero)).doit() is S.Zero assert ((i & delop)(x)).doit() == 1 assert ((j & delop)(y)).doit() == 1 assert ((k & delop)(z)).doit() == 1 assert ((i & delop)(x * y * z)).doit() == y * z assert ((v & delop)(x)).doit() == x assert ((v & delop)(x * y * z)).doit() == 3 * x * y * z assert (v & delop)(x + y + z) == C.x + C.y + C.z assert ((v & delop)(x + y + z)).doit() == x + y + z assert ((v & delop)(v)).doit() == v assert ((i & delop)(v)).doit() == i assert ((j & delop)(v)).doit() == j assert ((k & delop)(v)).doit() == k assert ((v & delop)(Vector.zero)).doit() == Vector.zero # Tests for laplacian on scalar fields assert laplacian(x * y * z) is S.Zero assert laplacian(x ** 2) == S(2) assert ( laplacian(x ** 2 * y ** 2 * z ** 2) == 2 * y ** 2 * z ** 2 + 2 * x ** 2 * z ** 2 + 2 * x ** 2 * y ** 2 ) A = CoordSys3D( "A", transformation="spherical", variable_names=["r", "theta", "phi"] ) B = CoordSys3D( "B", transformation="cylindrical", variable_names=["r", "theta", "z"] ) assert laplacian(A.r + A.theta + A.phi) == 2 / A.r + cos(A.theta) / ( A.r ** 2 * sin(A.theta) ) assert laplacian(B.r + B.theta + B.z) == 1 / B.r # Tests for laplacian on vector fields assert laplacian(x * y * z * (i + j + k)) == Vector.zero assert ( laplacian(x * y ** 2 * z * (i + j + k)) == 2 * x * z * i + 2 * x * z * j + 2 * x * z * k )
def test_Function(): assert precedence(sin(x)) == PRECEDENCE["Atom"] assert precedence(Derivative(x, y)) == PRECEDENCE["Atom"]
def _eval_derivative(self, x): return re(Derivative(self.args[0], x, **{'evaluate': True}))
def test_core_function(): x = Symbol("x") for f in (Derivative, Derivative(x), Function, FunctionClass, Lambda,\ WildFunction): check(f)
def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a / 2 - b / 2 + x * (a / 2 + b / 2 + 1) assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n) * gegenbauer( n, a + S.Half, x) / RisingFactorial(2 * a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a * (-x + 1)**(-a / 2) * (x + 1)**(a / 2) * assoc_legendre(n, a, x) * factorial(-a + n) * gamma(a + n + 1) / (factorial(a + n) * gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b / 2) * (x + 1)**(-b / 2) * assoc_legendre(n, b, x) * gamma(-b + n + 1) / gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(Rational( 3, 2), n) * chebyshevu(n, x) / factorial(n + 1) assert jacobi( n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial(S.Half, n) * chebyshevt(n, x) / factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n * jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n) * gamma(a + n + 1) * hyper( (-b - n, -n), (a + 1, ), -1) / (factorial(n) * gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n) / factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo * RisingFactorial(a + b + m + 1, m) assert unchanged(jacobi, n, a, b, oo) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) _k = Dummy('k') assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), a).dummy_eq( Sum((jacobi(n, a, b, x) + (2 * _k + a + b + 1) * RisingFactorial(_k + b + 1, -_k + n) * jacobi(_k, a, b, x) / ((-_k + n) * RisingFactorial(_k + a + b + 1, -_k + n))) / (_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), b).dummy_eq( Sum(((-1)**(-_k + n) * (2 * _k + a + b + 1) * RisingFactorial(_k + a + 1, -_k + n) * jacobi(_k, a, b, x) / ((-_k + n) * RisingFactorial(_k + a + b + 1, -_k + n)) + jacobi(n, a, b, x)) / (_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), x) == \ (a/2 + b/2 + n/2 + S.Half)*jacobi(n - 1, a + 1, b + 1, x) assert jacobi_normalized(n, a, b, x) == \ (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1) /((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))) raises(ValueError, lambda: jacobi(-2.1, a, b, x)) raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo)) assert jacobi(n, a, b, x).rewrite("polynomial").dummy_eq( Sum((S.Half - x / 2)**_k * RisingFactorial(-n, _k) * RisingFactorial(_k + a + 1, -_k + n) * RisingFactorial(a + b + n + 1, _k) / factorial(_k), (_k, 0, n)) / factorial(n)) raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def _as_finite_diff(derivative, points=1, x0=None, wrt=None): """ Returns an approximation of a derivative of a function in the form of a finite difference formula. The expression is a weighted sum of the function at a number of discrete values of (one of) the independent variable(s). Parameters ========== derivative: a Derivative instance points: sequence or coefficient, optional If sequence: discrete values (length >= order+1) of the independent variable used for generating the finite difference weights. If it is a coefficient, it will be used as the step-size for generating an equidistant sequence of length order+1 centered around ``x0``. default: 1 (step-size 1) x0: number or Symbol, optional the value of the independent variable (``wrt``) at which the derivative is to be approximated. Default: same as ``wrt``. wrt: Symbol, optional "with respect to" the variable for which the (partial) derivative is to be approximated for. If not provided it is required that the Derivative is ordinary. Default: ``None``. Examples ======== >>> from sympy import symbols, Function, exp, sqrt, Symbol, as_finite_diff >>> from sympy.utilities.exceptions import SymPyDeprecationWarning >>> import warnings >>> warnings.simplefilter("ignore", SymPyDeprecationWarning) >>> x, h = symbols('x h') >>> f = Function('f') >>> as_finite_diff(f(x).diff(x)) -f(x - 1/2) + f(x + 1/2) The default step size and number of points are 1 and ``order + 1`` respectively. We can change the step size by passing a symbol as a parameter: >>> as_finite_diff(f(x).diff(x), h) -f(-h/2 + x)/h + f(h/2 + x)/h We can also specify the discretized values to be used in a sequence: >>> as_finite_diff(f(x).diff(x), [x, x+h, x+2*h]) -3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h) The algorithm is not restricted to use equidistant spacing, nor do we need to make the approximation around ``x0``, but we can get an expression estimating the derivative at an offset: >>> e, sq2 = exp(1), sqrt(2) >>> xl = [x-h, x+h, x+e*h] >>> as_finite_diff(f(x).diff(x, 1), xl, x+h*sq2) 2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/((-h + E*h)*(h + E*h)) + (-(-sqrt(2)*h + h)/(2*h) - (-sqrt(2)*h + E*h)/(2*h))*f(-h + x)/(h + E*h) + (-(h + sqrt(2)*h)/(2*h) + (-sqrt(2)*h + E*h)/(2*h))*f(h + x)/(-h + E*h) Partial derivatives are also supported: >>> y = Symbol('y') >>> d2fdxdy=f(x,y).diff(x,y) >>> as_finite_diff(d2fdxdy, wrt=x) -Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y) See also ======== sympy.calculus.finite_diff.apply_finite_diff sympy.calculus.finite_diff.finite_diff_weights """ if derivative.is_Derivative: pass elif derivative.is_Atom: return derivative else: return derivative.fromiter( [_as_finite_diff(ar, points, x0, wrt) for ar in derivative.args], **derivative.assumptions0) if wrt is None: old = None for v in derivative.variables: if old is v: continue derivative = _as_finite_diff(derivative, points, x0, v) old = v return derivative order = derivative.variables.count(wrt) if x0 is None: x0 = wrt if not iterable(points): if getattr(points, 'is_Function', False) and wrt in points.args: points = points.subs(wrt, x0) # points is simply the step-size, let's make it a # equidistant sequence centered around x0 if order % 2 == 0: # even order => odd number of points, grid point included points = [x0 + points*i for i in range(-order//2, order//2 + 1)] else: # odd order => even number of points, half-way wrt grid point points = [x0 + points*S(i)/2 for i in range(-order, order + 1, 2)] others = [wrt, 0] for v in set(derivative.variables): if v == wrt: continue others += [v, derivative.variables.count(v)] if len(points) < order+1: raise ValueError("Too few points for order %d" % order) return apply_finite_diff(order, points, [ Derivative(derivative.expr.subs({wrt: x}), *others) for x in points], x0)
"\\frac{d}{d x}( \\frac{f{\\left(x \\right)}}{g{\left(x \\right)}})") expresion = expresion.replace( "\\frac{- f{\\left(x \\right)} \\frac{d}{d x} g{\\left(x \\right)} + g{\\left(x \\right)} \\frac{d}{d x} f{\\left(x \\right)}}{g^{2}{\\left(x \\right)}}", "\\frac{- f{\\left(x \\right)} \\frac{d}{d x}( g{\\left(x \\right)}) + g{\\left(x \\right)} \\frac{d}{d x}( f{\\left(x \\right))}}{g^{2}{\\left(x \\right)}}") expresion = expresion.replace("\\frac{d}{d x} f{\\left(x \\right)}", "\\frac{d}{d x}( f{\\left(x \\right)})") expresion = expresion.replace("\\frac{d}{d x} g{\\left(x \\right)}", "\\frac{d}{d x}( g{\\left(x \\right)})") return expresion ##MAIN## salida = open("/tmp/solucion_28d36c18-7985-42e7-bce1-11babf379715.txt","w") x = symbols('x') expr = parse_latex(r"2x^3+12x^2-30x-1").subs({Symbol('pi'): pi}) salida.write("Obtener: $$%s$$<br><br>" % latex(Derivative(expr, x))) solucion = print_html_steps(expr, x) solucion = acomodaNotacion(solucion) salida.write(solucion) derivada = Derivative(expr) derivada = derivada.doit() anula = solve(derivada, x) puntos = [] xmin,xmax = 0,0 ymin,ymax = 0,0 solucion = "Resolviendo $$%s=0$$ obtenemos las raices<br/>" % (latex(derivada)) n = 1 for x_0 in anula: solucion = solucion + "$$x_%s=%s$$ <br/>" % (n, latex(x_0)) n = n+1 n = 1
def _eval_diff(self, *args, **kwargs): if kwargs.pop("evaluate", True): return self.diff(*args) else: return Derivative(self, *args, **kwargs)
def _eval_derivative(self, x): if not self.has(x): return S.Zero return re(Derivative(self.args[0], x, **{'evaluate': True}))
def _eval_derivative(self, t): x, y = re(self.args[0]), im(self.args[0]) if not self.has(t): return S.Zero return (x * Derivative(y, t, **{'evaluate': True}) - y * Derivative(x, t, **{'evaluate': True})) / (x**2 + y**2)
def test_count_ops_visual(): ADD, MUL, POW, SIN, COS, EXP, AND, D, G, M = symbols( 'Add Mul Pow sin cos exp And Derivative Integral Sum'.upper()) DIV, SUB, NEG = symbols('DIV SUB NEG') LT, LE, GT, GE, EQ, NE = symbols('LT LE GT GE EQ NE') NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, _ITE, BASIC, TUPLE = symbols( 'Not Or And Xor Implies Equivalent ITE Basic Tuple'.upper()) def count(val): return count_ops(val, visual=True) assert count(7) is S.Zero assert count(S(7)) is S.Zero assert count(-1) == NEG assert count(-2) == NEG assert count(S(2) / 3) == DIV assert count(Rational(2, 3)) == DIV assert count(pi / 3) == DIV assert count(-pi / 3) == DIV + NEG assert count(I - 1) == SUB assert count(1 - I) == SUB assert count(1 - 2 * I) == SUB + MUL assert count(x) is S.Zero assert count(-x) == NEG assert count(-2 * x / 3) == NEG + DIV + MUL assert count(Rational(-2, 3) * x) == NEG + DIV + MUL assert count(1 / x) == DIV assert count(1 / (x * y)) == DIV + MUL assert count(-1 / x) == NEG + DIV assert count(-2 / x) == NEG + DIV assert count(x / y) == DIV assert count(-x / y) == NEG + DIV assert count(x**2) == POW assert count(-x**2) == POW + NEG assert count(-2 * x**2) == POW + MUL + NEG assert count(x + pi / 3) == ADD + DIV assert count(x + S.One / 3) == ADD + DIV assert count(x + Rational(1, 3)) == ADD + DIV assert count(x + y) == ADD assert count(x - y) == SUB assert count(y - x) == SUB assert count(-1 / (x - y)) == DIV + NEG + SUB assert count(-1 / (y - x)) == DIV + NEG + SUB assert count(1 + x**y) == ADD + POW assert count(1 + x + y) == 2 * ADD assert count(1 + x + y + z) == 3 * ADD assert count(1 + x**y + 2 * x * y + y**2) == 3 * ADD + 2 * POW + 2 * MUL assert count(2 * z + y + x + 1) == 3 * ADD + MUL assert count(2 * z + y**17 + x + 1) == 3 * ADD + MUL + POW assert count(2 * z + y**17 + x + sin(x)) == 3 * ADD + POW + MUL + SIN assert count(2 * z + y**17 + x + sin(x**2)) == 3 * ADD + MUL + 2 * POW + SIN assert count(2 * z + y**17 + x + sin(x**2) + exp(cos(x))) == 4 * ADD + MUL + 2 * POW + EXP + COS + SIN assert count(Derivative(x, x)) == D assert count(Integral(x, x) + 2 * x / (1 + x)) == G + DIV + MUL + 2 * ADD assert count(Sum(x, (x, 1, x + 1)) + 2 * x / (1 + x)) == M + DIV + MUL + 3 * ADD assert count(Basic()) is S.Zero assert count({x + 1: sin(x)}) == ADD + SIN assert count([x + 1, sin(x) + y, None]) == ADD + SIN + ADD assert count({x + 1: sin(x), y: cos(x) + 1}) == SIN + COS + 2 * ADD assert count({}) is S.Zero assert count([x + 1, sin(x) * y, None]) == SIN + ADD + MUL assert count([]) is S.Zero assert count(Basic()) == 0 assert count(Basic(Basic(), Basic(x, x + y))) == ADD + 2 * BASIC assert count(Basic(x, x + y)) == ADD + BASIC assert [count(Rel(x, y, op)) for op in '< <= > >= == <> !='.split() ] == [LT, LE, GT, GE, EQ, NE, NE] assert count(Or(x, y)) == OR assert count(And(x, y)) == AND assert count(Or(x, Or(y, And(z, a)))) == AND + OR assert count(Nor(x, y)) == NOT + OR assert count(Nand(x, y)) == NOT + AND assert count(Xor(x, y)) == XOR assert count(Implies(x, y)) == IMPLIES assert count(Equivalent(x, y)) == EQUIVALENT assert count(ITE(x, y, z)) == _ITE assert count([Or(x, y), And(x, y), Basic(x + y)]) == ADD + AND + BASIC + OR assert count(Basic(Tuple(x))) == BASIC + TUPLE #It checks that TUPLE is counted as an operation. assert count(Eq(x + y, S(2))) == ADD + EQ
(r"\frac{a}{b}", a / b), (r"\dfrac{a}{b}", a / b), (r"\tfrac{a}{b}", a / b), (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))), (r"(\csc x)(\sec y)", csc(x) * sec(y)), (r"\lim_{x \to 3} a", Limit(a, x, 3)), (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \Longrightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \to 3^{+}} a", Limit(a, x, 3, dir='+')), (r"\lim_{x \to 3^{-}} a", Limit(a, x, 3, dir='-')), (r"\infty", oo), (r"\lim_{x \to \infty} \frac{1}{x}", Limit(_Pow(x, -1), x, oo)), (r"\frac{d}{dx} x", Derivative(x, x)), (r"\frac{d}{dt} x", Derivative(x, t)), (r"f(x)", f(x)), (r"f(x, y)", f(x, y)), (r"f(x, y, z)", f(x, y, z)), (r"\frac{d f(x)}{dx}", Derivative(f(x), x)), (r"\frac{d\theta(x)}{dx}", Derivative(Function('theta')(x), x)), (r"x \neq y", Unequality(x, y)), (r"|x|", _Abs(x)), (r"||x||", _Abs(Abs(x))), (r"|x||y|", _Abs(x) * _Abs(y)), (r"||x||y||", _Abs(_Abs(x) * _Abs(y))), (r"\pi^{|xy|}", Symbol('pi')**_Abs(x * y)), (r"\int x dx", Integral(x, x)), (r"\int x d\theta", Integral(x, theta)), (r"\int (x^2 - y)dx", Integral(x**2 - y, x)),
def test_Derivative(): assert str(Derivative(x, y)) == "Derivative(x, y)" assert str(Derivative(x**2, x, evaluate=False)) == "Derivative(x**2, x)" assert str(Derivative( x**2/y, x, y, evaluate=False)) == "Derivative(x**2/y, x, y)"
def _eval_derivative(self, x): if x.is_real or self.args[0].is_real: return im(Derivative(self.args[0], x, **{'evaluate': True})) if x.is_imaginary or self.args[0].is_imaginary: return -S.ImaginaryUnit \ * re(Derivative(self.args[0], x, **{'evaluate': True}))
def test_issue_16160(): assert Derivative(x**3, (x, x)).subs(x, 2) == Subs( Derivative(x**3, (x, 2)), x, 2) assert Derivative(1 + x**3, (x, x)).subs(x, 0 ) == Derivative(1 + y**3, (y, 0)).subs(y, 0)
def test_Derivative(): assert precedence(Derivative(x, y)) == PRECEDENCE["Atom"]
def _eval_derivative(self, t): x, y = re(self.args[0]), im(self.args[0]) return (x * Derivative(y, t, **{'evaluate': True}) - y * Derivative(x, t, **{'evaluate': True})) / (x**2 + y**2)
def test_del_operator(): #Tests for curl assert (delop ^ Vector.zero == (Derivative(0, C.y) - Derivative(0, C.z)) * C.i + (-Derivative(0, C.x) + Derivative(0, C.z)) * C.j + (Derivative(0, C.x) - Derivative(0, C.y)) * C.k) assert ((delop ^ Vector.zero).doit() == Vector.zero == curl( Vector.zero, C)) assert delop.cross(Vector.zero) == delop ^ Vector.zero assert (delop ^ i).doit() == Vector.zero assert delop.cross(2 * y**2 * j, doit=True) == Vector.zero assert delop.cross(2 * y**2 * j) == delop ^ 2 * y**2 * j v = x * y * z * (i + j + k) assert ((delop ^ v).doit() == (-x * y + x * z) * i + (x * y - y * z) * j + (-x * z + y * z) * k == curl(v, C)) assert delop ^ v == delop.cross(v) assert (delop.cross( 2 * x**2 * j) == (Derivative(0, C.y) - Derivative(2 * C.x**2, C.z)) * C.i + (-Derivative(0, C.x) + Derivative(0, C.z)) * C.j + (-Derivative(0, C.y) + Derivative(2 * C.x**2, C.x)) * C.k) assert (delop.cross(2 * x**2 * j, doit=True) == 4 * x * k == curl( 2 * x**2 * j, C)) #Tests for divergence assert delop & Vector.zero == S(0) == divergence(Vector.zero, C) assert (delop & Vector.zero).doit() == S(0) assert delop.dot(Vector.zero) == delop & Vector.zero assert (delop & i).doit() == S(0) assert (delop & x**2 * i).doit() == 2 * x == divergence(x**2 * i, C) assert (delop.dot(v, doit=True) == x * y + y * z + z * x == divergence( v, C)) assert delop & v == delop.dot(v) assert delop.dot(1/(x*y*z) * (i + j + k), doit = True) == \ - 1 / (x*y*z**2) - 1 / (x*y**2*z) - 1 / (x**2*y*z) v = x * i + y * j + z * k assert (delop & v == Derivative(C.x, C.x) + Derivative(C.y, C.y) + Derivative(C.z, C.z)) assert delop.dot(v, doit=True) == 3 == divergence(v, C) assert delop & v == delop.dot(v) assert simplify((delop & v).doit()) == 3 #Tests for gradient assert (delop.gradient(0, doit=True) == Vector.zero == gradient(0, C)) assert delop.gradient(0) == delop(0) assert (delop(S(0))).doit() == Vector.zero assert (delop(x) == (Derivative(C.x, C.x)) * C.i + (Derivative(C.x, C.y)) * C.j + (Derivative(C.x, C.z)) * C.k) assert (delop(x)).doit() == i == gradient(x, C) assert (delop(x * y * z) == (Derivative(C.x * C.y * C.z, C.x)) * C.i + (Derivative(C.x * C.y * C.z, C.y)) * C.j + (Derivative(C.x * C.y * C.z, C.z)) * C.k) assert (delop.gradient(x * y * z, doit=True) == y * z * i + z * x * j + x * y * k == gradient(x * y * z, C)) assert delop(x * y * z) == delop.gradient(x * y * z) assert (delop(2 * x**2)).doit() == 4 * x * i assert ((delop(a * sin(y) / x)).doit() == -a * sin(y) / x**2 * i + a * cos(y) / x * j) #Tests for directional derivative assert (Vector.zero & delop)(a) == S(0) assert ((Vector.zero & delop)(a)).doit() == S(0) assert ((v & delop)(Vector.zero)).doit() == Vector.zero assert ((v & delop)(S(0))).doit() == S(0) assert ((i & delop)(x)).doit() == 1 assert ((j & delop)(y)).doit() == 1 assert ((k & delop)(z)).doit() == 1 assert ((i & delop)(x * y * z)).doit() == y * z assert ((v & delop)(x)).doit() == x assert ((v & delop)(x * y * z)).doit() == 3 * x * y * z assert (v & delop)(x + y + z) == C.x + C.y + C.z assert ((v & delop)(x + y + z)).doit() == x + y + z assert ((v & delop)(v)).doit() == v assert ((i & delop)(v)).doit() == i assert ((j & delop)(v)).doit() == j assert ((k & delop)(v)).doit() == k assert ((v & delop)(Vector.zero)).doit() == Vector.zero
def _eval_derivative(self, symbol): new_expr = Derivative(self.expr, symbol) return DifferentialOperator(new_expr, self.args[-1])
def test_twave(): A1, phi1, A2, phi2, f = symbols('A1, phi1, A2, phi2, f') n = Symbol('n') # Refractive index t = Symbol('t') # Time x = Symbol('x') # Spatial variable E = Function('E') w1 = TWave(A1, f, phi1) w2 = TWave(A2, f, phi2) assert w1.amplitude == A1 assert w1.frequency == f assert w1.phase == phi1 assert w1.wavelength == c / (f * n) assert w1.time_period == 1 / f assert w1.angular_velocity == 2 * pi * f assert w1.wavenumber == 2 * pi * f * n / c assert w1.speed == c / n w3 = w1 + w2 assert w3.amplitude == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) assert w3.frequency == f assert w3.phase == atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2)) assert w3.wavelength == c / (f * n) assert w3.time_period == 1 / f assert w3.angular_velocity == 2 * pi * f assert w3.wavenumber == 2 * pi * f * n / c assert w3.speed == c / n assert simplify(w3.rewrite(sin) - w2.rewrite(sin) - w1.rewrite(sin)) == 0 assert w3.rewrite('pde') == epsilon * mu * Derivative(E( x, t), t, t) + Derivative(E(x, t), x, x) assert w3.rewrite( cos) == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * cos(pi * f * n * x * s / (149896229 * m) - 2 * pi * f * t + atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2))) assert w3.rewrite( exp) == sqrt(A1**2 + 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * exp( I * (-2 * pi * f * t + atan2(A1 * sin(phi1) + A2 * sin(phi2), A1 * cos(phi1) + A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m))) w4 = TWave(A1, None, 0, 1 / f) assert w4.frequency == f w5 = w1 - w2 assert w5.amplitude == sqrt(A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) assert w5.frequency == f assert w5.phase == atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) assert w5.wavelength == c / (f * n) assert w5.time_period == 1 / f assert w5.angular_velocity == 2 * pi * f assert w5.wavenumber == 2 * pi * f * n / c assert w5.speed == c / n assert simplify(w5.rewrite(sin) - w1.rewrite(sin) + w2.rewrite(sin)) == 0 assert w5.rewrite('pde') == epsilon * mu * Derivative(E( x, t), t, t) + Derivative(E(x, t), x, x) assert w5.rewrite(cos) == sqrt( A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * cos(-2 * pi * f * t + atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m)) assert w5.rewrite( exp) == sqrt(A1**2 - 2 * A1 * A2 * cos(phi1 - phi2) + A2**2) * exp( I * (-2 * pi * f * t + atan2(A1 * sin(phi1) - A2 * sin(phi2), A1 * cos(phi1) - A2 * cos(phi2)) + pi * s * f * n * x / (149896229 * m))) w6 = 2 * w1 assert w6.amplitude == 2 * A1 assert w6.frequency == f assert w6.phase == phi1 w7 = -w6 assert w7.amplitude == -2 * A1 assert w7.frequency == f assert w7.phase == phi1 raises(ValueError, lambda: TWave(A1)) raises(ValueError, lambda: TWave(A1, f, phi1, t))
def _eval_derivative(self, t): x, y = self.args[0].as_real_imag() return (x * Derivative(y, t, evaluate=True) - y * Derivative(x, t, evaluate=True)) / (x**2 + y**2)
def curl(vect, coord_sys=None, doit=True): """ Returns the curl of a vector field computed wrt the base scalars of the given coordinate system. Parameters ========== vect : Vector The vector operand coord_sys : CoordSys3D The coordinate system to calculate the gradient in. Deprecated since version 1.1 doit : bool If True, the result is returned after calling .doit() on each component. Else, the returned expression contains Derivative instances Examples ======== >>> from sympy.vector import CoordSys3D, curl >>> R = CoordSys3D('R') >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k >>> curl(v1) 0 >>> v2 = R.x*R.y*R.z*R.i >>> curl(v2) R.x*R.y*R.j + (-R.x*R.z)*R.k """ coord_sys = _get_coord_sys_from_expr(vect, coord_sys) if len(coord_sys) == 0: return Vector.zero elif len(coord_sys) == 1: coord_sys = next(iter(coord_sys)) i, j, k = coord_sys.base_vectors() x, y, z = coord_sys.base_scalars() h1, h2, h3 = coord_sys.lame_coefficients() vectx = vect.dot(i) vecty = vect.dot(j) vectz = vect.dot(k) outvec = Vector.zero outvec += (Derivative(vectz * h3, y) - Derivative(vecty * h2, z)) * i / (h2 * h3) outvec += (Derivative(vectx * h1, z) - Derivative(vectz * h3, x)) * j / (h1 * h3) outvec += (Derivative(vecty * h2, x) - Derivative(vectx * h1, y)) * k / (h2 * h1) if doit: return outvec.doit() return outvec else: if isinstance(vect, (Add, VectorAdd)): from sympy.vector import express try: cs = next(iter(coord_sys)) args = [express(i, cs, variables=True) for i in vect.args] except ValueError: args = vect.args return VectorAdd.fromiter(curl(i, doit=doit) for i in args) elif isinstance(vect, (Mul, VectorMul)): vector = [ i for i in vect.args if isinstance(i, (Vector, Cross, Gradient)) ][0] scalar = Mul.fromiter( i for i in vect.args if not isinstance(i, (Vector, Cross, Gradient))) res = Cross(gradient(scalar), vector).doit() + scalar * curl(vector, doit=doit) if doit: return res.doit() return res elif isinstance(vect, (Cross, Curl, Gradient)): return Curl(vect) else: raise Curl(vect)
def test_Derivative_kind(): A = MatrixSymbol('A', 2, 2) assert Derivative(comm_x, comm_x).kind is NumberKind assert Derivative(A, comm_x).kind is MatrixKind(NumberKind)
def unreplace(eq, var): return eq.replace(diffx, lambda e: Derivative(e, var))
def _eval_derivative(self, x): if x.is_real: return conjugate(Derivative(self.args[0], x, evaluate=True)) elif x.is_imaginary: return -conjugate(Derivative(self.args[0], x, evaluate=True))
def test_big_expr(): f = Function('f') x = symbols('x') e1 = Dagger(AntiCommutator(Operator('A') + Operator('B'), Pow(DifferentialOperator(Derivative(f(x), x), f(x)), 3))*TensorProduct(Jz**2, Operator('A') + Operator('B')))*(JzBra(1, 0) + JzBra(1, 1))*(JzKet(0, 0) + JzKet(1, -1)) e2 = Commutator(Jz**2, Operator('A') + Operator('B'))*AntiCommutator(Dagger(Operator('C')*Operator('D')), Operator('E').inv()**2)*Dagger(Commutator(Jz, J2)) e3 = Wigner3j(1, 2, 3, 4, 5, 6)*TensorProduct(Commutator(Operator('A') + Dagger(Operator('B')), Operator('C') + Operator('D')), Jz - J2)*Dagger(OuterProduct(Dagger(JzBra(1, 1)), JzBra(1, 0)))*TensorProduct(JzKetCoupled(1, 1, (1, 1)) + JzKetCoupled(1, 0, (1, 1)), JzKetCoupled(1, -1, (1, 1))) e4 = (ComplexSpace(1)*ComplexSpace(2) + FockSpace()**2)*(L2(Interval( 0, oo)) + HilbertSpace()) assert str(e1) == '(Jz**2)x(Dagger(A) + Dagger(B))*{Dagger(DifferentialOperator(Derivative(f(x), x),f(x)))**3,Dagger(A) + Dagger(B)}*(<1,0| + <1,1|)*(|0,0> + |1,-1>)' ascii_str = \ """\ / 3 \\ \n\ |/ +\\ | \n\ 2 / + +\\ <| /d \\ | + +> \n\ /J \\ x \\A + B /*||DifferentialOperator|--(f(x)),f(x)| | ,A + B |*(<1,0| + <1,1|)*(|0,0> + |1,-1>)\n\ \\ z/ \\\\ \\dx / / / \ """ ucode_str = \ """\ ⎧ 3 ⎫ \n\ ⎪⎛ †⎞ ⎪ \n\ 2 ⎛ † †⎞ ⎨⎜ ⎛d ⎞ ⎟ † †⎬ \n\ ⎛J ⎞ ⨂ ⎝A + B ⎠⋅⎪⎜DifferentialOperator⎜──(f(x)),f(x)⎟ ⎟ ,A + B ⎪⋅(⟨1,0❘ + ⟨1,1❘)⋅(❘0,0⟩ + ❘1,-1⟩)\n\ ⎝ z⎠ ⎩⎝ ⎝dx ⎠ ⎠ ⎭ \ """ assert pretty(e1) == ascii_str assert upretty(e1) == ucode_str assert latex(e1) == \ r'{J_z^{2}}\otimes \left({A^{\dagger} + B^{\dagger}}\right) \left\{\left(DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)^{\dagger}\right)^{3},A^{\dagger} + B^{\dagger}\right\} \left({\left\langle 1,0\right|} + {\left\langle 1,1\right|}\right) \left({\left|0,0\right\rangle } + {\left|1,-1\right\rangle }\right)' sT(e1, "Mul(TensorProduct(Pow(JzOp(Symbol('J')), Integer(2)), Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), AntiCommutator(Pow(Dagger(DifferentialOperator(Derivative(Function('f')(Symbol('x')), Tuple(Symbol('x'), Integer(1))),Function('f')(Symbol('x')))), Integer(3)),Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), Add(JzBra(Integer(1),Integer(0)), JzBra(Integer(1),Integer(1))), Add(JzKet(Integer(0),Integer(0)), JzKet(Integer(1),Integer(-1))))") assert str(e2) == '[Jz**2,A + B]*{E**(-2),Dagger(D)*Dagger(C)}*[J2,Jz]' ascii_str = \ """\ [ 2 ] / -2 + +\\ [ 2 ]\n\ [/J \\ ,A + B]*<E ,D *C >*[J ,J ]\n\ [\\ z/ ] \\ / [ z]\ """ ucode_str = \ """\ ⎡ 2 ⎤ ⎧ -2 † †⎫ ⎡ 2 ⎤\n\ ⎢⎛J ⎞ ,A + B⎥⋅⎨E ,D ⋅C ⎬⋅⎢J ,J ⎥\n\ ⎣⎝ z⎠ ⎦ ⎩ ⎭ ⎣ z⎦\ """ assert pretty(e2) == ascii_str assert upretty(e2) == ucode_str assert latex(e2) == \ r'\left[J_z^{2},A + B\right] \left\{E^{-2},D^{\dagger} C^{\dagger}\right\} \left[J^2,J_z\right]' sT(e2, "Mul(Commutator(Pow(JzOp(Symbol('J')), Integer(2)),Add(Operator(Symbol('A')), Operator(Symbol('B')))), AntiCommutator(Pow(Operator(Symbol('E')), Integer(-2)),Mul(Dagger(Operator(Symbol('D'))), Dagger(Operator(Symbol('C'))))), Commutator(J2Op(Symbol('J')),JzOp(Symbol('J'))))") assert str(e3) == \ "Wigner3j(1, 2, 3, 4, 5, 6)*[Dagger(B) + A,C + D]x(-J2 + Jz)*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x|1,-1,j1=1,j2=1>" ascii_str = \ """\ [ + ] / 2 \\ \n\ /1 3 5\\*[B + A,C + D]x |- J + J |*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x |1,-1,j1=1,j2=1>\n\ | | \\ z/ \n\ \\2 4 6/ \ """ ucode_str = \ """\ ⎡ † ⎤ ⎛ 2 ⎞ \n\ ⎛1 3 5⎞⋅⎣B + A,C + D⎦⨂ ⎜- J + J ⎟⋅❘1,0⟩⟨1,1❘⋅(❘1,0,j₁=1,j₂=1⟩ + ❘1,1,j₁=1,j₂=1⟩)⨂ ❘1,-1,j₁=1,j₂=1⟩\n\ ⎜ ⎟ ⎝ z⎠ \n\ ⎝2 4 6⎠ \ """ assert pretty(e3) == ascii_str assert upretty(e3) == ucode_str assert latex(e3) == \ r'\left(\begin{array}{ccc} 1 & 3 & 5 \\ 2 & 4 & 6 \end{array}\right) {\left[B^{\dagger} + A,C + D\right]}\otimes \left({- J^2 + J_z}\right) {\left|1,0\right\rangle }{\left\langle 1,1\right|} \left({{\left|1,0,j_{1}=1,j_{2}=1\right\rangle } + {\left|1,1,j_{1}=1,j_{2}=1\right\rangle }}\right)\otimes {{\left|1,-1,j_{1}=1,j_{2}=1\right\rangle }}' sT(e3, "Mul(Wigner3j(Integer(1), Integer(2), Integer(3), Integer(4), Integer(5), Integer(6)), TensorProduct(Commutator(Add(Dagger(Operator(Symbol('B'))), Operator(Symbol('A'))),Add(Operator(Symbol('C')), Operator(Symbol('D')))), Add(Mul(Integer(-1), J2Op(Symbol('J'))), JzOp(Symbol('J')))), OuterProduct(JzKet(Integer(1),Integer(0)),JzBra(Integer(1),Integer(1))), TensorProduct(Add(JzKetCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1)))), JzKetCoupled(Integer(1),Integer(1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))), JzKetCoupled(Integer(1),Integer(-1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))))") assert str(e4) == '(C(1)*C(2)+F**2)*(L2(Interval(0, oo))+H)' ascii_str = \ """\ // 1 2\\ x2\\ / 2 \\\n\ \\\\C x C / + F / x \\L + H/\ """ ucode_str = \ """\ ⎛⎛ 1 2⎞ ⨂2⎞ ⎛ 2 ⎞\n\ ⎝⎝C ⨂ C ⎠ ⊕ F ⎠ ⨂ ⎝L ⊕ H⎠\ """ assert pretty(e4) == ascii_str assert upretty(e4) == ucode_str assert latex(e4) == \ r'\left(\left(\mathcal{C}^{1}\otimes \mathcal{C}^{2}\right)\oplus {\mathcal{F}}^{\otimes 2}\right)\otimes \left({\mathcal{L}^2}\left( \left[0, \infty\right) \right)\oplus \mathcal{H}\right)' sT(e4, "TensorProductHilbertSpace((DirectSumHilbertSpace(TensorProductHilbertSpace(ComplexSpace(Integer(1)),ComplexSpace(Integer(2))),TensorPowerHilbertSpace(FockSpace(),Integer(2)))),(DirectSumHilbertSpace(L2(Interval(Integer(0), oo, false, true)),HilbertSpace())))")
def test_derivative2(): f = Function("f") x = Symbol("x") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) e = Derivative(f(x), x) assert e.match(Derivative(f(x), x)) == {} assert e.match(Derivative(f(x), x, x)) is None e = Derivative(f(x), x, x) assert e.match(Derivative(f(x), x)) is None assert e.match(Derivative(f(x), x, x)) == {} e = Derivative(f(x), x) + x**2 assert e.match(a*Derivative(f(x), x) + b) == {a: 1, b: x**2} assert e.match(a*Derivative(f(x), x, x) + b) is None e = Derivative(f(x), x, x) + x**2 assert e.match(a*Derivative(f(x), x) + b) is None assert e.match(a*Derivative(f(x), x, x) + b) == {a: 1, b: x**2}
def _eval_derivative(self, x): if self.args[0].is_real: return Derivative(self.args[0], x, **{'evaluate': True}) * sign(self.args[0]) return (re(self.args[0]) * re(Derivative(self.args[0], x, **{'evaluate': True})) + im(self.args[0]) * im(Derivative(self.args[0], x, **{'evaluate': True}))) / Abs(self.args[0])