<h1>Calculus</h1>

<p><a href="https://docs.sympy.org/latest/tutorial/calculus.html">From</a></p>

<p>This section covers how to do basic calculus tasks such as derivatives, integrals, limits, and series expansions in SymPy.  If you are not familiar with the math of any part of this section, you may safely skip it.</p>


```julia
    >>> from sympy import *
    >>> x, y, z = symbols('x y z')
    >>> init_printing(use_unicode=True)
```

<h5>In <code>Julia</code></h5>

<p>we have</p>

<ul>
<li><p>all functions from <code>sympy</code> are imported by default</p>
</li>
<li><p>unicode printing is enabled by default</p>
</li>
<li><p>double quotes are for strings</p>
</li>
</ul>

<p>so the above can be:</p>


```julia
using SymPy
x, y, z = symbols("x y z")
```




    (x, y, z)



<p>We can also use the convenient <code>@vars</code> macro, as with</p>


```julia
@vars x y z
```




    (x, y, z)



<hr />

<h2>Derivatives</h2>

<p>To take derivatives, use the <code>diff</code> function.</p>


```julia
    >>> diff(cos(x), x)
    -sin(x)
    >>> diff(exp(x**2), x)
         ⎛ 2⎞
         ⎝x ⎠
    2⋅x⋅ℯ
```

<h5>In  <code>Julia</code></h5>

<p>save for <code>**</code> becoming <code>^</code> this is the same</p>


```julia
diff(cos(x), x)
```




\begin{equation*}- \sin{\left (x \right )}\end{equation*}




```julia
diff(exp(x^2), x)
```




\begin{equation*}2 x e^{x^{2}}\end{equation*}



<hr />

<p><code>diff</code> can take multiple derivatives at once.  To take multiple derivatives, pass the variable as many times as you wish to differentiate, or pass a number after the variable.  For example, both of the following find the third derivative of <code>x^4</code>.</p>


```julia
    >>> diff(x**4, x, x, x)
    24⋅x
    >>> diff(x**4, x, 3)
    24⋅x
```

<h5>In <code>Julia</code></h5>


```julia
diff(x^4, x, x, x)
```




\begin{equation*}24 x\end{equation*}




```julia
diff(x^4, x, 3)
```




\begin{equation*}24 x\end{equation*}



<hr />

<p>You can also take derivatives with respect to many variables at once.  Just pass each derivative in order, using the same syntax as for single variable derivatives.  For example, each of the following will compute $\frac{\partial^7}{\partial x\partial y^2\partial z^4} e^{x y z}$.</p>


```julia
    >>> expr = exp(x*y*z)
    >>> diff(expr, x, y, y, z, z, z, z)
     3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z
    x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ
    >>> diff(expr, x, y, 2, z, 4)
     3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z
    x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ
    >>> diff(expr, x, y, y, z, 4)
     3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z
    x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ
```

<h5>In <code>Julia</code>:</h5>


```julia
expr = exp(x*y*z)
```




\begin{equation*}e^{x y z}\end{equation*}




```julia
diff(expr, x, y, y, z, z, z, z)
```




\begin{equation*}x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\end{equation*}




```julia
diff(expr, x, y, 2, z, 4)
```




\begin{equation*}x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\end{equation*}




```julia
diff(expr, x, y, y, z, 4)
```




\begin{equation*}x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\end{equation*}



<hr />

<p><code>diff</code> can also be called as a method.  The two ways of calling <code>diff</code> are exactly the same, and are provided only for convenience.</p>


```julia
    >>> expr.diff(x, y, y, z, 4)
     3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z
    x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ
```

<h5>In <code>Julia</code>:</h5>


```julia
expr.diff(x, y, y, z, 4)
```




\begin{equation*}x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\end{equation*}



<hr />

<p>To create an unevaluated derivative, use the <code>Derivative</code> class.  It has the same syntax as <code>diff</code>.</p>


```julia
    >>> deriv = Derivative(expr, x, y, y, z, 4)
    >>> deriv
         7
        ∂     ⎛ x⋅y⋅z⎞
    ──────────⎝ℯ     ⎠
      4   2
      ∂z  ∂y  ∂x
```

<h5>In <code>Julia</code>,</h5>

<p>classes are not exported, so we use <code>sympy.Derivative</code>:</p>


```julia
deriv = sympy.Derivative(expr, x, y, y, z, 4)
deriv
```




\begin{equation*}\frac{\partial^{7}}{\partial z^{4}\partial y^{2}\partial x} e^{x y z}\end{equation*}



<hr />

<p>To evaluate an unevaluated derivative, use the <code>doit</code> method.</p>


```julia
    >>> deriv.doit()
     3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z
    x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ
```

<h5>In <code>Julia</code>:</h5>


```julia
deriv.doit()
```




\begin{equation*}x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\end{equation*}



<hr />

<p>These unevaluated objects are useful for delaying the evaluation of the derivative, or for printing purposes.  They are also used when SymPy does not know how to compute the derivative of an expression (for example, if it contains an undefined function, which are described in the :ref:<code>Solving Differential Equations &lt;tutorial-dsolve&gt;</code> section).</p>

<p>Derivatives of unspecified order can be created using tuple <code>&#40;x, n&#41;</code> where <code>n</code> is the order of the derivative with respect to <code>x</code>.</p>


```julia
    >>> m, n, a, b = symbols('m n a b')
    >>> expr = (a*x + b)**m
    >>> expr.diff((x, n))
      n
     ∂ ⎛         m⎞
    ───⎝(a⋅x + b) ⎠
      n
    ∂x
```

<h5>In <code>Julia</code>:</h5>


```julia
@vars m n a b
expr = (a*x + b)^m
expr.diff((x, n))
```




\begin{equation*}\frac{\partial^{n}}{\partial x^{n}} \left(a x + b\right)^{m}\end{equation*}



<hr />

<h2>Integrals</h2>

<p>To compute an integral, use the <code>integrate</code> function.  There are two kinds of integrals, definite and indefinite.  To compute an indefinite integral, that is, an antiderivative, or primitive, just pass the variable after the expression.</p>


```julia
    >>> integrate(cos(x), x)
    sin(x)
```

<h5>In <code>Julia</code>:</h5>


```julia
integrate(cos(x), x)
```




\begin{equation*}\sin{\left (x \right )}\end{equation*}



<hr />

<p>Note that SymPy does not include the constant of integration.  If you want it, you can add one yourself, or rephrase your problem as a differential equation and use <code>dsolve</code> to solve it, which does add the constant (see :ref:<code>tutorial-dsolve</code>).</p>

<blockquote>
<p>Quick Tip:</p>
</blockquote>

