Skip to content

Commit e2a77f2

Browse files
Rafael-SOWNetclaude
andcommitted
A cosine of a sum is expanded, as a sine of one is
The expansion rules had the sine's addition theorem and not the cosine's, so no shift of a cosine was reduced: sin(pi - x) - sin(x) was 0 and cos(pi - x) + cos(x) stayed as written, and so did cos(s + t) - cos(s) cos(t) + sin(s) sin(t). cos(a + b) and cos(a - b) are added beside the sine's, in the switch and in the matcher's data, as the same addition theorem: Sound for every complex a and b. A shift whose reduced form is shorter comes back reduced: cos(pi/2 - x) is sin(x), cos(2pi - x) is cos(x). cos(pi - x) on its own stays, as sin(pi + x) always has, because -cos(x) is rated no shorter than it. The rule census moves by two in WritingARule.md and the tests that pin it, and Saturation's prose had gone stale since 324 rules and is brought to the measured 337. Measured: CanonCheck, Confluence and RuleCheck unchanged against their baselines. Rubi's trigonometric family (1339 sampled) and its textbook family (338): every verdict the same on this branch and its base, time +0.9% and +0.5%. One integrand is slower alone, cos(e+f*x)^2/((a+a*sin(e+f*x))^(5/2)*sqrt(c-c*sin(e+f*x))) from 163 ms to 636 ms, still solved. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent bc65529 commit e2a77f2

11 files changed

Lines changed: 129 additions & 22 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ its arguments and answers may change in a minor release. `amcli mcp` no longer l
203203
| `am_classify` on `G*M*m/r^2`, to `amcli mcp` | `solved`, `Newtonian gravitation` first | `failed`: `'am_classify' is preliminary, and is served only when the server is started as 'amcli mcp --preliminary'` |
204204
| the same, to `amcli mcp --preliminary` | `solved` (the flag is ignored) | `solved`, and `"preliminary": true` |
205205

206+
### A cosine of a sum is expanded, as a sine of one is
207+
208+
**Silent: identities in a shifted cosine reduce, and `Expand` opens a cosine of a sum.** The expansion rules had the
209+
sine's addition theorem and not the cosine's, so no shift of a cosine was reduced: `sin(pi - x) - sin(x)` was `0` and
210+
`cos(pi - x) + cos(x)` stayed as written. `cos(a + b)` and `cos(a - b)` are now expanded too. A shift whose reduced
211+
form is shorter comes back reduced. `cos(pi - x)` on its own stays, as `sin(pi + x)` always has, because `-cos(x)`
212+
is rated no shorter ([#2135](https://github.com/asc-community/AngouriMath/issues/2135)).
213+
214+
| Input | Was (2.6.0) | Now |
215+
|---|---|---|
216+
| `"cos(pi/2 - x)".Simplify()` | `cos(pi / 2 - x)` | `sin(x)` |
217+
| `"cos(2*pi - x)".Simplify()` | `cos(2 * pi - x)` | `cos(x)` |
218+
| `"cos(pi - x) + cos(x)".Simplify()` | `cos(pi - x) + cos(x)` | `0` |
219+
| `"cos(s + t) - cos(s)*cos(t) + sin(s)*sin(t)".Simplify()` | unchanged | `0` |
220+
| `"cos(a + b)".Expand()` | `cos(a + b)` | `cos(a) * cos(b) + -sin(a) * sin(b)` |
221+
| `"cos(a - b)".Expand()` | `cos(a - b)` | `cos(a) * cos(b) + sin(a) * sin(b)` |
222+
206223
### A subscript may have a subscript
207224

208225
**Not silent: what was a parse error is a name.** A name took at most one `_`, so `x_1_2` and `X_a_beta` were lexer

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
276276
[Tests/UnitTests/Algebra/SolveTest/NegativePowerInversionTest.cs]
277277
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
278278

279+
[Tests/UnitTests/Common/CosineOfASumTest.cs]
280+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
281+
279282
[Tests/UnitTests/Common/LambdaArrowSyntaxTest.cs]
280283
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
281284

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,39 @@ internal static class MatchedRules
612612
MatchPattern.Node<Sinf>(MatchPattern.Any("b")),
613613
MatchPattern.Node<Cosf>(MatchPattern.Any("a")))),
614614
Soundness.Sound,
615-
description: "sin(a - b) = sin(a)cos(b) - sin(b)cos(a)"));
615+
description: "sin(a - b) = sin(a)cos(b) - sin(b)cos(a)"),
616+
617+
// cos(a + b) -> cos(a)cos(b) - sin(a)sin(b)
618+
// https://github.com/asc-community/AngouriMath/issues/2135
619+
new MatchedRule(
620+
"cosine-of-a-sum",
621+
MatchPattern.Node<Cosf>(
622+
MatchPattern.Node<Sumf>(MatchPattern.Any("a"), MatchPattern.Any("b"))),
623+
MatchPattern.Node<Minusf>(
624+
MatchPattern.Node<Mulf>(
625+
MatchPattern.Node<Cosf>(MatchPattern.Any("a")),
626+
MatchPattern.Node<Cosf>(MatchPattern.Any("b"))),
627+
MatchPattern.Node<Mulf>(
628+
MatchPattern.Node<Sinf>(MatchPattern.Any("a")),
629+
MatchPattern.Node<Sinf>(MatchPattern.Any("b")))),
630+
// The addition theorem again, for every complex a and b.
631+
Soundness.Sound,
632+
description: "cos(a + b) = cos(a)cos(b) - sin(a)sin(b)"),
633+
634+
// cos(a - b) -> cos(a)cos(b) + sin(a)sin(b)
635+
new MatchedRule(
636+
"cosine-of-a-difference",
637+
MatchPattern.Node<Cosf>(
638+
MatchPattern.Node<Minusf>(MatchPattern.Any("a"), MatchPattern.Any("b"))),
639+
MatchPattern.Node<Sumf>(
640+
MatchPattern.Node<Mulf>(
641+
MatchPattern.Node<Cosf>(MatchPattern.Any("a")),
642+
MatchPattern.Node<Cosf>(MatchPattern.Any("b"))),
643+
MatchPattern.Node<Mulf>(
644+
MatchPattern.Node<Sinf>(MatchPattern.Any("a")),
645+
MatchPattern.Node<Sinf>(MatchPattern.Any("b")))),
646+
Soundness.Sound,
647+
description: "cos(a - b) = cos(a)cos(b) + sin(a)sin(b)"));
616648

