Skip to content

Commit c90db9f

Browse files
A squared sine and cosine with one coefficient are found anywhere in a sum (#2175)
The gathered Pythagorean rule matches sin(x)^2 and cos(x)^2 wherever they sit in a sum, but only with a coefficient of one. With a coefficient the pair was left to SharedFactor, which sees two siblings, so the sort decided: y + (-2) cos(x)^2 + (-2) sin(x)^2 came out y - 2, and 2 + (-2) cos(x)^2 + (-2) sin(x)^2, where the constant sorts between the two, stayed. #2133's 2 + (-2) cos(2x) + (-4) sin(x)^2 opens to that and stopped there, while 2 - 2 cos(2x) - 4 sin(x)^2 reached 0. squared-sine-and-cosine-with-one-coefficient-sum-to-it gathers k sin(x)^2 and k cos(x)^2 of one argument and one coefficient and writes k: Sound for every complex k and x, two-sided, Collects, and with no backwards reading, since it forgets the angle as the first rule does. The census moves by one in WritingARule.md, Saturation's remark and the tests that pin them; ReversibleRuleTest asks every rule of the set instead of Single(). (cos(2x) - 1)^2 + sin(2x)^2 - 4 sin(x)^2 still stops at 2 + (-2) cos(2x) + (-4) sin(x)^2, which one more Simplify now takes to 0: that is #2133's main point, Simplify not simplifying its own answer, and is not this change. On 2.6.0 and on this branch, Simplify: 2 + (-2)*cos(x)^2 + (-2)*sin(x)^2 unchanged -> 0 2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2 unchanged -> 0 Chain against master 966f26a: verdicts identical, 0 moves; performance gate passed on all 19; unit suite 17,159 passed, 0 failed. RuleCheck passes, Confluence and CanonCheck match their baselines, SimpSweep 10,463/10,463. Part of #2133 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 966f26a commit c90db9f

9 files changed

Lines changed: 126 additions & 24 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ its rows in the order it finds them, which can differ from the eliminator's
175175
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
176176
| `MathS.Equations("x*y + 3*x - 5*y - 15", "x^2 - 3*x - 4*y - 10").Solve("x", "y")` | `[[1, -3], [2, -3]]` | `[[5, 0], [1, -3], [2, -3]]` |
177177

178+
### A squared sine and cosine with one coefficient are found anywhere in a sum
179+
180+
**Silent: an identity reaches its value however the sum is written.** `k sin(x)^2 + k cos(x)^2` was reduced to `k`
181+
only where the two terms were siblings, so the order a sum was sorted into decided: with the constant `2` sorted
182+
between them, `2 + (-2) cos(x)^2 + (-2) sin(x)^2` stayed as written while `2 - 2 cos(x)^2 - 2 sin(x)^2` was `0`. The
183+
Pythagorean identity with a coefficient is now a rule of its own, found wherever the two terms sit
184+
([#2133](https://github.com/asc-community/AngouriMath/issues/2133)).
185+
186+
| Input | Was (2.6.0) | Now |
187+
|---|---|---|
188+
| `"2 + (-2)*cos(x)^2 + (-2)*sin(x)^2".Simplify()` | `2 + (-2) * cos(x) ^ 2 + (-2) * sin(x) ^ 2` | `0` |
189+
| `"2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2".Simplify()` | unchanged | `0` |
190+
178191
### A lambda applied to a vector multiplies by it as a vector
179192

180193
**Silent: a matrix of vectors becomes the product.** Applying a lambda simplified its body first, and a simplified

‎Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,32 @@ internal static class MatchedRules
345345
// "x" is bound by the first part and re-matched by the second, so the two terms
346346
// are required to be about the same argument rather than merely both squared.
347347
MatchPattern.Node<Sumf>(MatchPattern.Exact(Integer.Create(1)), MatchPattern.Any("rest")),
348-
Soundness.Sound));
348+
Soundness.Sound),
349+
350+
// ... + k sin(x)^2 + ... + k cos(x)^2 + ... -> k + (everything else)
351+
// With a coefficient the pair was left to SharedFactor, which sees two siblings
352+
// only, so the sort decided: y + (-2) cos(x)^2 + (-2) sin(x)^2 came out y - 2, and
353+
// 2 + (-2) cos(x)^2 + (-2) sin(x)^2, where the constant sorts between them, stayed.
354+
// "k" is bound by the first part and re-matched by the second, so the coefficients
355+
// are required to be the same, as "x" requires the arguments to be.
356+
// https://github.com/asc-community/AngouriMath/issues/2133
357+
new MatchedRule(
358+
"squared-sine-and-cosine-with-one-coefficient-sum-to-it",
359+
MatchPattern.Gathered<Sumf>(
360+
"rest",
361+
MatchPattern.Commutative<Mulf>(
362+
MatchPattern.Any("k"),
363+
MatchPattern.Node<Powf>(
364+
MatchPattern.Node<Sinf>(MatchPattern.Any("x")),
365+
MatchPattern.Exact(Integer.Create(2)))),
366+
MatchPattern.Commutative<Mulf>(
367+
MatchPattern.Any("k"),
368+
MatchPattern.Node<Powf>(
369+
MatchPattern.Node<Cosf>(MatchPattern.Any("x")),
370+
MatchPattern.Exact(Integer.Create(2))))),
371+
MatchPattern.Node<Sumf>(MatchPattern.Any("k"), MatchPattern.Any("rest")),
372+
Soundness.Sound,
373+
description: "k sin(x)^2 + k cos(x)^2 = k"));
349374

