Skip to content

Commit e8446b6

Browse files
A product is written letters before functions (#1635)
By hand a product is written the number first, then the letters and their powers, then the functions: y sin(x), x e^x, z Γ(z) (DLMF 5.5.1). ISO 80000-2 lets a function's argument go without parentheses, sin nπ, so a letter written after a function reads as part of its argument. Simplify wrote sin(x) * y and e ^ x * x, the order its search sorts by. The answer of Simplify is now put in that order as its last step, each kind of factor keeping the order it had, and a leading number is taken into a quotient over a number: 2 / s, not 2 * 1 / s. The search keeps its own order. Its product rules are binary and meet 2 cos(x) sin(x) as neighbours only because nothing sorts between them; sorting letters first inside the search stopped 2 x sin(x) cos(x) becoming x sin(2x) and made lim 1/x^2 - 1/sin(x)^2 at 0 NaN. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent dcf5ba7 commit e8446b6

12 files changed

Lines changed: 203 additions & 22 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,32 @@ quotient or whole power with a matrix in it to GenericTensor's operation of that
797797
| `"[[x, 2 x], [x + 1, x^2]]".ToEntity().Compile<double, GenTensor<double, DoubleOperations>>("x")` | `UncompilableNodeException` | a delegate; at `3` the matrix `[[3, 6], [4, 9]]` |
798798
| `"A * B".ToEntity().Compile<GenTensor<…>, GenTensor<…>, GenTensor<…>>("A", "B")` | `UncompilableNodeException` | the matrix product |
799799
| `"[[1, 0]] * [[a, b], [c, d]] * [[0], [1]]".ToEntity().Compile<double, double, double, double, double>("a", "b", "c", "d")` | `2` at `(1, 2, 3, 4)` — simplified to the scalar first | unchanged |
800+
### A product is written letters before functions
801+
802+
`Simplify` wrote `y * sin(x)` as `sin(x) * y` and `x * e^x` as `e ^ x * x`: a product came out in
803+
the order the simplifier sorts by, which puts a sine before every letter. By hand the number comes
804+
first, then the letters and their powers, then the functions: DLMF 5.5.1 is `Γ(z + 1) = z Γ(z)`,
805+
and ISO 80000-2 lets a function's argument go without parentheses (`sin nπ`), so a letter written
806+
after a function reads as part of its argument. The answer of `Simplify` is now written that way:
807+
the number, the letters and their powers, then everything else -- a function, an exponential such
808+
as `e^x` or `2^n`, a sum -- each kind in the order it had, with a leading number taken into a
809+
quotient over a number (`2 / s`, not `2 * 1 / s`). Only the answer is reordered: the simplifier
810+
still sorts a sine before a letter, because the rules that combine factors meet them as neighbours
811+
in that order, and `InnerSimplified` and `Differentiate` return that order as before.
812+
[#1628](https://github.com/asc-community/AngouriMath/issues/1628), Algebrite's issue 29 from
813+
[#180](https://github.com/asc-community/AngouriMath/issues/180). Both columns measured on a build,
814+
`v2.5.0` against this change.
815+
816+
| `"….".ToEntity().Simplify()` of | Was (2.5.0) | Is |
817+
|---|---|---|
818+
| `sin(x) * y` | `sin(x) * y` | `y * sin(x)` |
819+
| `x * e ^ x` | `e ^ x * x` | `x * e ^ x` |
820+
| `2 * sin(x) * y` | `2 * sin(x) * y` | `2 * y * sin(x)` |
821+
| `a / sin(x)` | `csc(x) * a` | `a * csc(x)` |
822+
| `a * log(x) + y * sin(x)` | `sin(x) * y + a * log(10, x)` | `y * sin(x) + a * log(10, x)` |
823+
| `2 * x * sin(x) * cos(x)` | `sin(2 * x) * x` | `x * sin(2 * x)` |
824+
| `derivative(arcsin(2 * x), x)` | `1 / sqrt(1 - (2 * x) ^ 2) * 2` | `2 / sqrt(1 - (2 * x) ^ 2)` |
825+
800826
### `π` is `pi` and the script `ℯ` is `e`
801827

802828
`π` (U+03C0) was a free variable, a Greek letter like any other, so `sin(π)` did not simplify; the

‎Sources/AngouriMath/Functions/Simplification/Simplificator.cs‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,100 @@ internal static Entity Simplify(Entity expr, int level)
5151
// run `x/sin(x)` for a minute where it declined in half a second.
5252
// https://github.com/asc-community/AngouriMath/issues/1394
5353
var mark = recording?.Mark() ?? 0;
54-
return Noted(recording, simplified, WithoutTheConditionsItStates(simplified), "ConditionsTheExpressionStates", mark);
54+
var stated = Noted(recording, simplified, WithoutTheConditionsItStates(simplified), "ConditionsTheExpressionStates", mark);
55+
mark = recording?.Mark() ?? 0;
56+
return Noted(recording, stated, InPrintOrder(stated), "ProductsInPrintOrder", mark);
5557
}
5658

59+
/// <summary>
60+
/// The answer with each product's factors in the order a product is written by hand: the
61+
/// number, then the letters and their powers, then the rest -- a function, an exponential,
62+
/// a sum -- each kind in the order it had. So <c>2 y sin(x)</c> and <c>x e^x</c>, not
63+
/// <c>2 sin(x) y</c> and <c>e^x x</c>: the references write it so (DLMF 5.5.1 is
64+
/// <c>Γ(z + 1) = z Γ(z)</c>), and ISO 80000-2 lets a function's argument go without
65+
/// parentheses, <c>sin nπ</c>, so a letter written after a function reads as part of its
66+
/// argument. https://github.com/asc-community/AngouriMath/issues/1628
67+
/// </summary>
68+
/// <remarks>
69+
/// Only the answer is put in this order. The canonical order the search sorts by puts a
70+
/// sine before every letter, and the rules that combine factors are binary: they meet
71+
/// <c>2 cos(x) sin(x)</c> as neighbours only because nothing sorts between them. Sorting
72+
/// letters first inside the search put <c>x</c> there, so <c>2 x sin(x) cos(x)</c> no
73+
/// longer became <c>x sin(2x)</c>, l'Hopital's rule saw its quotients grow a step sooner
74+
/// and <c>lim 1/x^2 - 1/sin(x)^2</c> at 0 was <c>NaN</c> where it is <c>-1/3</c>. Reordering a
75+
/// product changes no node count, so nothing that ranks candidates sees it, and a caller
76+
/// simplifying the answer again has it sorted back before any rule reads it.
77+
/// </remarks>
78+
internal static Entity InPrintOrder(Entity expression)
79+
=> expression.Replace(static node => node is Entity.Mulf product ? FactorsInPrintOrder(product) : node);
80+
81+
private static Entity FactorsInPrintOrder(Entity.Mulf product)
82+
{
83+
// Read before anything is built: this runs on every answer, and most products are
84+
// in order already.
85+
var highest = 0;
86+
if (InOrder(product, ref highest))
87+
return product;
88+
var factors = new System.Collections.Generic.List<Entity>();
89+
Gather(product, factors);
90+
var ordered = new System.Collections.Generic.List<Entity>(factors.Count);
91+
for (var rank = 0; rank <= 2; rank++)
92+
foreach (var factor in factors)
93+
if (FactorRank(factor) == rank)
94+
ordered.Add(factor);
95+
// A number brought to the front of a quotient over a number is that quotient's
96+
// numerator: 2 * (1 / s) is written 2 / s rather than 2 * 1 / s.
97+
if (ordered.Count > 1 && ordered[0] is Entity.Number number
98+
&& ordered[1] is Entity.Divf(Entity.Number numerator, var divisor))
99+
{
100+
ordered[1] = new Entity.Divf((number * numerator).InnerSimplified, divisor);
101+
ordered.RemoveAt(0);
102+
}
103+
Entity rebuilt = ordered[0];
104+
for (var j = 1; j < ordered.Count; j++)
105+
rebuilt = new Entity.Mulf(rebuilt, ordered[j]);
106+
return rebuilt;
107+
108+
static bool InOrder(Entity node, ref int highest)
109+
{
110+
if (node is Entity.Mulf(var left, var right))
111+
return InOrder(left, ref highest) && InOrder(right, ref highest);
112+
var rank = FactorRank(node);
113+
if (rank < highest)
114+
return false;
115+
highest = rank;
116+
return true;
117+
}
118+
119+
static void Gather(Entity node, System.Collections.Generic.List<Entity> into)
120+
{
121+
if (node is Entity.Mulf(var left, var right))
122+
{
123+
Gather(left, into);
124+
Gather(right, into);
125+
}
126+
else
127+
into.Add(node);
128+
}
129+
}
130+
131+
/// <summary>0 for a number, 1 for a letter or a power of one, 2 for anything else.</summary>
132+
/// <remarks>
133+
/// A power with a number for its exponent ranks as its base, so <c>x^2</c> and
134+
/// <c>sqrt(x)</c> are letters and <c>sqrt(2)</c> is a number. A letter raised to a letter
135+
/// is still a power of that letter, as in <c>a x^n</c>; but <c>e</c> raised to anything
136+
/// but a number is the exponential function, and so is a number raised to a letter:
137+
/// <c>x e^x</c>, <c>n 2^n</c>.
138+
/// </remarks>
139+
private static int FactorRank(Entity factor) => factor switch
140+
{
141+
Entity.Number => 0,
142+
Entity.Variable => 1,
143+
Entity.Powf(var @base, Entity.Number) => FactorRank(@base),
144+
Entity.Powf(Entity.Variable @base, _) when @base != Entity.Variable.e => 1,
145+
_ => 2
146+
};
147+
57148
/// <summary>
58149
/// <paramref name="expression"/> with every conjunct of its condition that the
59150
/// expression states on its own taken off. At the top only: a case of a piecewise is

‎Sources/Tests/UnitTests/Calculus/DerivativeTest.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestSin()
3737
public void TestCosCustom()
3838
{
3939
var func = MathS.Cos(MathS.Pow(x, 3));
40-
var expected = -3 * MathS.Sin(MathS.Pow(x, 3)) * MathS.Sqr(x);
40+
var expected = -3 * MathS.Sqr(x) * MathS.Sin(MathS.Pow(x, 3));
4141
var actual = func.Differentiate(x).Simplify();
4242
Assert.Equal(expected, actual);
4343
}
@@ -81,13 +81,13 @@ public void TestArc1()
8181
public void TestArc2()
8282
{
8383
var func = MathS.Arcsin(2 * x);
84-
Assert.Equal(1 / MathS.Sqrt(1 - MathS.Sqr(2 * x)) * 2, func.Differentiate(x).Simplify());
84+
Assert.Equal(2 / MathS.Sqrt(1 - MathS.Sqr(2 * x)), func.Differentiate(x).Simplify());
8585
}
8686
[Fact]
8787
public void TestArc3()
8888
{
8989
var func = MathS.Arccos(2 * x);
90-
Assert.Equal((-1) / MathS.Sqrt(1 - MathS.Sqr(2 * x)) * 2, func.Differentiate(x).Simplify());
90+
Assert.Equal((-2) / MathS.Sqrt(1 - MathS.Sqr(2 * x)), func.Differentiate(x).Simplify());
9191
}
9292
[Fact]
9393
public void TestArc4()

‎Sources/Tests/UnitTests/Calculus/IntegrationTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class IntegrationTest
2121
[InlineData("x2 + x", "x ^ 3 / 3 + x ^ 2 / 2 + C")]
2222
[InlineData("x2 - x", "C + x ^ 3 / 3 - x ^ 2 / 2")]
2323
[InlineData("a / x", "a * ln(abs(x)) + C")]
24-
[InlineData("x cos(x)", "cos(x) + sin(x) * x + C")]
24+
[InlineData("x cos(x)", "cos(x) + x * sin(x) + C")]
2525
[InlineData("sin(x)cos(x)", "sin(x) ^ 2 / 2 + C")]
2626
[InlineData("ln(x)", "x * (ln(x) - 1) + C")]
2727
[InlineData("log(a, x)", "C + x * (ln(x) - 1) / ln(a)")]
@@ -333,7 +333,7 @@ public void TestLnAbsSquared()
333333
// ln(abs(x)) uncombined and the 2 * x outside the bracket.
334334
// (ln|x| - 1)^2 + 1 multiplied out is ln^2|x| - 2ln|x| + 2, which is what
335335
// Expand now returns, having collected the like terms it used to leave apart.
336-
Assert.Equal("C + (2 + ln(abs(x)) ^ 2 + (-2) * ln(abs(x))) * x", result.Stringize());
336+
Assert.Equal("C + x * (2 + ln(abs(x)) ^ 2 + (-2) * ln(abs(x)))", result.Stringize());
337337

338338
// Verify the result by differentiation
339339
var derivative = result.Differentiate("x"); // TODO: Make this simplify to expr with Simplify()

‎Sources/Tests/UnitTests/Common/LambdaCalculusTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ [Fact] public void EtaReduction6()
179179
=> Assert.Equal(
180180
@"lambda(x,
181181
apply(
182-
(34 + sin(x)) * a,
182+
a * (34 + sin(x)),
183183
x
184184
)
185185
)
@@ -194,7 +194,7 @@ [Fact] public void EtaReduction6()
194194
);
195195
[Fact] public void EtaReduction7()
196196
=> Assert.Equal(
197-
@"(34 + sin(y)) * a".ToEntity(),
197+
@"a * (34 + sin(y))".ToEntity(),
198198
@"lambda(x,
199199
apply(
200200
a * (sin(y) + 34),

‎Sources/Tests/UnitTests/Core/BinomialIdentityTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void AnIdentityIsTrue(string identity)
3636
[InlineData("binomial(n - 1, k) + binomial(n - 1, k - 1)", "binomial(n, k)")]
3737
[InlineData("binomial(n, n - 2)", "binomial(n, 2)")]
3838
[InlineData("binomial(n, n - k)", "binomial(n, k)")]
39-
[InlineData("n * binomial(n - 1, k - 1)", "binomial(n, k) * k")]
39+
[InlineData("n * binomial(n - 1, k - 1)", "k * binomial(n, k)")]
4040
[InlineData("binomial(7, 3) + binomial(7, 2)", "56")]
4141
public void ARewriteCollects(string input, string expected)
4242
=> Assert.Equal(expected.ToEntity(), input.ToEntity().Simplify());

‎Sources/Tests/UnitTests/Core/Sets/IntervalImageTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void TheImageOfAnInterval(string expression, string image)
6767
[InlineData("[1; 2] / [-1; 1]")]
6868
[InlineData("arcsin([0; 2])")]
6969
[InlineData("sin([0; 1])")]
70-
[InlineData("(0; 1) * k")]
70+
[InlineData("k * (0; 1)")]
7171
[InlineData("[a; b]^2")]
7272
public void LeftAsWritten(string expression)
7373
=> Assert.Equal(expression.ToEntity(), expression.ToEntity().Simplify());

‎Sources/Tests/UnitTests/Core/Sets/IntervalScalingTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public void MembershipIsWhatTheReflectionIsFor(string expression, bool expected)
9393
expression.ToEntity().Simplify());
9494

9595
/// <summary>
96-
/// <b>An unknown sign is answered by not answering.</b> <c>(0; 1) * k</c> is one interval
96+
/// <b>An unknown sign is answered by not answering.</b> <c>k * (0; 1)</c> is one interval
9797
/// when <c>k</c> is positive and the reflected one when it is negative; picking either
9898
/// would be choosing which, so it is left alone — which is what an unevaluated node means.
9999
/// </summary>
100100
[Theory]
101-
[InlineData("(0; 1) * k")]
101+
[InlineData("k * (0; 1)")]
102102
[InlineData("(0; 1) / k")]
103103
public void AnUnknownSignIsLeftAlone(string expression)
104104
=> Assert.Equal(expression.ToEntity(), expression.ToEntity().Simplify());
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.Extensions;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.PatternsTest
12+
{
13+
/// <summary>
14+
/// A product is written the way it is by hand: the number, then the letters and their powers,
15+
/// then the functions, so <c>y sin(x)</c> and <c>x e^x</c>. Algebrite's issue 29, from #180.
16+
/// https://github.com/asc-community/AngouriMath/issues/1628
17+
/// </summary>
18+
[Trait("Area", "PatternsTest")]
19+
public sealed class FactorOrderTest
20+
{
21+
[Theory]
22+
[InlineData("sin(x) * y", "y * sin(x)")]
23+
[InlineData("sin(x) * b", "b * sin(x)")]
24+
[InlineData("y * 2 * sin(x)", "2 * y * sin(x)")]
25+
[InlineData("log(x) * z", "z * log(10, x)")]
26+
[InlineData("sin(x) * x ^ 2", "x ^ 2 * sin(x)")]
27+
[InlineData("a * log(x) + y * sin(x)", "y * sin(x) + a * log(10, x)")]
28+
public void LettersComeBeforeFunctions(string input, string expected)
29+
=> Assert.Equal(expected, input.ToEntity().Simplify().Stringize());
30+
31+
/// <summary>
32+
/// An exponential is a function, whether its base is <c>e</c> or a number; a letter raised
33+
/// to a letter is a power of that letter.
34+
/// </summary>
35+
[Theory]
36+
[InlineData("e ^ x * x", "x * e ^ x")]
37+
[InlineData("2 ^ n * n", "n * 2 ^ n")]
38+
[InlineData("x ^ n * a", "a * x ^ n")]
39+
public void AnExponentialIsAFunctionAndAPowerOfALetterIsALetter(string input, string expected)
40+
=> Assert.Equal(expected, input.ToEntity().Simplify().Stringize());
41+
42+
/// <summary>
43+
/// Only the answer is put in this order. The rules that combine factors still meet them in
44+
/// the order the search sorts by, where <c>2 cos(x) sin(x)</c> are neighbours: sorting the
45+
/// letters first inside the search put <c>x</c> between them, and this stopped being
46+
/// <c>x sin(2x)</c>.
47+
/// </summary>
48+
[Theory]
49+
[InlineData("2 * x * sin(x) * cos(x)", "x * sin(2 * x)")]
50+
[InlineData("x * (x - 1)!", "x!")]
51+
public void TheRulesStillMeetTheFactorsTheyCombine(string input, string expected)
52+
=> Assert.Equal(expected, input.ToEntity().Simplify().Stringize());
53+
54+
/// <summary>The order is the tree's, so the printed form parses back to the same product.</summary>
55+
[Theory]
56+
[InlineData("sin(x) * y")]
57+
[InlineData("e ^ x * x * 3")]
58+
public void ThePrintedFormReadsBackAsTheSameProduct(string input)
59+
{
60+
var simplified = input.ToEntity().Simplify();
61+
Assert.Equal(simplified, simplified.Stringize().ToEntity());
62+
}
63+
}
64+
}

‎Sources/Tests/UnitTests/PatternsTest/FractionSimplify.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed class FractionSimplify
2424
[InlineData("0*(x+3)^-4", "0 provided not 3 + x = 0")]
2525
[InlineData("(a - 1) / (1 - b) - (1 - a) / (b - 1)", "0 provided not (b - 1) * (1 - b) = 0")] // #254
2626
[InlineData("(4a - 2) / (2x) + (1 - 2a) / x", "0 provided not x = 0")]
27-
[InlineData("sin(a) * a * b / a", "sin(a) * b provided not a = 0")] // #311
27+
[InlineData("sin(a) * a * b / a", "b * sin(a) provided not a = 0")] // #311
2828
[InlineData("sin(a) * cos(b) * tan(c) / (tan(c)3 * sin(a)2 * cos(b)^(-2))", "csc(a) * cos(b) ^ 3 * cotan(c) ^ 2")]
2929
public void TestSimplify(string testeeRaw, string expectedRaw)
3030
{

0 commit comments

Comments
 (0)