Skip to content

Commit d2eaa25

Browse files
Rafael-SOWNetclaude
andcommitted
Dimensional analysis reads every node type there is, and has a page
The walk named no node types added since it was written, and read any Function it did not name as a function of pure numbers: dot, cross, det, trace, transpose, a matrix entry and norm were each taken for transcendental. Every arm now names a concrete node type and the last one reports Undetermined, so a node added later is not read as a pure number by default. DimensionsTest.EveryNodeTypeHasADecidedReading lists the verdict on a sample of every concrete node type and fails, by name, on one it does not list. The new rules: dot and cross multiply dimensions; norm, trace, transpose and a matrix entry keep the entries' dimension; a matrix is read where its entries share one and is undetermined where they do not; det of a written n by n matrix is D^n; partialderivative divides by the slot's dimension; a product over 1..n of dimension D is D^n for written bounds; zeta, beta, the exponential and error integrals, W, polygamma, gcd/lcm and the rest of number theory, the connectives and iverson take pure numbers. A variable ranging over a set (a sum or product over a set, max/min, forall/exists, x in S, argmax, indexed unions) takes the dimension the set's members share; a number set fits a declared variable of any dimension and makes an undeclared one a pure number. A set on its own, lambda and application stay undetermined. Docs/Usage/Dimensions.md has the rules and the issue's examples, the water wave among them; its four samples pass docsamples. A BREAKING entry records the new entry point, as #2470 did for the shaders. Part of #2292. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b575525 commit d2eaa25

6 files changed