617649
/// <summary>
618650
/// <see cref="Functions.Patterns.ExpandTrigonometricRules"/>, as data.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ 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 324 rules in
47-
/// <see cref="MatchedRules"/>, <b>124 collect, 49 rearrange, 32 expand and 123 are
48-
/// unjudged</b>. A ceiling that refuses the fourth value still refuses 38% of the library,
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
48+
/// 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
5151
/// <see cref="RewriteRuleGrowth.Rearranges"/> ceiling proved two, and

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

Lines changed: 6 additions & 6 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-
**335** rules live there today.
21+
**337** 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 306 distinct rule names are,
67+
So the name has to be a clause that survives being read that way. **All 308 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. **332** rules carry one today; a new rule should.
94+
rule happens to be applied in. **334** rules carry one today; a new rule should.
9595

9696
## The pattern language
9797

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

124124
## Replacement: a pattern, or code
125125

126-
**35** of the 328 rules have a pattern on both sides. The rest build their answer in code, and both
126+
**38** of the 337 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 **32** of the 36
131+
- **a direction.** `MatchedRule.Reversed` is the rule read the other way, and **34** of the 38
132132
two-sided rules have one. The other four forget something: `sin²+cos² = 1` forgets the angle,
133133
`{ x : x in S } = S` forgets the name the set builder bound, and `c ^ log(c, a) = a` and
134134
`c ^ (k * log(c, a)) = a ^ k` for a constant `c` forget which constant, so none has anything to
@@ -226,7 +226,7 @@ limit, against nothing for handing the node over.
226226
| `SoundUnderAssumptions` | holds given something the rule does not check |
227227
| `Heuristic` | usually right |
228228

229-
**186** of the 335 rules are `Sound` and **149** are conditional. Every one of the thirty registered
229+
**188** of the 337 rules are `Sound` and **149** are conditional. Every one of the thirty registered
230230
*sets* declares `SoundUnderAssumptions`, because a set's tier is the **minimum** over its rules — so
231231
the set grain says nothing and the rule grain says everything. A derivation reports the rule's tier
232232
(`RewriteStep.Soundness`), which is why getting it right matters beyond the label.

