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.Reflection;
7
9{
13 public sealed class PhysicalQuantity : FieldElement, IComparable, IPhysicalQuantity
14 {
18 public static readonly PhysicalQuantity ZeroElement = new PhysicalQuantity(0, Unit.Empty);
19
23 public static readonly PhysicalQuantity OneElement = new PhysicalQuantity(1, Unit.Empty);
24
25 private static readonly PhysicalQuantities associatedField = new PhysicalQuantities();
26
27 private double magnitude;
28 private Unit unit;
29
36 {
37 this.magnitude = Magnitude;
38 this.unit = Unit;
39 }
40
44 public double Magnitude
45 {
46 get => this.magnitude;
47 set => this.magnitude = value;
48 }
49
53 public Unit Unit
54 {
55 get => this.unit;
56 set => this.unit = value;
57 }
58
64
66 public override string ToString()
67 {
68 if (this.unit.IsEmpty)
69 return Expression.ToString(this.magnitude);
70 else
71 return Expression.ToString(this.magnitude) + " " + this.unit.ToString();
72 }
73
77 public override IField AssociatedField => associatedField;
78
82 public override object AssociatedObjectValue => this;
83
90 {
92 {
94 Unit Unit = Unit.Multiply(this.unit, Q.unit, out int ResidueExponential);
95 double Magnitude = this.magnitude * Q.magnitude;
96 if (ResidueExponential != 0)
97 Magnitude *= Math.Pow(10, ResidueExponential);
98
99 return new PhysicalQuantity(Magnitude, Unit);
100 }
101 else if (Element.AssociatedObjectValue is double d)
102 return new PhysicalQuantity(this.magnitude * d, this.unit);
103 else
104 return null;
105 }
106
111 public override IRingElement Invert()
112 {
113 Unit Unit = this.unit.Invert(out int ResidueExponential);
114 double Magnitude = 1 / this.magnitude;
115 if (ResidueExponential != 0)
116 Magnitude *= Math.Pow(10, ResidueExponential);
117
118 return new PhysicalQuantity(Magnitude, Unit);
119 }
120
127 {
129 {
131 if (Unit.TryConvert(Q.magnitude, Q.unit, this.unit, out double d))
132 return new PhysicalQuantity(this.magnitude + d, this.unit);
133 }
134 else if (Element.AssociatedObjectValue is double d)
135 return new PhysicalQuantity(this.magnitude + d, this.unit);
136
137 return null;
138 }
139
144 public override IGroupElement Negate()
145 {
146 return new PhysicalQuantity(-this.magnitude, this.unit);
147 }
148
150 public override bool Equals(object obj)
151 {
152 if (!(obj is PhysicalQuantity E))
153 return false;
154
155 if (this.unit.Equals(E.unit, true))
156 return this.magnitude == E.magnitude;
157 else
158 {
159 double m1 = this.magnitude;
160 this.unit.ToReferenceUnits(ref m1);
161
162 double m2 = E.magnitude;
163 E.unit.ToReferenceUnits(ref m2);
164
165 return m1 == m2;
166 }
167 }
168
170 public override int GetHashCode()
171 {
172 int Result = this.magnitude.GetHashCode();
173 Result ^= Result << 5 ^ this.unit.GetHashCode();
174 return Result;
175 }
176
181
186
193 public override bool TryConvertTo(Type DesiredType, out object Value)
194 {
195 if (DesiredType == typeof(byte))
196 {
197 if (this.magnitude >= byte.MinValue && this.magnitude <= byte.MaxValue)
198 {
199 Value = (byte)this.magnitude;
200 return true;
201 }
202 }
203 else if (DesiredType == typeof(decimal))
204 {
205 Value = (decimal)this.magnitude;
206 return true;
207 }
208 else if (DesiredType == typeof(double))
209 {
210 Value = (double)this.magnitude;
211 return true;
212 }
213 else if (DesiredType == typeof(short))
214 {
215 if (this.magnitude >= short.MinValue && this.magnitude <= short.MaxValue)
216 {
217 Value = (short)this.magnitude;
218 return true;
219 }
220 }
221 else if (DesiredType == typeof(int))
222 {
223 if (this.magnitude >= int.MinValue && this.magnitude <= int.MaxValue)
224 {
225 Value = (int)this.magnitude;
226 return true;
227 }
228 }
229 else if (DesiredType == typeof(long))
230 {
231 if (this.magnitude >= long.MinValue && this.magnitude <= long.MaxValue)
232 {
233 Value = (long)this.magnitude;
234 return true;
235 }
236 }
237 else if (DesiredType == typeof(sbyte))
238 {
239 if (this.magnitude >= sbyte.MinValue && this.magnitude <= sbyte.MaxValue)
240 {
241 Value = (sbyte)this.magnitude;
242 return true;
243 }
244 }
245 else if (DesiredType == typeof(float))
246 {
247 Value = (float)this.magnitude;
248 return true;
249 }
250 else if (DesiredType == typeof(ushort))
251 {
252 if (this.magnitude >= ushort.MinValue && this.magnitude <= ushort.MaxValue)
253 {
254 Value = (ushort)this.magnitude;
255 return true;
256 }
257 }
258 else if (DesiredType == typeof(uint))
259 {
260 if (this.magnitude >= uint.MinValue && this.magnitude <= uint.MaxValue)
261 {
262 Value = (uint)this.magnitude;
263 return true;
264 }
265 }
266 else if (DesiredType == typeof(ulong))
267 {
268 if (this.magnitude >= ulong.MinValue && this.magnitude <= ulong.MaxValue)
269 {
270 Value = (ulong)this.magnitude;
271 return true;
272 }
273 }
274 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(PhysicalQuantity).GetTypeInfo()))
275 {
276 Value = this;
277 return true;
278 }
279
280 return Expression.TryConvert(this, DesiredType, out Value);
281 }
282
286 public int CompareTo(object obj)
287 {
288 if (!(obj is IPhysicalQuantity PQ))
289 throw new ScriptException("Values not comparable.");
290
292
293 if (this.unit.Equals(Q.unit, true))
294 return this.magnitude.CompareTo(Q.magnitude);
295
296 if (!Unit.TryConvert(Q.magnitude, Q.unit, this.unit, out double d))
297 throw new ScriptException("Values not comparable.");
298
299 return this.magnitude.CompareTo(d);
300 }
301
308 public static bool TryParse(string s, out PhysicalQuantity Value)
309 {
310 int i = s.Length - 1;
311
312 while (i >= 0 && char.IsWhiteSpace(s[i]))
313 i--;
314
315 int j = i;
316
317 while (i >= 0 && !char.IsWhiteSpace(s[i]))
318 i--;
319
320 if (i < 0 ||
321 !Unit.TryParse(s.Substring(i + 1, j - i), out Unit ParsedUnit) ||
322 !double.TryParse(s.Substring(0, i).Trim().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "."), out double ParsedValue))
323 {
324 Value = null;
325 return false;
326 }
327
328 Value = new PhysicalQuantity(ParsedValue, ParsedUnit);
329
330 return true;
331 }
332 }
333}
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
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:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
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:15
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:1201
override string ToString()
Definition: Unit.cs:517
bool IsEmpty
If the unit is empty. (A unit of only a prefix, but no factors, is not empty.)
Definition: Unit.cs:415
Unit Invert(out int ResidueExponent)
Inverts the unit.
Definition: Unit.cs:444
override bool Equals(object obj)
Definition: Unit.cs:456
static readonly Unit Empty
Empty unit.
Definition: Unit.cs:436
static Unit Multiply(Unit Left, Unit Right, out int ResidueExponent)
Multiplies two units with each other.
Definition: Unit.cs:617
override int GetHashCode()
Definition: Unit.cs:506
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:807
static bool TryParse(string UnitString, out Unit Unit)
Tries to parse a string into a unit.
Definition: Unit.cs:137
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.