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
107 changes: 86 additions & 21 deletions Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,21 +1587,31 @@ is var (divided, remainder)
/ (1 - (Real)bound["a"] * (Real)bound["b"])).InnerSimplified),
Soundness.SoundUnderAssumptions,
when: bound => ((Real)bound["a"] * (Real)bound["b"]).Evaled is Real product && product < 1,
description: "arctan(a) + arctan(b) = arctan((a + b) / (1 - a*b)), while a*b < 1"),
description: "arctan(a) + arctan(b) = arctan((a + b) / (1 - a*b)), while a*b < 1",
// Both operands are matched as Real and so are leaves, and the tangent formula is
// folded to a single one by the InnerSimplified on it, so five nodes become two:
// exactly -3, for every pair the `when` admits.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"the-arctangent-of-root-three",
MatchPattern.Node<Arctanf>(MatchPattern.Node<Powf>(MatchPattern.Exact(Integer.Create(3)), MatchPattern.Exact(Rational.Create(1, 2)))),
bound => MathS.pi / 3,
Soundness.Sound,
description: "arctan(sqrt(3)) = pi/3"),
description: "arctan(sqrt(3)) = pi/3",
// The pattern binds nothing, so both sides are a fixed size and the delta is a
// number rather than an expression in the operands.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"the-arctangent-of-one-over-root-three",
MatchPattern.Node<Arctanf>(MatchPattern.Node<Divf>(MatchPattern.Exact(Integer.Create(1)), MatchPattern.Node<Powf>(MatchPattern.Exact(Integer.Create(3)), MatchPattern.Exact(Rational.Create(1, 2))))),
bound => MathS.pi / 6,
Soundness.Sound,
description: "arctan(1 / sqrt(3)) = pi/6"),
description: "arctan(1 / sqrt(3)) = pi/6",
// The pattern binds nothing, so both sides are a fixed size and the delta is a
// number rather than an expression in the operands.
growth: RewriteRuleGrowth.Collects),

// The cosecant's own condition has to be carried: 2cos(u) is a number where
// sin(u) is zero and sin(2u) csc(u) is not.
Expand Down Expand Up @@ -1629,31 +1639,43 @@ is var (divided, remainder)
bound => bound["a"],
Soundness.SoundUnderAssumptions,
when: bound => Functions.Patterns.WithinHalfPi(bound["a"], closed: true),
description: "arcsin(sin(a)) = a, for a in [-pi/2; pi/2]"),
description: "arcsin(sin(a)) = a, for a in [-pi/2; pi/2]",
// The function and its inverse both go and the angle is handed back untouched, so
// two nodes disappear whatever the angle is: exactly -2.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"arccosine-of-a-cosine-inside-its-own-interval",
MatchPattern.Node<Arccosf>(MatchPattern.Node<Cosf>(MatchPattern.Any("a"))),
bound => bound["a"],
Soundness.SoundUnderAssumptions,
when: bound => Functions.Patterns.WithinZeroAndPi(bound["a"], closed: true),
description: "arccos(cos(a)) = a, for a in [0; pi]"),
description: "arccos(cos(a)) = a, for a in [0; pi]",
// The function and its inverse both go and the angle is handed back untouched, so
// two nodes disappear whatever the angle is: exactly -2.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"arctangent-of-a-tangent-inside-its-own-interval",
MatchPattern.Node<Arctanf>(MatchPattern.Node<Tanf>(MatchPattern.Any("a"))),
bound => bound["a"],
Soundness.SoundUnderAssumptions,
when: bound => Functions.Patterns.WithinHalfPi(bound["a"], closed: false),
description: "arctan(tan(a)) = a, for a in (-pi/2; pi/2)"),
description: "arctan(tan(a)) = a, for a in (-pi/2; pi/2)",
// The function and its inverse both go and the angle is handed back untouched, so
// two nodes disappear whatever the angle is: exactly -2.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"arccotangent-of-a-cotangent-inside-its-own-range",
MatchPattern.Node<Arccotanf>(MatchPattern.Node<Cotanf>(MatchPattern.Any("a"))),
bound => bound["a"],
Soundness.SoundUnderAssumptions,
when: bound => Functions.Patterns.WithinArccotanRange(bound["a"]),
description: "arccotan(cotan(a)) = a, for a in arccotan's own range"),
description: "arccotan(cotan(a)) = a, for a in arccotan's own range",
// The function and its inverse both go and the angle is handed back untouched, so
// two nodes disappear whatever the angle is: exactly -2.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"a-sine-of-an-arcsine",
Expand Down Expand Up @@ -1967,7 +1989,10 @@ is var (divided, remainder)
MatchPattern.Any<Set.FiniteSet>("s", finite => finite.Count == 1)),
bound => bound["x"].EqualTo(((Set.FiniteSet)bound["s"]).First()),
Soundness.Sound,
description: "x in {a} = (x = a)"),
description: "x in {a} = (x = a)",
// The singleton set goes and the membership becomes an equality, one node for one,
// so exactly -1. `EqualTo` is a bare `Equalsf` and does not chain.
growth: RewriteRuleGrowth.Collects),

