Interpolation with test done

This commit is contained in:
Иван Ивашкин
2024-10-31 17:35:31 +05:00
parent 96b0fbd6a3
commit bcea9639f6
12 changed files with 130 additions and 24 deletions

View File

@@ -102,7 +102,7 @@
</Grid> </Grid>
<lvc:CartesianChart Grid.Row="0" Grid.Column="1" <lvc:CartesianChart Grid.Row="0" Grid.Column="1"
Series="{Binding SeriesCollection, UpdateSourceTrigger=PropertyChanged}" Margin="5" Series="{Binding SeriesCollection, UpdateSourceTrigger=PropertyChanged}" Margin="5"
Zoom="None"> Zoom="Xy">
<lvc:CartesianChart.AxisY> <lvc:CartesianChart.AxisY>
<lvc:Axis Title="Y"></lvc:Axis> <lvc:Axis Title="Y"></lvc:Axis>
</lvc:CartesianChart.AxisY> </lvc:CartesianChart.AxisY>

View File

@@ -97,8 +97,12 @@ namespace StructureHelper.Windows.MainGraph
f1.Name = "Табличная системная функция"; f1.Name = "Табличная системная функция";
f1.Table = new List<GraphPoint>() f1.Table = new List<GraphPoint>()
{ {
new GraphPoint(1, 0), new GraphPoint(1, 1),
new GraphPoint(0, 1), new GraphPoint(2, 2),
new GraphPoint(3, 3),
new GraphPoint(4, 4),
new GraphPoint(5, 5),
new GraphPoint(6, 6),
}; };
f1.IsUser = false; f1.IsUser = false;
f1.Description = "Описание табличной системной функции"; f1.Description = "Описание табличной системной функции";

View File

@@ -107,7 +107,7 @@ namespace StructureHelper.Windows.MainGraph
Function.Name = Name; Function.Name = Name;
Function.Description = Description; Function.Description = Description;
Function.IsUser = true; Function.IsUser = true;
(Function as TableFunction).Table = Table.ToList(); (Function as TableFunction).Table = Table.OrderBy(x => x.X).ToList();
var window = parameter as Window; var window = parameter as Window;
window.DialogResult = true; window.DialogResult = true;
window.Close(); window.Close();

View File

@@ -78,7 +78,7 @@
</Grid> </Grid>
<lvc:CartesianChart Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" <lvc:CartesianChart Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"
Series="{Binding SeriesCollection}" Margin="5" Series="{Binding SeriesCollection}" Margin="5"
Zoom="None"> Zoom="Xy">
<lvc:CartesianChart.AxisY> <lvc:CartesianChart.AxisY>
<lvc:Axis Title="Y"></lvc:Axis> <lvc:Axis Title="Y"></lvc:Axis>
</lvc:CartesianChart.AxisY> </lvc:CartesianChart.AxisY>

View File

