Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down
28 changes: 14 additions & 14 deletions Sources/AngouriMath/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,27 @@ private static void ExpandSuperscripts(IList<IToken> tokenList, AngouriMathLexer
}

/// <summary>
/// A call of <c>beta</c> or <c>norm</c> with one argument, handed back as the name and a bracket,
/// which is what it was read as before the two were functions: <c>beta(x)</c> is <c>beta * x</c>
/// and <c>norm([3, 4])</c> is <c>norm * [3, 4]</c>, the bracket binding as it did, so that
/// <c>norm(x)^2</c> is still <c>norm * x ^ 2</c>. 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 <c>beta</c> or <c>norm</c> with one argument, refused. Each was read as the product of
/// a variable named for it with its argument: <c>norm([3, 4])</c> was <c>norm * [3, 4]</c>, which
/// simplifies to <c>[3 * norm, 4 * norm]</c> with no error, and <c>beta(x)</c> was <c>beta * x</c>.
/// 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
/// </summary>
private static void SplitOneArgumentCalls(IList<IToken> tokenList)
private static void RefuseOneArgumentCalls(IList<IToken> 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);
Expand Down Expand Up @@ -240,7 +240,7 @@ internal static Either<Entity, Failure<ReasonWhyParsingFailed>> ParseSilent(stri
);

ExpandSuperscripts(tokenList, lexer);
SplitOneArgumentCalls(tokenList);
RefuseOneArgumentCalls(tokenList);
if (InsertOmittedTokensOrProvideDiagnostic(tokenList, lexer).Is<ReasonWhyParsingFailed>(out var whyFailed))
return new Failure<ReasonWhyParsingFailed>(whyFailed);

Expand Down
10 changes: 5 additions & 5 deletions Sources/AngouriMath/Docs/Usage/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/MCP/AngouriMath.Mcp/Parsing.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -69,9 +69,9 @@ internal static bool IsFunction(string name) => Calls.GetOrAdd(name, static cand
private static readonly ConcurrentDictionary<string, bool> Calls = new(StringComparer.Ordinal);

/// <summary>
/// Whether the grammar reads <c>name(x, y)</c> as a call. <c>beta</c> and <c>norm</c> are functions of two
/// arguments only, and with one are still the product they were, so <see cref="IsFunction"/> 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 <c>name(x, y)</c> as a call: with a comma, a name that is no function does not
/// parse at all, the bracket not being a factor. <c>beta</c> and <c>norm</c> are functions of two arguments
/// only; with one each is refused, so <see cref="IsFunction"/> says yes for them as well.
/// https://github.com/asc-community/AngouriMath/issues/2182
/// </summary>
internal static bool IsFunctionOfSeveralArguments(string name) => SeveralArgumentCalls.GetOrAdd(name, static candidate =>
Expand Down
4 changes: 2 additions & 2 deletions Sources/Tests/McpUnitTests/ParsingTest.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -69,7 +69,7 @@ public void TheSyntaxDocumentNamesEveryFunctionTheGrammarAccepts(string name)
}

[Theory]
[InlineData("beta")]
[InlineData("kappa")]
[InlineData("Γ")]
[InlineData("e")]
[InlineData("pi")]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Tests/McpUnitTests/ToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"}""");
Expand Down
23 changes: 16 additions & 7 deletions Sources/Tests/UnitTests/Convenience/BetaTest.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -216,6 +210,21 @@ public void TheNameWithoutTwoArgumentsIsWhatItWas(string written, string? expect
Assert.Equal(expected, written.ToEntity().Stringize());
}

/// <summary>
/// With one argument it is refused: it was the product <c>beta * x</c>, with no error.
/// </summary>
[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<FunctionArgumentCountException>(() => written.ToEntity());
Assert.Contains("beta(a, b)", refused.Message);
}

[Fact]
public void ItTakesTwoArguments()
{
Expand Down
35 changes: 25 additions & 10 deletions Sources/Tests/UnitTests/Convenience/NormTest.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -121,22 +121,37 @@ public void ASymbolicOrderKeepsTheNodeAndItsCondition()
}

/// <summary>
/// With one argument it is the product it was, <c>norm * v</c>, binding as it did: what a one-argument
/// <c>norm</c> should mean waits on <a href="https://github.com/asc-community/AngouriMath/issues/1019">#1019</a>.
/// The name alone is a variable, and with a space before the bracket a product, as the other names
/// refused by name are.
/// </summary>
[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());

/// <summary>
/// With one argument it is refused. It was the product <c>norm * v</c>, so <c>norm([3, 4])</c> simplified
/// to <c>[3 * norm, 4 * norm]</c> with no error, where the length is 5.
/// </summary>
[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<FunctionArgumentCountException>(() => 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<FunctionArgumentCountException>(() => "norm([3, 4])".ToEntity());

[Fact]
public void ItTakesTwoArguments()
Expand Down
9 changes: 5 additions & 4 deletions Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -327,11 +327,12 @@ public void IntegralTakesNoOrder()

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

Expand Down
Loading