From c43a9d49c6a815dd1ef2f39e44408d8d4326e23c Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 16 Nov 2026 18:00:07 +0000 Subject: [PATCH] Refuse norm(v) and beta(x) with one argument, read as products with no error A one-argument call of norm or beta was read as the product of a variable named for it with its argument: norm([3, 4]) simplified to [3 * norm, 4 * norm], where the length is 5, and beta(x) was beta * x. Each now throws FunctionArgumentCountException naming what to write: norm(v, 2) for the length, which is abs(v), and beta(a, b), or norm * (...) and beta * (...) for the product. The name alone is a variable and with a space before the bracket a product, as the other names refused by name are. Refused before the grammar, so the grammar is unchanged. The MCP server's IsFunction now says yes for both, a one-argument call no longer being a product; kappa takes beta's place in its test, and in the library's test of a short name read as a product. Syntax.md says both are refused. Full check: 21112 of 21125 unit tests pass, 13 skipped; MCP 459 of 459; harnesses clean and the three baselines matched. MathS.Parse throwing for these, as it already does for factorize(360), is https://github.com/asc-community/AngouriMath/issues/2498. Part of https://github.com/asc-community/AngouriMath/issues/2182 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 16 +++++++++ Sources/AngouriMath/Core/Parser.cs | 28 +++++++-------- Sources/AngouriMath/Docs/Usage/Syntax.md | 10 +++--- Sources/MCP/AngouriMath.Mcp/Parsing.cs | 8 ++--- Sources/Tests/McpUnitTests/ParsingTest.cs | 4 +-- Sources/Tests/McpUnitTests/ToolsTest.cs | 2 +- .../Tests/UnitTests/Convenience/BetaTest.cs | 23 ++++++++---- .../Tests/UnitTests/Convenience/NormTest.cs | 35 +++++++++++++------ .../Convenience/SyntaxDocumentedTest.cs | 9 ++--- 9 files changed, 88 insertions(+), 47 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index f5be46ced..ebeaaa66a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -69,6 +69,22 @@ is now put back in, and kept where the equation is exactly zero. The roots Newto | `"sin(sqrt(x)) = x".ToEntity().Solve("x")` | `{ 0.76864885676094940… }` | `{ 0, 0.76864885676094940… }` | | `"cos(1 - sqrt(x)) - x = cos(1)".ToEntity().Solve("x")` | `{ 0.39003113021627860… }` | `{ 0, 0.39003113021627860… }` | +### `norm(v)` and `beta(x)` with one argument are refused + +**Not silent: what parsed now throws.** A call of `norm` or `beta` with one argument was read as the product of a +variable named `norm` or `beta` with its argument. So `norm([3, 4])` simplified to `[3 * norm, 4 * norm]` with no +error, where the length is `5`. Each now throws `FunctionArgumentCountException`, saying what to write: `norm(v, 2)` +for the length, which is `abs(v)`, and `beta(a, b)` for the beta function, or `norm * (…)` and `beta * (…)` for the +product. The name alone is still a variable, and with a space before the bracket, `norm (x)`, still a product, as the +other names refused by name are ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). + +| Input | Was (2.8.0) | Now | +|---|---|---| +| `"norm([3, 4])".ToEntity()` | `norm * [3, 4]` | throws `FunctionArgumentCountException` | +| `"norm([3, 4])".ToEntity().Simplify()` | `[3 * norm, 4 * norm]` | throws, as above | +| `"beta(x)".ToEntity()` | `beta * x` | throws `FunctionArgumentCountException` | +| `"norm (x)".ToEntity()` | `norm * x` | `norm * x`, unchanged | + ## 2.8.0 — since 2.7.0 **Read these first.** diff --git a/Sources/AngouriMath/Core/Parser.cs b/Sources/AngouriMath/Core/Parser.cs index 88b8640b6..f1e36f68c 100644 --- a/Sources/AngouriMath/Core/Parser.cs +++ b/Sources/AngouriMath/Core/Parser.cs @@ -110,27 +110,27 @@ private static void ExpandSuperscripts(IList tokenList, AngouriMathLexer } /// - /// A call of beta or norm with one argument, handed back as the name and a bracket, - /// which is what it was read as before the two were functions: beta(x) is beta * x - /// and norm([3, 4]) is norm * [3, 4], the bracket binding as it did, so that - /// norm(x)^2 is still norm * x ^ 2. The functions are the two-argument forms, - /// B(a, b) and the p-norm, which were parse errors; what the one-argument forms mean waits on - /// https://github.com/asc-community/AngouriMath/issues/1019. + /// A call of beta or norm with one argument, refused. Each was read as the product of + /// a variable named for it with its argument: norm([3, 4]) was norm * [3, 4], which + /// simplifies to [3 * norm, 4 * norm] with no error, and beta(x) was beta * x. + /// The functions are the two-argument forms, B(a, b) and the p-norm. Refused here, before the + /// grammar, which would otherwise report only a count; the name without a bracket, and with a + /// space before one, is still a variable, as the other names refused by name are. /// https://github.com/asc-community/AngouriMath/issues/2182 /// - private static void SplitOneArgumentCalls(IList tokenList) + private static void RefuseOneArgumentCalls(IList tokenList) { for (var i = 0; i < tokenList.Count; i++) { var call = tokenList[i]; if (call.Channel != 0 || DisplayName(call) is not ("'beta('" or "'norm('") || HasASecondArgument(tokenList, i)) continue; - var name = call.Text.Substring(0, call.Text.Length - 1); - tokenList[i] = new CommonToken(AngouriMathLexer.VARIABLE, name) { Line = call.Line, Column = call.Column }; - var bracket = 0; - while (AngouriMathLexer.DefaultVocabulary.GetDisplayName(bracket) != "'('") - bracket++; - tokenList.Insert(i + 1, new CommonToken(bracket, "(") { Line = call.Line, Column = call.Column + name.Length }); + throw new FunctionArgumentCountException(DisplayName(call) == "'norm('" + ? "norm(v, p) is the p-norm of a vector and takes two arguments: the length is norm(v, 2), which is abs(v). " + + "A one-argument norm(...) is refused rather than read as the product of a variable named norm " + + "with its argument; write norm * (...) for that" + : "beta(a, b) is the beta function and takes two arguments. A one-argument beta(...) is refused " + + "rather than read as the product of a variable named beta with its argument; write beta * (...) for that"); } static string DisplayName(IToken token) => AngouriMathLexer.DefaultVocabulary.GetDisplayName(token.Type); @@ -240,7 +240,7 @@ internal static Either> ParseSilent(stri ); ExpandSuperscripts(tokenList, lexer); - SplitOneArgumentCalls(tokenList); + RefuseOneArgumentCalls(tokenList); if (InsertOmittedTokensOrProvideDiagnostic(tokenList, lexer).Is(out var whyFailed)) return new Failure(whyFailed); diff --git a/Sources/AngouriMath/Docs/Usage/Syntax.md b/Sources/AngouriMath/Docs/Usage/Syntax.md index a5928f56d..7086787d3 100644 --- a/Sources/AngouriMath/Docs/Usage/Syntax.md +++ b/Sources/AngouriMath/Docs/Usage/Syntax.md @@ -301,9 +301,9 @@ row, real or complex, is taken when simplified: `norm([3, 4], 1)` is `7`, `norm( `norm([3, 4], 3)` is `91^(1/3)`. The 2-norm is `abs(v)`, and `norm(v, 2)` is computed as that, so the two are the same expression. Below `p = 1` it is not a norm, the triangle inequality failing, so a `p` that is a number below `1`, or off the real line, is refused, as is a matrix written out that is not a vector, -whose norms are other functions; a symbolic `p` stays as written, with the condition `p >= 1`. With one -argument, `norm(v)` is still the product `norm * v`, as it was before the norm was a function: what it -should mean waits on [#1019](https://github.com/asc-community/AngouriMath/issues/1019) +whose norms are other functions; a symbolic `p` stays as written, with the condition `p >= 1`. A call +with one argument, `norm(v)`, is refused: the length is `norm(v, 2)`. It was read as the product +`norm * v`, so `norm([3, 4])` gave `[3 * norm, 4 * norm]` with no error. `beta(x)` is refused the same way ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). ## Functions @@ -507,8 +507,8 @@ of the rational function above, so `beta(2, -3)` is `1/6`, as mpmath's `beta` ta is one it is `0`. Anything else numeric, real or complex, is evaluated to the working precision. It is symmetric, `beta(b, a)` simplifying to the same node, and its derivative in `a` is `beta(a, b) * (digamma(a) - digamma(a + b))`. As `gamma` is, `beta` alone is a variable and `beta (x)` -with a space a product; with one argument, `beta(x)` is still the product `beta * x` -([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). +with a space a product; a call with one argument, `beta(x)`, is refused, where it was the product +`beta * x` ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). **Polygamma functions** — `polygamma(n, z)` `digamma(z)`. The polygamma function `psi^(n)(z)`, the `(n + 1)`-th derivative of `ln Gamma(z)` for a whole `n >= 0`, and the digamma function diff --git a/Sources/MCP/AngouriMath.Mcp/Parsing.cs b/Sources/MCP/AngouriMath.Mcp/Parsing.cs index be284a48a..53f80847c 100644 --- a/Sources/MCP/AngouriMath.Mcp/Parsing.cs +++ b/Sources/MCP/AngouriMath.Mcp/Parsing.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -69,9 +69,9 @@ internal static bool IsFunction(string name) => Calls.GetOrAdd(name, static cand private static readonly ConcurrentDictionary Calls = new(StringComparer.Ordinal); /// - /// Whether the grammar reads name(x, y) as a call. beta and norm are functions of two - /// arguments only, and with one are still the product they were, so says no for them; - /// with a comma, a name that is no function does not parse at all, the bracket not being a factor. + /// Whether the grammar reads name(x, y) as a call: with a comma, a name that is no function does not + /// parse at all, the bracket not being a factor. beta and norm are functions of two arguments + /// only; with one each is refused, so says yes for them as well. /// https://github.com/asc-community/AngouriMath/issues/2182 /// internal static bool IsFunctionOfSeveralArguments(string name) => SeveralArgumentCalls.GetOrAdd(name, static candidate => diff --git a/Sources/Tests/McpUnitTests/ParsingTest.cs b/Sources/Tests/McpUnitTests/ParsingTest.cs index 99ebd327a..e9c81e410 100644 --- a/Sources/Tests/McpUnitTests/ParsingTest.cs +++ b/Sources/Tests/McpUnitTests/ParsingTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -69,7 +69,7 @@ public void TheSyntaxDocumentNamesEveryFunctionTheGrammarAccepts(string name) } [Theory] - [InlineData("beta")] + [InlineData("kappa")] [InlineData("Γ")] [InlineData("e")] [InlineData("pi")] diff --git a/Sources/Tests/McpUnitTests/ToolsTest.cs b/Sources/Tests/McpUnitTests/ToolsTest.cs index 75be6319f..8d11b9e9a 100644 --- a/Sources/Tests/McpUnitTests/ToolsTest.cs +++ b/Sources/Tests/McpUnitTests/ToolsTest.cs @@ -51,7 +51,7 @@ public void ACorrectSpellingIsNotWarnedAbout(string expression, string parsed) [Theory] [InlineData("Γ(x+1)", "Γ * (x + 1)")] - [InlineData("beta(z)", "beta * z")] + [InlineData("kappa(z)", "kappa * z")] public void AnUnknownNameBeforeABracketIsWarnedAboutAsAProduct(string expression, string parsed) { var answer = Mcp.Call("am_parse", $$"""{"expression":"{{expression}}"}"""); diff --git a/Sources/Tests/UnitTests/Convenience/BetaTest.cs b/Sources/Tests/UnitTests/Convenience/BetaTest.cs index 7dca77f0b..ce5d166de 100644 --- a/Sources/Tests/UnitTests/Convenience/BetaTest.cs +++ b/Sources/Tests/UnitTests/Convenience/BetaTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -202,12 +202,6 @@ public void ItsDerivativeIsTheBetaFunctionTimesADifferenceOfDigammas(string vari [InlineData("beta (a, b)", null)] [InlineData("beta (x)", "beta * x")] [InlineData("gamma (x)", "gamma * x")] - // With one argument it is the product it was before beta(a, b) was a function. - [InlineData("beta(x)", "beta * x")] - [InlineData("beta(x)^2", "beta * x ^ 2")] - [InlineData("2^beta(x)", "2 ^ beta * x")] - [InlineData("beta(log(2, x))", "beta * log(2, x)")] - [InlineData("beta([1, 2])", "beta * [1, 2]")] public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string? expected) { if (expected is null) @@ -216,6 +210,21 @@ public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string? expect Assert.Equal(expected, written.ToEntity().Stringize()); } + /// + /// With one argument it is refused: it was the product beta * x, with no error. + /// + [Theory] + [InlineData("beta(x)")] + [InlineData("beta(x)^2")] + [InlineData("2^beta(x)")] + [InlineData("beta(log(2, x))")] + [InlineData("beta([1, 2])")] + public void OneArgumentIsRefused(string written) + { + var refused = Assert.Throws(() => written.ToEntity()); + Assert.Contains("beta(a, b)", refused.Message); + } + [Fact] public void ItTakesTwoArguments() { diff --git a/Sources/Tests/UnitTests/Convenience/NormTest.cs b/Sources/Tests/UnitTests/Convenience/NormTest.cs index 148213856..83334b4a1 100644 --- a/Sources/Tests/UnitTests/Convenience/NormTest.cs +++ b/Sources/Tests/UnitTests/Convenience/NormTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -121,22 +121,37 @@ public void ASymbolicOrderKeepsTheNodeAndItsCondition() } /// - /// With one argument it is the product it was, norm * v, binding as it did: what a one-argument - /// norm should mean waits on #1019. + /// The name alone is a variable, and with a space before the bracket a product, as the other names + /// refused by name are. /// [Theory] [InlineData("norm", "norm")] - [InlineData("norm([3, 4])", "norm * [3, 4]")] - [InlineData("norm(x)^2", "norm * x ^ 2")] [InlineData("norm (x)", "norm * x")] - [InlineData("norm({1, 2})", "norm * { 1, 2 }")] - [InlineData("norm([0; 1))", "norm * [0; 1)")] - public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string expected) + [InlineData("norm * [3, 4]", "norm * [3, 4]")] + public void TheNameWithoutACallIsWhatItWas(string written, string expected) => Assert.Equal(expected, written.ToEntity().Stringize()); + /// + /// With one argument it is refused. It was the product norm * v, so norm([3, 4]) simplified + /// to [3 * norm, 4 * norm] with no error, where the length is 5. + /// + [Theory] + [InlineData("norm([3, 4])")] + [InlineData("norm(x)^2")] + [InlineData("norm({1, 2})")] + [InlineData("norm([0; 1))")] + [InlineData("1 + norm(v)")] + [InlineData("norm(norm(v, 2))")] + public void OneArgumentIsRefused(string written) + { + var refused = Assert.Throws(() => written.ToEntity()); + Assert.Contains("norm(v, 2)", refused.Message); + } + + // A second argument inside a bracket of the first is not the call's own. [Fact] - public void TheOneArgumentProductIsNotChanged() - => Assert.Equal("[3 * norm, 4 * norm]".ToEntity(), "norm([3, 4])".ToEntity().Simplify()); + public void ACommaInsideTheFirstArgumentIsNotASecondArgument() + => Assert.Throws(() => "norm([3, 4])".ToEntity()); [Fact] public void ItTakesTwoArguments() diff --git a/Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs b/Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs index 9000e3b6d..a31bee068 100644 --- a/Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs +++ b/Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2022 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -327,11 +327,12 @@ public void IntegralTakesNoOrder() /// /// Short enough to be somebody's variable, so read as a product and not refused. re and - /// im were the examples until they became the real and the imaginary part. + /// im were the examples until they became the real and the imaginary part, and beta + /// and norm until a one-argument call of either was refused. /// [Theory] - [InlineData("beta(x)")] - [InlineData("norm(x)")] + [InlineData("kappa(x)")] + [InlineData("w(x)")] public void ANameTheLibraryDoesNotRefuseIsAProduct(string written) => Assert.IsType(written.ToEntity());