Lines changed: 722 additions & 46 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ are short, and a stale one is worse than none — if you change what a file desc
822822
| [`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) |
823823
| [`Docs/Usage/Exceptions.md`](Sources/AngouriMath/Docs/Usage/Exceptions.md) | the exception hierarchy under `AngouriMathBaseException` |
824824
| [`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 |
825+
| [`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 |
825826
| [`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 |
826827
| [`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 |
827828
| [`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 |

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,25 @@ both, and `curl` alone, are still variables ([#2182](https://github.com/asc-comm
579579

580580
---
581581

582+
### Dimensional analysis: `MathS.Dimensions.Of` and `DimensionlessGroups`
583+
584+
**Not silent: a new entry point.** `MathS.Dimensions.Of(expr, dimensions)` reads a formula against the physical
585+
dimension of each variable, a `Dimension` over the seven SI base dimensions passed in a map, and returns a
586+
`DimensionCheck`: `Consistent` with the dimension of the result, `Inconsistent` with the subexpression where two
587+
dimensions are added or compared or a quantity is passed to a function that takes a pure number, or `Undetermined`
588+
where a variable has no dimension or a node is not read. Nothing is simplified first. `MathS.Dimensions.DimensionlessGroups`
589+
gives the Buckingham π groups of a list of variables. The catalogue has them as `dimension` and `dimensionless_groups`,
590+
not yet offered by the MCP server or the terminal ([#2298](https://github.com/asc-community/AngouriMath/issues/2298)).
591+
Nothing that existed changes. See [`Docs/Usage/Dimensions.md`](Sources/AngouriMath/Docs/Usage/Dimensions.md)
592+
([#2292](https://github.com/asc-community/AngouriMath/issues/2292)).
593+
594+
| Input, with `g` an acceleration, `h` a length, `t` a time | Was (2.7.0) | Now |
595+
|---|---|---|
596+
| `MathS.Dimensions.Of("sqrt(2 g h)", dims).ToString()` | no such member | `Consistent: L T^-1` |
597+
| `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` |
598+
| `MathS.Dimensions.DimensionlessGroups(["h", "g", "t"], dims)` | no such member | `[g * t ^ 2 / h]` |
599+
600+
---
582601

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

‎Sources/AngouriMath/Convenience/MathS.Dimensions.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ partial class MathS
3535
/// <b>Undetermined is not a verdict.</b> An undeclared variable, or a node the check does
3636
/// not read, gives <see cref="DimensionStatus.Undetermined"/>, never a guess.
3737
/// </para>
38+
/// <para>
39+
/// The page <c>Docs/Usage/Dimensions.md</c> lists the rule for each kind of node and
40+
/// works the water-wave example through.
41+
/// </para>
3842
/// </remarks>
3943
public static class Dimensions
4044
{
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Dimensional analysis
2+
3+
`MathS.Dimensions.Of` reads a formula against the physical dimension of each of its variables and
4+
says whether it means anything: the dimension of the result, or the subexpression where two
5+
dimensions are added, compared or passed to a function that only takes a pure number.
6+
`MathS.Dimensions.DimensionlessGroups` gives the Buckingham π groups of a set of variables.
7+
8+
A dimension is a product of powers of the seven SI base dimensions, `L`, `M`, `T`, `I`, `Θ`, `N`
9+
and `J`, with rational exponents, and is written as `Dimension.Length / Dimension.Time.Pow(2)` or
10+
`Dimension.Create(1, 0, -2)`. It says what kind of quantity something is and nothing about its
11+
unit: checking values in metres and feet is what a units library is for. What a units library
12+
cannot do is look at `sqrt(2 g h)` and say whether it is a speed.
13+
14+
## Checking a formula
15+
16+
The dimensions are passed in a map from each variable to its dimension, not attached to the
17+
variables, so the same expression can be read under different assignments and nothing about how
18+
it prints, parses or simplifies changes. A body falling from a height `h`:
19+
20+
```cs
21+
using System.Collections.Generic;
22+
using AngouriMath.Core;
23+
24+
var dims = new Dictionary<Variable, Dimension>
25+
{
26+
["g"] = Dimension.Length / Dimension.Time.Pow(2),
27+
["h"] = Dimension.Length,
28+
["t"] = Dimension.Time,
29+
};
30+
Console.WriteLine(MathS.Dimensions.Of("sqrt(2 g h)", dims));
31+
Console.WriteLine(MathS.Dimensions.Of("h - g t^2 / 2 = 0", dims));
32+
Console.WriteLine(MathS.Dimensions.Of("h + g t", dims));
33+
```
34+
35+
Output:
36+
37+
```
38+
Consistent: L T^-1
39+
Consistent: 1
40+
Inconsistent at h + g * t: h has dimension L and g * t has dimension L T^-1, so they cannot be added
41+
```
42+
43+
The answer is a `DimensionCheck`: its `Status` is one of three, its `Dimension` is the dimension of
44+
the result where it is `Consistent`, and `At` and `Reason` say where and why it is not. A statement
45+
such as an equation is a truth value, a pure number written `1`, once the sides of every
46+
comparison in it agree.
47+
48+
**`Undetermined` is not a verdict.** A variable with no declared dimension, a dimensional quantity
49+
raised to a symbolic power, and a node the check does not read are each reported as undetermined,
50+
with the subexpression, rather than guessed at:
51+
52+
```cs
53+
using System.Collections.Generic;
54+
using AngouriMath.Core;
55+
56+
var dims = new Dictionary<Variable, Dimension>
57+
{
58+
["h"] = Dimension.Length,
59+
["n"] = Dimension.Dimensionless,
60+
};
61+
Console.WriteLine(MathS.Dimensions.Of("h + w", dims));
62+
Console.WriteLine(MathS.Dimensions.Of("h^n", dims));
63+
```
64+
65+
Output:
66+
67+
```
68+
Undetermined at w: no dimension is declared for w
69+
Undetermined at h ^ n: h has dimension L and is raised to n, whose value is not known
70+
```
71+
72+
## The rules
73+
74+
**Nothing is simplified first.** The check is about the formula as written: `sin(x) - sin(x)` with
75+
`x` a length is inconsistent, although it simplifies to `0`.
76+
77+
| | |
78+
|---|---|
79+
| `a + b`, `a - b`, `a mod b`, `max(a, b)`, `min(a, b)`, `a = b`, `a < b` | `a` and `b` have one dimension. A literal `0` or `oo` fits any, so `v = 0` is meaningful |
80+
| `a * b`, `a / b`, `dot(a, b)`, `cross(a, b)` | the dimensions multiply or divide |
81+
| `a ^ p` | `p` is a pure number; where `a` has a dimension, `p` is rational: `L^pi` is no dimension |
82+
| `abs`, `conj`, `re`, `im`, `norm(v, p)`, `trace`, `transpose`, an entry of a matrix | the dimension of the argument |
83+
| `sign`, `arg` | a pure number, whatever the argument measures |
84+
| `sin` and the other trigonometric functions, `exp` and `ln`, the hyperbolic functions, `erf`, `Ei`, `Si`, `Ci`, `li`, `gamma` and `n!`, `beta`, `zeta`, `polygamma`, `lambertw`, `floor`, `ceil`, `round`, the functions of number theory | each argument is a pure number, and so is the value |
85+
| `and`, `or`, `not`, `iverson(P)`, divisibility and congruence | each operand is a truth value or a pure number, and so is the result |
86+
| a matrix or vector | its entries share a dimension, which is the matrix's; a state vector `[x, v]` of mixed dimensions is undetermined |
87+
| `det(A)` of an `n` by `n` matrix | the entries' dimension to the `n`th power, undetermined where `n` is not written |
88+
| `derivative(f, x, n)`, `partialderivative(f, x, n)` | `f`'s dimension over `x`'s to the `n`th power |
89+
| `integral(f, x, a, b)` | `f`'s dimension times `x`'s, with `a` and `b` of `x`'s dimension |
90+
| `limit(f, x, a)` | `f`'s dimension, with `a` of `x`'s |
91+
| `sum(f, k, a, b)`, `product(f, k, a, b)` | the index counts terms and is a pure number unless declared otherwise; a product of `b - a + 1` terms of dimension `D` is `D^(b - a + 1)`, undetermined where that count is not a number |
92+
| `sum(f, x in S)`, `product(f, x in S)`, `max(f, x in S)`, `forall`, `exists`, `x in S` | a variable ranging over `S` takes the dimension `S`'s members share: `x in [0; l]` makes `x` a length. A number set, `RR` or `ZZ`, fits a declared variable of any dimension, since `l in RR` says a length is real, and makes an undeclared one a pure number |
93+
| `f provided P`, a piecewise | `P` is a consistent statement, and the cases share a dimension |
94+
| a set on its own, `lambda`, a function applied | undetermined: none of these is a quantity |
95+
96+
A node type the check has not been taught is undetermined too, never read as a pure number by
97+
default; a test in the suite lists the verdict on every node type there is and fails on a new one
98+
until someone decides what its dimension is.
99+
100+
## Buckingham π groups
101+
102+
A relation between `n` quantities whose dimension matrix has rank `r` can be written as a relation
103+
between `n - r` dimensionless products of them. `DimensionlessGroups` gives a basis of those
104+
products, each with the smallest whole powers, and checks each is a pure number before returning
105+
it. Flow in a pipe, with density, speed, diameter and viscosity, has one, the reciprocal of the
106+
Reynolds number:
107+
108+
```cs
109+
using System.Collections.Generic;
110+
using AngouriMath.Core;
111+
112+
var (L, M, T) = (Dimension.Length, Dimension.Mass, Dimension.Time);
113+
var pipe = new Dictionary<Variable, Dimension>
114+
{
115+
["rho"] = M / L.Pow(3), ["v"] = L / T, ["d"] = L, ["mu"] = M / (L * T),
116+
};
117+
foreach (var group in MathS.Dimensions.DimensionlessGroups(new Variable[] { "rho", "v", "d", "mu" }, pipe))
118+
Console.WriteLine(group);
119+
```
120+
121+
Output:
122+
123+
```
124+
mu / (rho * v * d)
125+
```
126+
127+
The order of the variables chooses the basis. The ones listed first recur across the groups, as a
128+
physicist's "repeating variables" do, and each later one appears to the first power in exactly
129+
one group.
130+
131+
## A worked example: water waves
132+
133+
The dispersion relation of waves on water of depth `h` is `ω² = g k tanh(k h)`, with `ω` the
134+
angular frequency and `k` the wave number. In deep water `tanh(k h)` is close to `1`, and in
135+
shallow water close to `k h`, which gives the shallow-water speed `ω / k = sqrt(g h)`. Writing
136+
`ω = sqrt(g k h)` for that, a factor of `k` short, is a typical slip, and the check finds it inside
137+
the phase of the wave it is used in:
138+
139+
```cs
140+
using System.Collections.Generic;
141+
using AngouriMath.Core;
142+
143+
var (L, T) = (Dimension.Length, Dimension.Time);
144+
var dims = new Dictionary<Variable, Dimension>
145+
{
146+
["omega"] = Dimension.Dimensionless / T, ["g"] = L / T.Pow(2), ["k"] = Dimension.Dimensionless / L,
147+
["h"] = L, ["A"] = L, ["x"] = L, ["t"] = T,
148+
};
149+
Console.WriteLine(MathS.Dimensions.Of("omega^2 = g k tanh(k h)", dims));
150+
Console.WriteLine(MathS.Dimensions.Of("A sin(k x - omega t)", dims));
151+
Console.WriteLine(MathS.Dimensions.Of("A sin(k x - sqrt(g k h) t)", dims));
152+
foreach (var group in MathS.Dimensions.DimensionlessGroups(new Variable[] { "k", "g", "A", "h", "t" }, dims))
153+
Console.WriteLine(group);
154+
```
155+
156+
Output:
157+
158+
```
159+
Consistent: 1
160+
Consistent: L
161+
Inconsistent at k * x - sqrt(g * k * h) * t: k * x has dimension 1 and sqrt(g * k * h) * t has dimension L^(1/2), so they cannot be subtracted
162+
k * A
163+
k * h
164+
k * g * t ^ 2
165+
```
166+
167+
The three groups say that the surface of a wave started from rest depends on the amplitude, the
168+
depth and the time only through `k A`, the steepness, `k h`, the depth in wavelengths, and
169+
`g k t²`, the time in units of the deep-water period. A simulation tuned over those three is tuned
170+
over every wave with the same three values.

0 commit comments

Comments
 (0)