|
| 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.Core.Compilation.IntoLinq; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using GenericTensor.Core; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace AngouriMath.Tests.Common |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Compiled over <see cref="double"/>, a division by zero, and 0 to a power that is not positive, have no value, as in |
| 18 | + /// evaluation, FastExpression and the compiler over <see cref="System.Numerics.Complex"/>: the IEEE quotient made 1/0 |
| 19 | + /// +oo, and <see cref="System.Math.Pow(double, double)"/> made 0^(-1) +oo and 0^0 1. |
| 20 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2286">#2286</a> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Common")] |
| 23 | + public sealed class DivisionByZeroOverDoubleTest |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [InlineData("1/x", 0)] |
| 27 | + [InlineData("-1/x", 0)] |
| 28 | + [InlineData("1/(x - x)", 3)] |
| 29 | + [InlineData("x / floor(x)", 0.5)] |
| 30 | + [InlineData("x^(-1)", 0)] |
| 31 | + [InlineData("x^(-1/2)", 0)] |
| 32 | + [InlineData("x^x", 0)] |
| 33 | + public void ItHasNoValue(string expression, double at) |
| 34 | + { |
| 35 | + var overDoubles = expression.ToEntity().Compile<double, double>("x")(at); |
| 36 | + Assert.True(double.IsNaN(overDoubles), $"{expression} at {at}: {overDoubles} over double"); |
| 37 | + Assert.True(float.IsNaN(expression.ToEntity().Compile<float, float>("x")((float)at)), $"{expression} at {at} over float"); |
| 38 | + Assert.True(expression.ToEntity().Substitute("x", at).Evaled.IsNaN, $"{expression} at {at} evaluates to a value"); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// A matrix argument divided by a zero scalar, entry by entry as a scalar is. A matrix written into the expression |
| 43 | + /// is divided before it is compiled, and only an argument reaches the compiled matrix division. |
| 44 | + /// </summary> |
| 45 | + [Fact] |
| 46 | + public void AMatrixOverZeroHasNoEntries() |
| 47 | + { |
| 48 | + var matrix = GenTensor<double, DoubleOperations>.CreateMatrix(new double[,] { { 1, 2 }, { 3, 4 } }); |
| 49 | + var divided = "A / x".ToEntity() |
| 50 | + .Compile<GenTensor<double, DoubleOperations>, double, GenTensor<double, DoubleOperations>>("A", "x")(matrix, 0); |
| 51 | + for (var i = 0; i < 2; i++) |
| 52 | + for (var j = 0; j < 2; j++) |
| 53 | + Assert.True(double.IsNaN(divided[i, j]), $"[{i}, {j}] is {divided[i, j]}"); |
| 54 | + } |
| 55 | + |
| 56 | + [Theory] |
| 57 | + [InlineData("1/x", 4, 0.25)] |
| 58 | + [InlineData("x^(1/2)", 0, 0)] |
| 59 | + [InlineData("x^2", 0, 0)] |
| 60 | + [InlineData("0 / x", 2, 0)] |
| 61 | + public void WhereItHasOneItIsThatValue(string expression, double at, double value) |
| 62 | + => Assert.Equal(value, expression.ToEntity().Compile<double, double>("x")(at)); |
| 63 | + } |
| 64 | +} |
0 commit comments