Derivative (functions/derivative.py)
functions/derivative.py provides derivatives for explicit and implicit functions. Both functions return a SymPy expression simplified by radsimp — they do not return LaTeX strings. Input is parsed via core.sympify.sympify; on parse failure sympify returns the string "不规范的表达式输入" (this module does not catch that exception — the UI layer normally handles it).
derivative — explicit function derivative
Purpose: differentiate explicit function f with respect to variable v, n times; if x is given, substitute it into the derivative to obtain a numeric expression.
def derivative(f, v, n, x, fs):
...
| Param | Type | Meaning |
|---|---|---|
f |
str |
original function expression, Python/SymPy syntax, e.g. "x**3 + sin(x)" |
v |
str |
differentiation variable symbol, e.g. "x" |
n |
str |
order of differentiation, an integer as a string, e.g. "1", "2" |
x |
str |
value to substitute for the variable; "" or None means keep the derivative unevaluated |
fs |
dict |
function dictionary; key = name, value = [name, body, domain, var] |
Returns: a SymPy expression from radsimp(diff(sympify(f), sympify(v), int(sympify(n)), sympify(x))) (a numeric expression when x is substituted).
Example
python
derivative("x**3", "x", "1", "", {}) # -> 3*x**2
derivative("x**3", "x", "1", "2", {}) # -> 12 (substitute x=2)
yinhanshu_derivative — implicit function derivative
Purpose: differentiate the implicit function f(x, y)=0 with respect to x, n times, via sympy.idiff.
def yinhanshu_derivative(f, v1, v2, n, x, fs):
...
| Param | Type | Meaning |
|---|---|---|
f |
str |
implicit expression, assumed equal to 0 (F(x, y) = 0), e.g. "x**2 + y**2 - 1" |
v1 |
str |
independent variable symbol, e.g. "x" |
v2 |
str |
dependent variable symbol, e.g. "y" |
n |
str |
order of differentiation, integer as a string |
x |
str |
value to substitute for the independent variable; "" or None means none |
fs |
dict |
function dictionary |
Returns: a SymPy expression from radsimp(idiff(...)).
Example
Circle x**2 + y**2 - 1 = 0 w.r.t. x:
```python
yinhanshu_derivative("x2+y2-1", "x", "y", "1", "", {})
-> -x/y
```
Note
x is a concrete value for the independent variable (substitution), not a second variable. For partial derivatives of multiple variables, call repeatedly or differentiate the result again.