|
| 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 System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using AngouriMath.Core.Transformations; |
| 12 | +using static AngouriMath.Entity; |
| 13 | +using static AngouriMath.Entity.Number; |
| 14 | + |
| 15 | +namespace AngouriMath.Core.Operations |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Every operation the library offers by name, which the terminal and the MCP server take |
| 19 | + /// theirs from. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// <para> |
| 23 | + /// The names are the ones |
| 24 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/1019">#1019</a> item 16 |
| 25 | + /// proposes for <c>amcli</c>'s commands in v3: the library's own verbs, so <c>simplify</c> |
| 26 | + /// rather than <c>simp</c> and <c>differentiate</c> rather than <c>diff</c>. |
| 27 | + /// </para> |
| 28 | + /// <para> |
| 29 | + /// An operation is in this list when a surface offers it, or could offer it with nothing |
| 30 | + /// but text in and text out. The building blocks <see cref="Transformation"/> also names, |
| 31 | + /// such as <see cref="Transformation.RationalCanonicalization"/> or |
| 32 | + /// <see cref="Transformation.EqualitySaturation"/>, are not: a caller composes them, and |
| 33 | + /// none answers a question by itself. A test lists every factory of |
| 34 | + /// <see cref="Transformation"/> as one or the other, so that a new one fails the build |
| 35 | + /// until it is placed. |
| 36 | + /// </para> |
| 37 | + /// https://github.com/asc-community/AngouriMath/issues/1684 |
| 38 | + /// </remarks> |
| 39 | + internal static class Catalogue |
| 40 | + { |
| 41 | + [ConstantField] |
| 42 | + private static readonly Parameter TheVariable = new("variable", "The variable it is over."); |
| 43 | + |
| 44 | + // ---------------------------------------------------------------- transformations |
| 45 | + |
| 46 | + /// <summary><see cref="Entity.Simplify(int)"/>.</summary> |
| 47 | + public static TransformationOperation<Func<Entity, Entity>> Simplify { get; } = new( |
| 48 | + "simplify", "Simplifies, searching the expression's rewritings for the simplest.", |
| 49 | + TransformationRelation.Equivalence, static e => e.Simplify()); |
| 50 | + |
| 51 | + /// <summary><see cref="Entity.InnerSimplified"/>.</summary> |
| 52 | + public static TransformationOperation<Func<Entity, Entity>> Normalize { get; } = new( |
| 53 | + "normalize", "Tidies the expression in one structural pass, without the search simplify makes.", |
| 54 | + TransformationRelation.Equivalence, static e => e.InnerSimplified); |
| 55 | + |
| 56 | + /// <summary><see cref="Entity.Expand(int)"/>.</summary> |
| 57 | + public static TransformationOperation<Func<Entity, Entity>> Expand { get; } = new( |
| 58 | + "expand", "Multiplies products over sums out.", |
| 59 | + TransformationRelation.Equivalence, static e => e.Expand()); |
| 60 | + |
| 61 | + /// <summary><see cref="Entity.Factorize(int)"/>.</summary> |
| 62 | + public static TransformationOperation<Func<Entity, Entity>> Factorize { get; } = new( |
| 63 | + "factorize", "Gathers common factors out and factors polynomials.", |
| 64 | + TransformationRelation.Equivalence, static e => e.Factorize()); |
| 65 | + |
| 66 | + /// <summary><see cref="Entity.Differentiate(Variable, int)"/>.</summary> |
| 67 | + public static TransformationOperation<Func<Entity, Variable, int, Entity>> Differentiate { get; } = new( |
| 68 | + "differentiate", "Differentiates over a variable, as many times as the order says.", |
| 69 | + TransformationRelation.Derivation, static (e, variable, order) => e.Differentiate(variable, order), |
| 70 | + TheVariable, new Parameter("order", "How many times; a negative order integrates instead.")); |
| 71 | + |
| 72 | + /// <summary><see cref="Entity.Integrate(Variable)"/>.</summary> |
| 73 | + public static TransformationOperation<Func<Entity, Variable, Entity>> Integrate { get; } = new( |
| 74 | + "integrate", "Finds an antiderivative over a variable, with a constant of integration, or leaves the integral unevaluated.", |
| 75 | + TransformationRelation.Derivation, static (e, variable) => e.Integrate(variable), |
| 76 | + TheVariable); |
| 77 | + |
| 78 | + /// <summary><see cref="Entity.Limit(Variable, Entity, ApproachFrom)"/>.</summary> |
| 79 | + public static TransformationOperation<Func<Entity, Variable, Entity, ApproachFrom, Entity>> Limit { get; } = new( |
| 80 | + "limit", "Takes the limit as a variable approaches a value, or leaves it unevaluated.", |
| 81 | + TransformationRelation.Derivation, static (e, variable, destination, side) => e.Limit(variable, destination, side), |
| 82 | + TheVariable, new Parameter("destination", "The value it approaches."), new Parameter("side", "From the left, from the right, or from both sides.")); |
| 83 | + |
| 84 | + /// <summary><see cref="Entity.Substitute(Entity, Entity)"/>.</summary> |
| 85 | + public static TransformationOperation<Func<Entity, Entity, Entity, Entity>> Substitute { get; } = new( |
| 86 | + "substitute", "Replaces every occurrence of a subexpression with another expression.", |
| 87 | + TransformationRelation.Derivation, static (e, what, with) => e.Substitute(what, with), |
| 88 | + new Parameter("what", "The subexpression to replace."), new Parameter("with", "What to put in its place.")); |
| 89 | + |
| 90 | + // ---------------------------------------------------------------- queries on an expression |
| 91 | + |
| 92 | + /// <summary><see cref="Entity.Solve(Variable)"/>.</summary> |
| 93 | + public static Query<Func<Entity, Variable, Set>> Solve { get; } = new( |
| 94 | + "solve", "Solves an equation, an inequality or a statement for a variable.", |
| 95 | + static (e, variable) => e.Solve(variable), |
| 96 | + TheVariable); |
| 97 | + |
| 98 | + /// <summary><see cref="EquationSystem.Solve(Variable[])"/>.</summary> |
| 99 | + public static Query<Func<IEnumerable<Entity>, IReadOnlyList<Variable>, Matrix?>> SolveSystem { get; } = new( |
| 100 | + "solve_system", "Solves equations jointly for several variables, one row of the answer for each solution.", |
| 101 | + static (equations, variables) => MathS.Equations(equations).Solve([.. variables]), |
| 102 | + new Parameter("variables", "The variables to solve for, in the order of the answer's columns.")); |
| 103 | + |
| 104 | + /// <summary><see cref="Entity.Evaled"/>.</summary> |
| 105 | + public static Query<Func<Entity, Entity>> Evaluate { get; } = new( |
| 106 | + "evaluate", "Evaluates as far as it goes: to a number, a boolean or a set where it can, and as an expression where it cannot.", |
| 107 | + static e => e.Evaled); |
| 108 | + |
| 109 | + /// <summary><see cref="Entity.EvalNumerical"/>.</summary> |
| 110 | + public static Query<Func<Entity, Complex>> EvaluateNumerically { get; } = new( |
| 111 | + "evaluate_numerically", "Evaluates to a complex number, at the working precision.", |
| 112 | + static e => e.EvalNumerical()); |
| 113 | + |
| 114 | + /// <summary><see cref="Entity.Vars"/>.</summary> |
| 115 | + public static Query<Func<Entity, IReadOnlyList<Variable>>> Variables { get; } = new( |
| 116 | + "variables", "Lists the free variables.", |
| 117 | + static e => e.Vars); |
| 118 | + |
| 119 | + /// <summary><see cref="Entity.Alternate(int)"/>.</summary> |
| 120 | + public static Query<Func<Entity, int, IEnumerable<Entity>>> Alternatives { get; } = new( |
| 121 | + "alternatives", "Lists forms of the expression that simplify finds, simplest first.", |
| 122 | + static (e, level) => e.Alternate(level), |
| 123 | + new Parameter("level", "How hard to look, as simplify's level.")); |
| 124 | + |
| 125 | + /// <summary><see cref="Entity.Latexize()"/>.</summary> |
| 126 | + public static Query<Func<Entity, string>> Latex { get; } = new( |
| 127 | + "latex", "Writes the expression as LaTeX.", |
| 128 | + static e => e.Latexize()); |
| 129 | + |
| 130 | + /// <summary><see cref="MathS.ToSympyCode(Entity)"/>.</summary> |
| 131 | + public static Query<Func<Entity, string>> Sympy { get; } = new( |
| 132 | + "sympy", "Writes the expression as Python code for SymPy.", |
| 133 | + static e => MathS.ToSympyCode(e)); |
| 134 | + |
| 135 | + /// <summary><see cref="MathS.Boolean.BuildTruthTable(Entity, Variable[])"/>.</summary> |
| 136 | + public static Query<Func<Entity, IReadOnlyList<Variable>, Matrix?>> TruthTable { get; } = new( |
| 137 | + "truth_table", "Tabulates a statement over every assignment of true and false to its variables.", |
| 138 | + static (e, variables) => MathS.Boolean.BuildTruthTable(e, [.. variables]), |
| 139 | + new Parameter("variables", "The variables, in the order of the table's columns.")); |
| 140 | + |
| 141 | + /// <summary><see cref="MathS.SolveBooleanTable(Entity, Variable[])"/>.</summary> |
| 142 | + public static Query<Func<Entity, IReadOnlyList<Variable>, Matrix?>> SatisfyingAssignments { get; } = new( |
| 143 | + "satisfying_assignments", "The assignments of true and false to a statement's variables under which it holds, one row each.", |
| 144 | + static (e, variables) => MathS.SolveBooleanTable(e, [.. variables]), |
| 145 | + new Parameter("variables", "The variables, in the order of the rows' columns.")); |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// <see cref="MathS.Series.Taylor(Entity, int, ValueTuple{Variable, Entity}[])"/>, or |
| 149 | + /// <see cref="MathS.Series.Maclaurin(Entity, int, Variable[])"/> where there is no point. |
| 150 | + /// </summary> |
| 151 | + public static Query<Func<Entity, Variable, int, Entity?, Entity>> Series { get; } = new( |
| 152 | + "series", "Expands as a Taylor series in a variable, up to a degree, around a point or around zero.", |
| 153 | + static (e, variable, degree, around) => around is null |
| 154 | + ? MathS.Series.Maclaurin(e, degree, variable) |
| 155 | + : MathS.Series.Taylor(e, degree, (variable, around)), |
| 156 | + TheVariable, new Parameter("degree", "The highest power kept."), new Parameter("around", "The point, or none for zero.")); |
| 157 | + |
| 158 | + // ---------------------------------------------------------------- queries on a number |
| 159 | + |
| 160 | + /// <summary><see cref="MathS.ToBaseN(Real, int)"/>.</summary> |
| 161 | + public static Query<Func<Real, int, string>> ToBase { get; } = new( |
| 162 | + "to_base", "Writes a real number in a base from 2 to 36.", |
| 163 | + static (number, numberBase) => MathS.ToBaseN(number, numberBase), |
| 164 | + new Parameter("base", "The base, from 2 to 36.")); |
| 165 | + |
| 166 | + /// <summary><see cref="MathS.FromBaseN(string, int)"/>.</summary> |
| 167 | + public static Query<Func<string, int, Real>> FromBase { get; } = new( |
| 168 | + "from_base", "Reads a real number written in a base from 2 to 36.", |
| 169 | + static (digits, numberBase) => MathS.FromBaseN(digits, numberBase), |
| 170 | + new Parameter("base", "The base, from 2 to 36.")); |
| 171 | + |
| 172 | + // ---------------------------------------------------------------- queries on a matrix |
| 173 | + |
| 174 | + /// <summary><see cref="Matrix.Determinant"/>.</summary> |
| 175 | + public static Query<Func<Matrix, Entity?>> Determinant { get; } = new( |
| 176 | + "determinant", "The determinant of a square matrix, or none for a matrix that is not square.", |
| 177 | + static m => m.Determinant); |
| 178 | + |
| 179 | + /// <summary><see cref="Matrix.Inverse"/>.</summary> |
| 180 | + public static Query<Func<Matrix, Matrix?>> Inverse { get; } = new( |
| 181 | + "inverse", "The inverse of a square matrix, or none where it has none.", |
| 182 | + static m => m.Inverse); |
| 183 | + |
| 184 | + /// <summary><see cref="Matrix.T"/>.</summary> |
| 185 | + public static Query<Func<Matrix, Matrix>> Transpose { get; } = new( |
| 186 | + "transpose", "The transpose of a matrix.", |
| 187 | + static m => m.T); |
| 188 | + |
| 189 | + /// <summary><see cref="Matrix.Rank"/>.</summary> |
| 190 | + public static Query<Func<Matrix, int>> Rank { get; } = new( |
| 191 | + "rank", "The rank of a matrix.", |
| 192 | + static m => m.Rank); |
| 193 | + |
| 194 | + /// <summary><see cref="Matrix.ReducedRowEchelonForm"/>.</summary> |
| 195 | + public static Query<Func<Matrix, Matrix>> ReducedRowEchelonForm { get; } = new( |
| 196 | + "reduced_row_echelon_form", "A matrix in reduced row echelon form.", |
| 197 | + static m => m.ReducedRowEchelonForm); |
| 198 | + |
| 199 | + /// <summary><see cref="Matrix.Trace"/>.</summary> |
| 200 | + public static Query<Func<Matrix, Entity>> Trace { get; } = new( |
| 201 | + "trace", "The trace of a square matrix.", |
| 202 | + static m => m.Trace); |
| 203 | + |
| 204 | + /// <summary><see cref="Matrix.TensorProduct(Matrix, Matrix)"/>.</summary> |
| 205 | + public static Query<Func<Matrix, Matrix, Matrix>> TensorProduct { get; } = new( |
| 206 | + "tensor_product", "The tensor product of two matrices.", |
| 207 | + static (a, b) => Matrix.TensorProduct(a, b), |
| 208 | + new Parameter("other", "The matrix on the right.")); |
| 209 | + |
| 210 | + // ---------------------------------------------------------------- queries on an integer |
| 211 | + |
| 212 | + /// <summary><see cref="MathS.NumberTheory.Factorize(Integer)"/>.</summary> |
| 213 | + public static Query<Func<Integer, IEnumerable<(Integer Prime, Integer Power)>>> PrimeFactorization { get; } = new( |
| 214 | + "prime_factorization", "The primes that divide an integer, each with its power.", |
| 215 | + static n => MathS.NumberTheory.Factorize(n)); |
| 216 | + |
| 217 | + /// <summary><see cref="MathS.NumberTheory.Phi(Entity)"/>, evaluated.</summary> |
| 218 | + public static Query<Func<Integer, Entity>> Totient { get; } = new( |
| 219 | + "totient", "Euler's totient of an integer: how many positive integers up to it are coprime to it.", |
| 220 | + static n => MathS.NumberTheory.Phi(n).Evaled); |
| 221 | + |
| 222 | + /// <summary><see cref="MathS.NumberTheory.GreatestCommonDivisor(Integer, Integer)"/>.</summary> |
| 223 | + public static Query<Func<Integer, Integer, Integer>> GreatestCommonDivisor { get; } = new( |
| 224 | + "greatest_common_divisor", "The greatest common divisor of two integers.", |
| 225 | + static (a, b) => MathS.NumberTheory.GreatestCommonDivisor(a, b), |
| 226 | + new Parameter("other", "The second integer.")); |
| 227 | + |
| 228 | + /// <summary><see cref="MathS.NumberTheory.CountDivisors(Integer)"/>.</summary> |
| 229 | + public static Query<Func<Integer, Integer>> DivisorCount { get; } = new( |
| 230 | + "divisor_count", "How many positive divisors an integer has.", |
| 231 | + static n => MathS.NumberTheory.CountDivisors(n)); |
| 232 | + |
| 233 | + /// <summary><see cref="Integer.IsPrime"/>.</summary> |
| 234 | + public static Query<Func<Integer, bool>> IsPrime { get; } = new( |
| 235 | + "is_prime", "Whether an integer is prime.", |
| 236 | + static n => n.IsPrime); |
| 237 | + |
| 238 | + // ---------------------------------------------------------------- the list |
| 239 | + |
| 240 | + /// <summary>Every entry above, in the order written.</summary> |
| 241 | + /// <remarks>Last in the file, because static initialisers run in the order they are written.</remarks> |
| 242 | + public static IReadOnlyList<Operation> All { get; } = |
| 243 | + [ |
| 244 | + Simplify, Normalize, Expand, Factorize, Differentiate, Integrate, Limit, Substitute, |
| 245 | + Solve, SolveSystem, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, TruthTable, SatisfyingAssignments, Series, |
| 246 | + ToBase, FromBase, |
| 247 | + Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct, |
| 248 | + PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime, |
| 249 | + ]; |
| 250 | + |
| 251 | + /// <summary>The entry named <paramref name="name"/>, or <see langword="null"/> where there is none.</summary> |
| 252 | + /// <param name="name">Its name, such as <c>simplify</c>.</param> |
| 253 | + public static Operation? Find(string name) => All.FirstOrDefault(operation => operation.Name == name); |
| 254 | + } |
| 255 | +} |
0 commit comments