350375
/// <summary>
351376
/// <see cref="Functions.Patterns.InvertNegativePowers"/>, as data.

‎Sources/AngouriMath/Core/Transformations/Saturation.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ internal static class Saturation
4343
/// one, and that is a measurement rather than a preference.</b> It reads like the value a
4444
/// ceiling should refuse — it means the rule's growth was not judged, because its
4545
/// replacement is code rather than a written pattern, so admitting it accepts a rewrite
46-
/// nobody measured. But it is where the rules are: of the 337 rules in
47-
/// <see cref="MatchedRules"/>, <b>126 collect, 49 rearrange, 36 expand and 126 are
46+
/// nobody measured. But it is where the rules are: of the 338 rules in
47+
/// <see cref="MatchedRules"/>, <b>127 collect, 49 rearrange, 36 expand and 126 are
4848
/// unjudged</b>. A ceiling that refuses the fourth value still refuses 37% of the library,
4949
/// and what is left could not do much when this was written — over six expression pairs
5050
/// equal only through a larger intermediate form, the

‎Sources/AngouriMath/Docs/Contributing/WritingARule.md‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ effect". This is how to supply those seven things.
1818
## Where a rule goes
1919

2020
`Core/Transformations/Matching/MatchedRules.cs`, as a value in a `MatchedRuleSet`. **33** sets and
21-
**337** rules live there today.
21+
**338** rules live there today.
2222

