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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ are short, and a stale one is worse than none — if you change what a file desc
| [`Docs/Usage/Syntax.md`](Sources/AngouriMath/Docs/Usage/Syntax.md) | what the parser accepts. The grammar was the only statement of it until [#706](https://github.com/asc-community/AngouriMath/pull/706) |
| [`Docs/Usage/Exceptions.md`](Sources/AngouriMath/Docs/Usage/Exceptions.md) | the exception hierarchy under `AngouriMathBaseException` |
| [`Docs/Usage/Shaders.md`](Sources/AngouriMath/Docs/Usage/Shaders.md) | `MathS.Shaders.Hlsl` and `Glsl`: a worked example, and where the printed shader and `Compile<double>` differ |
| [`Docs/Usage/Dimensions.md`](Sources/AngouriMath/Docs/Usage/Dimensions.md) | `MathS.Dimensions.Of` and `DimensionlessGroups`: the rule for each kind of node, and a worked example |
| [`Docs/Usage/Comparison.md`](Sources/AngouriMath/Docs/Usage/Comparison.md) | how we measure against Math.NET Symbolics, Symbolism and SymPy, with the versions and what each comparison does not establish |
| [`Docs/References.md`](Sources/AngouriMath/Docs/References.md) | the books, papers, corpora and reference works the code and its tests draw on, with the link and what each is used for. Add a work there when it is first cited |
| [`Docs/Acknowledgements.md`](Sources/AngouriMath/Docs/Acknowledgements.md) and [`Docs/Articles.md`](Sources/AngouriMath/Docs/Articles.md) | the projects AngouriMath is built with, and the maintainers' articles about it, moved from the archived AngouriMathLab |
Expand Down
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,25 @@ both, and `curl` alone, are still variables ([#2182](https://github.com/asc-comm

---

### Dimensional analysis: `MathS.Dimensions.Of` and `DimensionlessGroups`

**Not silent: a new entry point.** `MathS.Dimensions.Of(expr, dimensions)` reads a formula against the physical
dimension of each variable, a `Dimension` over the seven SI base dimensions passed in a map, and returns a
`DimensionCheck`: `Consistent` with the dimension of the result, `Inconsistent` with the subexpression where two
dimensions are added or compared or a quantity is passed to a function that takes a pure number, or `Undetermined`
where a variable has no dimension or a node is not read. Nothing is simplified first. `MathS.Dimensions.DimensionlessGroups`
gives the Buckingham π groups of a list of variables. The catalogue has them as `dimension` and `dimensionless_groups`,
not yet offered by the MCP server or the terminal ([#2298](https://github.com/asc-community/AngouriMath/issues/2298)).
Nothing that existed changes. See [`Docs/Usage/Dimensions.md`](Sources/AngouriMath/Docs/Usage/Dimensions.md)
([#2292](https://github.com/asc-community/AngouriMath/issues/2292)).

| Input, with `g` an acceleration, `h` a length, `t` a time | Was (2.7.0) | Now |
|---|---|---|
| `MathS.Dimensions.Of("sqrt(2 g h)", dims).ToString()` | no such member | `Consistent: L T^-1` |
| `MathS.Dimensions.Of("h + g t", dims).ToString()` | no such member | `Inconsistent at h + g * t: h has dimension L and g * t has dimension L T^-1, so they cannot be added` |
| `MathS.Dimensions.DimensionlessGroups(["h", "g", "t"], dims)` | no such member | `[g * t ^ 2 / h]` |

---

### A system row that is not a solution declines the system

Expand Down
9 changes: 9 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,15 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Convenience/ShadersTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[AngouriMath/Functions/Dimensions/*.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[AngouriMath/Convenience/MathS.Dimensions.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Convenience/DimensionsTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Convenience/PrimeFactorizationNodeTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

Expand Down
141 changes: 141 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.Dimensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//



namespace AngouriMath
{
using static Entity;

partial class MathS
{
/// <summary>
/// Dimensional analysis: whether a formula is physically meaningful, what kind of
/// quantity it produces, and which combinations of its variables are pure numbers.
/// </summary>
/// <remarks>
/// <para>
/// A variable is a symbol, and nothing in an <see cref="Entity"/> says that <c>v</c>
/// is a speed. The caller says so, with a map from each variable to its
/// <see cref="Dimension"/>, and the check reads the formula against it. The map is
/// passed rather than attached to the variables, so the same expression can be read
/// under different assignments, and nothing about how an expression prints, parses or
/// simplifies changes.
/// </para>
/// <para>
/// This complements a units library rather than replacing one. UnitsNet and its kind
/// check a <i>value</i>: a length divided by a time is a speed. Neither can look at
/// <c>sqrt(2 g h)</c> and say whether it is one, and that is what this does.
/// </para>
/// <para>
/// <b>Undetermined is not a verdict.</b> An undeclared variable, or a node the check does
/// not read, gives <see cref="DimensionStatus.Undetermined"/>, never a guess.
/// </para>
/// <para>
/// The page <c>Docs/Usage/Dimensions.md</c> lists the rule for each kind of node and
/// works the water-wave example through.
/// </para>
/// </remarks>
public static class Dimensions
{
/// <summary>
/// The dimension of <paramref name="expr"/> as written, or the subexpression where
/// it stops making physical sense.
/// </summary>
/// <param name="expr">The formula, an expression or a statement.</param>
/// <param name="dimensions">The dimension of each variable it mentions. The constants
/// <c>pi</c> and <c>e</c> and every number are pure numbers and need no entry.</param>
/// <returns>
/// <see cref="DimensionStatus.Consistent"/> with the dimension of the result — a
/// statement such as an equation is a pure number once its sides agree;
/// <see cref="DimensionStatus.Inconsistent"/> with the subexpression at fault and why;
/// or <see cref="DimensionStatus.Undetermined"/> where it could not be settled.
/// </returns>
/// <remarks>
/// <para>
/// A literal <c>0</c> or <c>oo</c> fits any dimension where it is added or compared,
/// since it is the same quantity in every unit: <c>v = 0</c> is meaningful.
/// </para>
/// <para>
/// Nothing is simplified first. <c>sin(x) - sin(x)</c> with <c>x</c> a length is
/// inconsistent, although it simplifies to <c>0</c>.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// using System;
/// using System.Collections.Generic;
/// using AngouriMath;
/// using AngouriMath.Core;
///
/// var acceleration = Dimension.Length / Dimension.Time.Pow(2);
/// var dims = new Dictionary&lt;Entity.Variable, Dimension&gt;
/// {
/// ["g"] = acceleration, ["h"] = Dimension.Length, ["t"] = Dimension.Time,
/// };
/// Console.WriteLine(MathS.Dimensions.Of("sqrt(2 g h)", dims));
/// Console.WriteLine(MathS.Dimensions.Of("h - g t^2 / 2 = 0", dims));
/// Console.WriteLine(MathS.Dimensions.Of("h + g t", dims));
/// </code>
/// Prints
/// <code>
/// Consistent: L T^-1
/// Consistent: 1
/// Inconsistent at h + g * t: h has dimension L and g * t has dimension L T^-1, so they cannot be added
/// </code>
/// </example>
public static DimensionCheck Of(Entity expr, IReadOnlyDictionary<Variable, Dimension> dimensions)
=> DimensionAnalysis.Of(expr, dimensions);

/// <summary>
/// A basis of the dimensionless products of powers of <paramref name="variables"/>:
/// the Buckingham π groups.
/// </summary>
/// <param name="variables">
/// The variables of the problem, each declared in <paramref name="dimensions"/>.
/// Their order chooses the basis: the earlier ones are the repeating variables, and
/// each later variable appears to the first power in exactly one group.
/// </param>
/// <param name="dimensions">The dimension of each variable.</param>
/// <returns>
/// One group per variable beyond the rank of the dimension matrix, each written with
/// the smallest whole powers and each checked to be a pure number before it is
/// returned. An empty list means no product of these variables is dimensionless.
/// </returns>
/// <remarks>
/// A model whose output depends on these variables depends on them only through
/// the groups, which is what makes a scale model of a ship mean anything, and what
/// reduces the parameters a simulation has to be tuned over.
/// </remarks>
/// <example>
/// <code>
/// using System;
/// using System.Collections.Generic;
/// using AngouriMath;
/// using AngouriMath.Core;
///
/// // Flow in a pipe: density, speed, diameter, viscosity.
/// var dims = new Dictionary&lt;Entity.Variable, Dimension&gt;
/// {
/// ["rho"] = Dimension.Mass / Dimension.Length.Pow(3),
/// ["v"] = Dimension.Length / Dimension.Time,
/// ["d"] = Dimension.Length,
/// ["mu"] = Dimension.Mass / (Dimension.Length * Dimension.Time),
/// };
/// foreach (var group in MathS.Dimensions.DimensionlessGroups(new Entity.Variable[] { "rho", "v", "d", "mu" }, dims))
/// Console.WriteLine(group);
/// </code>
/// Prints
/// <code>
/// mu / (rho * v * d)
/// </code>
/// </example>
public static IReadOnlyList<Entity> DimensionlessGroups(IReadOnlyList<Variable> variables, IReadOnlyDictionary<Variable, Dimension> dimensions)
=> AngouriMath.Functions.DimensionlessGroups.Of(variables, dimensions);
}
}
}
18 changes: 18 additions & 0 deletions Sources/AngouriMath/Core/Operations/Catalogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ internal static class Catalogue
"is_prime", "Whether an integer is prime.",
static n => n.IsPrime);

// ---------------------------------------------------------------- queries on physical dimensions

[ConstantField]
private static readonly Parameter TheDimensions = new("dimensions", "The physical dimension of each variable.");

/// <summary><see cref="MathS.Dimensions.Of(Entity, IReadOnlyDictionary{Variable, Dimension})"/>.</summary>
public static Query<Func<Entity, IReadOnlyDictionary<Variable, Dimension>, DimensionCheck>> Dimension { get; } = new(
"dimension", "The physical dimension of an expression from those of its variables, or the part where two dimensions disagree.",
static (e, dimensions) => MathS.Dimensions.Of(e, dimensions),
TheDimensions);

/// <summary><see cref="MathS.Dimensions.DimensionlessGroups(IReadOnlyList{Variable}, IReadOnlyDictionary{Variable, Dimension})"/>.</summary>
public static Query<Func<IReadOnlyList<Variable>, IReadOnlyDictionary<Variable, Dimension>, IReadOnlyList<Entity>>> DimensionlessGroups { get; } = new(
"dimensionless_groups", "The independent products of powers of variables that have no dimension: their Buckingham pi groups.",
static (variables, dimensions) => MathS.Dimensions.DimensionlessGroups(variables, dimensions),
TheDimensions);

// ---------------------------------------------------------------- the list

/// <summary>Every entry above, in the order written.</summary>
Expand All @@ -291,6 +308,7 @@ internal static class Catalogue
ToBase, FromBase,
Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct,
PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime,
Dimension, DimensionlessGroups,
];

/// <summary>The entry named <paramref name="name"/>, or <see langword="null"/> where there is none.</summary>
Expand Down
Loading
Loading