Skip to content

Commit 0cfc9ec

Browse files
Refuse norm(v) and beta(x) with one argument, read as products with no error (#2499)
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 #2498. Part of #2182 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dce1653 commit 0cfc9ec

9 files changed

Lines changed: 88 additions & 47 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ is now put back in, and kept where the equation is exactly zero. The roots Newto
6969
| `"sin(sqrt(x)) = x".ToEntity().Solve("x")` | `{ 0.76864885676094940… }` | `{ 0, 0.76864885676094940… }` |
7070
| `"cos(1 - sqrt(x)) - x = cos(1)".ToEntity().Solve("x")` | `{ 0.39003113021627860… }` | `{ 0, 0.39003113021627860… }` |
7171

72+
### `norm(v)` and `beta(x)` with one argument are refused
73+
74+
**Not silent: what parsed now throws.** A call of `norm` or `beta` with one argument was read as the product of a
75+
variable named `norm` or `beta` with its argument. So `norm([3, 4])` simplified to `[3 * norm, 4 * norm]` with no
76+
error, where the length is `5`. Each now throws `FunctionArgumentCountException`, saying what to write: `norm(v, 2)`
77+
for the length, which is `abs(v)`, and `beta(a, b)` for the beta function, or `norm * (…)` and `beta * (…)` for the
78+
product. The name alone is still a variable, and with a space before the bracket, `norm (x)`, still a product, as the
79+
other names refused by name are ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
80+
81+
| Input | Was (2.8.0) | Now |
82+
|---|---|---|
83+
| `"norm([3, 4])".ToEntity()` | `norm * [3, 4]` | throws `FunctionArgumentCountException` |
84+
| `"norm([3, 4])".ToEntity().Simplify()` | `[3 * norm, 4 * norm]` | throws, as above |
85+
| `"beta(x)".ToEntity()` | `beta * x` | throws `FunctionArgumentCountException` |
86+
| `"norm (x)".ToEntity()` | `norm * x` | `norm * x`, unchanged |
87+
7288
## 2.8.0 — since 2.7.0
7389

7490
**Read these first.**

‎Sources/AngouriMath/Core/Parser.cs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,27 @@ private static void ExpandSuperscripts(IList<IToken> tokenList, AngouriMathLexer
110110
}
111111

112112
/// <summary>
113-
/// A call of <c>beta</c> or <c>norm</c> with one argument, handed back as the name and a bracket,
114-
/// which is what it was read as before the two were functions: <c>beta(x)</c> is <c>beta * x</c>
115-
/// and <c>norm([3, 4])</c> is <c>norm * [3, 4]</c>, the bracket binding as it did, so that
116-
/// <c>norm(x)^2</c> is still <c>norm * x ^ 2</c>. The functions are the two-argument forms,
117-
/// B(a, b) and the p-norm, which were parse errors; what the one-argument forms mean waits on
118-
/// https://github.com/asc-community/AngouriMath/issues/1019.
113+
/// A call of <c>beta</c> or <c>norm</c> with one argument, refused. Each was read as the product of
114+
/// a variable named for it with its argument: <c>norm([3, 4])</c> was <c>norm * [3, 4]</c>, which
115+
/// simplifies to <c>[3 * norm, 4 * norm]</c> with no error, and <c>beta(x)</c> was <c>beta * x</c>.
116+
/// The functions are the two-argument forms, B(a, b) and the p-norm. Refused here, before the
117+
/// grammar, which would otherwise report only a count; the name without a bracket, and with a
118+
/// space before one, is still a variable, as the other names refused by name are.
119119
/// https://github.com/asc-community/AngouriMath/issues/2182
120120
/// </summary>
121-
private static void SplitOneArgumentCalls(IList<IToken> tokenList)
121+
private static void RefuseOneArgumentCalls(IList<IToken> tokenList)
122122
{
123123
for (var i = 0; i < tokenList.Count; i++)
124124
{
125125
var call = tokenList[i];
126126
if (call.Channel != 0 || DisplayName(call) is not ("'beta('" or "'norm('") || HasASecondArgument(tokenList, i))
127127
continue;
128-
var name = call.Text.Substring(0, call.Text.Length - 1);
129-
tokenList[i] = new CommonToken(AngouriMathLexer.VARIABLE, name) { Line = call.Line, Column = call.Column };
130-
var bracket = 0;
131-
while (AngouriMathLexer.DefaultVocabulary.GetDisplayName(bracket) != "'('")
132-
bracket++;
133-
tokenList.Insert(i + 1, new CommonToken(bracket, "(") { Line = call.Line, Column = call.Column + name.Length });
128+
throw new FunctionArgumentCountException(DisplayName(call) == "'norm('"
129+
? "norm(v, p) is the p-norm of a vector and takes two arguments: the length is norm(v, 2), which is abs(v). " +
130+
"A one-argument norm(...) is refused rather than read as the product of a variable named norm " +
131+
"with its argument; write norm * (...) for that"
132+
: "beta(a, b) is the beta function and takes two arguments. A one-argument beta(...) is refused " +
133+
"rather than read as the product of a variable named beta with its argument; write beta * (...) for that");
134134
}
135135

136136
static string DisplayName(IToken token) => AngouriMathLexer.DefaultVocabulary.GetDisplayName(token.Type);
@@ -240,7 +240,7 @@ internal static Either<Entity, Failure<ReasonWhyParsingFailed>> ParseSilent(stri
240240
);
241241

242242
ExpandSuperscripts(tokenList, lexer);
243-
SplitOneArgumentCalls(tokenList);
243+
RefuseOneArgumentCalls(tokenList);
244244
if (InsertOmittedTokensOrProvideDiagnostic(tokenList, lexer).Is<ReasonWhyParsingFailed>(out var whyFailed))
245245
return new Failure<ReasonWhyParsingFailed>(whyFailed);
246246

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ row, real or complex, is taken when simplified: `norm([3, 4], 1)` is `7`, `norm(
301301
`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
302302
are the same expression. Below `p = 1` it is not a norm, the triangle inequality failing, so a `p` that is
303303
a number below `1`, or off the real line, is refused, as is a matrix written out that is not a vector,
304-
whose norms are other functions; a symbolic `p` stays as written, with the condition `p >= 1`. With one
305-
argument, `norm(v)` is still the product `norm * v`, as it was before the norm was a function: what it
306-
should mean waits on [#1019](https://github.com/asc-community/AngouriMath/issues/1019)
304+
whose norms are other functions; a symbolic `p` stays as written, with the condition `p >= 1`. A call
305+
with one argument, `norm(v)`, is refused: the length is `norm(v, 2)`. It was read as the product
306+
`norm * v`, so `norm([3, 4])` gave `[3 * norm, 4 * norm]` with no error. `beta(x)` is refused the same way
307307
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
308308

309309
## Functions
@@ -507,8 +507,8 @@ of the rational function above, so `beta(2, -3)` is `1/6`, as mpmath's `beta` ta
507507
is one it is `0`. Anything else numeric, real or complex, is evaluated to the working precision. It is
508508
symmetric, `beta(b, a)` simplifying to the same node, and its derivative in `a` is
509509
`beta(a, b) * (digamma(a) - digamma(a + b))`. As `gamma` is, `beta` alone is a variable and `beta (x)`
510-
with a space a product; with one argument, `beta(x)` is still the product `beta * x`
511-
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
510+
with a space a product; a call with one argument, `beta(x)`, is refused, where it was the product
511+
`beta * x` ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
512512

513513
**Polygamma functions** — `polygamma(n, z)` `digamma(z)`. The polygamma function `psi^(n)(z)`, the
514514
`(n + 1)`-th derivative of `ln Gamma(z)` for a whole `n >= 0`, and the digamma function

‎Sources/MCP/AngouriMath.Mcp/Parsing.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2026 Angouri.
33
// AngouriMath is licensed under MIT.
44
// 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
6969
private static readonly ConcurrentDictionary<string, bool> Calls = new(StringComparer.Ordinal);
7070

7171
/// <summary>
72-
/// Whether the grammar reads <c>name(x, y)</c> as a call. <c>beta</c> and <c>norm</c> are functions of two
73-
/// arguments only, and with one are still the product they were, so <see cref="IsFunction"/> says no for them;
74-
/// with a comma, a name that is no function does not parse at all, the bracket not being a factor.
72+
/// Whether the grammar reads <c>name(x, y)</c> as a call: with a comma, a name that is no function does not
73+
/// parse at all, the bracket not being a factor. <c>beta</c> and <c>norm</c> are functions of two arguments
74+
/// only; with one each is refused, so <see cref="IsFunction"/> says yes for them as well.
7575
/// https://github.com/asc-community/AngouriMath/issues/2182
7676
/// </summary>
7777
internal static bool IsFunctionOfSeveralArguments(string name) => SeveralArgumentCalls.GetOrAdd(name, static candidate =>

‎Sources/Tests/McpUnitTests/ParsingTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2026 Angouri.
33
// AngouriMath is licensed under MIT.
44
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
@@ -69,7 +69,7 @@ public void TheSyntaxDocumentNamesEveryFunctionTheGrammarAccepts(string name)
6969
}
7070

7171
[Theory]
72-
[InlineData("beta")]
72+
[InlineData("kappa")]
7373
[InlineData("Γ")]
7474
[InlineData("e")]
7575
[InlineData("pi")]

‎Sources/Tests/McpUnitTests/ToolsTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void ACorrectSpellingIsNotWarnedAbout(string expression, string parsed)
5151

5252
[Theory]
5353
[InlineData("Γ(x+1)", "Γ * (x + 1)")]
54-
[InlineData("beta(z)", "beta * z")]
54+
[InlineData("kappa(z)", "kappa * z")]
5555
public void AnUnknownNameBeforeABracketIsWarnedAboutAsAProduct(string expression, string parsed)
5656
{
5757
var answer = Mcp.Call("am_parse", $$"""{"expression":"{{expression}}"}""");

‎Sources/Tests/UnitTests/Convenience/BetaTest.cs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2026 Angouri.
33
// AngouriMath is licensed under MIT.
44
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
@@ -202,12 +202,6 @@ public void ItsDerivativeIsTheBetaFunctionTimesADifferenceOfDigammas(string vari
202202
[InlineData("beta (a, b)", null)]
203203
[InlineData("beta (x)", "beta * x")]
204204
[InlineData("gamma (x)", "gamma * x")]
205-
// With one argument it is the product it was before beta(a, b) was a function.
206-
[InlineData("beta(x)", "beta * x")]
207-
[InlineData("beta(x)^2", "beta * x ^ 2")]
208-
[InlineData("2^beta(x)", "2 ^ beta * x")]
209-
[InlineData("beta(log(2, x))", "beta * log(2, x)")]
210-
[InlineData("beta([1, 2])", "beta * [1, 2]")]
211205
public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string? expected)
212206
{
213207
if (expected is null)
@@ -216,6 +210,21 @@ public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string? expect
216210
Assert.Equal(expected, written.ToEntity().Stringize());
217211
}
218212

213+
/// <summary>
214+
/// With one argument it is refused: it was the product <c>beta * x</c>, with no error.
215+
/// </summary>
216+
[Theory]
217+
[InlineData("beta(x)")]
218+
[InlineData("beta(x)^2")]
219+
[InlineData("2^beta(x)")]
220+
[InlineData("beta(log(2, x))")]
221+
[InlineData("beta([1, 2])")]
222+
public void OneArgumentIsRefused(string written)
223+
{
224+
var refused = Assert.Throws<FunctionArgumentCountException>(() => written.ToEntity());
225+
Assert.Contains("beta(a, b)", refused.Message);
226+
}
227+
219228
[Fact]
220229
public void ItTakesTwoArguments()
221230
{

‎Sources/Tests/UnitTests/Convenience/NormTest.cs‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2026 Angouri.
33
// AngouriMath is licensed under MIT.
44
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
@@ -121,22 +121,37 @@ public void ASymbolicOrderKeepsTheNodeAndItsCondition()
121121
}
122122

123123
/// <summary>
124-
/// With one argument it is the product it was, <c>norm * v</c>, binding as it did: what a one-argument
125-
/// <c>norm</c> should mean waits on <a href="https://github.com/asc-community/AngouriMath/issues/1019">#1019</a>.
124+
/// The name alone is a variable, and with a space before the bracket a product, as the other names
125+
/// refused by name are.
126126
/// </summary>
127127
[Theory]
128128
[InlineData("norm", "norm")]
129-
[InlineData("norm([3, 4])", "norm * [3, 4]")]
130-
[InlineData("norm(x)^2", "norm * x ^ 2")]
131129
[InlineData("norm (x)", "norm * x")]
132-
[InlineData("norm({1, 2})", "norm * { 1, 2 }")]
133-
[InlineData("norm([0; 1))", "norm * [0; 1)")]
134-
public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string expected)
130+
[InlineData("norm * [3, 4]", "norm * [3, 4]")]
131+
public void TheNameWithoutACallIsWhatItWas(string written, string expected)
135132
=> Assert.Equal(expected, written.ToEntity().Stringize());
136133

134+
/// <summary>
135+
/// With one argument it is refused. It was the product <c>norm * v</c>, so <c>norm([3, 4])</c> simplified
136+
/// to <c>[3 * norm, 4 * norm]</c> with no error, where the length is 5.
137+
/// </summary>
138+
[Theory]
139+
[InlineData("norm([3, 4])")]
140+
[InlineData("norm(x)^2")]
141+
[InlineData("norm({1, 2})")]
142+
[InlineData("norm([0; 1))")]
143+
[InlineData("1 + norm(v)")]
144+
[InlineData("norm(norm(v, 2))")]
145+
public void OneArgumentIsRefused(string written)
146+
{
147+
var refused = Assert.Throws<FunctionArgumentCountException>(() => written.ToEntity());
148+
Assert.Contains("norm(v, 2)", refused.Message);
149+
}
150+
151+
// A second argument inside a bracket of the first is not the call's own.
137152
[Fact]
138-
public void TheOneArgumentProductIsNotChanged()
139-
=> Assert.Equal("[3 * norm, 4 * norm]".ToEntity(), "norm([3, 4])".ToEntity().Simplify());
153+
public void ACommaInsideTheFirstArgumentIsNotASecondArgument()
154+
=> Assert.Throws<FunctionArgumentCountException>(() => "norm([3, 4])".ToEntity());
140155

141156
[Fact]
142157
public void ItTakesTwoArguments()

‎Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2022 Angouri.
33
// AngouriMath is licensed under MIT.
44
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
@@ -327,11 +327,12 @@ public void IntegralTakesNoOrder()
327327

328328
/// <summary>
329329
/// Short enough to be somebody's variable, so read as a product and not refused. <c>re</c> and
330-
/// <c>im</c> were the examples until they became the real and the imaginary part.
330+
/// <c>im</c> were the examples until they became the real and the imaginary part, and <c>beta</c>
331+
/// and <c>norm</c> until a one-argument call of either was refused.
331332
/// </summary>
332333
[Theory]
333-
[InlineData("beta(x)")]
334-
[InlineData("norm(x)")]
334+
[InlineData("kappa(x)")]
335+
[InlineData("w(x)")]
335336
public void ANameTheLibraryDoesNotRefuseIsAProduct(string written) =>
336337
Assert.IsType<Entity.Mulf>(written.ToEntity());
337338

0 commit comments

Comments
 (0)