2323
The `switch` statements in `Functions/Simplification/Patterns` are the older form. **All thirty
2424
registered sets now run as data and describe what they run**; none executes its `switch` any more.
@@ -64,7 +64,7 @@ in a name and putting the identity in brackets after it:
6464
2. Tangent is sine over cosine (tan(a) = sin(a) / cos(a)), so tan(x) becomes sin(x) / cos(x).
6565
```
6666

67-
So the name has to be a clause that survives being read that way. **All 308 distinct rule names are,
67+
So the name has to be a clause that survives being read that way. **All 309 distinct rule names are,
6868
and `StepAsASentenceTest` holds them to it** — a name with a capital, a bracket or an underscore
6969
fails that test rather than degrading the prose quietly.
7070

@@ -91,7 +91,7 @@ debugged for an afternoon.
9191
| `Left.ToString()` | `Divf(var a, Divf(var b, var c))` — how the matcher spells it |
9292

9393
Write the identity with `=`, not `->`: it is an equality, and the arrow belongs to the direction the
94-
rule happens to be applied in. **334** rules carry one today; a new rule should.
94+
rule happens to be applied in. **335** rules carry one today; a new rule should.
9595

9696
## The pattern language
9797

@@ -123,13 +123,14 @@ matches and silently builds nothing. `BuildableNodeTypesTest` is the list.
123123

124124
## Replacement: a pattern, or code
125125

126-
**38** of the 337 rules have a pattern on both sides. The rest build their answer in code, and both
126+
**39** of the 338 rules have a pattern on both sides. The rest build their answer in code, and both
127127
are legitimate — but the choice costs two things, so make it deliberately.
128128

129129
A pattern replacement gets:
130130

131-
- **a direction.** `MatchedRule.Reversed` is the rule read the other way, and **34** of the 38
132-
two-sided rules have one. The other four forget something: `sin²+cos² = 1` forgets the angle,
131+
- **a direction.** `MatchedRule.Reversed` is the rule read the other way, and **34** of the 39
132+
two-sided rules have one. The other five forget something: `sin²+cos² = 1` and
133+
`k sin² + k cos² = k` forget the angle,
133134
`{ x : x in S } = S` forgets the name the set builder bound, and `c ^ log(c, a) = a` and
134135
`c ^ (k * log(c, a)) = a ^ k` for a constant `c` forget which constant, so none has anything to
135136
read back.
@@ -226,7 +227,7 @@ limit, against nothing for handing the node over.
226227
| `SoundUnderAssumptions` | holds given something the rule does not check |
227228
| `Heuristic` | usually right |
228229

229-
**188** of the 337 rules are `Sound` and **149** are conditional. Every one of the thirty registered
230+
**189** of the 338 rules are `Sound` and **149** are conditional. Every one of the thirty registered
230231
*sets* declares `SoundUnderAssumptions`, because a set's tier is the **minimum** over its rules — so
231232
the set grain says nothing and the rule grain says everything. A derivation reports the rule's tier
232233
(`RewriteStep.Soundness`), which is why getting it right matters beyond the label.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Number;
13+
14+
namespace AngouriMath.Tests.Common
15+
{
16+
/// <summary>
17+
/// <c>k sin(x)^2 + k cos(x)^2</c> was reduced only where the two terms were siblings, so the
18+
/// sort decided: <c>y + (-2) cos(x)^2 + (-2) sin(x)^2</c> came out <c>y - 2</c>, and
19+
/// <c>2 + (-2) cos(x)^2 + (-2) sin(x)^2</c>, where the constant sorts between the two, stayed.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2133">#2133</a>
21+
/// </summary>
22+
[Trait("Area", "Common")]
23+
public sealed class PythagoreanPairWithACoefficientTest
24+
{
25+
[Theory]
26+
[InlineData("2 + (-2)*cos(x)^2 + (-2)*sin(x)^2", "0")]
27+
[InlineData("2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2", "0")]
28+
[InlineData("a + b*sin(t)^2 + c + b*cos(t)^2", "a + b + c")]
29+
[InlineData("y + (-2)*cos(x)^2 + (-2)*sin(x)^2", "y - 2")]
30+
public void ThePairIsItsCoefficient(string input, string expected)
31+
=> Assert.Equal(Integer.Zero, (input.Simplify() - expected.ToEntity()).Simplify());
32+
33+
/// <summary>
34+
/// Two coefficients, or two arguments, that differ are not the identity.
35+
/// </summary>
36+
[Theory]
37+
[InlineData("2*sin(x)^2 + 3*cos(x)^2")]
38+
[InlineData("k*sin(x)^2 + k*cos(y)^2")]
39+
public void UnequalPartsAreLeft(string input)
40+
=> Assert.Contains("sin", input.Simplify().Stringize());
41+
42+
/// <summary>
43+
/// The first rows of #2133's table, which are rational identities, and are answered by the
44+
/// rational canonical form since
45+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2137">#2137</a>.
46+
/// </summary>
47+
[Theory]
48+
[InlineData("a*b*c*(1/a + 1/b + 1/c) - (a*b + b*c + c*a)")]
49+
[InlineData("(a*b + b*c + c*a)/3 - (a*b*c)*(1/a + 1/b + 1/c)/3")]
50+
public void TheRationalRowsAreZero(string input)
51+
{
52+
var simplified = input.Simplify();
53+
while (simplified is Providedf(var inner, _))
54+
simplified = inner;
55+
Assert.Equal(Integer.Zero, simplified);
56+
}
57+
}
58+
}

‎Sources/Tests/UnitTests/Core/Transformations/ReversibleRuleTest.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ public void EveryDataRuleIsClassifiedAsWritten()
370370
"one-less-a-squared-sine-is-a-squared-cosine: ReplacementIsCode",
371371
"sine-of-a-whole-multiple-of-an-angle: ReplacementIsCode",
372372
"squared-sine-and-cosine-of-one-argument-sum-to-one: ReplacementDropsHoles",
373+
"squared-sine-and-cosine-with-one-coefficient-sum-to-it: ReplacementDropsHoles",
373374
"the-arctangent-of-one-over-root-three: ReplacementIsCode",
374375
"the-arctangent-of-root-three: ReplacementIsCode",
375376
"the-negation-of-a-greater-turns-it-round: ReplacementIsCode",
@@ -436,9 +437,12 @@ public void AReversedRuleSaysWhatAnExpressionCameFrom()
436437
[Fact]
437438
public void ARuleThatForgetsAHoleHasNoBackwardsReading()
438439
{
439-
var pythagoras = MatchedRules.PythagoreanIdentity.Rules.Single();
440-
Assert.Equal(RuleReversal.ReplacementDropsHoles, pythagoras.Reversal);
441-
Assert.Null(pythagoras.Reversed);
440+
// Both rules of the set, with a coefficient and without, drop the argument.
441+
foreach (var pythagoras in MatchedRules.PythagoreanIdentity.Rules)
442+
{
443+
Assert.Equal(RuleReversal.ReplacementDropsHoles, pythagoras.Reversal);
444+
Assert.Null(pythagoras.Reversed);
445+
}
442446

443447
// And the reversed set is empty rather than absent, which is the difference being
444448
// reported rather than hidden.

‎Sources/Tests/UnitTests/Core/Transformations/RuleAuthoringGuideTest.cs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static void Stated(int stated, int measured, string claim)
4343
public void WhereARuleGoes()
4444
{
4545
Stated(33, MatchedRules.All.Count, "the number of rule sets written as data");
46-
Stated(337, MatchedRules.All.Sum(set => set.Rules.Count), "the number of rules written as data");
46+
Stated(338, MatchedRules.All.Sum(set => set.Rules.Count), "the number of rules written as data");
4747
Stated(30, RewriteRules.All.Count, "the number of registered sets");
4848
// Two families register under one name and run a data set under another --
4949
// CommonDenominator over MatchedRules.CommonDenominator(level), CanonicalOrder over
@@ -64,7 +64,7 @@ public void TheNameIsASentence()
6464
{
6565
var names = MatchedRules.All.SelectMany(set => set.Rules)
6666
.Select(rule => rule.Name).Distinct(StringComparer.Ordinal).ToList();
67-
Stated(308, names.Count, "the number of distinct rule names");
67+
Stated(309, names.Count, "the number of distinct rule names");
6868

6969
var words = names.Select(name => name.Split('-').Length).ToList();
7070
Stated(4, words.Min(), "the shortest rule name in words");
@@ -73,7 +73,7 @@ public void TheNameIsASentence()
7373

7474
[Fact]
7575
public void TheIdentityIsNotTheName()
76-
=> Stated(334,
76+
=> Stated(335,
7777
MatchedRules.All.SelectMany(set => set.Rules).Count(rule => rule.Description is not null),
7878
"how many rules carry an identity");
7979

@@ -85,7 +85,7 @@ public void ThePatternLanguage()
8585
public void ReplacementAPatternOrCode()
8686
{
8787
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
88-
Stated(38, rules.Count(rule => rule.Right is not null),
88+
Stated(39, rules.Count(rule => rule.Right is not null),
8989
"how many rules have a pattern on both sides");
9090
Stated(34, rules.Count(rule => rule.Reversed is not null),
9191
"how many two-sided rules have a direction");
@@ -97,17 +97,17 @@ public void ReplacementAPatternOrCode()
9797
// rearrange, 9 expand and 270 unjudged", measured before the growth declarations
9898
// went in. The argument survived; the arithmetic did not. A number a remark reasons
9999
// from belongs somewhere that fails when it moves.
100-
Stated(126, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Collects),
100+
Stated(127, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Collects),
101101
"how many rules are declared Collects");
102102
Stated(49, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Rearranges),
103103
"how many rules are declared Rearranges");
104104
Stated(36, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Expands),
105105
"how many rules are declared Expands");
106106

107-
// And the three the document names. By their own names rather than by what the prose
107+
// And the ones the document names. By their own names rather than by what the prose
108108
// calls them: the first is `squared-sine-and-cosine-of-one-argument-sum-to-one` and
109109
// the prose calls it the Pythagorean identity, which is right and is not what to
110-
// match on. All three forget something the backwards direction would have to invent
110+
// match on. All of them forget something the backwards direction would have to invent
111111
// -- the angle, the name the set builder bound, and which constant was raised.
112112
var oneWay = rules
113113
.Where(rule => rule.Right is not null && rule.Reversed is null)
@@ -121,6 +121,7 @@ public void ReplacementAPatternOrCode()
121121
"a-constant-raised-to-a-logarithm-of-itself-is-the-antilogarithm",
122122
"a-constant-raised-to-a-multiple-of-a-logarithm-of-itself-is-a-power-of-the-antilogarithm",
123123
"squared-sine-and-cosine-of-one-argument-sum-to-one",
124+
"squared-sine-and-cosine-with-one-coefficient-sum-to-it",
124125
},
125126
oneWay);
126127
Assert.All(
@@ -132,7 +133,7 @@ public void ReplacementAPatternOrCode()
132133
public void SoundnessIsPerRule()
133134
{
134135
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
135-
Stated(188, rules.Count(rule => rule.Soundness is Soundness.Sound),
136+
Stated(189, rules.Count(rule => rule.Soundness is Soundness.Sound),
136137
"how many rules are Sound");
137138
Stated(149, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions),
138139
"how many rules are SoundUnderAssumptions");

‎Sources/Tests/UnitTests/Core/Transformations/RuleMetadataTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public void AddressableGrowthIsTheRulesOwnAnswer()
8888
public void SoundnessIsFinerPerRuleThanPerSet()
8989
{
9090
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
91-
Assert.Equal(337, rules.Count);
92-
Assert.Equal(188,rules.Count(rule => rule.Soundness is Soundness.Sound));
91+
Assert.Equal(338, rules.Count);
92+
Assert.Equal(189,rules.Count(rule => rule.Soundness is Soundness.Sound));
9393
Assert.Equal(149, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions));
9494

9595
// And every set still reports the weakest of them, which is what makes the set grain

‎Sources/Tests/UnitTests/Core/Transformations/StepAsASentenceTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void EveryRuleWrittenAsDataIsNamedInProse()
5959
{
6060
var names = MatchedRules.All.SelectMany(set => set.Rules).Select(rule => rule.Name)
6161
.Distinct(StringComparer.Ordinal).ToList();
62-
Assert.Equal(308, names.Count);
62+
Assert.Equal(309, names.Count);
6363
Assert.All(names, name => Assert.True(Explanation.IsProse(name),
6464
$"'{name}' is a rule name that does not read as a phrase in English"));
6565

0 commit comments

Comments
 (0)