Skip to content

Commit 856b3cb

Browse files
One and a squared tangent are found anywhere in a sum (#2176)
1 + tan(x)^2 = sec(x)^2 and 1 + cot(x)^2 = csc(x)^2 are rules in TrigonometricRules for two siblings, and the sort decides whether the 1 and the square are siblings. Secant sorts before tangent, so 1 + tan(x)^2 - sec(x)^2 became 1 - sec(x)^2 + tan(x)^2 and stayed, while 1 + cot(x)^2 - cosec(x)^2, where cotangent sorts before cosecant, was 0 provided not sin(x) = 0. #2142 has the pair. Both identities are added to PythagoreanIdentity as gathered rules, as that set already finds sin^2 + cos^2 wherever the two terms sit: SoundUnderAssumptions as the sibling rules are, since each side is defined only where cos (sin) is not 0, two-sided, reversible, Collects, and each with its identity. The census moves by two in WritingARule.md, Saturation's remark and the tests that pin them; ReversibleRuleTest's hole-dropping check names the two Pythagorean rules rather than every rule of the set, since these two keep their argument. On 2.6.0 and on this branch, Simplify: 1 + tan(x)^2 - sec(x)^2 unchanged -> 0 provided not cos(x) = 0 a + 1 + tan(y)^2 - sec(y)^2 unchanged -> a provided not cos(y) = 0 Chain against master c90db9f: verdicts identical, 0 moves; performance gate passed on all 19; unit suite 17,164 passed, 0 failed. RuleCheck passes, Confluence and CanonCheck match their baselines, SimpSweep 10,463/10,463. Part of #2142 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c90db9f commit 856b3cb

9 files changed

Lines changed: 131 additions & 26 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ for `u` made a matrix of vectors. A lambda written in place is now applied befor
200200
| `apply(lambda(u, [[0, -1], [1, 0]] * u), [p, q])` | `[[0, [-p, -q]], [[p, q], 0]]` | `[-q, p]` |
201201
| `apply(lambda(u, [1, 1]T * u), [p, q])` | `[[[p, q], [p, q]]]` | `p + q` |
202202

203+
### One and a squared tangent are found anywhere in a sum
204+
205+
**Silent: an identity reaches its value whichever way the sum is sorted.** `1 + tan(x)^2 = sec(x)^2` and
206+
`1 + cot(x)^2 = csc(x)^2` were rules for two siblings, and secant sorts before tangent, so `1 + tan(x)^2 - sec(x)^2`
207+
became `1 - sec(x)^2 + tan(x)^2` and stayed, while its cotangent twin, sorted the other way, was `0`. Both identities
208+
are now also found wherever the `1` and the square sit in a sum, under the condition the tangent's domain carries
209+
([#2142](https://github.com/asc-community/AngouriMath/issues/2142)).
210+
211+
| Input | Was (2.6.0) | Now |
212+
|---|---|---|
213+
| `"1 + tan(x)^2 - sec(x)^2".Simplify()` | `1 + tan(x) ^ 2 - sec(x) ^ 2` | `0 provided not cos(x) = 0` |
214+
| `"a + 1 + tan(y)^2 - sec(y)^2".Simplify()` | `a + 1 + tan(y) ^ 2 - sec(y) ^ 2` | `a provided not cos(y) = 0` |
215+
203216
### A square system is not answered with rows that are no solution
204217

205218
**Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.**

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,46 @@ internal static class MatchedRules
370370
MatchPattern.Exact(Integer.Create(2))))),
371371
MatchPattern.Node<Sumf>(MatchPattern.Any("k"), MatchPattern.Any("rest")),
372372
Soundness.Sound,
373-
description: "k sin(x)^2 + k cos(x)^2 = k"));
373+
description: "k sin(x)^2 + k cos(x)^2 = k"),
374+
375+
// ... + 1 + ... + tan(x)^2 + ... -> sec(x)^2 + (everything else), and the same with
376+
// the cotangent. The identity divided through by cos^2 and by sin^2 is also in
377+
// TrigonometricRules, but for two siblings only, so the sort decided again: secant
378+
// sorts before tangent, and 1 + tan(x)^2 - sec(x)^2 became 1 - sec(x)^2 + tan(x)^2
379+
// and stayed, while cotangent sorts before cosecant and its twin was 0.
380+
// SoundUnderAssumptions as there: each side is defined only where cos (sin) is not 0.
381+
// https://github.com/asc-community/AngouriMath/issues/2142
382+
new MatchedRule(
383+
"one-and-a-squared-tangent-anywhere-in-a-sum-make-a-squared-secant",
384+
MatchPattern.Gathered<Sumf>(
385+
"rest",
386+
MatchPattern.Exact(Integer.Create(1)),
387+
MatchPattern.Node<Powf>(
388+
MatchPattern.Node<Tanf>(MatchPattern.Any("x")),
389+
MatchPattern.Exact(Integer.Create(2)))),
390+
MatchPattern.Node<Sumf>(
391+
MatchPattern.Node<Powf>(
392+
MatchPattern.Node<Secantf>(MatchPattern.Any("x")),
393+
MatchPattern.Exact(Integer.Create(2))),
394+
MatchPattern.Any("rest")),
395+
Soundness.SoundUnderAssumptions,
396+
description: "1 + tan(x)^2 = sec(x)^2"),
397+
398+
new MatchedRule(
399+
"one-and-a-squared-cotangent-anywhere-in-a-sum-make-a-squared-cosecant",
400+
MatchPattern.Gathered<Sumf>(
401+
"rest",
402+
MatchPattern.Exact(Integer.Create(1)),
403+
MatchPattern.Node<Powf>(
404+
MatchPattern.Node<Cotanf>(MatchPattern.Any("x")),
405+
MatchPattern.Exact(Integer.Create(2)))),
406+
MatchPattern.Node<Sumf>(
407+
MatchPattern.Node<Powf>(
408+
MatchPattern.Node<Cosecantf>(MatchPattern.Any("x")),
409+
MatchPattern.Exact(Integer.Create(2))),
410+
MatchPattern.Any("rest")),
411+
Soundness.SoundUnderAssumptions,
412+
description: "1 + cotan(x)^2 = cosec(x)^2"));
374413

