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
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ The calculus nodes and the binders — `derivative`, `integral`, `limit`, `sum`,
names the node and says to compile `.Evaled` or `Simplify()` instead. The LINQ compiler said the bound variable was not
among the arguments.

### `MathS.NumberTheory.Factorize` refuses 0 and negative integers

**Not silent: a wrong answer now throws.** `Factorize` answered 0, -1 and -12 with the empty list, which is the
factorisation of 1. 0 has no factorisation into primes, and a negative integer is -1 times the factorisation of its
absolute value, which a list of primes cannot write. Each now throws `InvalidNumberException`, the type for a numeric
argument an operation cannot use, as a negative term count for a Taylor expansion does. 1 is still the empty list. `phi`,
`divisorsigma`, `mobius`, `primenu`, `primeomega`, `valuation` and `CountDivisors` do not go through it and are unchanged
([#2466](https://github.com/asc-community/AngouriMath/issues/2466)).

| Input | Was (2.7.0) | Now |
|---|---|---|
| `MathS.NumberTheory.Factorize(0)` | `[]` | throws `InvalidNumberException` |
| `MathS.NumberTheory.Factorize(-1)` | `[]` | throws `InvalidNumberException` |
| `MathS.NumberTheory.Factorize(-12)` | `[]` | throws `InvalidNumberException` |
| `MathS.NumberTheory.Factorize(1)` | `[]` | unchanged |
| `MathS.NumberTheory.Factorize(360)` | `[(2, 3), (3, 2), (5, 1)]` | unchanged |

### A definite integral whose antiderivative splits into cases finds the breaks of each case

**Silent: a wrong value is corrected.** The antiderivative of `1/(t^2 + y^2)` is a case split on `y`, and its case
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 @@ -179,10 +179,13 @@ public static class NumberTheory
public static Integer CountDivisors(Integer integer) => integer.CountDivisors();

/// <summary>
/// Factorization of integer. Returns a sequence of unique primes
/// and their corresponding power. Sorted by value of prime (starting
/// from the smallest).
/// The prime factorisation of a positive integer: each prime that divides it with its
/// power, the smallest prime first. Empty for 1, the empty product.
/// </summary>
/// <exception cref="InvalidNumberException">
/// Thrown where <paramref name="integer"/> is 0 or negative, which have no factorisation
/// into primes alone. https://github.com/asc-community/AngouriMath/issues/2466
/// </exception>
/// <example>
/// <code>
/// var factors = MathS.NumberTheory.Factorize(1872);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,20 @@ public static Integer Create(EInteger value)
public Integer Phi() => EInteger.Phi();

/// <summary>
/// Factorization of integer
/// </summary>
/// The prime factorisation of a positive integer: each prime that divides it with its power, the
/// smallest prime first. Empty for 1, the empty product.
/// </summary>
/// <exception cref="Core.Exceptions.InvalidNumberException">
/// Thrown where the integer is 0 or negative. 0 has no factorisation into primes, and a negative
/// integer factors as -1 times the factorisation of its absolute value, which a list of primes
/// cannot write; so neither is answered as a list, which would claim the integer is 1 or its
/// absolute value. https://github.com/asc-community/AngouriMath/issues/2466
/// </exception>
public IEnumerable<(Integer prime, Integer power)> Factorize() =>
EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power));
EInteger.Sign > 0
? EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power))
: throw new Core.Exceptions.InvalidNumberException(
$"A prime factorisation is of a positive integer, and {EInteger} was given");

/// <summary>
/// Count of all divisors of an integer
Expand Down
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Core/Operations/Catalogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ internal static class Catalogue

/// <summary><see cref="MathS.NumberTheory.Factorize(Integer)"/>.</summary>
public static Query<Func<Integer, IEnumerable<(Integer Prime, Integer Power)>>> PrimeFactorization { get; } = new(
"prime_factorization", "The primes that divide an integer, each with its power.",
"prime_factorization", "The primes that divide a positive integer, each with its power.",
static n => MathS.NumberTheory.Factorize(n));

/// <summary><see cref="MathS.NumberTheory.Phi(Entity)"/>, evaluated.</summary>
Expand Down
46 changes: 46 additions & 0 deletions Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AngouriMath.Core.Exceptions;
using AngouriMath.Core.Operations;
using AngouriMath.Extensions;
using PeterO.Numbers;
using Xunit;
Expand Down Expand Up @@ -80,6 +82,50 @@ public void APowerOfALargePrimeHasItsTotient()
Within(() => "phi((2^89 - 1)^k)".ToEntity().Simplify()));
}

/// <summary>
/// An empty list is the factorisation of 1, so it answered 0, -1 and -12 as though each were 1.
/// https://github.com/asc-community/AngouriMath/issues/2466
/// </summary>
[Theory]
[InlineData("0")]
[InlineData("-1")]
[InlineData("-12")]
[InlineData("-18446744073709551616")]
public void Issue2466_ANumberThatIsNotPositiveIsRefused(string number)
{
var integer = Integer.Create(EInteger.FromString(number));
Assert.Throws<InvalidNumberException>(() => MathS.NumberTheory.Factorize(integer));
Assert.Throws<InvalidNumberException>(() => integer.Factorize());
Assert.Throws<InvalidNumberException>(() => Catalogue.PrimeFactorization.Ask(integer));
}

[Fact]
public void Issue2466_OneIsTheEmptyProduct()
=> Assert.Empty(MathS.NumberTheory.Factorize(Integer.One));

/// <summary>
/// The functions of a factorisation do not ask <see cref="MathS.NumberTheory.Factorize"/>, and keep their
/// own answers at 0 and below. https://github.com/asc-community/AngouriMath/issues/2466
/// </summary>
[Theory]
[InlineData("phi(0)", "0")]
[InlineData("phi(-12)", "0")]
[InlineData("divisorsigma(1, -12)", "NaN")]
[InlineData("divisorsigma(1, 0)", "NaN")]
[InlineData("mobius(-12)", "NaN")]
[InlineData("primenu(-12)", "NaN")]
[InlineData("primeomega(0)", "NaN")]
[InlineData("valuation(-12, 2)", "2")]
[InlineData("valuation(0, 2)", "+oo")]
public void Issue2466_TheFunctionsOfAFactorisationKeepTheirAnswers(string input, string expected)
=> Assert.Equal(expected == "NaN" ? MathS.NaN : expected.ToEntity(), input.ToEntity().Evaled);

[Theory]
[InlineData("0")]
[InlineData("-12")]
public void Issue2466_DivisorsOfANumberThatIsNotPositiveStillCountZero(string number)
=> Assert.Equal(Integer.Zero, MathS.NumberTheory.CountDivisors(Integer.Create(EInteger.FromString(number))));

private static T Within<T>(Func<T> evaluation)
{
var task = Task.Factory.StartNew(evaluation, TaskCreationOptions.LongRunning);
Expand Down
Loading