|
| 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 PeterO.Numbers; |
| 10 | + |
| 11 | +namespace AngouriMath.Core |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// One of the seven base dimensions of the SI, in the order the SI brochure lists them. |
| 15 | + /// </summary> |
| 16 | + public enum BaseDimension |
| 17 | + { |
| 18 | + /// <summary>L, measured in metres.</summary> |
| 19 | + Length, |
| 20 | + /// <summary>M, measured in kilograms.</summary> |
| 21 | + Mass, |
| 22 | + /// <summary>T, measured in seconds.</summary> |
| 23 | + Time, |
| 24 | + /// <summary>I, measured in amperes.</summary> |
| 25 | + Current, |
| 26 | + /// <summary>Θ, measured in kelvins.</summary> |
| 27 | + Temperature, |
| 28 | + /// <summary>N, measured in moles.</summary> |
| 29 | + Amount, |
| 30 | + /// <summary>J, measured in candelas.</summary> |
| 31 | + LuminousIntensity, |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// A physical dimension: a product of powers of the seven <see cref="BaseDimension"/>s, |
| 36 | + /// such as <c>L T^-2</c> for an acceleration. |
| 37 | + /// </summary> |
| 38 | + /// <remarks> |
| 39 | + /// <para> |
| 40 | + /// The exponents are rationals, not integers, because a square root of a dimensional |
| 41 | + /// quantity is a legitimate intermediate: <c>sqrt(L)</c> appears inside the period of a |
| 42 | + /// pendulum before it is divided by <c>sqrt(L T^-2)</c>. |
| 43 | + /// </para> |
| 44 | + /// <para> |
| 45 | + /// A dimension says what kind of quantity something is and nothing about its unit: a |
| 46 | + /// metre and a foot are both <see cref="Length"/>. Units and their conversions are a |
| 47 | + /// separate concern, which UnitsNet and its kind already handle. |
| 48 | + /// </para> |
| 49 | + /// </remarks> |
| 50 | + public sealed class Dimension : IEquatable<Dimension> |
| 51 | + { |
| 52 | + private const int Count = 7; |
| 53 | + [ConstantField] private static readonly string[] symbols = { "L", "M", "T", "I", "Θ", "N", "J" }; |
| 54 | + private readonly ERational[] exponents; |
| 55 | + |
| 56 | + // PeterO keeps a rational as it was computed, 2/2 included; reducing here is what lets |
| 57 | + // ToString and IsIntegral read the numerator and denominator directly. |
| 58 | + private Dimension(ERational[] exponents) |
| 59 | + => this.exponents = exponents.Select(e => |
| 60 | + { |
| 61 | + var g = e.Numerator.Gcd(e.Denominator); |
| 62 | + return g.IsZero || g.Equals(EInteger.One) ? e : ERational.Create(e.Numerator / g, e.Denominator / g); |
| 63 | + }).ToArray(); |
| 64 | + |
| 65 | + private static Dimension Base(BaseDimension b) |
| 66 | + { |
| 67 | + var exponents = Enumerable.Repeat(ERational.Zero, Count).ToArray(); |
| 68 | + exponents[(int)b] = ERational.One; |
| 69 | + return new Dimension(exponents); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary>A pure number: a ratio, an angle in radians, a count.</summary> |
| 73 | + [ConstantField] public static readonly Dimension Dimensionless = new(Enumerable.Repeat(ERational.Zero, Count).ToArray()); |
| 74 | + /// <summary>L.</summary> |
| 75 | + [ConstantField] public static readonly Dimension Length = Base(BaseDimension.Length); |
| 76 | + /// <summary>M.</summary> |
| 77 | + [ConstantField] public static readonly Dimension Mass = Base(BaseDimension.Mass); |
| 78 | + /// <summary>T.</summary> |
| 79 | + [ConstantField] public static readonly Dimension Time = Base(BaseDimension.Time); |
| 80 | + /// <summary>I.</summary> |
| 81 | + [ConstantField] public static readonly Dimension Current = Base(BaseDimension.Current); |
| 82 | + /// <summary>Θ.</summary> |
| 83 | + [ConstantField] public static readonly Dimension Temperature = Base(BaseDimension.Temperature); |
| 84 | + /// <summary>N.</summary> |
| 85 | + [ConstantField] public static readonly Dimension Amount = Base(BaseDimension.Amount); |
| 86 | + /// <summary>J.</summary> |
| 87 | + [ConstantField] public static readonly Dimension LuminousIntensity = Base(BaseDimension.LuminousIntensity); |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// The dimension with the given exponent on each base dimension, in the order of |
| 91 | + /// <see cref="BaseDimension"/>; unlisted trailing exponents are zero. |
| 92 | + /// </summary> |
| 93 | + /// <example> |
| 94 | + /// <code> |
| 95 | + /// using System; |
| 96 | + /// using AngouriMath.Core; |
| 97 | + /// |
| 98 | + /// Console.WriteLine(Dimension.Create(1, 1, -2)); |
| 99 | + /// </code> |
| 100 | + /// Prints |
| 101 | + /// <code> |
| 102 | + /// L M T^-2 |
| 103 | + /// </code> |
| 104 | + /// </example> |
| 105 | + public static Dimension Create(params int[] exponents) |
| 106 | + { |
| 107 | + if (exponents.Length > Count) |
| 108 | + throw new ArgumentException($"There are {Count} base dimensions, and {exponents.Length} exponents were given.", nameof(exponents)); |
| 109 | + var e = Enumerable.Repeat(ERational.Zero, Count).ToArray(); |
| 110 | + for (var k = 0; k < exponents.Length; k++) |
| 111 | + e[k] = ERational.FromInt32(exponents[k]); |
| 112 | + return new Dimension(e); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary>The exponent of <paramref name="b"/>.</summary> |
| 116 | + public Entity.Number.Rational this[BaseDimension b] => Entity.Number.Rational.Create(exponents[(int)b]); |
| 117 | + |
| 118 | + /// <summary>Whether every exponent is zero.</summary> |
| 119 | + public bool IsDimensionless => exponents.All(e => e.IsZero); |
| 120 | + |
| 121 | + /// <summary>Whether every exponent is a whole number, as a unit system's base units need.</summary> |
| 122 | + public bool IsIntegral => exponents.All(e => e.Denominator.Equals(EInteger.One)); |
| 123 | + |
| 124 | + /// <summary>The product of two dimensions: exponents add.</summary> |
| 125 | + public static Dimension operator *(Dimension a, Dimension b) |
| 126 | + => new(a.exponents.Zip(b.exponents, (x, y) => x + y).ToArray()); |
| 127 | + |
| 128 | + /// <summary>The quotient of two dimensions: exponents subtract.</summary> |
| 129 | + public static Dimension operator /(Dimension a, Dimension b) |
| 130 | + => new(a.exponents.Zip(b.exponents, (x, y) => x - y).ToArray()); |
| 131 | + |
| 132 | + /// <summary>This dimension to a rational power: every exponent is multiplied by it.</summary> |
| 133 | + public Dimension Pow(Entity.Number.Rational power) => Pow(power.ERational); |
| 134 | + |
| 135 | + internal Dimension Pow(ERational power) => new(exponents.Select(e => e * power).ToArray()); |
| 136 | + |
| 137 | + internal ERational Exponent(int k) => exponents[k]; |
| 138 | + |
| 139 | + /// <inheritdoc/> |
| 140 | + public bool Equals(Dimension? other) |
| 141 | + => other is not null && exponents.Zip(other.exponents, (x, y) => x.CompareTo(y) == 0).All(eq => eq); |
| 142 | + |
| 143 | + /// <inheritdoc/> |
| 144 | + public override bool Equals(object? obj) => Equals(obj as Dimension); |
| 145 | + |
| 146 | + /// <inheritdoc/> |
| 147 | + public override int GetHashCode() |
| 148 | + => exponents.Aggregate(17, (h, e) => unchecked(h * 31 + e.Numerator.GetHashCode() * 7 + e.Denominator.GetHashCode())); |
| 149 | + |
| 150 | + /// <summary>Equality of every exponent.</summary> |
| 151 | + public static bool operator ==(Dimension? a, Dimension? b) => a is null ? b is null : a.Equals(b); |
| 152 | + |
| 153 | + /// <summary>Inequality of some exponent.</summary> |
| 154 | + public static bool operator !=(Dimension? a, Dimension? b) => !(a == b); |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// The base dimensions with nonzero exponents, in SI order, as <c>L M T^-2</c>; |
| 158 | + /// <c>1</c> for <see cref="Dimensionless"/>. |
| 159 | + /// </summary> |
| 160 | + public override string ToString() |
| 161 | + { |
| 162 | + var parts = new List<string>(); |
| 163 | + for (var k = 0; k < Count; k++) |
| 164 | + { |
| 165 | + var e = exponents[k]; |
| 166 | + if (e.IsZero) continue; |
| 167 | + parts.Add(e.CompareTo(ERational.One) == 0 ? symbols[k] |
| 168 | + : e.Denominator.Equals(EInteger.One) ? $"{symbols[k]}^{e.Numerator}" |
| 169 | + : $"{symbols[k]}^({e.Numerator}/{e.Denominator})"); |
| 170 | + } |
| 171 | + return parts.Count == 0 ? "1" : string.Join(" ", parts); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments