pank - 1 year ago 149

Python Question

I have this code:

`from sympy import *`

x = Symbol("x")

f = Function("f")(x)

diff(3*x + 2, x) # == 3

diff(f, x).subs(f, 3*x + 2) # == Derivative(3*x + 2, x) <- Why not 3?

Why

`sympy`

`f`

`x`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

`diff(f, x)`

can't derivate the function yet, so it results in the abstract `Derivative(f(x), x)`

. Substitution doesn't do any evaluation, so substituting `f`

for `3*x + 2`

results in `Derivative(3*x + 2, x)`

. If you want to evaluate it, just append `doit()`

:

```
>>> diff(f, x).subs(f, 3*x + 2).doit()
3
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**