375414
/// <summary>
376415
/// <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 338 rules in
47-
/// <see cref="MatchedRules"/>, <b>127 collect, 49 rearrange, 36 expand and 126 are
46+
/// nobody measured. But it is where the rules are: of the 340 rules in
47+
/// <see cref="MatchedRules"/>, <b>129 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: 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-
**338** rules live there today.
21+
**340** 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 309 distinct rule names are,
67+
So the name has to be a clause that survives being read that way. **All 311 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. **335** rules carry one today; a new rule should.
94+
rule happens to be applied in. **337** 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-
**39** of the 338 rules have a pattern on both sides. The rest build their answer in code, and both
126+
**41** of the 340 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 39
131+
- **a direction.** `MatchedRule.Reversed` is the rule read the other way, and **36** of the 41
132132
two-sided rules have one. The other five forget something: `sin²+cos² = 1` and
133133
`k sin² + k cos² = k` forget the angle,
134134
`{ x : x in S } = S` forgets the name the set builder bound, and `c ^ log(c, a) = a` and
@@ -227,7 +227,7 @@ limit, against nothing for handing the node over.
227227
| `SoundUnderAssumptions` | holds given something the rule does not check |
228228
| `Heuristic` | usually right |
229229

230-
**189** of the 338 rules are `Sound` and **149** are conditional. Every one of the thirty registered
230+
**189** of the 340 rules are `Sound` and **151** are conditional. Every one of the thirty registered
231231
*sets* declares `SoundUnderAssumptions`, because a set's tier is the **minimum** over its rules — so
232232
the set grain says nothing and the rule grain says everything. A derivation reports the rule's tier
233233
(`RewriteStep.Soundness`), which is why getting it right matters beyond the label.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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>1 + tan(x)^2 = sec(x)^2</c> was found for two siblings only, and secant sorts before
18+
/// tangent, so <c>1 + tan(x)^2 - sec(x)^2</c> became <c>1 - sec(x)^2 + tan(x)^2</c> and stayed,
19+
/// while its cotangent twin, where cotangent sorts first, was <c>0</c>.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2142">#2142</a>
21+
/// </summary>
22+
[Trait("Area", "Common")]
23+
public sealed class TangentSecantIdentityTest
24+
{
25+
[Theory]
26+
[InlineData("1 + tan(x)^2 - sec(x)^2", "0")]
27+
[InlineData("1 + cot(x)^2 - cosec(x)^2", "0")]
28+
[InlineData("a + 1 + tan(y)^2 - sec(y)^2", "a")]
29+
[InlineData("tan(x)^2 - sec(x)^2 + 1", "0")]
30+
public void TheIdentityHolds(string input, string expected)
31+
{
32+
var difference = (input.Simplify() - expected.ToEntity()).Simplify();
33+
while (difference is Providedf(var inner, _))
34+
difference = inner;
35+
Assert.Equal(Integer.Zero, difference);
36+
}
37+
38+
/// <summary>
39+
/// Where the tangent is undefined the answer is too, as the input is.
40+
/// </summary>
41+
[Fact]
42+
public void TheAnswerKeepsTheTangentsDomain()
43+
=> Assert.Contains("cos(x) = 0", "1 + tan(x)^2 - sec(x)^2".Simplify().Stringize());
44+
}
45+
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,24 @@ public void AReversedRuleSaysWhatAnExpressionCameFrom()
437437
[Fact]
438438
public void ARuleThatForgetsAHoleHasNoBackwardsReading()
439439
{
440-
// Both rules of the set, with a coefficient and without, drop the argument.
441-
foreach (var pythagoras in MatchedRules.PythagoreanIdentity.Rules)
440+
// Both Pythagorean rules of the set, with a coefficient and without, drop the
441+
// argument. The set's other two, 1 + tan^2 = sec^2 and its cotangent twin, keep it,
442+
// and they are the only ones its reversal holds.
443+
var pythagoras = MatchedRules.PythagoreanIdentity.Rules
444+
.Where(rule => rule.Name.StartsWith("squared-sine-and-cosine", StringComparison.Ordinal))
445+
.ToList();
446+
Assert.Equal(2, pythagoras.Count);
447+
foreach (var rule in pythagoras)
442448
{
443-
Assert.Equal(RuleReversal.ReplacementDropsHoles, pythagoras.Reversal);
444-
Assert.Null(pythagoras.Reversed);
449+
Assert.Equal(RuleReversal.ReplacementDropsHoles, rule.Reversal);
450+
Assert.Null(rule.Reversed);
445451
}
446452

447-
// And the reversed set is empty rather than absent, which is the difference being
448-
// reported rather than hidden.
449-
Assert.Empty(MatchedRules.PythagoreanIdentity.Reversed.Rules);
453+
// And what the reversed set holds is the reversals of the others only, rather than
454+
// anything standing for the two that have none.
455+
Assert.Equal(
456+
MatchedRules.PythagoreanIdentity.Rules.Count(rule => rule.Reversed is not null),
457+
MatchedRules.PythagoreanIdentity.Reversed.Rules.Count);
450458
Assert.Equal(
451459
"1 + a".ToEntity(),
452460
MatchedRules.PythagoreanIdentity.Reversed.ApplyHere("1 + a".ToEntity()));

‎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(338, MatchedRules.All.Sum(set => set.Rules.Count), "the number of rules written as data");
46+
Stated(340, 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(309, names.Count, "the number of distinct rule names");
67+
Stated(311, 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(335,
76+
=> Stated(337,
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(39, rules.Count(rule => rule.Right is not null),
88+
Stated(41, rules.Count(rule => rule.Right is not null),
8989
"how many rules have a pattern on both sides");
90-
Stated(34, rules.Count(rule => rule.Reversed is not null),
90+
Stated(36, 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");
@@ -97,7 +97,7 @@ 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(127, rules.Count(rule => rule.Growth is RewriteRuleGrowth.Collects),
100+
Stated(129, 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");
@@ -135,7 +135,7 @@ public void SoundnessIsPerRule()
135135
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
136136
Stated(189, rules.Count(rule => rule.Soundness is Soundness.Sound),
137137
"how many rules are Sound");
138-
Stated(149, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions),
138+
Stated(151, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions),
139139
"how many rules are SoundUnderAssumptions");
140140
Assert.All(RewriteRules.All,
141141
set => Assert.Equal(Soundness.SoundUnderAssumptions, set.Soundness));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public void AddressableGrowthIsTheRulesOwnAnswer()
8888
public void SoundnessIsFinerPerRuleThanPerSet()
8989
{
9090
var rules = MatchedRules.All.SelectMany(set => set.Rules).ToList();
91-
Assert.Equal(338, rules.Count);
91+
Assert.Equal(340, rules.Count);
9292
Assert.Equal(189,rules.Count(rule => rule.Soundness is Soundness.Sound));
93-
Assert.Equal(149, rules.Count(rule => rule.Soundness is Soundness.SoundUnderAssumptions));
93+
Assert.Equal(151, 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
9696
// uninformative rather than wrong.

‎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(309, names.Count);
62+
Assert.Equal(311, 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)