‎Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ internal static partial class Patterns
2121
{
2222
Sinf(Sumf(var any1, var any2)) => new Sinf(any1) * new Cosf(any2) + new Sinf(any2) * new Cosf(any1),
2323
Sinf(Minusf(var any1, var any2)) => new Sinf(any1) * new Cosf(any2) - new Sinf(any2) * new Cosf(any1),
24+
// The cosine's, which were missing: cos(pi - x) + cos(x) did not reduce to 0 where
25+
// sin(pi - x) - sin(x) did. https://github.com/asc-community/AngouriMath/issues/2135
26+
Cosf(Sumf(var any1, var any2)) => new Cosf(any1) * new Cosf(any2) - new Sinf(any1) * new Sinf(any2),
27+
Cosf(Minusf(var any1, var any2)) => new Cosf(any1) * new Cosf(any2) + new Sinf(any1) * new Sinf(any2),
2428

2529
_ => x
2630
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.Number;
12+
13+
namespace AngouriMath.Tests.Common
14+
{
15+
/// <summary>
16+
/// The expansion rules opened a sine of a sum and not a cosine, so no shift of a cosine was
17+
/// reduced: <c>sin(pi - x) - sin(x)</c> was 0 and <c>cos(pi - x) + cos(x)</c> stayed as it
18+
/// was, and so did <c>cos(s + t) - cos(s) cos(t) + sin(s) sin(t)</c>.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2135">#2135</a>
20+
/// </summary>
21+
[Trait("Area", "Common")]
22+
public sealed class CosineOfASumTest
23+
{
24+
[Theory]
25+
[InlineData("cos(pi - x) + cos(x)")]
26+
[InlineData("cos(x - pi) + cos(x)")]
27+
[InlineData("cos(pi/2 + x) + sin(x)")]
28+
[InlineData("cos(s + t) - cos(s) * cos(t) + sin(s) * sin(t)")]
29+
[InlineData("cos(s - t) - cos(s) * cos(t) - sin(s) * sin(t)")]
30+
[InlineData("cos(pi/3 - x) - cos(x) / 2 - sqrt(3) * sin(x) / 2")]
31+
public void TheIdentityIsZero(string identity)
32+
=> Assert.Equal(Integer.Zero, identity.Simplify());
33+
34+
/// <summary>
35+
/// Where the shifted form is longer than what it reduces to, the reduced one is the
36+
/// answer. <c>cos(pi - x)</c> on its own stays, as <c>sin(pi + x)</c> always has:
37+
/// <c>-cos(x)</c> is rated no shorter.
38+
/// </summary>
39+
[Theory]
40+
[InlineData("cos(pi/2 - x)", "sin(x)")]
41+
[InlineData("cos(2 * pi - x)", "cos(x)")]
42+
public void AShiftIsReducedWhereThatIsShorter(string shifted, string reduced)
43+
=> Assert.Equal(reduced.ToEntity(), shifted.Simplify());
44+
45+
[Theory]
46+
[InlineData("cos(a + b)", "cos(a) * cos(b) - sin(a) * sin(b)")]
47+
[InlineData("cos(a - b)", "cos(a) * cos(b) + sin(a) * sin(b)")]
48+
public void ExpandOpensACosineOfASum(string cosine, string expanded)
49+
=> Assert.Equal(Integer.Zero, (cosine.ToEntity().Expand() - expanded).Simplify());
50+
}
51+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void TheRegistryIsAddressableAsFarAsItSaysItIs()
299299
// are thirty-three; NumericNeat's sixteen are eleven, six of them being three rules
300300
// written once per side a negative factor can sit on; and the two factorial sets are
301301
// eight arms each written as three. Every other repointed set is one arm for one rule.
302-
Assert.Equal(332, withRules.Sum(set => set.Rules.Count));
302+
Assert.Equal(334, withRules.Sum(set => set.Rules.Count));
303303
}
304304

305305
/// <summary>

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

Lines changed: 7 additions & 7 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(335, MatchedRules.All.Sum(set => set.Rules.Count), "the number of rules written as data");
46+
Stated(337, 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(306, names.Count, "the number of distinct rule names");
67+
Stated(308, 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(332,
76+
=> Stated(334,
7777
MatchedRules.All.SelectMany(set => set.Rules).Count(rule => rule.Description is not null),
7878
"how many rules carry an identity");
7979

@@ -85,9 +85,9 @@ public void ThePatternLanguage()
8585
public void ReplacementAPatternOrCode()
8686
{
8787
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
88-
Stated(36, rules.Count(rule => rule.Right is not null),
88+
Stated(38, rules.Count(rule => rule.Right is not null),
8989
"how many rules have a pattern on both sides");
90-
Stated(32, rules.Count(rule => rule.Reversed is not null),
90+
Stated(34, rules.Count(rule => rule.Reversed is not null),
9191
"how many two-sided rules have a direction");
9292
Stated(126, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Unknown),
9393
"how many rules sit at Unknown growth");
@@ -101,7 +101,7 @@ public void ReplacementAPatternOrCode()
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");
104-
Stated(34, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Expands),
104+
Stated(36, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Expands),
105105
"how many rules are declared Expands");
106106

107107
// And the three the document names. By their own names rather than by what the prose
@@ -132,7 +132,7 @@ public void ReplacementAPatternOrCode()
132132
public void SoundnessIsPerRule()
133133
{
134134
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
135-
Stated(186, rules.Count(rule => rule.Soundness is Soundness.Sound),
135+
Stated(188, rules.Count(rule => rule.Soundness is Soundness.Sound),
136136
"how many rules are Sound");
137137
Stated(149, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions),
138138
"how many rules are SoundUnderAssumptions");

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

Lines changed: 3 additions & 3 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(335, rules.Count);
92-
Assert.Equal(186,rules.Count(rule => rule.Soundness is Soundness.Sound));
91+
Assert.Equal(337, rules.Count);
92+
Assert.Equal(188,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
@@ -174,7 +174,7 @@ public void EveryRuleOfARepointedSetCarriesItsIdentity()
174174
var repointed = RewriteRules.All
175175
.Where(set => set.Rules.Count > 0 && set.Rules.All(rule => rule.Soundness is not null))
176176
.ToList();
177-
Assert.Equal(332, repointed.Sum(set => set.Rules.Count));
177+
Assert.Equal(334, repointed.Sum(set => set.Rules.Count));
178178
Assert.All(repointed, set => Assert.All(set.Rules, rule => Assert.NotNull(rule.Description)));
179179
}
180180

0 commit comments

Comments
 (0)