Skip to content

Substituting for a derivative's variable is dropped: derivative(x^3, x) at x = 1 evaluates to 3 x^2 #2189

Closed

Description

@Rafael-SOWNet

Substitute leaves the variable of a derivative alone, and so does the variable of an indefinite integral. The answer evaluated afterwards is wrong:

master 5e7731f and 2.6.0 should be
"derivative(x^3, x)".ToEntity().Substitute("x", 1).Evaled 3 * x ^ 2 3
"derivative(x^3, x, 2)".ToEntity().Substitute("x", 2).Evaled 2 * x * 3 12
"integral(x^2, x)".ToEntity().Substitute("x", 1).Evaled x ^ 3 / 3 + C 1/3 + C

Substituting and evaluating do not commute: derivative(x^3, x).Evaled is 3 * x ^ 2, in which x is free.

FreeVariables already reports that x as free. #1045 left both nodes out of its binders because "d/dt denotes a function of t", and pinned that with a test. Substitute still treats the variable as bound. That reading dates from 2020 (x = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + a, temp_1)), and #1811 kept it.

What is open is how to write a derivative that cannot be computed, evaluated at a point: derivative(f(x), x) with x := 1. SymPy writes Subs(Derivative(f(x), x), x, 1). Two candidates here:

  • apply(lambda(x, derivative(f(x), x)), 1);
  • a node for evaluation at a point.

Activity

  1. added this to the 2.7.0 milestone on Oct 28, 2026
  2. Rafael-SOWNet commented on Oct 29, 2026

    @Rafael-SOWNet
    MemberAuthor

    Proposal, which I'm implementing now:

    • derivative(f, x) with x := v becomes apply(lambda(x, derivative(f, x)), v), the derivative evaluated at v, written in syntax the library already has. The same goes for the indefinite integral(f, x). Substitute stays structural and computes nothing. A definite integral, a sum, a product and a limit really do bind their variable, and stay as they are.
    • Applying a lambda whose body is a derivative or an indefinite integral in its parameter simplifies that body before substituting, so the derivative is taken as a function of x and only then evaluated. Every other body is still substituted first, as Applying a lambda to a vector treats the vector as a number #2132 needs. A derivative the library cannot compute keeps the application, unevaluated, rather than answering wrongly.
    • Capture: substituting a := x into derivative(a x^2, x) currently renames the bound variable and leaves it free, giving derivative(x * x_1 ^ 2, x_1), which is 2 * x_1 * x. The right answer is 2 x^2, which apply(lambda(x_1, derivative(x x_1^2, x_1)), x) gives.
    now with this
    derivative(x^3, x), x := 1, Evaled 3 * x ^ 2 3
    apply(lambda(x, derivative(x^3, x)), 1) 3 * x ^ 2 3
    integral(x^2, x), x := 1, Evaled x ^ 3 / 3 + C 1/3 + C
    derivative(a x^2, x), a := x, Evaled 2 * x_1 * x 2 * x ^ 2
  3. moved this from Todo to In Progress in AngouriMathon Oct 29, 2026
  4. moved this from In Progress to Done in AngouriMathon Oct 29, 2026
  5. added a commit that references this issue on Oct 29, 2026
  6. added a commit that references this issue on Nov 4, 2026
    533ee4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions