Skip to content

Commit

Permalink
#368 - Limit types that can be used as Parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 8, 2020
1 parent 9324c1c commit 5c5582f
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 155 deletions.
139 changes: 12 additions & 127 deletions xFunc.Maths/Expressions/Collections/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

using System;
using System.Globalization;
using System.Numerics;
using xFunc.Maths.Expressions.Angles;
using xFunc.Maths.Expressions.Matrices;
using xFunc.Maths.Resources;
using Vector = xFunc.Maths.Expressions.Matrices.Vector;

namespace xFunc.Maths.Expressions.Collections
{
Expand All @@ -28,92 +24,15 @@ namespace xFunc.Maths.Expressions.Collections
/// </summary>
public class Parameter : IComparable<Parameter>, IEquatable<Parameter>
{
private object value = default!;
private ParameterValue value;

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, double value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, NumberValue value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, AngleValue value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, Complex value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, bool value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, Vector value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
public Parameter(string key, Matrix value, ParameterType type = ParameterType.Normal)
: this(key, (object)value, type)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Parameter" /> class.
/// </summary>
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <param name="type">The type of parameter.</param>
internal Parameter(string key, object value, ParameterType type = ParameterType.Normal)
public Parameter(string key, ParameterValue value, ParameterType type = ParameterType.Normal)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -129,17 +48,16 @@ internal Parameter(string key, object value, ParameterType type = ParameterType.
/// <param name="key">The name of parameter.</param>
/// <param name="value">The value of parameter.</param>
/// <returns>A constant.</returns>
public static Parameter CreateConstant(string key, object value)
public static Parameter Constant(string key, ParameterValue value)
=> new Parameter(key, value, ParameterType.Constant);

/// <inheritdoc />
public override bool Equals(object? obj)
{
var param = obj as Parameter;
if (param is null)
return false;
if (obj is Parameter param)
return Equals(param);

return Equals(param);
return false;
}

/// <inheritdoc />
Expand All @@ -148,7 +66,7 @@ public override int GetHashCode()

/// <inheritdoc />
public override string ToString()
=> string.Format(CultureInfo.InvariantCulture, "{0}: {1} ({2})", Key, value, Type);
=> string.Format(CultureInfo.InvariantCulture, "{0}: {1} ({2})", Key, value.Value, Type);

/// <inheritdoc />
public int CompareTo(Parameter? other)
Expand All @@ -173,12 +91,7 @@ public bool Equals(Parameter? other)
/// <param name="right">The right parameter.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator ==(Parameter? left, Parameter? right)
{
if (left is null)
return right is null;

return left.Equals(right);
}
=> Equals(left, right);

/// <summary>
/// Indicates whether <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter.
Expand All @@ -187,7 +100,7 @@ public bool Equals(Parameter? other)
/// <param name="right">The right parameter.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator !=(Parameter? left, Parameter? right)
=> !(left == right);
=> !Equals(left, right);

/// <summary>
/// Indicates whether <paramref name="left"/> parameter is greater than the <paramref name="right"/> parameter.
Expand Down Expand Up @@ -243,46 +156,18 @@ public bool Equals(Parameter? other)
/// <summary>
/// Gets or sets the value of parameter.
/// </summary>
public object Value
public ParameterValue Value
{
get
{
return value;
}
get => value;
set
{
if (value is null)
throw new ArgumentNullException(nameof(value));

if (Type != ParameterType.Normal)
throw new ParameterIsReadOnlyException(Resource.ReadOnlyError, Key);

if (IsNumber(value))
{
var d = Convert.ToDouble(value, CultureInfo.InvariantCulture);

this.value = new NumberValue(d);
}
else
{
this.value = value;
}
this.value = value;
}
}

private static bool IsNumber(object value) =>
value is sbyte ||
value is byte ||
value is short ||
value is ushort ||
value is int ||
value is uint ||
value is long ||
value is ulong ||
value is float ||
value is double ||
value is decimal;

/// <summary>
/// Gets the type of parameter.
/// </summary>
Expand Down
37 changes: 17 additions & 20 deletions xFunc.Maths/Expressions/Collections/ParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,46 +101,46 @@ protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args
private void InitializeConstants()
{
// Archimedes' constant
AddConstant(Parameter.CreateConstant("π", AngleValue.Radian(Math.PI)));
AddConstant(Parameter.Constant("π", AngleValue.Radian(Math.PI)));

// Archimedes' constant
AddConstant(Parameter.CreateConstant("pi", AngleValue.Radian(Math.PI)));
AddConstant(Parameter.Constant("pi", AngleValue.Radian(Math.PI)));

// Euler's number
AddConstant(Parameter.CreateConstant("e", Math.E));
AddConstant(Parameter.Constant("e", Math.E));

// Imaginary unit
AddConstant(Parameter.CreateConstant("i", Complex.ImaginaryOne));
AddConstant(Parameter.Constant("i", Complex.ImaginaryOne));

// Gravity on Earth
AddConstant(Parameter.CreateConstant("g", 9.80665));
AddConstant(Parameter.Constant("g", 9.80665));

// Speed of Light (c0)
AddConstant(Parameter.CreateConstant("c", 299792458));
AddConstant(Parameter.Constant("c", 299792458));

// Planck Constant
AddConstant(Parameter.CreateConstant("h", 6.62607004E-34));
AddConstant(Parameter.Constant("h", 6.62607004E-34));

// Faraday Constant
AddConstant(Parameter.CreateConstant("F", 96485.33289));
AddConstant(Parameter.Constant("F", 96485.33289));

// Electric Constant (ε0)
AddConstant(Parameter.CreateConstant("ε", 8.854187817E-12));
AddConstant(Parameter.Constant("ε", 8.854187817E-12));

// Magnetic constant (µ0)
AddConstant(Parameter.CreateConstant("µ", 1.2566370614E-6));
AddConstant(Parameter.Constant("µ", 1.2566370614E-6));

// Gravitational constant
AddConstant(Parameter.CreateConstant("G", 6.64078E-11));
AddConstant(Parameter.Constant("G", 6.64078E-11));

// Feigenbaum constant
AddConstant(Parameter.CreateConstant("α", 2.5029078750958928222839));
AddConstant(Parameter.Constant("α", 2.5029078750958928222839));

// Stefan-Boltzmann constant
AddConstant(Parameter.CreateConstant("σ", 5.670367E-8));
AddConstant(Parameter.Constant("σ", 5.670367E-8));

// Euler–Mascheroni constant
AddConstant(Parameter.CreateConstant("γ", 0.57721566490153286060651));
AddConstant(Parameter.Constant("γ", 0.57721566490153286060651));
}

/// <inheritdoc />
Expand All @@ -164,12 +164,9 @@ public IEnumerator<Parameter> GetEnumerator()
/// </value>
/// <param name="key">The name of variable.</param>
/// <returns>The value of variable.</returns>
public object this[string key]
public ParameterValue this[string key]
{
get
{
return GetParameterByKey(key).Value;
}
get => GetParameterByKey(key).Value;
set
{
if (!collection.TryGetValue(key, out var param))
Expand Down Expand Up @@ -221,7 +218,7 @@ public void Add(Parameter param)
/// </summary>
/// <param name="key">The name of variable.</param>
/// <param name="value">The value of variable.</param>
public void Add(string key, object value)
public void Add(string key, ParameterValue value)
=> Add(new Parameter(key, value));

/// <summary>
Expand Down

0 comments on commit 5c5582f

Please sign in to comment.