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
14 changes: 14 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,20 @@ no value at `i`, where it is 1 ([#2256](https://github.com/asc-community/Angouri
| `"x! = 0".ToEntity().Simplify()` | `False provided x in RR and (x >= 0 or not x in ZZ)` | `False provided x in ZZ implies x >= 0` |
| `"x!".ToEntity().DomainCondition` | `x in RR and (x >= 0 or not x in ZZ)` | `x in ZZ implies x >= 0` |

### `am_series` keeps the power it is asked for

**Silent: `am_series` and `Catalogue.Series` keep the powers up to the degree given, where they kept one fewer.**
The degree, documented as the highest power kept, was passed to `MathS.Series.Maclaurin` and `Taylor` as their
number of terms. So degree 1 gave the constant term alone, and linearising a model gave its value at the point.
`MathS.Series` is unchanged, and its documentation now says that its argument counts terms
([#2299](https://github.com/asc-community/AngouriMath/issues/2299)).

| Input | Was (2.6.0) | Now |
|---|---|---|
| `am_series`, `sin(x)`, degree 7 | `x ^ 5 / 120 - x ^ 3 / 6 + x` | `x ^ 5 / 120 - x ^ 7 / 5040 - x ^ 3 / 6 + x` |
| `am_series`, `sin(x)`, degree 1 | `0` | `x` |
| `am_series`, `e^x`, degree 1 | `1` | `1 + x` |

### A number's conjugate is made under the setting in force

**Silent: `Complex.Conjugate` is made under the downcasting setting of the caller, where the first caller's setting was
Expand Down
9 changes: 6 additions & 3 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,8 @@ public static class Series
/// The function to find the Maclaurin expansion of
/// </param>
/// <param name="degree">
/// The degree of the resulting Maclaurin polynomial (and the variable in the resulting series)
/// The number of terms, one more than the highest power kept: 4 keeps the powers up to the
/// cube, as the example shows. https://github.com/asc-community/AngouriMath/issues/2299
/// </param>
/// <param name="exprVariables">
/// The variable/s to take the series over (and the variable the series will be over)
Expand Down Expand Up @@ -2711,7 +2712,8 @@ public static Entity Maclaurin(Entity expr, int degree, params Variable[] exprVa
/// The function to find the Taylor expansion of
/// </param>
/// <param name="degree">
/// The degree of the resulting taylor polynomial
/// The number of terms, one more than the highest power kept: 4 keeps the powers up to the
/// cube, as the example shows. https://github.com/asc-community/AngouriMath/issues/2299
/// </param>
/// <param name="exprVariables">
/// The variable/s to take the series over (and the variable in the resulting series),
Expand Down Expand Up @@ -2799,7 +2801,8 @@ public static Entity Taylor(Entity expr, int degree, params (Variable exprVariab
/// The function to find the Taylor expansion of
/// </param>
/// <param name="degree">
/// The degree of the resulting taylor polynomial
/// The number of terms, one more than the highest power kept: 4 keeps the powers up to the
/// cube, as the example shows. https://github.com/asc-community/AngouriMath/issues/2299
/// </param>
/// <param name="exprToPolyVars">
/// The variable/s to take the series over, the variable the series will be over,
Expand Down
8 changes: 5 additions & 3 deletions Sources/AngouriMath/Core/Operations/Catalogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ internal static class Catalogue

/// <summary>
/// <see cref="MathS.Series.Taylor(Entity, int, ValueTuple{Variable, Entity}[])"/>, or
/// <see cref="MathS.Series.Maclaurin(Entity, int, Variable[])"/> where there is no point.
/// <see cref="MathS.Series.Maclaurin(Entity, int, Variable[])"/> where there is no point,
/// with one term more than the degree, since those count terms: <c>sin(x)</c> to degree 1 is
/// <c>x</c>, and was <c>0</c>. https://github.com/asc-community/AngouriMath/issues/2299
/// </summary>
public static Query<Func<Entity, Variable, int, Entity?, Entity>> Series { get; } = new(
"series", "Expands as a Taylor series in a variable, up to a degree, around a point or around zero.",
static (e, variable, degree, around) => around is null
? MathS.Series.Maclaurin(e, degree, variable)
: MathS.Series.Taylor(e, degree, (variable, around)),
? MathS.Series.Maclaurin(e, degree + 1, variable)
: MathS.Series.Taylor(e, degree + 1, (variable, around)),
TheVariable, new Parameter("degree", "The highest power kept."), new Parameter("around", "The point, or none for zero."));

// ---------------------------------------------------------------- queries on a number
Expand Down
11 changes: 8 additions & 3 deletions Sources/Tests/McpUnitTests/ToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,14 @@ public void EigenvaluesHoldAtZeroToo()
Assert.DoesNotContain("provided", answer.Field("eigenvalues"));
}

[Fact]
public void Series() =>
Assert.Equal("x ^ 5 / 120 - x ^ 3 / 6 + x", Mcp.Call("am_series", """{"expression":"sin(x)","variable":"x","degree":7}""").Field("result"));
// The degree is the highest power kept, x ^ 7 at 7. It was passed on as the number of terms, so
// degree 7 stopped at x ^ 5 and degree 1 was the constant. https://github.com/asc-community/AngouriMath/issues/2299
[Theory]
[InlineData("sin(x)", 7, "x ^ 5 / 120 - x ^ 7 / 5040 - x ^ 3 / 6 + x")]
[InlineData("sin(x)", 1, "x")]
[InlineData("e^x", 1, "1 + x")]
public void Series(string expression, int degree, string expected) =>
Assert.Equal(expected, Mcp.Call("am_series", $$"""{"expression":"{{expression}}","variable":"x","degree":{{degree}}}""").Field("result"));

[Fact]
public void Factorize() =>
Expand Down
21 changes: 19 additions & 2 deletions Sources/Tests/UnitTests/Core/Operations/CatalogueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ public void AnEntryClaimsWhatItsTransformationClaims(Transformation transformati
Assert.Equal(transformation.Relation, relation);
}

/// <summary>
/// The series is taken to the degree asked for, which is the highest power it keeps. It
/// was passed on as the number of terms, one fewer: <c>sin(x)</c> to degree 1 was <c>0</c>.
/// https://github.com/asc-community/AngouriMath/issues/2299
/// </summary>
[Theory]
[InlineData("sin(x)", 1, null, "x")]
[InlineData("e^x", 1, null, "1 + x")]
[InlineData("sin(x)", 7, null, "x - x^3/6 + x^5/120 - x^7/5040")]
[InlineData("e^x", 2, "1", "e + e (x - 1) + e (x - 1)^2 / 2")]
[InlineData("cos(x)", 0, null, "1")]
public void ASeriesKeepsTheHighestPowerItIsAskedFor(string expression, int degree, string? around, string expected)
{
var series = Catalogue.Series.Ask(expression, MathS.Var("x"), degree, around is null ? null : MathS.FromString(around));
Assert.Equal(Entity.Number.Integer.Zero, (series - MathS.FromString(expected)).Simplify());
}

/// <summary>
/// One answer from each entry, against the method the entry names, on an input where
/// that method and its neighbours answer differently -- so that an entry holding the
Expand Down Expand Up @@ -203,8 +220,8 @@ public void EachEntryAnswersAsTheMethodItNames()
Assert.Equal(MathS.Boolean.BuildTruthTable("a and not b", "a", "b"), Catalogue.TruthTable.Ask("a and not b", [MathS.Var("a"), MathS.Var("b")]));
Assert.Equal(MathS.SolveBooleanTable("a and not b", "a", "b"), Catalogue.SatisfyingAssignments.Ask("a and not b", [MathS.Var("a"), MathS.Var("b")]));
Assert.NotEqual(Catalogue.TruthTable.Ask("a and not b", [MathS.Var("a"), MathS.Var("b")]), Catalogue.SatisfyingAssignments.Ask("a and not b", [MathS.Var("a"), MathS.Var("b")]));
Assert.Equal(MathS.Series.Maclaurin("e^x", 3, x), Catalogue.Series.Ask("e^x", x, 3, null));
Assert.Equal(MathS.Series.Taylor("e^x", 3, (x, 1)), Catalogue.Series.Ask("e^x", x, 3, 1));
Assert.Equal(MathS.Series.Maclaurin("e^x", 4, x), Catalogue.Series.Ask("e^x", x, 3, null));
Assert.Equal(MathS.Series.Taylor("e^x", 4, (x, 1)), Catalogue.Series.Ask("e^x", x, 3, 1));
Assert.NotEqual(Catalogue.Series.Ask("e^x", x, 3, null), Catalogue.Series.Ask("e^x", x, 3, 1));

Assert.Equal(MathS.ToBaseN(10.5m, 2), Catalogue.ToBase.Ask(10.5m, 2));
Expand Down
Loading