Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PhysicalQuantity.cs
1using System;
2using System.Globalization;
3using System.Reflection;
8
10{
14 public sealed class PhysicalQuantity : FieldElement, IComparable, IPhysicalQuantity
15 {
19 public static readonly PhysicalQuantity ZeroElement = new PhysicalQuantity(0, Unit.Empty);
20
24 public static readonly PhysicalQuantity OneElement = new PhysicalQuantity(1, Unit.Empty);
25
26 private static readonly PhysicalQuantities associatedField = new PhysicalQuantities();
27
28 private double magnitude;
29 private Unit unit;
30
37 {
38 this.magnitude = Magnitude;
39 this.unit = Unit;
40 }
41
45 public double Magnitude
46 {
47 get => this.magnitude;
48 set => this.magnitude = value;
49 }
50
54 public Unit Unit
55 {
56 get => this.unit;
57 set => this.unit = value;
58 }
59
65
67 public override string ToString()
68 {
69 if (this.unit.IsEmpty)
70 return Expression.ToString(this.magnitude);
71 else
72 return Expression.ToString(this.magnitude) + " " + this.unit.ToString();
73 }
74
78 public override IField AssociatedField => associatedField;
79
83 public override object AssociatedObjectValue => this;
84
91 {
93 {
95 Unit Unit = Unit.Multiply(this.unit, Q.unit, out int ResidueExponential);
96 double Magnitude = this.magnitude * Q.magnitude;
97 if (ResidueExponential != 0)
98 Magnitude *= Math.Pow(10, ResidueExponential);
99
100 return new PhysicalQuantity(Magnitude, Unit);
101 }
102 else if (Element.AssociatedObjectValue is double d)
103 return new PhysicalQuantity(this.magnitude * d, this.unit);
104 else
105 return null;
106 }
107
112 public override IRingElement Invert()
113 {
114 Unit Unit = this.unit.Invert(out int ResidueExponential);
115 double Magnitude = 1 / this.magnitude;
116 if (ResidueExponential != 0)
117 Magnitude *= Math.Pow(10, ResidueExponential);
118
119 return new PhysicalQuantity(Magnitude, Unit);
120 }
121
128 {
130 {
132 if (Unit.TryConvert(Q.magnitude, Q.unit, this.unit, out double d))
133 return new PhysicalQuantity(this.magnitude + d, this.unit);
134 }
135 else if (Element.AssociatedObjectValue is double d)
136 return new PhysicalQuantity(this.magnitude + d, this.unit);
137
138 return null;
139 }
140
145 public override IGroupElement Negate()
146 {
147 return new PhysicalQuantity(-this.magnitude, this.unit);
148 }
149
151 public override bool Equals(object obj)
152 {
153 if (!(obj is PhysicalQuantity E))
154 return false;
155
156 if (this.unit.Equals(E.unit, true))
157 return this.magnitude == E.magnitude;
158 else
159 {
160 double m1 = this.magnitude;
161 this.unit.ToReferenceUnits(ref m1);
162
163 double m2 = E.magnitude;
164 E.unit.ToReferenceUnits(ref m2);
165
166 return m1 == m2;
167 }
168 }
169
171 public override int GetHashCode()
172 {
173 int Result = this.magnitude.GetHashCode();
174 Result ^= Result << 5 ^ this.unit.GetHashCode();
175 return Result;
176 }
177
182
187
194 public override bool TryConvertTo(Type DesiredType, out object Value)
195 {
196 if (DesiredType == typeof(byte))
197 {
198 if (this.magnitude >= byte.MinValue && this.magnitude <= byte.MaxValue)
199 {
200 Value = (byte)this.magnitude;
201 return true;
202 }
203 }
204 else if (DesiredType == typeof(decimal))
205 {
206 Value = (decimal)this.magnitude;
207 return true;
208 }
209 else if (DesiredType == typeof(double))
210 {
211 Value = (double)this.magnitude;
212 return true;
213 }
214 else if (DesiredType == typeof(short))
215 {
216 if (this.magnitude >= short.MinValue && this.magnitude <= short.MaxValue)
217 {
218 Value = (short)this.magnitude;
219 return true;
220 }
221 }
222 else if (DesiredType == typeof(int))
223 {
224 if (this.magnitude >= int.MinValue && this.magnitude <= int.MaxValue)
225 {
226 Value = (int)this.magnitude;
227 return true;
228 }
229 }
230 else if (DesiredType == typeof(long))
231 {
232 if (this.magnitude >= long.MinValue && this.magnitude <= long.MaxValue)
233 {
234 Value = (long)this.magnitude;
235 return true;
236 }
237 }
238 else if (DesiredType == typeof(sbyte))
239 {
240 if (this.magnitude >= sbyte.MinValue && this.magnitude <= sbyte.MaxValue)
241 {
242 Value = (sbyte)this.magnitude;
243 return true;
244 }
245 }
246 else if (DesiredType == typeof(float))
247 {
248 Value = (float)this.magnitude;
249 return true;
250 }
251 else if (DesiredType == typeof(ushort))
252 {
253 if (this.magnitude >= ushort.MinValue && this.magnitude <= ushort.MaxValue)
254 {
255 Value = (ushort)this.magnitude;
256 return true;
257 }
258 }
259 else if (DesiredType == typeof(uint))
260 {
261 if (this.magnitude >= uint.MinValue && this.magnitude <= uint.MaxValue)
262 {
263 Value = (uint)this.magnitude;
264 return true;
265 }
266 }
267 else if (DesiredType == typeof(ulong))
268 {
269 if (this.magnitude >= ulong.MinValue && this.magnitude <= ulong.MaxValue)
270 {
271 Value = (ulong)this.magnitude;
272 return true;
273 }
274 }
275 else if (DesiredType.IsAssignableFrom(typeof(PhysicalQuantity).GetTypeInfo()))
276 {
277 Value = this;
278 return true;
279 }
280
281 return Expression.TryConvert(this, DesiredType, true, out Value);
282 }
283
287 public int CompareTo(object obj)
288 {
289 if (!(obj is IPhysicalQuantity PQ))
290 throw new ScriptException("Values not comparable.");
291
293
294 if (this.unit.Equals(Q.unit, true))
295 return this.magnitude.CompareTo(Q.magnitude);
296
297 if (!Unit.TryConvert(Q.magnitude, Q.unit, this.unit, out double d))
298 throw new ScriptException("Values not comparable.");
299
300 return this.magnitude.CompareTo(d);
301 }
302
309 public static bool TryParse(string s, out PhysicalQuantity Value)
310 {
311 int i = s.Length - 1;
312
313 while (i >= 0 && char.IsWhiteSpace(s[i]))
314 i--;
315
316 int j = i;
317
318 while (i >= 0 && !char.IsWhiteSpace(s[i]))
319 i--;
320
321 if (i < 0 ||
322 !Unit.TryParse(s.Substring(i + 1, j - i), out Unit ParsedUnit) ||
323 !double.TryParse(s.Substring(0, i).Trim(), NumberStyles.None, CultureInfo.InvariantCulture, out double ParsedValue))
324 {
325 Value = null;
326 return false;
327 }
328
329 Value = new PhysicalQuantity(ParsedValue, ParsedUnit);
330
331 return true;
332 }
333 }
334}
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Base class for all types of field elements.
Definition: FieldElement.cs:10
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
Pseudo-field of physical quantities.
override IRingElement Invert()
Inverts the element, if possible.
PhysicalQuantity ToPhysicalQuantity()
Converts underlying object to a physical quantity.
override object AssociatedObjectValue
Associated object value.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override IField AssociatedField
Associated Field.
static readonly PhysicalQuantity ZeroElement
0
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
static bool TryParse(string s, out PhysicalQuantity Value)
Tries to parse a string to a physical quantity.
PhysicalQuantity(double Magnitude, Unit Unit)
Physical quantity.
override IGroupElement Negate()
Negates the element.
override ICommutativeRingElement Multiply(ICommutativeRingElement Element)
Tries to multiply an element to the current element.
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
static readonly PhysicalQuantity OneElement
1
override bool Equals(object obj)
Compares the element to another. If elements are equal.
int CompareTo(object obj)
IComparable.CompareTo
Represents a unit.
Definition: Unit.cs:16
static bool TryConvert(double From, Unit FromUnit, Unit ToUnit, out double To)
Tries to convert a magnitude in one unit to a magnitude in another.
Definition: Unit.cs:1216
override string ToString()
Definition: Unit.cs:535
bool IsEmpty
If the unit is empty. (A unit of only a prefix, but no factors, is not empty.)
Definition: Unit.cs:433
Unit Invert(out int ResidueExponent)
Inverts the unit.
Definition: Unit.cs:462
override bool Equals(object obj)
Definition: Unit.cs:474
static readonly Unit Empty
Empty unit.
Definition: Unit.cs:454
static Unit Multiply(Unit Left, Unit Right, out int ResidueExponent)
Multiplies two units with each other.
Definition: Unit.cs:635
override int GetHashCode()
Definition: Unit.cs:524
Unit ToReferenceUnits(ref double Magnitude)
Converts the unit to a series of reference unit factors. (Unrecognized units will be assumed to be re...
Definition: Unit.cs:822
static bool TryParse(string UnitString, out Unit Unit)
Tries to parse a string into a unit.
Definition: Unit.cs:120
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of ring elements.
Definition: IRingElement.cs:10
Basic interface for all types of fields.
Definition: IField.cs:9
Interface for objects that can be represented as a physical quantity.