From 78902ff32dc349e7d25d29b2bd5ea12a670a9045 Mon Sep 17 00:00:00 2001 From: Lehonti Ramos Date: Mon, 4 Sep 2023 20:48:02 +0200 Subject: [PATCH] Improvements to operations with arrays/indexables --- Sources/AngouriMath/AngouriMath/Convenience/SettingClass.cs | 2 +- .../AngouriMath/AngouriMath/Functions/Boolean/TableSolver.cs | 4 ++-- .../Functions/Compilation/IntoLinq/CompilationProtocol.cs | 4 ++-- .../Functions/Continuous/Solvers/EquationSolver.cs | 4 ++-- .../Solvers/EquationSolver/AnalyticalEquationSolver.cs | 2 +- .../AngouriMath/AngouriMath/Functions/NumberTheory/Primes.cs | 4 ++-- Sources/Tests/UnitTests/Convenience/FromStringTest.cs | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/AngouriMath/AngouriMath/Convenience/SettingClass.cs b/Sources/AngouriMath/AngouriMath/Convenience/SettingClass.cs index 308cdc13c..40a231b9d 100644 --- a/Sources/AngouriMath/AngouriMath/Convenience/SettingClass.cs +++ b/Sources/AngouriMath/AngouriMath/Convenience/SettingClass.cs @@ -158,7 +158,7 @@ internal sealed class KeyStack { private readonly List<(TKey key, TValue value)> list = new(); - internal TValue Peek() => list[list.Count - 1].value; + internal TValue Peek() => list[^1].value; internal bool Remove(TKey key) { diff --git a/Sources/AngouriMath/AngouriMath/Functions/Boolean/TableSolver.cs b/Sources/AngouriMath/AngouriMath/Functions/Boolean/TableSolver.cs index 208fbf95f..5ef389845 100644 --- a/Sources/AngouriMath/AngouriMath/Functions/Boolean/TableSolver.cs +++ b/Sources/AngouriMath/AngouriMath/Functions/Boolean/TableSolver.cs @@ -46,7 +46,7 @@ internal static bool Next(in Span states) /// internal static Matrix? SolveTable(Entity expr, Variable[] variables) { - var count = expr.Vars.Count(); + var count = expr.Vars.Count; // TODO: we probably also should verify the uniqueness of the given variables if (count != variables.Length) throw new WrongNumberOfArgumentsException("Number of variables must equal number of variables in the expression"); @@ -67,7 +67,7 @@ internal static bool Next(in Span states) internal static Matrix? BuildTruthTable(Entity expr, Variable[] variables) { - var count = expr.Vars.Count(); + var count = expr.Vars.Count; // TODO: we probably also should verify the uniqueness of the given variables if (count != variables.Length) throw new WrongNumberOfArgumentsException("Number of variables must equal number of variables in the expression"); diff --git a/Sources/AngouriMath/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs b/Sources/AngouriMath/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs index 78fa64bcf..4c93d97ca 100644 --- a/Sources/AngouriMath/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs +++ b/Sources/AngouriMath/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs @@ -258,8 +258,8 @@ private Expression HandlePiecewise(IEnumerable en) } // last case - var expression = children[children.Length - 2]; - var predicate = children[children.Length - 1]; + var expression = children[^2]; + var predicate = children[^1]; var nan = ConvertNaN(maxType); (expression, nan) = EqualizeTypesIfAble(expression, nan); diff --git a/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs b/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs index f05e33d41..a1cc1d053 100644 --- a/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs +++ b/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs @@ -26,7 +26,7 @@ internal static Set Solve(Entity equation, Variable x) static Entity simplifier(Entity entity) => entity.InnerSimplified; static Entity evaluator(Entity entity) => entity.Evaled; - var factorizer = equation.Vars.Count() == 1 ? (Func)evaluator : simplifier; + var factorizer = equation.Vars.Count == 1 ? (Func)evaluator : simplifier; if (solutions is FiniteSet finiteSet) @@ -75,7 +75,7 @@ internal static Set Solve(Entity equation, Variable x) /// internal static List> InSolveSystem(List equations, ReadOnlySpan vars) { - var var = vars[vars.Length - 1]; + var var = vars[^1]; if (equations.Count == 1) return equations[0].InnerSimplified.SolveEquation(var) is FiniteSet els ? els.Select(sol => new List { sol }).ToList() diff --git a/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs b/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs index 3f82d99db..527c08982 100644 --- a/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs +++ b/Sources/AngouriMath/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs @@ -209,7 +209,7 @@ static Entity TryDowncast(Entity equation, Variable x, Entity root) // https://mathoverflow.net/a/28977 // if nothing has been found so far - if (MathS.Settings.AllowNewton && expr.Vars.Count() == 1) + if (MathS.Settings.AllowNewton && expr.Vars.Count == 1) return expr.SolveNt(x).Select(ent => TryDowncast(expr, x, ent)).ToSet(); return Enumerable.Empty().ToSet(); diff --git a/Sources/AngouriMath/AngouriMath/Functions/NumberTheory/Primes.cs b/Sources/AngouriMath/AngouriMath/Functions/NumberTheory/Primes.cs index 8f8e9c6ea..ff12d80e5 100644 --- a/Sources/AngouriMath/AngouriMath/Functions/NumberTheory/Primes.cs +++ b/Sources/AngouriMath/AngouriMath/Functions/NumberTheory/Primes.cs @@ -58,7 +58,7 @@ static bool IsPrimeI(Integer num) static void AddPrimeC() { - var n = (primes[primes.Count - 1].cache + 2) ?? throw new AngouriBugException("It was supposed to be not null"); + var n = (primes[^1].cache + 2) ?? throw new AngouriBugException("It was supposed to be not null"); while (!IsPrimeC(n)) n += 2; primes.Add((n, n)); @@ -66,7 +66,7 @@ static void AddPrimeC() static void AddPrimeI() { - var n = (primes[primes.Count - 1].actual + 2) ?? throw new AngouriBugException("It was supposed to be not null"); + var n = (primes[^1].actual + 2) ?? throw new AngouriBugException("It was supposed to be not null"); while (!IsPrimeI(n)) n += 2; primes.Add((n, null)); diff --git a/Sources/Tests/UnitTests/Convenience/FromStringTest.cs b/Sources/Tests/UnitTests/Convenience/FromStringTest.cs index 9a15054bb..57a8178a6 100644 --- a/Sources/Tests/UnitTests/Convenience/FromStringTest.cs +++ b/Sources/Tests/UnitTests/Convenience/FromStringTest.cs @@ -318,7 +318,7 @@ public void TestInequalityCompositionPrettySyntax( public void TestUnicodeVariablesParser(string exprRaw, string expectedName) { Entity actual = exprRaw; - var any = actual.Vars.First(); + var any = actual.Vars[0]; Assert.Equal(expectedName, any.Name); }