<p>$\infty$ in SymPy is <code>oo</code> (that's the lowercase letter "oh" twice).  This    is because <code>oo</code> looks like $\infty$, and is easy to type.</p>

<hr />

<p>To compute a definite integral, pass the argument <code>&#40;integration_variable, lower_limit, upper_limit&#41;</code>.  For example, to compute</p>


$$

   \int_0^\infty e^{-x}\,dx,
$$


<p>we would do</p>


```julia
    >>> integrate(exp(-x), (x, 0, oo))
    1
```

<h5>In Julia:</h5>


```julia
integrate(exp(-x), (x, 0, oo))
```




\begin{equation*}1\end{equation*}



<hr />

<p>As with indefinite integrals, you can pass multiple limit tuples to perform a multiple integral.  For example, to compute</p>


$$
   \int_{-\infty}^{\infty}\int_{-\infty}^{\infty} e^{- x^{2} - y^{2}}\, dx\, dy,
$$


<p>do</p>


```julia
    >>> integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
    π
```

<h5>In <code>Julia</code>:</h5>


```julia
integrate(exp(-x^2 - y^2), (x, -oo, oo), (y, -oo, oo))
```




\begin{equation*}\pi\end{equation*}



<hr />

<p>If <code>integrate</code> is unable to compute an integral, it returns an unevaluated <code>Integral</code> object.</p>


```julia
    >>> expr = integrate(x**x, x)
    >>> print(expr)
    Integral(x**x, x)
    >>> expr
    ⌠
    ⎮  x
    ⎮ x  dx
    ⌡
```

<h5>In Julia:</h5>


```julia
expr = integrate(x^x, x)
expr
```




\begin{equation*}\int x^{x}\, dx\end{equation*}



<hr />

<p>As with <code>Derivative</code>, you can create an unevaluated integral using <code>Integral</code>.  To later evaluate this integral, call <code>doit</code>.</p>


```julia
    >>> expr = Integral(log(x)**2, x)
    >>> expr
    ⌠
    ⎮    2
    ⎮ log (x) dx
    ⌡
    >>> expr.doit()
             2
    x⋅log (x) - 2⋅x⋅log(x) + 2⋅x
```

<h5>In <code>Julia</code>:</h5>

<p>the <code>Integral</code> class is not exported, so it must be qualified:</p>


```julia
expr = sympy.Integral(log(x)^2, x)
expr
```




\begin{equation*}\int \log{\left (x \right )}^{2}\, dx\end{equation*}




```julia
expr.doit()
```




\begin{equation*}x \log{\left (x \right )}^{2} - 2 x \log{\left (x \right )} + 2 x\end{equation*}



<hr />

<p><code>integrate</code> uses powerful algorithms that are always improving to compute both definite and indefinite integrals, including heuristic pattern matching type algorithms, a partial implementation of the <code>Risch algorithm &lt;http://en.wikipedia.org/wiki/Risch_algorithm&gt;</code>, and an algorithm using <code>Meijer G-functions &lt;http://en.wikipedia.org/wiki/Meijer_g-function&gt;</code> that is useful for computing integrals in terms of special functions, especially definite integrals.  Here is a sampling of some of the power of <code>integrate</code>.</p>


```julia
    >>> integ = Integral((x**4 + x**2*exp(x) - x**2 - 2*x*exp(x) - 2*x -
    ...     exp(x))*exp(x)/((x - 1)**2*(x + 1)**2*(exp(x) + 1)), x)
    >>> integ
    ⌠
    ⎮ ⎛ 4    2  x    2        x          x⎞  x
    ⎮ ⎝x  + x ⋅ℯ  - x  - 2⋅x⋅ℯ  - 2⋅x - ℯ ⎠⋅ℯ
    ⎮ ──────────────────────────────────────── dx
    ⎮               2        2 ⎛ x    ⎞
    ⎮        (x - 1) ⋅(x + 1) ⋅⎝ℯ  + 1⎠
    ⌡
    >>> integ.doit()
                     x
       ⎛ x    ⎞     ℯ
    log⎝ℯ  + 1⎠ + ──────
                   2
                  x  - 1

    >>> integ = Integral(sin(x**2), x)
    >>> integ
    ⌠
    ⎮    ⎛ 2⎞
    ⎮ sin⎝x ⎠ dx
    ⌡
    >>> integ.doit()
                    ⎛√2⋅x⎞
    3⋅√2⋅√π⋅fresnels⎜────⎟⋅Γ(3/4)
                    ⎝ √π ⎠
    ─────────────────────────────
               8⋅Γ(7/4)

    >>> integ = Integral(x**y*exp(-x), (x, 0, oo))
    >>> integ
    ∞
    ⌠
    ⎮  y  -x
    ⎮ x ⋅ℯ   dx
    ⌡
    0
    >>> integ.doit()
    ⎧ Γ(y + 1)    for -re(y) < 1
    ⎪
    ⎪∞
    ⎪⌠
    ⎨⎮  y  -x
    ⎪⎮ x ⋅ℯ   dx    otherwise
    ⎪⌡
    ⎪0
    ⎩
```

<h5>In <code>Julia</code>:</h5>


```julia
integ = sympy.Integral((x^4 + x^2*exp(x) - x^2 - 2*x*exp(x) - 2*x - exp(x))*exp(x)/((x - 1)^2*(x + 1)^2*(exp(x) + 1)), x)
integ
```




\begin{equation*}\int \frac{\left(x^{4} + x^{2} e^{x} - x^{2} - 2 x e^{x} - 2 x - e^{x}\right) e^{x}}{\left(x - 1\right)^{2} \left(x + 1\right)^{2} \left(e^{x} + 1\right)}\, dx\end{equation*}




```julia
integ.doit()
```




\begin{equation*}\log{\left (e^{x} + 1 \right )} + \frac{e^{x}}{x^{2} - 1}\end{equation*}




```julia
integ = sympy.Integral(sin(x^2), x)
integ.doit()
```




\begin{equation*}\frac{3 \sqrt{2} \sqrt{\pi} S\left(\frac{\sqrt{2} x}{\sqrt{\pi}}\right) \Gamma\left(\frac{3}{4}\right)}{8 \Gamma\left(\frac{7}{4}\right)}\end{equation*}




```julia
integ = sympy.Integral(x^y*exp(-x), (x, 0, oo))
integ.doit()
```




\begin{equation*}\begin{cases} \Gamma\left(y + 1\right) & \text{for}\: - \Re{\left(y\right)} < 1 \\\int\limits_{0}^{\infty} x^{y} e^{- x}\, dx & \text{otherwise} \end{cases}\end{equation*}



<hr />

<p>This last example returned a <code>Piecewise</code> expression because the integral does not converge unless $\Re(y) > 1.$</p>

<h2>Limits</h2>

<p>SymPy can compute symbolic limits with the <code>limit</code> function.  The syntax to compute</p>


$$
   \lim_{x\to x_0} f(x)
$$


<p>is <code>limit&#40;f&#40;x&#41;, x, x0&#41;</code>.</p>


```julia
    >>> limit(sin(x)/x, x, 0)
    1
```

<h5>In <code>Julia</code>:</h5>


```julia
limit(sin(x)/x, x, 0)
```




\begin{equation*}1\end{equation*}



<p>In <code>Julia</code>, a pair can be used to indicate the limit:</p>


```julia
limit(sin(x)/x, x=>0)
```




\begin{equation*}1\end{equation*}



<hr />

<p><code>limit</code> should be used instead of <code>subs</code> whenever the point of evaluation is a singularity.  Even though SymPy has objects to represent $\infty$, using them for evaluation is not reliable because they do not keep track of things like rate of growth.  Also, things like $\infty - \infty$ and $\frac{\infty}{\infty}$ return $\mathrm{nan}$ (not-a-number).  For example</p>


```julia
    >>> expr = x**2/exp(x)
    >>> expr.subs(x, oo)
    nan
    >>> limit(expr, x, oo)
    0
```

<h5>In <code>Julia</code>:</h5>


```julia
expr = x^2/exp(x)
expr.subs(x, oo)
```




\begin{equation*}\mathrm{NaN}\end{equation*}




```julia
limit(expr, x, oo)
```




\begin{equation*}0\end{equation*}



<hr />

<p>Like <code>Derivative</code> and <code>Integral</code>, <code>limit</code> has an unevaluated counterpart, <code>Limit</code>.  To evaluate it, use <code>doit</code>.</p>


```julia
    >>> expr = Limit((cos(x) - 1)/x, x, 0)
    >>> expr
         ⎛cos(x) - 1⎞
     lim ⎜──────────⎟
    x─→0⁺⎝    x     ⎠
    >>> expr.doit()
    0
```

<h5>In <code>Julia</code>:</h5>


```julia
expr = sympy.Limit((cos(x) - 1)/x, x, 0)
expr
```




\begin{equation*}\lim_{x \to 0^+}\left(\frac{\cos{\left (x \right )} - 1}{x}\right)\end{equation*}




```julia
expr.doit()
```




\begin{equation*}0\end{equation*}



<p>To evaluate a limit at one side only, pass <code>&#39;&#43;&#39;</code> or <code>&#39;-&#39;</code> as a third argument to <code>limit</code>.  For example, to compute</p>


$$
   \lim_{x\to 0^+}\frac{1}{x},
$$


<p>do</p>


```julia
    >>> limit(1/x, x, 0, '+')
    ∞
```

<h5>In <code>Julia</code>:</h5>


```julia
limit(1/x, x => 0, "+")
```




\begin{equation*}\infty\end{equation*}



<hr />

<p>As opposed to</p>


```julia
    >>> limit(1/x, x, 0, '-')
    -∞
```

<h5>In <code>Julia</code>:</h5>


```julia
limit(1/x, x, 0, "-")
```




\begin{equation*}-\infty\end{equation*}



<hr />

<h2>Series Expansion</h2>

<p>SymPy can compute asymptotic series expansions of functions around a point. To compute the expansion of <code>f&#40;x&#41;</code> around the point <code>x &#61; x_0</code> terms of order <code>x^n</code>, use <code>f&#40;x&#41;.series&#40;x, x0, n&#41;</code>.  <code>x0</code> and <code>n</code> can be omitted, in which case the defaults <code>x0&#61;0</code> and <code>n&#61;6</code> will be used.</p>


```julia
    >>> expr = exp(sin(x))
    >>> expr.series(x, 0, 4)
             2
            x     ⎛ 4⎞
    1 + x + ── + O⎝x ⎠
            2
```

<h5>In <code>Julia</code>:</h5>


```julia
expr = exp(sin(x))
expr.series(x, 0, 4)
```




\begin{equation*}1 + x + \frac{x^{2}}{2} + O\left(x^{4}\right)\end{equation*}



<hr />

<p>The <code>O\left &#40;x^4\right &#41;</code> term at the end represents the Landau order term at <code>x&#61;0</code> (not to be confused with big O notation used in computer science, which generally represents the Landau order term at $x=\infty$).  It means that all x terms with power greater than or equal to <code>x^4</code> are omitted.  Order terms can be created and manipulated outside of <code>series</code>.  They automatically absorb higher order terms.</p>


```julia
    >>> x + x**3 + x**6 + O(x**4)
         3    ⎛ 4⎞
    x + x  + O⎝x ⎠
    >>> x*O(1)
    O(x)
```

<h5>In <code>Julia</code>:</h5>

<p><code>O</code> is not exported, so we must qualify it:</p>


```julia
x + x^3 + x^6 + sympy.O(x^4)

```




\begin{equation*}x + x^{3} + O\left(x^{4}\right)\end{equation*}




```julia
x*sympy.O(1)
```




\begin{equation*}O\left(x\right)\end{equation*}



<hr />

<p>If you do not want the order term, use the <code>removeO</code> method.</p>


```julia
    >>> expr.series(x, 0, 4).removeO()
     2
    x
    ── + x + 1
    2
```

<h5>In <code>Julia</code>:</h5>


```julia
expr.series(x, 0, 4).removeO()
```




\begin{equation*}\frac{x^{2}}{2} + x + 1\end{equation*}



<hr />

<p>The <code>O</code> notation supports arbitrary limit points (other than 0):</p>


```julia
    >>> exp(x - 6).series(x, x0=6)
                2          3          4          5
         (x - 6)    (x - 6)    (x - 6)    (x - 6)         ⎛       6       ⎞
    -5 + ──────── + ──────── + ──────── + ──────── + x + O⎝(x - 6) ; x → 6⎠
            2          6          24        120
```

<h5>In <code>Julia</code>:</h5>


```julia
exp(x - 6).series(x, x0=6)
```




\begin{equation*}-5 + \frac{\left(x - 6\right)^{2}}{2} + \frac{\left(x - 6\right)^{3}}{6} + \frac{\left(x - 6\right)^{4}}{24} + \frac{\left(x - 6\right)^{5}}{120} + x + O\left(\left(x - 6\right)^{6}; x\rightarrow 6\right)\end{equation*}



<h2>Finite differences</h2>

<p>So far we have looked at expressions with analytic derivatives and primitive functions respectively. But what if we want to have an expression to estimate a derivative of a curve for which we lack a closed form representation, or for which we don't know the functional values for yet. One approach would be to use a finite difference approach.</p>

<p>The simplest way the differentiate using finite differences is to use the <code>differentiate_finite</code> function:</p>


```julia
    >>> f, g = symbols('f g', cls=Function)
    >>> differentiate_finite(f(x)*g(x))
    -f(x - 1/2)⋅g(x - 1/2) + f(x + 1/2)⋅g(x + 1/2)
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p><code>differentiate_finite</code> is not exported</p>
</li>
</ul>


```julia
f, g = symbols("f g", cls=sympy.Function)
sympy.differentiate_finite(f(x)*g(x))
```




\begin{equation*}- f{\left (x - \frac{1}{2} \right )} g{\left (x - \frac{1}{2} \right )} + f{\left (x + \frac{1}{2} \right )} g{\left (x + \frac{1}{2} \right )}\end{equation*}



<p>(The functions <code>f</code> and <code>g</code> can also be created with the command <code>@symfuns f g</code>, using the <code>@symfuns</code> macro.)</p>

<hr />

<p>If we want to expand the intermediate derivative we may pass the flag <code>evaluate&#61;True</code>:</p>


```julia
    >>> differentiate_finite(f(x)*g(x), evaluate=True)
    (-f(x - 1/2) + f(x + 1/2))⋅g(x) + (-g(x - 1/2) + g(x + 1/2))⋅f(x)
```

<h5>In <code>Julia</code>:</h5>


```julia
sympy.differentiate_finite(f(x)*g(x), evaluate=true)
```




\begin{equation*}\left(- f{\left (x - \frac{1}{2} \right )} + f{\left (x + \frac{1}{2} \right )}\right) g{\left (x \right )} + \left(- g{\left (x - \frac{1}{2} \right )} + g{\left (x + \frac{1}{2} \right )}\right) f{\left (x \right )}\end{equation*}



<hr />

<p>This form however does not respect the product rule.</p>

<p>If you already have a <code>Derivative</code> instance, you can use the <code>as_finite_difference</code> method to generate approximations of the derivative to arbitrary order:</p>


```julia
    >>> f = Function('f')
    >>> dfdx = f(x).diff(x)
    >>> dfdx.as_finite_difference()
    -f(x - 1/2) + f(x + 1/2)
```

<h5>In <code>Julia</code>:</h5>


```julia
f = sympy.Function("f")
dfdx = f(x).diff(x)
dfdx.as_finite_difference()
```




\begin{equation*}- f{\left (x - \frac{1}{2} \right )} + f{\left (x + \frac{1}{2} \right )}\end{equation*}



<hr />

<p>here the first order derivative was approximated around x using a minimum number of points (2 for 1st order derivative) evaluated equidistantly using a step-size of 1. We can use arbitrary steps (possibly containing symbolic expressions):</p>


```julia
    >>> f = Function('f')
    >>> d2fdx2 = f(x).diff(x, 2)
    >>> h = Symbol('h')
    >>> d2fdx2.as_finite_difference([-3*h,-h,2*h])
    f(-3⋅h)   f(-h)   2⋅f(2⋅h)
    ─────── - ───── + ────────
         2        2        2
      5⋅h      3⋅h     15⋅h
```

<h5>In <code>Julia</code>:</h5>


```julia
f = sympy.Function("f")
d2fdx2 = f(x).diff(x, 2)
h = sympy.Symbol("h")
d2fdx2.as_finite_difference([-3*h,-h,2*h])
```




\begin{equation*}\frac{f{\left (- 3 h \right )}}{5 h^{2}} - \frac{f{\left (- h \right )}}{3 h^{2}} + \frac{2 f{\left (2 h \right )}}{15 h^{2}}\end{equation*}



<hr />

<p>If you are just interested in evaluating the weights, you can do so manually:</p>


```julia
    >>> finite_diff_weights(2, [-3, -1, 2], 0)[-1][-1]
    [1/5, -1/3, 2/15]
```

<h5>In <code>Julia</code>:</h5>

<p>the <code>finite_diff_weights</code> function that is not exported:</p>


```julia
sympy.finite_diff_weights(2, [-3, -1, 2], 0)[end][end]
```




\begin{equation*}\frac{2}{15}\end{equation*}



<hr />

<p>note that we only need the last element in the last sublist returned from <code>finite_diff_weights</code>. The reason for this is that the function also generates weights for lower derivatives and using fewer points (see the documentation of <code>finite_diff_weights</code> for more details).</p>

<p>If using <code>finite_diff_weights</code> directly looks complicated, and the <code>as_finite_difference</code> method of <code>Derivative</code> instances is not flexible enough, you can use <code>apply_finite_diff</code> which takes <code>order</code>, <code>x_list</code>, <code>y_list</code> and <code>x0</code> as parameters:</p>


```julia
    >>> x_list = [-3, 1, 2]
    >>> y_list = symbols('a b c')
    >>> apply_finite_diff(1, x_list, y_list, 0)
      3⋅a   b   2⋅c
    - ─── - ─ + ───
       20   4    5
```

<h5>In <code>Julia</code>,</h5>

<ul>
<li><p><code>apply_finite_diff</code> is not exported:</p>
</li>
</ul>


```julia
x_list = [-3, 1, 2]
y_list = symbols("a b c")
sympy.apply_finite_diff(1, x_list, y_list, 0)
```




\begin{equation*}- \frac{3 a}{20} - \frac{b}{4} + \frac{2 c}{5}\end{equation*}



<hr />

<p><a href="./index.html">return to index</a></p>

06beeb24d9f421ea29df3817173fbf120b9f3d46
ipynb
Jupyter Notebook
examples/calculus.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
examples/calculus.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
examples/calculus.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
Qwen/Qwen-72B
1. YES
2. YES