|
| 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 System.Numerics; |
| 9 | +using AngouriMath; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Convenience |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// A logarithm to base 0 has no value, as <c>log</c>'s condition says, where <c>ln(x) / ln(0)</c>, which is |
| 17 | + /// <c>ln(x) / (-oo)</c>, made it 0 in evaluation and in the compilers. |
| 18 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2255">#2255</a> |
| 19 | + /// </summary> |
| 20 | + [Trait("Area", "Convenience")] |
| 21 | + public sealed class LogarithmToBaseZeroTest |
| 22 | + { |
| 23 | + [Theory] |
| 24 | + [InlineData("log(0, 2)")] |
| 25 | + [InlineData("log(0, 1/2)")] |
| 26 | + [InlineData("log(0, -3)")] |
| 27 | + [InlineData("log(0, i)")] |
| 28 | + [InlineData("log(0, 0)")] |
| 29 | + [InlineData("log(0, 1)")] |
| 30 | + public void ItHasNoValue(string expression) |
| 31 | + { |
| 32 | + Assert.True(expression.ToEntity().Evaled.IsNaN, $"{expression} evaluates to {expression.ToEntity().Evaled}"); |
| 33 | + Assert.True(expression.ToEntity().Simplify().IsNaN, $"{expression} simplifies to {expression.ToEntity().Simplify()}"); |
| 34 | + } |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [InlineData(2)] |
| 38 | + [InlineData(0.5)] |
| 39 | + [InlineData(-3)] |
| 40 | + [InlineData(1)] |
| 41 | + public void CompiledItHasNoValue(double antilogarithm) |
| 42 | + { |
| 43 | + var expression = $"log(x, {antilogarithm})".ToEntity(); |
| 44 | + var fast = expression.Compile("x").Call(0); |
| 45 | + var linq = expression.Compile<Complex, Complex>("x")(0); |
| 46 | + Assert.True(double.IsNaN(fast.Real) || double.IsNaN(fast.Imaginary), $"{expression} at 0: {fast} by FastExpression"); |
| 47 | + Assert.True(double.IsNaN(linq.Real) || double.IsNaN(linq.Imaginary), $"{expression} at 0: {linq} by LINQ"); |
| 48 | + Assert.True(double.IsNaN(expression.Compile<double, double>("x")(0)), $"{expression} at 0 over double"); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// <c>log(x, 1)</c> is 0 for every base but 0 and 1, and its simplification says so: it said only <c>not x = 1</c>, |
| 53 | + /// and was 0 at <c>x = 0</c>. |
| 54 | + /// </summary> |
| 55 | + [Fact] |
| 56 | + public void TheLogarithmOfOneExcludesTheBase0() |
| 57 | + { |
| 58 | + var simplified = "log(x, 1)".ToEntity().Simplify(); |
| 59 | + Assert.True(simplified.Substitute("x", 0).Evaled.IsNaN, $"{simplified} at 0"); |
| 60 | + Assert.True(simplified.Substitute("x", 1).Evaled.IsNaN, $"{simplified} at 1"); |
| 61 | + Assert.Equal(Entity.Number.Integer.Zero, simplified.Substitute("x", 2).Evaled); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>A base that is small and not 0 has its logarithm.</summary> |
| 65 | + [Theory] |
| 66 | + [InlineData("log(2, 8)", 3)] |
| 67 | + [InlineData("log(1/2, 4)", -2)] |
| 68 | + [InlineData("log(10^(-100), 10^(-200))", 2)] |
| 69 | + public void AnotherBaseHasItsLogarithm(string expression, double value) |
| 70 | + { |
| 71 | + var evaluated = ((Entity.Number.Complex)expression.ToEntity().Evaled).ToNumerics(); |
| 72 | + Assert.True(Complex.Abs(evaluated - value) < 1e-12, $"{expression} evaluates to {evaluated}"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments