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
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ partial record Entity
public Entity Limit(Variable x, Entity destination, ApproachFrom side)
{
var res = ComputeLimit(this, x, destination, side);
if (res is null || res == MathS.NaN)
return new Limitf(this, x, destination, side);
if (res is null) return new Limitf(this, x, destination, side);
return res.InnerSimplified;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ private static Entity ExpandLogarithm(Entity expr)
return fromLeft;
if (ExpressionNumerical.AreEqual(fromLeft, fromRight) && (acceptNaN || fromLeft.Evaled != MathS.NaN))
return fromLeft;
expr = ApplylHopitalRule(expr, x, dest);
return ComputeLimit(expr, x, dest, acceptNaN: true);
var lhopital = ApplylHopitalRule(expr, x, dest);
if (lhopital != null) return ComputeLimit(lhopital, x, dest, acceptNaN: true);
else return MathS.NaN; // A two-sided limit cannot exist if the limit from left and right don't match.
}
else
{
expr = ApplylHopitalRule(expr, x, dest);
return ComputeLimit(expr, x, dest);
var lhopital = ApplylHopitalRule(expr, x, dest);
if (lhopital != null) return ComputeLimit(lhopital, x, dest);
else return null;
}
}
throw new AngouriBugException($"Unresolved enum parameter {side}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static bool IsInfiniteNode(Entity expr)
private static bool IsFiniteNode(Entity expr)
=> !IsInfiniteNode(expr) && expr != MathS.NaN;

private static Entity ApplylHopitalRule(Entity expr, Variable x, Entity dest)
private static Entity? ApplylHopitalRule(Entity expr, Variable x, Entity dest)
{
if (expr is Divf(var num, var den))
if (EvalAssumingContinuous(num.Limit(x, dest)) is var numLimit && EvalAssumingContinuous(den.Limit(x, dest)) is var denLimit)
Expand All @@ -69,7 +69,7 @@ private static Entity ApplylHopitalRule(Entity expr, Variable x, Entity dest)
if (ComputeLimit(applied, x, dest) is { } resLim)
return resLim;
}
return expr;
return null;
}

private static Entity ApplyTrivialTransformations(Entity expr, Variable x, Entity dest, Func<Entity, Entity, Entity> transformation)
Expand Down
19 changes: 19 additions & 0 deletions Sources/Tests/UnitTests/Calculus/LimitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,24 @@ public void TestComplicated()
Assert.NotNull(limit);
Assert.Equal("sqrt(a / c * 3 / sin(a / c) + sin(d))", limit?.Stringize());
}
[Theory]
[InlineData("limit(1/x,x,0)")]
[InlineData("limit(1/x^3,x,0)")]
[InlineData("limit(x^x,x,0)")]
public void TestNoLimit(string input) // a two-sided limit does not exist
{
var limit = input.ToEntity();
Assert.Equal(MathS.NaN, limit.InnerSimplified);
Assert.Equal(MathS.NaN, limit.Evaled);
}
[Theory]
[InlineData("limit(apply(f, x),x,-1)")]
[InlineData("limit(x!,x,-1)")]
public void TestCantSolve(string input) // this is different from no limit
{
var limit = input.ToEntity();
Assert.Equal(input, limit.InnerSimplified);
Assert.Equal(input, limit.Evaled);
}
}
}
6 changes: 6 additions & 0 deletions Sources/Tests/UnitTests/PatternsTest/SimplifyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ [Fact] public void BigSimple1() => AssertSimplifyToString(
[InlineData("ln(a) - ln(b)", "ln(a / b)")]
[InlineData("log(2, a) + ln(b)", "log(2, a) + ln(b)")]
public void PowerRulesTest(string input, string output) => AssertSimplifyToString(input, output);

[Theory]
[InlineData("a/a", "1 provided not a = 0")]
[InlineData("(-a)/a", "-1 provided not a = 0")]
[InlineData("a/(-a)", "-1 provided not a = 0")]
public void MakeProvided(string input, string output) => AssertSimplifyToString(input, output, 1);
}
}

Loading