Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Numerics/Data/Interpolation/CubicSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;

namespace Numerics.Data
{
/// <summary>
Expand Down Expand Up @@ -78,7 +75,7 @@ public CubicSpline(IList<double> xValues, IList<double> yValues, SortOrder sortO
/// <summary>
/// Stores the array of second derivatives.
/// </summary>
private double[] y2;
private double[] y2 = Array.Empty<double>();

/// <summary>
/// Auxiliary routine to set the second derivatives. If you make changes to the x- or y-values, then you need to call this routine afterwards.
Expand Down
22 changes: 13 additions & 9 deletions Numerics/Data/Paired Data/OrderedPairedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using Numerics.Distributions;

namespace Numerics.Data
Expand Down Expand Up @@ -102,7 +103,7 @@ public class OrderedPairedData : IList<Ordinate>, INotifyCollectionChanged
private bool _strictY;
private SortOrder _orderX;
private SortOrder _orderY;
private List<Ordinate> _ordinates;
private readonly List<Ordinate> _ordinates;

/// <inheritdoc/>
public event NotifyCollectionChangedEventHandler? CollectionChanged;
Expand Down Expand Up @@ -265,20 +266,24 @@ public OrderedPairedData(XElement el)
{
// Get Strictness
bool strict = false;
if (el.Attribute(nameof(StrictX)) != null) { bool.TryParse(el.Attribute(nameof(StrictX)).Value, out strict); }
var strictXAttr = el.Attribute(nameof(StrictX));
if (strictXAttr != null) { bool.TryParse(strictXAttr.Value, out strict); }
StrictX = strict;

strict = false;
if (el.Attribute(nameof(StrictY)) != null) { bool.TryParse(el.Attribute(nameof(StrictY)).Value, out strict); }
var strictYAttr = el.Attribute(nameof(StrictY));
if (strictYAttr != null) { bool.TryParse(strictYAttr.Value, out strict); }
StrictY = strict;

// Get Order
SortOrder order = SortOrder.None;
if (el.Attribute(nameof(OrderX)) != null) { Enum.TryParse(el.Attribute(nameof(OrderX)).Value, out order); }
var orderXAttr = el.Attribute(nameof(OrderX));
if (orderXAttr != null) { Enum.TryParse(orderXAttr.Value, out order); }
OrderX = order;

order = SortOrder.None;
if (el.Attribute(nameof(OrderY)) != null) { Enum.TryParse(el.Attribute(nameof(OrderY)).Value, out order); }
var orderYAttr = el.Attribute(nameof(OrderY));
if (orderYAttr != null) { Enum.TryParse(orderYAttr.Value, out order); }
OrderY = order;

// Ordinates
Expand Down Expand Up @@ -1432,15 +1437,14 @@ private double TriangleArea(Ordinate point1, Ordinate point2, Ordinate point3)
/// and number of points in the search region.</returns>
public OrderedPairedData LangSimplify(double tolerance, int lookAhead)
{
if (_ordinates == null | lookAhead <= 1 | tolerance <= 0)
return this;
if (lookAhead <= 1 | tolerance <= 0) { return this; }

List<Ordinate> ordinates = new List<Ordinate>();

int count = _ordinates.Count;
int offset;
if (lookAhead > count - 1)
lookAhead = count - 1;
if (lookAhead > count - 1) { lookAhead = count - 1; }

ordinates.Add(_ordinates[0]);

for (int i = 0; i < count; i++)
Expand Down
7 changes: 4 additions & 3 deletions Numerics/Data/Paired Data/Ordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public Ordinate(double xValue, double yValue)
public Ordinate(XElement xElement)
{
double x = 0, y = 0;

if (xElement.Attribute(nameof(X)) != null) double.TryParse(xElement.Attribute(nameof(X))?.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
if (xElement.Attribute(nameof(Y)) != null) double.TryParse(xElement.Attribute(nameof(Y))?.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out y);
var xAttribute = xElement.Attribute(nameof(X));
var yAttribute = xElement.Attribute(nameof(Y));
if (xAttribute != null) double.TryParse(xAttribute.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
if (yAttribute != null) double.TryParse(yAttribute.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out y);
X = x;
Y = y;
IsValid = true;
Expand Down
81 changes: 56 additions & 25 deletions Numerics/Data/Paired Data/UncertainOrderedPairedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
Expand Down Expand Up @@ -171,7 +172,7 @@ public SortOrder OrderY
/// <summary>
/// Handles the event of CollectionChanged
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event NotifyCollectionChangedEventHandler? CollectionChanged;

#endregion

Expand Down Expand Up @@ -243,7 +244,12 @@ public UncertainOrderedPairedData(IList<UncertainOrdinate> data, bool strictOnX,
_orderY = yOrder;
_uncertainOrdinates = new List<UncertainOrdinate>(data.Count);
for (int i = 0; i < data.Count; i++)
_uncertainOrdinates.Add(new UncertainOrdinate(data[i].X, data[i].Y.Clone()));
{
var o = data[i];
UnivariateDistributionBase? yValue = o.Y?.Clone();
if (yValue is not null) { _uncertainOrdinates.Add(new UncertainOrdinate(o.X, yValue)); }
}

Validate();
}

Expand All @@ -266,8 +272,12 @@ private UncertainOrderedPairedData(IList<UncertainOrdinate> data, bool strictOnX
_orderY = yOrder;
_uncertainOrdinates = new List<UncertainOrdinate>(data.Count);
for (int i = 0; i < data.Count; i++)
_uncertainOrdinates.Add(new UncertainOrdinate(data[i].X, data[i].Y.Clone()));

{
var o = data[i];
UnivariateDistributionBase? yValue = o.Y?.Clone();
if (yValue is not null) { _uncertainOrdinates.Add(new UncertainOrdinate(o.X, yValue)); }
}

_isValid = dataValid;
}

Expand All @@ -277,34 +287,44 @@ private UncertainOrderedPairedData(IList<UncertainOrdinate> data, bool strictOnX
/// <param name="el">The XElement the UncertainOrderPairedData object is being created from.</param>
public UncertainOrderedPairedData(XElement el)
{
var strictX = el.Attribute("X_Strict");
// Get Order
if (el.Attribute("X_Strict") != null)
bool.TryParse(el.Attribute("X_Strict").Value, out _strictX);
if (el.Attribute("Y_Strict") != null)
bool.TryParse(el.Attribute("Y_Strict").Value, out _strictY);
if (strictX != null) { bool.TryParse(strictX.Value, out _strictX); }

var strictY = el.Attribute("Y_Strict");
if (strictY != null) { bool.TryParse(strictY.Value, out _strictY); }

// Get Strictness
if (el.Attribute("X_Order") != null)
Enum.TryParse(el.Attribute("X_Order").Value, out _orderX);
if (el.Attribute("Y_Order") != null)
Enum.TryParse(el.Attribute("Y_Order").Value, out _orderY);
var orderX = el.Attribute("X_Order");
if (orderX != null) { Enum.TryParse(orderX.Value, out _orderX); }

var orderY = el.Attribute("Y_Order");
if (orderY != null) { Enum.TryParse(orderY.Value, out _orderY); }

// Distribution type
Distribution = UnivariateDistributionType.Deterministic;
if (el.Attribute("Distribution") != null)
var distributionAttr = el.Attribute("Distribution");
if (distributionAttr != null)
{
var argresult = Distribution;
Enum.TryParse(el.Attribute("Distribution").Value, out argresult);
Enum.TryParse(distributionAttr.Value, out argresult);
Distribution = argresult;
}
// new prop

if (el.Attribute(nameof(AllowDifferentDistributionTypes)) != null)
var allowDiffAtr = el.Attribute(nameof(AllowDifferentDistributionTypes));
if (allowDiffAtr != null)
{
bool.TryParse(el.Attribute(nameof(AllowDifferentDistributionTypes)).Value, out _allowDifferentDistributionTypes);
bool.TryParse(allowDiffAtr.Value, out _allowDifferentDistributionTypes);
// Get Ordinates
var curveEl = el.Element("Ordinates");
_uncertainOrdinates = new List<UncertainOrdinate>();
foreach (XElement ord in curveEl.Elements(nameof(UncertainOrdinate)))
_uncertainOrdinates.Add(new UncertainOrdinate(ord));

if (curveEl != null)
{
foreach (XElement ord in curveEl.Elements(nameof(UncertainOrdinate)))
_uncertainOrdinates.Add(new UncertainOrdinate(ord));
}

}
else
{
Expand All @@ -315,15 +335,19 @@ public UncertainOrderedPairedData(XElement el)
{
foreach (XElement o in curveEl.Elements("Ordinate"))
{
double.TryParse(o.Attribute("X").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var xout);
xData.Add(xout);
var xAttr = o.Attribute("X");
if ( xAttr != null && double.TryParse(xAttr.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var xout)) { xData.Add(xout); }
else { xData.Add(0.0); }

var dist = UnivariateDistributionFactory.CreateDistribution(Distribution);
var props = dist.GetParameterPropertyNames;
var paramVals = new double[(props.Count())];

for (int i = 0; i < props.Count(); i++)
{
double.TryParse(o.Attribute(props[i]).Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result);
paramVals[i] = result;
var pAttr = o.Attribute(props[i]);
if ( pAttr != null && double.TryParse(pAttr.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) { paramVals[i] = result; }
else { paramVals[i] = 0.0; }
}

dist.SetParameters(paramVals);
Expand Down Expand Up @@ -488,7 +512,14 @@ public List<string> GetErrors()
{
if (left._uncertainOrdinates[i].X != right._uncertainOrdinates[i].X)
return false;
if (left._uncertainOrdinates[i].Y == right._uncertainOrdinates[i].Y == false)

var leftY = left._uncertainOrdinates[i].Y;
var rightY = right._uncertainOrdinates[i].Y;
if (leftY is null && rightY is null)
continue;
if (leftY is null || rightY is null)
return false;
if (!leftY.Equals(rightY))
return false;
}
return true;
Expand All @@ -510,7 +541,7 @@ public List<string> GetErrors()
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns>True if the specified object is equal to the current object; otherwise, False.</returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is UncertainOrderedPairedData other)
{
Expand Down
Loading