Skip to content

Commit 533ee4f

Browse files
A value put in for a derivative's variable is where it is evaluated (#2199)
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. 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. 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) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent c05a452 commit 533ee4f

6 files changed

Lines changed: 186 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ one inside another was answered with the empty set, a claim that it has no solut
5757
| `"1/(2 + p^(-3)) = 1/3".Solve("p")` | `{ }` | `{ 1, -1/2 + i * 1/2 * sqrt(3), -1/2 + i * -1/2 * sqrt(3) }` |
5858
| `"1/(1 + x^(-2)) = y".Solve("x")` | `{ }` | `{ ((-1) / (-y) - 1) ^ (-1/2), -((-1) / (-y) - 1) ^ (-1/2) }` |
5959

60+
### A value put in for a derivative's variable is where it is evaluated
61+
62+
**Silent: substituting for the variable of a derivative or an indefinite integral evaluates it there, where the value was
63+
dropped.** That variable is free in the derivative, which is a function of it, and treating it as bound gave
64+
`3 x^2` for `derivative(x^3, x)` at `x = 1`. The value now makes `apply(lambda(x, derivative(f, x)), v)`, the
65+
derivative evaluated at `v`, and a value holding the variable goes in under a fresh one rather than being captured. A
66+
definite integral, a sum, a product and a limit bind their variable, and are unchanged
67+
([#2189](https://github.com/asc-community/AngouriMath/issues/2189)).
68+
69+
| Input | Was (2.6.0) | Now |
70+
|---|---|---|
71+
| `"derivative(x^3, x)".ToEntity().Substitute("x", 1).Evaled` | `3 * x ^ 2` | `3` |
72+
| `"integral(x^2, x)".ToEntity().Substitute("x", 1).Evaled` | `x ^ 3 / 3 + C` | `1/3 + C` |
73+
| `"derivative(a x^2, x)".ToEntity().Substitute("a", "x").Evaled` | `2 * x_1 * x` | `2 * x * x` |
74+
| `"derivative(x^3, x)".ToEntity().Substitute("x", 1)` | `derivative(x ^ 3, x)` | `apply(lambda(x, derivative(x ^ 3, x)), 1)` |
75+
6076
### A quadratic whose discriminant is a square is answered without the root
6177

6278
**Silent: the same roots, written as what they are.** The quadratic formula wrote `sqrt(b^2 - 4ac)` whatever the

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,5 +824,8 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
824824
[Tests/UnitTests/Convenience/MultinomialTest.cs]
825825
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
826826

827+
[Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs]
828+
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
829+
827830
[Tests/UnitTests/Convenience/CatalanNumberTest.cs]
828831
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

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,27 @@ protected override Entity InnerSimplify(bool isExact)
590590
// for u afterwards made a matrix of vectors where the product is [-q, p].
591591
// https://github.com/asc-community/AngouriMath/issues/2132
592592
=> Expression is Lambda(var parameter, var body) && Arguments is (var first, var rest)
593-
? ApplyOthersIfNeeded(body.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact)
593+
? IsAFunctionOf(body, parameter)
594+
? TakenThenApplied(parameter, body, first, rest, isExact)
595+
: ApplyOthersIfNeeded(body.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact)
594596
: ApplyingTheSimplified(isExact);
595597

598+
// A derivative or an indefinite integral in the parameter is a function of it, and is
599+
// taken before the argument is put in: put in first, the argument would be what it is
600+
// taken in. One that cannot be taken is kept, applied, rather than answered wrongly.
601+
// https://github.com/asc-community/AngouriMath/issues/2189
602+
private static bool IsAFunctionOf(Entity body, Variable parameter)
603+
=> body is Derivativef { Var: var variable } && variable == parameter
604+
|| body is Integralf { Var: var integrated, Range: null } && integrated == parameter;
605+
606+
private Entity TakenThenApplied(Variable parameter, Entity body, Entity first, LList<Entity> rest, bool isExact)
607+
{
608+
var taken = body.InnerSimplified(isExact);
609+
if (taken.Nodes.Any(node => IsAFunctionOf(node, parameter)))
610+
return New(new Lambda(parameter, taken), Arguments.Map(argument => argument.InnerSimplified(isExact)));
611+
return ApplyOthersIfNeeded(taken.Substitute(parameter, first.InnerSimplified(isExact)), rest).InnerSimplified(isExact);
612+
}
613+
596614
private Entity ApplyingTheSimplified(bool isExact)
597615
=> ((Expression.InnerSimplified(isExact), Arguments.Map(arg => arg.InnerSimplified(isExact))) switch
598616
{

‎Sources/AngouriMath/Functions/Substitute.cs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ public override Entity Substitute(Entity x, Entity value)
729729
{
730730
if (this == x)
731731
return value;
732+
// An indefinite integral is a function of its variable, as a derivative is; a definite
733+
// one binds it.
734+
if (Range is null && EvaluatedAtAPoint(this, Expression, Var, (body, variable) => New(body, variable, null), x, value) is { } atAPoint)
735+
return atAPoint;
732736
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
733737

734738
return New(postSubs, boundName, Range is var (from, to) ? (from.Substitute(x, value), to.Substitute(x, value)) : null);
@@ -826,13 +830,52 @@ public override Entity Substitute(Entity x, Entity value)
826830
}
827831
}
828832

833+
/// <summary>
834+
/// A derivative or an indefinite integral, <paramref name="operation"/>, of <paramref name="body"/>
835+
/// in <paramref name="bound"/>, with <paramref name="x"/> replaced by <paramref name="value"/>
836+
/// where its variable is not only bound; <see langword="null"/> where it is, and an ordinary
837+
/// substitution under a binder does.
838+
/// </summary>
839+
/// <remarks>
840+
/// <c>derivative(x^3, x)</c> is <c>3 x^2</c>, a function of <c>x</c>, whose variable
841+
/// <see cref="FreeVariables"/> counts as free for that reason
842+
/// (https://github.com/asc-community/AngouriMath/issues/1045). Treated as bound, a value put
843+
/// in for it was dropped: <c>3 x^2</c> came back where <c>3</c> was owed at <c>x = 1</c>. So
844+
/// a value for the variable makes the derivative evaluated there,
845+
/// <c>apply(lambda(x, derivative(x^3, x)), 1)</c>, and a value that holds the variable, as
846+
/// <c>a := x</c> in <c>derivative(a x^2, x)</c>, goes in under a fresh variable that the
847+
/// derivative is then evaluated at, <c>apply(lambda(t, derivative(x t^2, t)), x)</c>, which is
848+
/// <c>2 x^2</c>. Renamed and left at the fresh variable, it was <c>2 x t</c>.
849+
/// https://github.com/asc-community/AngouriMath/issues/2189
850+
/// </remarks>
851+
private static Entity? EvaluatedAtAPoint(Entity operation, Entity body, Entity bound, System.Func<Entity, Variable, Entity> rebuild, Entity x, Entity value)
852+
{
853+
if (bound is not Variable variable)
854+
return null;
855+
if (x == variable)
856+
return value == variable ? operation
857+
// A variable the body does not hold is the derivative's own variable renamed, which
858+
// is exact, and is what renaming a lambda's parameter puts in: wrapped instead, the
859+
// lambda over the old name would be renamed again, without end.
860+
: value is Variable renamed && !body.ContainsNode(renamed) ? rebuild(body.Substitute(variable, renamed), renamed)
861+
: new Lambda(variable, operation).Apply(value);
862+
if (value.ContainsNode(variable) && body.ContainsNode(x))
863+
{
864+
var fresh = Variable.CreateUnique(body + variable + x + value, variable.Name);
865+
return new Lambda(fresh, rebuild(body.Substitute(variable, fresh).Substitute(x, value), fresh)).Apply(variable);
866+
}
867+
return null;
868+
}
869+
829870
partial record Derivativef
830871
{
831872
/// <inheritdoc/>
832873
public override Entity Substitute(Entity x, Entity value)
833874
{
834875
if (this == x)
835876
return value;
877+
if (EvaluatedAtAPoint(this, Expression, Var, (body, variable) => New(body, variable), x, value) is { } atAPoint)
878+
return atAPoint;
836879
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
837880

838881
return New(postSubs, boundName);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// The variable of a derivative and of an indefinite integral is free in it, so a value put in
17+
/// for it is where the derivative or the antiderivative is evaluated. It was dropped:
18+
/// <c>derivative(x^3, x)</c> at <c>x = 1</c> evaluated to <c>3 x^2</c>.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2189">#2189</a>
20+
/// </summary>
21+
[Trait("Area", "Calculus")]
22+
public sealed class DerivativeAtAPointTest
23+
{
24+
[Theory]
25+
[InlineData("derivative(x^3, x)", "1", "3")]
26+
[InlineData("derivative(x^3, x, 2)", "2", "12")]
27+
[InlineData("derivative(sin(x), x)", "0", "1")]
28+
[InlineData("derivative(x^2, x)", "y", "2 y")]
29+
[InlineData("derivative(x^2, x) + x", "3", "9")]
30+
[InlineData("integral(x^2, x)", "1", "1/3 + C")]
31+
public void ItIsEvaluatedAtTheValue(string expression, string value, string expected)
32+
=> Assert.Equal(Number.Integer.Zero,
33+
(expression.ToEntity().Substitute("x", value.ToEntity()) - expected.ToEntity()).Simplify());
34+
35+
/// <summary>Substituting and evaluating commute.</summary>
36+
[Theory]
37+
[InlineData("derivative(x^3 + sin(x), x)")]
38+
[InlineData("derivative(e^x x, x, 2)")]
39+
public void SubstitutingAndEvaluatingCommute(string expression)
40+
{
41+
var e = expression.ToEntity();
42+
Assert.Equal(e.Evaled.Substitute("x", 2).Evaled, e.Substitute("x", 2).Evaled);
43+
}
44+
45+
/// <summary>A lambda whose body is a derivative in its parameter is the derivative, evaluated where it is applied.</summary>
46+
[Fact]
47+
public void ALambdaOfADerivativeIsTheDerivative()
48+
=> Assert.Equal(Number.Integer.Create(3), "apply(lambda(x, derivative(x^3, x)), 1)".ToEntity().Evaled);
49+
50+
/// <summary>
51+
/// A value holding the variable is not captured: a := x in derivative(a x^2, x) is 2 x^2, the
52+
/// derivative 2 a x with a put in, and not the derivative of x^3.
53+
/// </summary>
54+
[Fact]
55+
public void AValueHoldingTheVariableIsNotCaptured()
56+
=> Assert.Equal(Number.Integer.Zero,
57+
("derivative(a x^2, x)".ToEntity().Substitute("a", "x") - "2 x^2".ToEntity()).Simplify());
58+
59+
/// <summary>Substituting the variable for itself changes nothing.</summary>
60+
[Fact]
61+
public void TheVariableForItselfIsTheSame()
62+
=> Assert.Equal("derivative(x^3, x)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "x"));
63+
64+
/// <summary>
65+
/// A derivative the library does not take is kept, evaluated at the point, rather than
66+
/// answered wrongly or taken again forever.
67+
/// </summary>
68+
[Fact]
69+
public void ADerivativeThatIsNotTakenIsKeptAtThePoint()
70+
{
71+
var atAPoint = "derivative(primepi(x), x)".ToEntity().Substitute("x", 2);
72+
Assert.IsType<Application>(atAPoint);
73+
Assert.IsType<Application>(atAPoint.Evaled);
74+
}
75+
76+
/// <summary>A variable the body does not hold renames the derivative's variable.</summary>
77+
[Fact]
78+
public void AFreshVariableRenamesIt()
79+
=> Assert.Equal("derivative(y^3, y)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "y"));
80+
81+
/// <summary>
82+
/// A lambda renaming its parameter puts a fresh variable in for it, which renames the
83+
/// derivative rather than wrapping it in a lambda over the old name to be renamed again, so
84+
/// factorizing derivative(apply(f, x), x, -2) finishes with the double integral.
85+
/// </summary>
86+
[Fact]
87+
public void RenamingTerminates()
88+
{
89+
var task = System.Threading.Tasks.Task.Factory.StartNew(
90+
() => "derivative(apply(f, x), x, -2)".ToEntity().Factorize(), System.Threading.Tasks.TaskCreationOptions.LongRunning);
91+
Assert.True(task.Wait(System.TimeSpan.FromSeconds(60)), "did not finish");
92+
Assert.Equal("integral(integral(apply(f, x), x), x)".ToEntity(), task.Result);
93+
}
94+
95+
/// <summary>A definite integral binds its variable, and a value for it changes nothing.</summary>
96+
[Fact]
97+
public void ADefiniteIntegralStillBindsItsVariable()
98+
=> Assert.Equal("integral(x, x, 0, 1)".ToEntity(), "integral(x, x, 0, 1)".ToEntity().Substitute("x", 5));
99+
}
100+
}

‎Sources/Tests/UnitTests/Common/SubstituteTest.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public sealed class SubstituteTest
2121
[InlineData("sin(x)", "sin(1)")]
2222
[InlineData("{ x : x > 3 }", "{ x : x > 3 }")]
2323
[InlineData("x + { x : x > 3 }", "1 + { x : x > 3 }")]
24-
[InlineData("x * derivative(x + 2, x, 1)", "1 * derivative(x + 2, x, 1)")]
25-
[InlineData("x * integral(x + 2, x)", "1 * integral(x + 2, x)")]
24+
// The variable of a derivative and of an indefinite integral is free in it, so the value is
25+
// where it is evaluated; it was dropped, and derivative(x^3, x) at x = 1 was 3 x^2.
26+
// https://github.com/asc-community/AngouriMath/issues/2189
27+
[InlineData("x * derivative(x + 2, x, 1)", "1 * apply(lambda(x, derivative(x + 2, x)), 1)")]
28+
[InlineData("x * integral(x + 2, x)", "1 * apply(lambda(x, integral(x + 2, x)), 1)")]
2629
[InlineData("x * integral(x + 2, x, x, x)", "1 * integral(x + 2, x, 1, 1)")]
2730
[InlineData("x * limit(x + 2, x, 1)", "1 * limit(x + 2, x, 1)")]
2831
[InlineData("sin(cos(sec(csc(x))))", "sin(cos(sec(csc(1))))")]

0 commit comments

Comments
 (0)