Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -824,5 +824,8 @@ 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

[Tests/UnitTests/Convenience/CatalanNumberTest.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
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> 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
{
Expand Down
43 changes: 43 additions & 0 deletions Sources/AngouriMath/Functions/Substitute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,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);
Expand Down Expand Up @@ -826,13 +830,52 @@ public override Entity Substitute(Entity x, Entity value)
}
}

/// <summary>
/// A derivative or an indefinite integral, <paramref name="operation"/>, of <paramref name="body"/>
/// in <paramref name="bound"/>, with <paramref name="x"/> replaced by <paramref name="value"/>
/// where its variable is not only bound; <see langword="null"/> where it is, and an ordinary
/// substitution under a binder does.
/// </summary>
/// <remarks>
/// <c>derivative(x^3, x)</c> is <c>3 x^2</c>, a function of <c>x</c>, whose variable
/// <see cref="FreeVariables"/> 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: <c>3 x^2</c> came back where <c>3</c> was owed at <c>x = 1</c>. So
/// a value for the variable makes the derivative evaluated there,
/// <c>apply(lambda(x, derivative(x^3, x)), 1)</c>, and a value that holds the variable, as
/// <c>a := x</c> in <c>derivative(a x^2, x)</c>, goes in under a fresh variable that the
/// derivative is then evaluated at, <c>apply(lambda(t, derivative(x t^2, t)), x)</c>, which is
/// <c>2 x^2</c>. Renamed and left at the fresh variable, it was <c>2 x t</c>.
/// https://github.com/asc-community/AngouriMath/issues/2189
/// </remarks>
private static Entity? EvaluatedAtAPoint(Entity operation, Entity body, Entity bound, System.Func<Entity, Variable, Entity> rebuild, Entity x, Entity value)
{
if (bound is not Variable variable)
return null;
if (x == variable)
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);
return new Lambda(fresh, rebuild(body.Substitute(variable, fresh).Substitute(x, value), fresh)).Apply(variable);
}
return null;
}

partial record Derivativef
{
/// <inheritdoc/>
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);
Expand Down
100 changes: 100 additions & 0 deletions Sources/Tests/UnitTests/Calculus/DerivativeAtAPointTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// 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
{
/// <summary>
/// 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:
/// <c>derivative(x^3, x)</c> at <c>x = 1</c> evaluated to <c>3 x^2</c>.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2189">#2189</a>
/// </summary>
[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());

/// <summary>Substituting and evaluating commute.</summary>
[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);
}

/// <summary>A lambda whose body is a derivative in its parameter is the derivative, evaluated where it is applied.</summary>
[Fact]
public void ALambdaOfADerivativeIsTheDerivative()
=> Assert.Equal(Number.Integer.Create(3), "apply(lambda(x, derivative(x^3, x)), 1)".ToEntity().Evaled);

/// <summary>
/// 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.
/// </summary>
[Fact]
public void AValueHoldingTheVariableIsNotCaptured()
=> Assert.Equal(Number.Integer.Zero,
("derivative(a x^2, x)".ToEntity().Substitute("a", "x") - "2 x^2".ToEntity()).Simplify());

/// <summary>Substituting the variable for itself changes nothing.</summary>
[Fact]
public void TheVariableForItselfIsTheSame()
=> Assert.Equal("derivative(x^3, x)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "x"));

/// <summary>
/// A derivative the library does not take is kept, evaluated at the point, rather than
/// answered wrongly or taken again forever.
/// </summary>
[Fact]
public void ADerivativeThatIsNotTakenIsKeptAtThePoint()
{
var atAPoint = "derivative(primepi(x), x)".ToEntity().Substitute("x", 2);
Assert.IsType<Application>(atAPoint);
Assert.IsType<Application>(atAPoint.Evaled);
}

/// <summary>A variable the body does not hold renames the derivative's variable.</summary>
[Fact]
public void AFreshVariableRenamesIt()
=> Assert.Equal("derivative(y^3, y)".ToEntity(), "derivative(x^3, x)".ToEntity().Substitute("x", "y"));

/// <summary>
/// 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.
/// </summary>
[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);
}

/// <summary>A definite integral binds its variable, and a value for it changes nothing.</summary>
[Fact]
public void ADefiniteIntegralStillBindsItsVariable()
=> Assert.Equal("integral(x, x, 0, 1)".ToEntity(), "integral(x, x, 0, 1)".ToEntity().Substitute("x", 5));
}
}
7 changes: 5 additions & 2 deletions Sources/Tests/UnitTests/Common/SubstituteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))))")]
Expand Down
Loading