// x in (a; b) is written out as the inequalities it stands for
new MatchedRule(
Expand All @@ -1992,7 +2017,10 @@ is var (divided, remainder)
"s", finite => finite == Functions.Patterns.FullBooleanSet),
bound => Set.SpecialSet.Create(Domain.Boolean),
Soundness.Sound,
description: "{ True, False } = the boolean domain"),
description: "{ True, False } = the boolean domain",
// The pattern binds nothing, so both sides are a fixed size and the delta is a
// number rather than an expression in the operands.
growth: RewriteRuleGrowth.Collects),

// (-oo; +oo) is the domain it is an interval of, where that domain names a set.
// The Any case is refused in the condition rather than left to the replacement:
Expand All @@ -2008,7 +2036,10 @@ is var (divided, remainder)
&& interval.Codomain is not Domain.Any),
bound => Set.SpecialSet.Create(((Set.Interval)bound["i"]).Codomain),
Soundness.Sound,
description: "(-oo; +oo) = the domain it is an interval of"));
description: "(-oo; +oo) = the domain it is an interval of",
// The interval and its two bounds become the one node of a special set, and the
// bounds are matched exactly rather than bound, so the delta is a number.
growth: RewriteRuleGrowth.Collects));

/// <summary>
/// <see cref="Functions.Patterns.SortRules"/>, as data — one set per sort level.
Expand Down Expand Up @@ -2697,7 +2728,11 @@ is var (divided, remainder)
MatchPattern.Node<Logf>(MatchPattern.Any("c"), MatchPattern.Any("a")))),
bound => new Powf(bound["c"], bound["n"]),
Soundness.SoundUnderAssumptions,
description: "a ^ (n / log(c, a)) = c ^ n"),
description: "a ^ (n / log(c, a)) = c ^ n",
// The base is matched twice -- once as the base and once inside the logarithm -- and
// written not at all, and the quotient and the logarithm go with it: the delta is
// -(3 + 2|a|), at most -5.
growth: RewriteRuleGrowth.Collects),

// Both orientations are written out in the `switch`, so one commutative rule fires
// exactly where the pair did.
Expand Down Expand Up @@ -2800,7 +2835,10 @@ is var (divided, remainder)
MatchPattern.Any("n")))),
bound => new Powf(bound["a"] / new Powf(bound["b"], bound["c"]), bound["n"]),
Soundness.SoundUnderAssumptions,
description: "a ^ n / b ^ (c * n) = (a / b ^ c) ^ n, for a positive whole c"),
description: "a ^ n / b ^ (c * n) = (a / b ^ c) ^ n, for a positive whole c",
// The exponent is matched twice and written once, and one power node goes: the delta
// is -(1 + |n|), at most -2 and further the larger the exponent is.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"a-quotient-of-powers-whose-exponents-differ-by-a-whole-factor-takes-it-into-the-dividend",
Expand All @@ -2813,7 +2851,10 @@ is var (divided, remainder)
MatchPattern.Node<Powf>(MatchPattern.Any("b"), MatchPattern.Any("n"))),
bound => new Powf(new Powf(bound["a"], bound["c"]) / bound["b"], bound["n"]),
Soundness.SoundUnderAssumptions,
description: "a ^ (c * n) / b ^ n = (a ^ c / b) ^ n, for a positive whole c"),
description: "a ^ (c * n) / b ^ n = (a ^ c / b) ^ n, for a positive whole c",
// The exponent is matched twice and written once, and one power node goes with it:
// the delta is -(1 + |n|), the mirror of the rule that takes it into the divisor.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"a-thing-over-a-power-of-itself-is-one-power",
Expand Down Expand Up @@ -2970,7 +3011,10 @@ is var (divided, remainder)
MatchPattern.Node<Powf>(MatchPattern.Any("b"), MatchPattern.Any("n"))),
bound => bound["a"] / new Powf(bound["b"], bound["n"] + bound["m"]),
Soundness.SoundUnderAssumptions,
description: "a / b ^ n / b ^ m = a / b ^ (n + m)"),
description: "a / b ^ n / b ^ m = a / b ^ (n + m)",
// The base is matched twice and written once, and one quotient and one power go:
// the delta is -(2 + |b|), at most -3.
growth: RewriteRuleGrowth.Collects),

