|
| 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 | +} |
0 commit comments