@@ -1,25 +1,61 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1"> <ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="StructureHelperCommon.Models.Functions.TableFunction" BaseTypeListCollapsed="true"> <Class Name="StructureHelperCommon.Models.Functions.TableFunction" BaseTypeListCollapsed="true">
<Position X="3" Y="5.75" Width="1.5" /> <Position X="3.25" Y="8" Width="1.5" />
<TypeIdentifier> <TypeIdentifier>
<HashCode>BAACgEgAAAAgACIAAAAAQAYAAAAMAAAAAQAAAAAAgBA=</HashCode> <HashCode>BAACgEgAAAAgBCIAAAAAQAYAAAAMAAAAAQAAgAAAgBA=</HashCode>
<FileName>Models\Functions\TableFunction.cs</FileName> <FileName>Models\Functions\TableFunction.cs</FileName>
</TypeIdentifier> </TypeIdentifier>
<Lollipop Position="0.2" /> <Lollipop Position="0.2" />
</Class> </Class>
<Class Name="StructureHelperCommon.Models.Functions.FormulaFunction" BaseTypeListCollapsed="true"> <Class Name="StructureHelperCommon.Models.Functions.FormulaFunction" BaseTypeListCollapsed="true">
<Position X="1.25" Y="5.75" Width="1.5" /> <Position X="1" Y="8" Width="1.5" />
<TypeIdentifier> <TypeIdentifier>
<HashCode>BAACgEgAAAAgACJAAgAAQAYAAAAIBAAAIQAAAAAAgBA=</HashCode> <HashCode>BAACgEgAAAAgBCIAAAAAAAYAAAAIBAAAIQAAgAAAgBA=</HashCode>
<FileName>Models\Functions\FormulaFunction.cs</FileName> <FileName>Models\Functions\FormulaFunction.cs</FileName>
</TypeIdentifier> </TypeIdentifier>
<Lollipop Position="0.2" /> <Lollipop Position="0.2" />
</Class> </Class>
<Interface Name="StructureHelperCommon.Infrastructures.Interfaces.IOneVariableFunction"> <Class Name="StructureHelperCommon.Models.Functions.Decorator.ScaleYDecorator">
<Position X="2" Y="0.5" Width="1.5" /> <Position X="7.25" Y="5.75" Width="1.5" />
<TypeIdentifier> <TypeIdentifier>
<HashCode>BAAAgEgAAAAgACAAAAAAQAYAAAAIAAAAAQAAAAAAgAA=</HashCode> <HashCode>AAAAAAgAAAAARAAAAAAAAAAAAAAIAAAAAAAAAAAAAAA=</HashCode>
<FileName>Models\Functions\Decorator\ScaleYDecorator.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="StructureHelperCommon.Models.Functions.Decorator.ScaleXDecorator">
<Position X="5.5" Y="5.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAgAAAAARAAAAAAAAAAAAAAIAAAAAAAAAAAAAAA=</HashCode>
<FileName>Models\Functions\Decorator\ScaleXDecorator.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="StructureHelperCommon.Models.Functions.Decorator.LimYDecorator">
<Position X="9.25" Y="5.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAQgAAAAABAAAAAAAAAAAAAAIAAAAAAAAAAAAAAg=</HashCode>
<FileName>Models\Functions\Decorator\LimYDecorator.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="StructureHelperCommon.Models.Functions.Decorator.LimXDecorator">
<Position X="11.5" Y="5.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAgAAgAAAAABAAAAAAAAAAAAAAIAAAAAAAAEAAAAAA=</HashCode>
<FileName>Models\Functions\Decorator\LimXDecorator.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="StructureHelperCommon.Infrastructures.Interfaces.FunctionDecorator" Collapsed="true">
<Position X="8.25" Y="2.75" Width="1.5" />
<TypeIdentifier>
<HashCode>BAACgEgAAAAgBAAAAgAAQAYAAAAIAAAAAQAAgAAAgBA=</HashCode>
<FileName>Infrastructures\Interfaces\FunctionDecorator.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Interface Name="StructureHelperCommon.Infrastructures.Interfaces.IOneVariableFunction">
<Position X="2.25" Y="2.75" Width="1.5" />
<TypeIdentifier>
<HashCode>BAAAgEgAAAAgBCAAAAAAAAYAAAAIAAAAAQAAgAAAgAA=</HashCode>
<FileName>Infrastructures\Interfaces\IOneVariableFunction.cs</FileName> <FileName>Infrastructures\Interfaces\IOneVariableFunction.cs</FileName>
</TypeIdentifier> </TypeIdentifier>
</Interface> </Interface>

View File

@@ -35,6 +35,9 @@ namespace StructureHelperCommon.Models.Functions.Decorator
{ {
var graphSettings = base.GetGraphSettings(); var graphSettings = base.GetGraphSettings();
var graphLimitGraphPoint = new List<GraphPoint>(); var graphLimitGraphPoint = new List<GraphPoint>();
var leftPoint = new GraphPoint(leftBound, GetByX(leftBound));
var rightPoint = new GraphPoint(rightBound, GetByX(rightBound));
graphLimitGraphPoint.Add(leftPoint);
foreach (GraphPoint point in graphSettings.GraphPoints) foreach (GraphPoint point in graphSettings.GraphPoints)
{ {
if (point.X > leftBound && point.X < rightBound) if (point.X > leftBound && point.X < rightBound)
@@ -42,6 +45,7 @@ namespace StructureHelperCommon.Models.Functions.Decorator
graphLimitGraphPoint.Add(point); graphLimitGraphPoint.Add(point);
} }
} }
graphLimitGraphPoint.Add(rightPoint);
graphSettings.GraphPoints = graphLimitGraphPoint; graphSettings.GraphPoints = graphLimitGraphPoint;
return graphSettings; return graphSettings;
} }

View File

@@ -36,6 +36,9 @@ namespace StructureHelperCommon.Models.Functions.Decorator
{ {
var graphSettings = base.GetGraphSettings(); var graphSettings = base.GetGraphSettings();
var graphLimitGraphPoint = new List<GraphPoint>(); var graphLimitGraphPoint = new List<GraphPoint>();
var downPoint = new GraphPoint(downBound, GetByX(downBound));
var upPoint = new GraphPoint(upBound, GetByX(upBound));
graphLimitGraphPoint.Add(downPoint);
foreach (GraphPoint point in graphSettings.GraphPoints) foreach (GraphPoint point in graphSettings.GraphPoints)
{ {
if (point.Y > downBound && point.Y < upBound) if (point.Y > downBound && point.Y < upBound)
@@ -43,6 +46,7 @@ namespace StructureHelperCommon.Models.Functions.Decorator
graphLimitGraphPoint.Add(point); graphLimitGraphPoint.Add(point);
} }
} }
graphLimitGraphPoint.Add(upPoint);
graphSettings.GraphPoints = graphLimitGraphPoint; graphSettings.GraphPoints = graphLimitGraphPoint;
return graphSettings; return graphSettings;
} }

View File

@@ -26,10 +26,8 @@ namespace StructureHelperCommon.Models.Functions
public string Name { get; set; } public string Name { get; set; }
public string Description { get ; set; } public string Description { get ; set; }
public int Step { get; set; } public int Step { get; set; }
public string Formula { get; set; } public string Formula { get; set; }
public Guid Id => throw new NotImplementedException(); public Guid Id => throw new NotImplementedException();
public ObservableCollection<IOneVariableFunction> Functions { get; set; } = new ObservableCollection<IOneVariableFunction>(); public ObservableCollection<IOneVariableFunction> Functions { get; set; } = new ObservableCollection<IOneVariableFunction>();
public double MinArg { get; set; } public double MinArg { get; set; }
public double MaxArg { get; set; } public double MaxArg { get; set; }

View File

@@ -8,6 +8,7 @@ namespace StructureHelperCommon.Models.Functions
{ {
public class GraphPoint : ICloneable public class GraphPoint : ICloneable
{ {
public bool Exclude { get; set; }
public double X { get; set; } public double X { get; set; }
public double Y { get; set; } public double Y { get; set; }
@@ -15,6 +16,7 @@ namespace StructureHelperCommon.Models.Functions
{ {
X = x; X = x;
Y = y; Y = y;
Exclude = false;
} }
public object Clone() public object Clone()
{ {

View File

@@ -27,7 +27,6 @@ namespace StructureHelperCommon.Models.Functions
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }
public List<GraphPoint> Table { get; set; } public List<GraphPoint> Table { get; set; }
public Guid Id => throw new NotImplementedException(); public Guid Id => throw new NotImplementedException();
public ObservableCollection<IOneVariableFunction> Functions { get; set; } = new ObservableCollection<IOneVariableFunction>(); public ObservableCollection<IOneVariableFunction> Functions { get; set; } = new ObservableCollection<IOneVariableFunction>();
public double MinArg { get; set; } public double MinArg { get; set; }
@@ -72,21 +71,36 @@ namespace StructureHelperCommon.Models.Functions
public double GetByX(double xValue) public double GetByX(double xValue)
{ {
//Реализовать взятие значения из таблицы и интерполяцию по таблице GraphPoint leftBound = null;
GraphPoint rightBound = null;
for (int i = 0; i < Table.Count - 1; i++)
{
return 100; leftBound = Table[i];
rightBound = Table[i + 1];
if (xValue == leftBound.X)
{
return leftBound.Y;
}
else if (xValue > leftBound.X && xValue < rightBound.X)
{
return MathUtils.Interpolation(xValue, leftBound.X, rightBound.X, leftBound.Y, rightBound.Y);
}
else if (xValue == rightBound.X)
{
return rightBound.Y;
}
}
return 0;
//Можно добавить экстраполяцию
} }
public GraphSettings GetGraphSettings() public GraphSettings GetGraphSettings()
{ {
var graphSettings = new GraphSettings(Color, Color); var graphSettings = new GraphSettings(Color, Color);
foreach(GraphPoint point in Table) foreach (GraphPoint point in Table)
{ {
var graphPoint = new GraphPoint(point.X, GetByX(point.X)); var graphPoint = new GraphPoint(point.X, GetByX(point.X));
graphSettings.GraphPoints.Add(graphPoint); graphSettings.GraphPoints.Add(graphPoint);
} }
return graphSettings; return graphSettings;
} }
} }

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services
{
public static class MathUtils
{
public static double Interpolation(double x, double x1, double x2, double y1, double y2)
{
if (Math.Abs(x2 - x1) < 10E-3)
{
return (y1 + y2) / 2;
}
var y = (y2 - y1) * (x - x1) / (x2 - x1) + y1;
return y;
}
}
}

View File

@@ -0,0 +1,23 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperTests.UnitTests.Interpolation
{
public class InterpolationTest
{
[TestCase(1.5, 5.5, 1, 2, 5, 6)]
public void InterpolationTestMethod(double x, double y, double x1, double x2, double y1, double y2)
{
//Arrange
//Act
var result = StructureHelperCommon.Services.MathUtils.Interpolation(x, x1, x2, y1, y2);
//Assert
Assert.AreEqual(result, y);
}
}
}