new MatchedRule(
"a-variable-times-a-power-puts-the-power-first",
Expand Down Expand Up @@ -3135,7 +3179,10 @@ is var (divided, remainder)
MatchPattern.Node<Mulf>(MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("c"), MatchPattern.Any<Function>("f")), MatchPattern.Any<Function>("g")),
bound => bound["f"] * bound["g"] * bound["c"],
Soundness.SoundUnderAssumptions,
description: "(c * f) * g = c * (f * g), for a numeric c"),
description: "(c * f) * g = c * (f * g), for a numeric c",
// The numeric factor moves to the outside of the product and the two functions come
// together, which is two products either way round: the same size for every input.
growth: RewriteRuleGrowth.Rearranges),
new MatchedRule(
"a-product-of-two-quotients-is-one-quotient",
MatchPattern.Node<Mulf>(MatchPattern.Node<Divf>(MatchPattern.Any("a"), MatchPattern.Any("b")), MatchPattern.Node<Divf>(MatchPattern.Any("c"), MatchPattern.Any("d"))),
Expand Down Expand Up @@ -3199,19 +3246,28 @@ is var (divided, remainder)
MatchPattern.Commutative<Mulf>(MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("c"), MatchPattern.Any<Function>("f")), MatchPattern.Any<Number>("d")),
bound => (Number)bound["c"] * (Number)bound["d"] * bound["f"],
Soundness.SoundUnderAssumptions,
description: "(c * f) * d = (c * d) * f, for numeric c and d"),
description: "(c * f) * d = (c * d) * f, for numeric c and d",
// The two numbers are folded into one as the replacement is built, so four nodes
// around the function become two: exactly -2.
growth: RewriteRuleGrowth.Collects),
new MatchedRule(
"two-numeric-multiples-of-functions-collect-their-numbers",
MatchPattern.Node<Mulf>(MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("c"), MatchPattern.Any<Function>("f")), MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("d"), MatchPattern.Any<Function>("g"))),
bound => bound["f"] * bound["g"] * ((Number)bound["c"] * (Number)bound["d"]),
Soundness.SoundUnderAssumptions,
description: "(c * f) * (d * g) = f * g * (c * d), for numeric c and d"),
description: "(c * f) * (d * g) = f * g * (c * d), for numeric c and d",
// The two numbers are folded into one as the replacement is built, so five nodes
// around the two functions become three: exactly -2.
growth: RewriteRuleGrowth.Collects),
new MatchedRule(
"two-functions-in-a-sum-come-together",
MatchPattern.Commutative<Sumf>(MatchPattern.Node<Sumf>(MatchPattern.Any<Function>("f"), MatchPattern.Any("a")), MatchPattern.Any<Function>("g")),
bound => bound["f"] + bound["g"] + bound["a"],
Soundness.SoundUnderAssumptions,
description: "(f + a) + g = f + g + a, for functions f and g"),
description: "(f + a) + g = f + g + a, for functions f and g",
// The three operands are regrouped and nothing else moves: two sums stay two sums
// and each hole is used once on both sides.
growth: RewriteRuleGrowth.Rearranges),
new MatchedRule(
"a-variable-times-a-number-puts-the-number-first",
MatchPattern.Node<Mulf>(MatchPattern.Any<Variable>("v"), MatchPattern.Any<Number>("c")),
Expand Down Expand Up @@ -3312,7 +3368,10 @@ is var (divided, remainder)
MatchPattern.Node<Minusf>(MatchPattern.Any("a"), MatchPattern.Any("a")),
bound => Integer.Zero,
Soundness.Sound,
description: "k - k = 0"),
description: "k - k = 0",
// Everything the pattern matched goes and one node arrives, and the operand was
// matched twice: the delta is -2|k|, at most -2 and further the larger it is.
growth: RewriteRuleGrowth.Collects),
new MatchedRule(
"a-factor-shared-by-a-quotient-and-a-product-added-comes-out",
MatchPattern.Node<Sumf>(MatchPattern.Node<Divf>(MatchPattern.Any("a"), MatchPattern.Any("b")), MatchPattern.Commutative<Mulf>(MatchPattern.Any("a"), MatchPattern.Any("c"))),
Expand Down Expand Up @@ -3412,7 +3471,10 @@ is var (divided, remainder)
MatchPattern.Node<Divf>(MatchPattern.Any<Number>("c"), MatchPattern.Commutative<Mulf>(MatchPattern.Any<Number>("d"), MatchPattern.Any("a"))),
bound => (Number)bound["c"] / (Number)bound["d"] / bound["a"],
Soundness.SoundUnderAssumptions,
description: "c / (d * a) = (c / d) / a, for numeric c and d"),
description: "c / (d * a) = (c / d) / a, for numeric c and d",
// The two numbers are folded into one as the replacement is built, so four nodes
// around the remaining hole become two: exactly -2.
growth: RewriteRuleGrowth.Collects),
new MatchedRule(
"two-numbers-around-a-factor-collect",
MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("c"), MatchPattern.Node<Mulf>(MatchPattern.Any<Number>("d"), MatchPattern.Any("a"))),
Expand Down Expand Up @@ -3519,7 +3581,10 @@ is var (divided, remainder)
MatchPattern.Commutative<Mulf>(MatchPattern.Node<Signumf>(MatchPattern.Any("a")), MatchPattern.Node<Absf>(MatchPattern.Any("a"))),
bound => bound["a"],
Soundness.SoundUnderAssumptions,
description: "sgn(a) * abs(a) = a"),
description: "sgn(a) * abs(a) = a",
// Three nodes around two copies of the operand become the one copy that is left, so
// the delta is -(3 + |a|): at most -4.
growth: RewriteRuleGrowth.Collects),
new MatchedRule(
"a-reciprocal-rational-factor-is-a-division",
MatchPattern.Commutative<Mulf>(MatchPattern.Any<Entity>("r", one => Functions.Patterns.IsWholeReciprocal(one, 1)), MatchPattern.Any("a")),
Expand Down
15 changes: 13 additions & 2 deletions Sources/AngouriMath/Docs/Contributing/WritingARule.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ A pattern replacement gets:
- **an exact growth**, counted from the two patterns rather than declared.

A code replacement gets neither, and its growth is `Unknown` unless you declare one. That is the
honest default — **175** rules sit at `Unknown` — but declare it where you can justify it:
honest default — **154** rules sit at `Unknown` — but declare it where you can justify it:

```csharp
// The Chebyshev expansion of sin(n * a) is a sum of n terms where the pattern is one node,
Expand All @@ -157,7 +157,11 @@ cannot argue the claim from the code, leave it `Unknown`.
of a few shapes. A hole matched twice and written once gives `-(1 + |a|)`, which is a `Collects`
stronger than the corpus will show you, since the corpus fills its holes with single nodes. A
replacement holding no hole at all — a constant, or `pi/2` — collects by the whole of the pattern.
Operators mapping one for one with every hole used once is `Rearranges`.
Operators mapping one for one with every hole used once is `Rearranges`. A pattern that binds
nothing — `arctan(sqrt(3)) = pi/3`, `{ True, False }` — has a fixed size on both sides, so its delta
is a number and there is nothing to argue. And two numbers combined with `Number` arithmetic **fold
into one node** as the replacement is built, which is worth two on its own: `(c * v) * d` collects
where the same rule written without the cast would only rearrange.

**Four shapes look declarable and are not.** Each of these measured a clean, constant delta over the
corpus, and each is false:
Expand All @@ -169,6 +173,13 @@ corpus, and each is false:
| a hole can be filled by two spellings of one thing | `IsWholeReciprocal` takes the literal `1/3`, which is one node, and a written `1 / c`, which is three — so the delta is 0 in one and −2 in the other |
| a hole is repeated and the replacement squares it | `a * (a * b) = a^2 * b` is `1 - |a|`: zero for a leaf and negative for anything bigger |

**Every rule still at `Unknown` has been looked at, and each falls into one of the shapes above.**
Most of them compute their answer through a helper, so there is nothing to count; the rest either
build a comparison, attach a `Provided` sized by their own operands, or are the repeated hole that
pays a literal to be gathered — `a ^ n * a`, `a / b / b`, `a * a`, `k + k` and their two dozen
relatives, every one of them `1 - |a|`. So `Unknown` here is a finding rather than a gap, and a new
`Unknown` should be one too.

**The comparison set is settled, and settled as `Unknown`.** Its sixty-odd rules were gone through
one at a time and all but seven fall into the shapes above: most build their replacement with `<` or
`>` and so chain; the rest either attach a `Provided` sized by their own operands — the chain
Expand Down
Loading
Loading