From 10c662a7adc166f7dc3dd2a7ab256b6b9b98b08d Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 29 Oct 2026 10:58:48 +0000 Subject: [PATCH 1/2] A value put in for a derivative's variable is where it is evaluated Substitute treated the variable of a derivative and of an indefinite integral as bound, and a value put in for it was dropped: derivative(x^3, x) at x = 1 evaluated to 3 x^2, and integral(x^2, x) at x = 1 to x^3/3 + C. That variable is free, the derivative being a function of it, as FreeVariables already counted it since #1045. Substituting and evaluating did not commute. A value for it now makes apply(lambda(x, derivative(f, x)), v), the derivative evaluated at v, in syntax the library already has, so Substitute still computes nothing. Applying a lambda whose body is a derivative or an indefinite integral in its parameter takes that body first and then puts the argument in. Every other body is substituted before it is simplified, as #2132 needs. That order had made apply(lambda(x, derivative(x^3, x)), 1) 3 x^2 as well, where 2.6.0 gave 3. A derivative the library does not take, such as that of primepi(x), keeps the application unevaluated rather than being taken again. A value that holds the variable, a := x in derivative(a x^2, x), was put in under a renamed variable that was then left free: derivative(x x_1^2, x_1), which is 2 x_1 x. It now goes in under a fresh variable at which the derivative is evaluated, and comes to 2 x^2, the derivative 2 a x with a put in. A definite integral, a sum, a product and a limit bind their variable, and are unchanged. Two rows of SubstituteTest pinned the dropped value, x * derivative(x + 2, x) at x = 1 as 1 * derivative(x + 2, x); they now read 1 * apply(lambda(x, derivative(x + 2, x)), 1). Fixes #2189. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 16 ++++ Sources/.editorconfig | 3 + .../Evaluation.Omni.Classes.cs | 20 ++++- Sources/AngouriMath/Functions/Substitute.cs | 38 +++++++++ .../Calculus/DerivativeAtAPointTest.cs | 81 +++++++++++++++++++ .../Tests/UnitTests/Common/SubstituteTest.cs | 7 +- 6 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 49105ad6e..4c03043fb 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -57,6 +57,22 @@ one inside another was answered with the empty set, a claim that it has no solut | `"1/(2 + p^(-3)) = 1/3".Solve("p")` | `{ }` | `{ 1, -1/2 + i * 1/2 * sqrt(3), -1/2 + i * -1/2 * sqrt(3) }` | | `"1/(1 + x^(-2)) = y".Solve("x")` | `{ }` | `{ ((-1) / (-y) - 1) ^ (-1/2), -((-1) / (-y) - 1) ^ (-1/2) }` | +### A value put in for a derivative's variable is where it is evaluated + +**Silent: substituting for the variable of a derivative or an indefinite integral evaluates it there, where the value was +dropped.** That variable is free in the derivative, which is a function of it, and treating it as bound gave +`3 x^2` for `derivative(x^3, x)` at `x = 1`. The value now makes `apply(lambda(x, derivative(f, x)), v)`, the +derivative evaluated at `v`, and a value holding the variable goes in under a fresh one rather than being captured. A +definite integral, a sum, a product and a limit bind their variable, and are unchanged +([#2189](https://github.com/asc-community/AngouriMath/issues/2189)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"derivative(x^3, x)".ToEntity().Substitute("x", 1).Evaled` | `3 * x ^ 2` | `3` | +| `"integral(x^2, x)".ToEntity().Substitute("x", 1).Evaled` | `x ^ 3 / 3 + C` | `1/3 + C` | +| `"derivative(a x^2, x)".ToEntity().Substitute("a", "x").Evaled` | `2 * x_1 * x` | `2 * x * x` | +| `"derivative(x^3, x)".ToEntity().Substitute("x", 1)` | `derivative(x ^ 3, x)` | `apply(lambda(x, derivative(x ^ 3, x)), 1)` | + ### A quadratic whose discriminant is a square is answered without the root **Silent: the same roots, written as what they are.** The quadratic formula wrote `sqrt(b^2 - 4ac)` whatever the diff --git a/Sources/.editorconfig b/Sources/.editorconfig index d3d6ea124..4fed21fa8 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -823,3 +823,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Convenience/MultinomialTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs index fa3b14bb6..bb6af7419 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs @@ -590,9 +590,27 @@ protected override Entity InnerSimplify(bool isExact) // for u afterwards made a matrix of vectors where the product is [-q, p]. // https://github.com/asc-community/AngouriMath/issues/2132 => Expression is Lambda(var parameter, var body) && Arguments is (var first, var rest) - ? ApplyOthersIfNeeded(body.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact) + ? IsAFunctionOf(body, parameter) + ? TakenThenApplied(parameter, body, first, rest, isExact) + : ApplyOthersIfNeeded(body.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact) : ApplyingTheSimplified(isExact); + // A derivative or an indefinite integral in the parameter is a function of it, and is + // taken before the argument is put in: put in first, the argument would be what it is + // taken in. One that cannot be taken is kept, applied, rather than answered wrongly. + // https://github.com/asc-community/AngouriMath/issues/2189 + private static bool IsAFunctionOf(Entity body, Variable parameter) + => body is Derivativef { Var: var variable } && variable == parameter + || body is Integralf { Var: var integrated, Range: null } && integrated == parameter; + + private Entity TakenThenApplied(Variable parameter, Entity body, Entity first, LList rest, bool isExact) + { + var taken = body.InnerSimplified(isExact); + if (taken.Nodes.Any(node => IsAFunctionOf(node, parameter))) + return New(new Lambda(parameter, taken), Arguments.Map(argument => argument.InnerSimplified(isExact))); + return ApplyOthersIfNeeded(taken.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact); + } + private Entity ApplyingTheSimplified(bool isExact) => ((Expression.InnerSimplified(isExact), Arguments.Map(arg => arg.InnerSimplified(isExact))) switch { diff --git a/Sources/AngouriMath/Functions/Substitute.cs b/Sources/AngouriMath/Functions/Substitute.cs index f0b22831e..de97e7287 100644 --- a/Sources/AngouriMath/Functions/Substitute.cs +++ b/Sources/AngouriMath/Functions/Substitute.cs @@ -722,6 +722,10 @@ public override Entity Substitute(Entity x, Entity value) { if (this == x) return value; + // An indefinite integral is a function of its variable, as a derivative is; a definite + // one binds it. + if (Range is null && EvaluatedAtAPoint(this, Expression, Var, (body, variable) => New(body, variable, null), x, value) is { } atAPoint) + return atAPoint; var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value); return New(postSubs, boundName, Range is var (from, to) ? (from.Substitute(x, value), to.Substitute(x, value)) : null); @@ -819,6 +823,38 @@ public override Entity Substitute(Entity x, Entity value) } } + /// + /// A derivative or an indefinite integral, , of + /// in , with replaced by + /// where its variable is not only bound; where it is, and an ordinary + /// substitution under a binder does. + /// + /// + /// derivative(x^3, x) is 3 x^2, a function of x, whose variable + /// counts as free for that reason + /// (https://github.com/asc-community/AngouriMath/issues/1045). Treated as bound, a value put + /// in for it was dropped: 3 x^2 came back where 3 was owed at x = 1. So + /// a value for the variable makes the derivative evaluated there, + /// apply(lambda(x, derivative(x^3, x)), 1), and a value that holds the variable, as + /// a := x in derivative(a x^2, x), goes in under a fresh variable that the + /// derivative is then evaluated at, apply(lambda(t, derivative(x t^2, t)), x), which is + /// 2 x^2. Renamed and left at the fresh variable, it was 2 x t. + /// https://github.com/asc-community/AngouriMath/issues/2189 + /// + private static Entity? EvaluatedAtAPoint(Entity operation, Entity body, Entity bound, System.Func rebuild, Entity x, Entity value) + { + if (bound is not Variable variable) + return null; + if (x == variable) + return value == variable ? operation : new Lambda(variable, operation).Apply(value); + if (value.ContainsNode(variable) && body.ContainsNode(x)) + { + var fresh = Variable.CreateUnique(body + variable + x + value, variable.Name); + return new Lambda(fresh, rebuild(body.Substitute(variable, fresh).Substitute(x, value), fresh)).Apply(variable); + } + return null; + } + partial record Derivativef { /// @@ -826,6 +862,8 @@ public override Entity Substitute(Entity x, Entity value) { if (this == x) return value; + if (EvaluatedAtAPoint(this, Expression, Var, (body, variable) => New(body, variable), x, value) is { } atAPoint) + return atAPoint; var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value); return New(postSubs, boundName); diff --git a/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs b/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs new file mode 100644 index 000000000..b417687d3 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs @@ -0,0 +1,81 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The variable of a derivative and of an indefinite integral is free in it, so a value put in + /// for it is where the derivative or the antiderivative is evaluated. It was dropped: + /// derivative(x^3, x) at x = 1 evaluated to 3 x^2. + /// #2189 + /// + [Trait("Area", "Calculus")] + public sealed class DerivativeAtAPointTest + { + [Theory] + [InlineData("derivative(x^3, x)", "1", "3")] + [InlineData("derivative(x^3, x, 2)", "2", "12")] + [InlineData("derivative(sin(x), x)", "0", "1")] + [InlineData("derivative(x^2, x)", "y", "2 y")] + [InlineData("derivative(x^2, x) + x", "3", "9")] + [InlineData("integral(x^2, x)", "1", "1/3 + C")] + public void ItIsEvaluatedAtTheValue(string expression, string value, string expected) + => Assert.Equal(Number.Integer.Zero, + (expression.ToEntity().Substitute("x", value.ToEntity()) - expected.ToEntity()).Simplify()); + + /// Substituting and evaluating commute. + [Theory] + [InlineData("derivative(x^3 + sin(x), x)")] + [InlineData("derivative(e^x x, x, 2)")] + public void SubstitutingAndEvaluatingCommute(string expression) + { + var e = expression.ToEntity(); + Assert.Equal(e.Evaled.Substitute("x", 2).Evaled, e.Substitute("x", 2).Evaled); + } + + /// A lambda whose body is a derivative in its parameter is the derivative, evaluated where it is applied. + [Fact] + public void ALambdaOfADerivativeIsTheDerivative() + => Assert.Equal(Number.Integer.Create(3), "apply(lambda(x, derivative(x^3, x)), 1)".ToEntity().Evaled); + + /// + /// A value holding the variable is not captured: a := x in derivative(a x^2, x) is 2 x^2, the + /// derivative 2 a x with a put in, and not the derivative of x^3. + /// + [Fact] + public void AValueHoldingTheVariableIsNotCaptured() + => Assert.Equal(Number.Integer.Zero, + ("derivative(a x^2, x)".ToEntity().Substitute("a", "x") - "2 x^2".ToEntity()).Simplify()); + + /// Substituting the variable for itself changes nothing. + [Fact] + public void TheVariableForItselfIsTheSame() + => Assert.Equal("derivative(x^3, x)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "x")); + + /// + /// A derivative the library does not take is kept, evaluated at the point, rather than + /// answered wrongly or taken again forever. + /// + [Fact] + public void ADerivativeThatIsNotTakenIsKeptAtThePoint() + { + var atAPoint = "derivative(primepi(x), x)".ToEntity().Substitute("x", 2); + Assert.IsType(atAPoint); + Assert.IsType(atAPoint.Evaled); + } + + /// A definite integral binds its variable, and a value for it changes nothing. + [Fact] + public void ADefiniteIntegralStillBindsItsVariable() + => Assert.Equal("integral(x, x, 0, 1)".ToEntity(), "integral(x, x, 0, 1)".ToEntity().Substitute("x", 5)); + } +} diff --git a/Sources/Tests/UnitTests/Common/SubstituteTest.cs b/Sources/Tests/UnitTests/Common/SubstituteTest.cs index 699fe1cd1..8d5ef40d6 100644 --- a/Sources/Tests/UnitTests/Common/SubstituteTest.cs +++ b/Sources/Tests/UnitTests/Common/SubstituteTest.cs @@ -21,8 +21,11 @@ public sealed class SubstituteTest [InlineData("sin(x)", "sin(1)")] [InlineData("{ x : x > 3 }", "{ x : x > 3 }")] [InlineData("x + { x : x > 3 }", "1 + { x : x > 3 }")] - [InlineData("x * derivative(x + 2, x, 1)", "1 * derivative(x + 2, x, 1)")] - [InlineData("x * integral(x + 2, x)", "1 * integral(x + 2, x)")] + // The variable of a derivative and of an indefinite integral is free in it, so the value is + // where it is evaluated; it was dropped, and derivative(x^3, x) at x = 1 was 3 x^2. + // https://github.com/asc-community/AngouriMath/issues/2189 + [InlineData("x * derivative(x + 2, x, 1)", "1 * apply(lambda(x, derivative(x + 2, x)), 1)")] + [InlineData("x * integral(x + 2, x)", "1 * apply(lambda(x, integral(x + 2, x)), 1)")] [InlineData("x * integral(x + 2, x, x, x)", "1 * integral(x + 2, x, 1, 1)")] [InlineData("x * limit(x + 2, x, 1)", "1 * limit(x + 2, x, 1)")] [InlineData("sin(cos(sec(csc(x))))", "sin(cos(sec(csc(1))))")] From dba83a8d2b7ac1bc4b46cc192e0ed17bb466db35 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 29 Oct 2026 11:45:04 +0000 Subject: [PATCH 2/2] A fresh variable put in for a derivative's variable renames it A variable the body does not hold is a renaming, and renames the derivative's variable: derivative(x^3, x) with x := y is derivative(y^3, y). That is what a lambda renaming its parameter puts in, and wrapped in a lambda over the old name instead it would be renamed again without end. Part of #2189. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- Sources/AngouriMath/Functions/Substitute.cs | 7 ++++++- .../Calculus/DerivativeAtAPointTest.cs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Sources/AngouriMath/Functions/Substitute.cs b/Sources/AngouriMath/Functions/Substitute.cs index de97e7287..930cc0da5 100644 --- a/Sources/AngouriMath/Functions/Substitute.cs +++ b/Sources/AngouriMath/Functions/Substitute.cs @@ -846,7 +846,12 @@ public override Entity Substitute(Entity x, Entity value) if (bound is not Variable variable) return null; if (x == variable) - return value == variable ? operation : new Lambda(variable, operation).Apply(value); + return value == variable ? operation + // A variable the body does not hold is the derivative's own variable renamed, which + // is exact, and is what renaming a lambda's parameter puts in: wrapped instead, the + // lambda over the old name would be renamed again, without end. + : value is Variable renamed && !body.ContainsNode(renamed) ? rebuild(body.Substitute(variable, renamed), renamed) + : new Lambda(variable, operation).Apply(value); if (value.ContainsNode(variable) && body.ContainsNode(x)) { var fresh = Variable.CreateUnique(body + variable + x + value, variable.Name); diff --git a/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs b/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs index b417687d3..e1c6e5739 100644 --- a/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs +++ b/Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs @@ -73,6 +73,25 @@ public void ADerivativeThatIsNotTakenIsKeptAtThePoint() Assert.IsType(atAPoint.Evaled); } + /// A variable the body does not hold renames the derivative's variable. + [Fact] + public void AFreshVariableRenamesIt() + => Assert.Equal("derivative(y^3, y)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "y")); + + /// + /// A lambda renaming its parameter puts a fresh variable in for it, which renames the + /// derivative rather than wrapping it in a lambda over the old name to be renamed again, so + /// factorizing derivative(apply(f, x), x, -2) finishes with the double integral. + /// + [Fact] + public void RenamingTerminates() + { + var task = System.Threading.Tasks.Task.Factory.StartNew( + () => "derivative(apply(f, x), x, -2)".ToEntity().Factorize(), System.Threading.Tasks.TaskCreationOptions.LongRunning); + Assert.True(task.Wait(System.TimeSpan.FromSeconds(60)), "did not finish"); + Assert.Equal("integral(integral(apply(f, x), x), x)".ToEntity(), task.Result); + } + /// A definite integral binds its variable, and a value for it changes nothing. [Fact] public void ADefiniteIntegralStillBindsItsVariable()