|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2022 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.Exceptions; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Common |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Compiling expressions that mention matrices. A matrix has no compiled form, but an |
| 17 | + /// expression built out of matrices often has a value that is an ordinary number. |
| 18 | + /// </summary> |
| 19 | + public sealed class MatrixCompilationTest |
| 20 | + { |
| 21 | + // https://github.com/asc-community/AngouriMath/issues/425 |
| 22 | + // This is the issue's own example of what ought to work, and did not: it is the |
| 23 | + // scalar c, but nothing simplified before compiling, so it reached the matrix node |
| 24 | + // and threw. |
| 25 | + [Fact] |
| 26 | + public void ScalarBuiltFromMatricesCompiles() |
| 27 | + { |
| 28 | + var compiled = "[0, 1]T * [[a, b], [c, d]] * [1, 0]".ToEntity() |
| 29 | + .Compile<double, double, double, double, double>("a", "b", "c", "d"); |
| 30 | + Assert.Equal(3, compiled(1, 2, 3, 4), 9); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void ScalarProductOfVectorsCompiles() |
| 35 | + { |
| 36 | + var compiled = "[1, 2]T * [a, b]".ToEntity().Compile<double, double, double>("a", "b"); |
| 37 | + Assert.Equal(1 * 3 + 2 * 4, compiled(3, 4), 9); |
| 38 | + } |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [InlineData("[[a, b], [c, d]]")] |
| 42 | + [InlineData("[[a, b], [c, d]] * [1, 0]")] |
| 43 | + [InlineData("[a, b]")] |
| 44 | + public void MatrixValuedExpressionsSayTheyCannotBeCompiled(string expression) |
| 45 | + { |
| 46 | + var thrown = Assert.Throws<UncompilableNodeException>( |
| 47 | + () => expression.ToEntity().Compile<double, double>("a")); |
| 48 | + Assert.Contains("Matrix", thrown.Message); |
| 49 | + } |
| 50 | + |
| 51 | + // Expressions with no matrix in them must be unaffected -- they do not go anywhere |
| 52 | + // near the extra simplification. |
| 53 | + [Theory] |
| 54 | + [InlineData("x + 1", 2.0, 3.0)] |
| 55 | + [InlineData("x ^ 2", 3.0, 9.0)] |
| 56 | + [InlineData("sin(x)", 0.0, 0.0)] |
| 57 | + public void OrdinaryExpressionsAreUnaffected(string expression, double argument, double expected) => |
| 58 | + Assert.Equal(expected, expression.ToEntity().Compile<double, double>("x")(argument), 9); |
| 59 | + } |
| 60 | +} |
0 commit comments