Skip to content

Commit

Permalink
Merge branch 'ico' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
prekel committed Jun 10, 2018
2 parents 74929c4 + 0fbbb6c commit 4d8c257
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 17 deletions.
26 changes: 16 additions & 10 deletions MyExpression.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class Program
{
private static void Main(string[] args)
{
System.Console.Write("Equation: ");
var s = System.Console.ReadLine();
System.Console.Write(" Epsilon: ");
var eps = System.Console.ReadLine();
var p = Polynomial.Parse(s);
var e = new PolynomialEquation(p, Double.Parse(eps));
e.Solve();
//System.Console.WriteLine(String.Join(" ", e.AllRoots));
System.Console.WriteLine(" Roots: " + String.Join(" ", e.Roots));
System.Console.ReadKey();
//System.Console.Write("Equation: ");
//var s = System.Console.ReadLine();
//System.Console.Write(" Epsilon: ");
//var eps = System.Console.ReadLine();
//var p = Polynomial.Parse(s);
//var e = new PolynomialEquation(p, Double.Parse(eps));
//e.Solve();
////System.Console.WriteLine(String.Join(" ", e.AllRoots));
//System.Console.WriteLine(" Roots: " + String.Join(" ", e.Roots));
//System.Console.ReadKey();

//var s = System.Console.ReadLine();
//var evaluator = new CodeDomEval(s);
Expand Down Expand Up @@ -51,6 +51,12 @@ private static void Main(string[] args)
//e.Solve();
////System.Console.WriteLine(String.Join(" ", e.AllRoots));
//System.Console.WriteLine(" Roots: " + String.Join(" ", e.Roots));

//var f = new CodeDomEval("pow(x, 3) - 2 * pow(x, 2) - x + 2");
//var f = new CodeDomEval("sin(x)");
var e = new FunctionEquation(Math.Sin, new Interval(-5, 5), eps: 1e-15);
e.Solve();
var r = e.AllRoots;
}
}
}
5 changes: 4 additions & 1 deletion MyExpression.Core.Tests/CodeDomEvalTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using NUnit.Framework;
// Copyright (c) 2018 Vladislav Prekel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;

namespace MyExpression.Core.Tests
{
[TestFixture]
Expand Down
23 changes: 23 additions & 0 deletions MyExpression.Core.Tests/FunctionEquationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2018 Vladislav Prekel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;

namespace MyExpression.Core.Tests
{
[TestFixture]
public class FunctionEquationTests
{
[Test]
public void CustomTest()
{
var e1 = new FunctionEquation((x) => Math.Sin(x) * Math.Sin(x) * Math.Cos(x) / Math.Tan(x), new Interval(-5, 5), 1e-3, 1e-5);
e1.Solve();
}
}
}
1 change: 1 addition & 0 deletions MyExpression.Core.Tests/MyExpression.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="CodeDomEvalTests.cs" />
<Compile Include="FunctionEquationTests.cs" />
<Compile Include="IntervalTests.cs" />
<Compile Include="OpenCloseIntervalTests.cs" />
<Compile Include="MyRandom.cs" />
Expand Down
70 changes: 70 additions & 0 deletions MyExpression.Core/FunctionEquation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2018 Vladislav Prekel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyExpression.Core
{
public class FunctionEquation : IEquation
{
public Func<double, double> Function { get; private set; }

public Interval Interval { get; private set; }
public double Step { get; private set; }
public double Epsilon { get; private set; }

public FunctionEquation(Func<double, double> function, Interval interval, double step = 1e-3, double eps = 1e-8)
{
Function = function;
Interval = interval;
Step = step;
Epsilon = eps;
}

public FunctionEquation(IFunctionX functionx, Interval interval, double step = 1e-3, double eps = 1e-8)
: this(functionx.Calculate, interval, step, eps) { }

public IList<double> AllRoots { get; private set; }

public IList<double> Roots => AllRoots;

public void Solve()
{
AllRoots = new List<double>();
var x0 = Interval.Left;
var y0 = Function(x0);
var y = 0d;
var fl = false;
var x1 = Double.NaN;
for (var x = Interval.Left + Step; x <= Interval.Right; x += Step)
{
y = Function(x);

if (!fl && Math.Abs(y) < Epsilon)
{
fl = true;
x1 = x;
}
if (fl && Math.Abs(y) >= Epsilon)
{
fl = false;
AllRoots.Add(x1);
}

var p = y0 * y;
if (!Double.IsInfinity(p) && !Double.IsNaN(p) && p < 0)
{
var bs = new RecursiveBinarySearch(Function, new Interval(x0, x), Epsilon);
x1 = bs.Solve();
if (!fl) AllRoots.Add(x1);
}

y0 = y;
x0 = x;
}
}
}
}
1 change: 1 addition & 0 deletions MyExpression.Core/MyExpression.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FunctionEquation.cs" />
<Compile Include="IEquation.cs" />
<Compile Include="CodeDomEval.cs" />
<Compile Include="IBinarySearch.cs" />
Expand Down
Binary file added MyExpression.Wpf/Icons/16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MyExpression.Wpf/Icons/240x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MyExpression.Wpf/Icons/32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MyExpression.Wpf/Icons/48x48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MyExpression.Wpf/Icons/Icon1.ico
Binary file not shown.
Binary file added MyExpression.Wpf/Icons/Thumbs.db
Binary file not shown.
15 changes: 10 additions & 5 deletions MyExpression.Wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,21 @@
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition Height="105"/>
<RowDefinition Height="134"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button x:Name="SolveButton" Content="Solve" Margin="0,85,0,0" VerticalAlignment="Top" Click="SolveButton_Click" Height="20" Grid.Row="1" Grid.Column="2" IsEnabled="False" />
<Button x:Name="SolveButton" Content="Solve" Margin="0,104,0,0" VerticalAlignment="Top" Click="SolveButton_Click" Height="20" Grid.Row="1" Grid.Column="4" IsEnabled="False" />
<Button x:Name="EraseButton" Content="Erase" Margin="0,85,0,0" VerticalAlignment="Top" Click="EraseButton_Click" Height="0" Grid.Row="1" Grid.Column="4" />
<CheckBox x:Name="MultipleRootsCheckBox" Content="Multiple roots" Margin="0,30,40,0" Grid.Row="1" VerticalAlignment="Top" FlowDirection="RightToLeft" Grid.Column="1" IsChecked="True" Height="15" Grid.ColumnSpan="2"/>
<CheckBox x:Name="MultipleRootsCheckBox" Content="Multiple roots" Margin="0,84,40,0" Grid.Row="1" VerticalAlignment="Top" FlowDirection="RightToLeft" Grid.Column="1" IsChecked="True" Height="15" Grid.ColumnSpan="2"/>
<TextBox x:Name="SolveEpsilon" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="1e-15" VerticalAlignment="Top" Grid.Column="2"/>
<Label Content="Epsilon" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Top" Width="105" FlowDirection="RightToLeft" Grid.Column="1" Height="26"/>
<TextBox x:Name="RootsTextBox" TextWrapping="Wrap" Grid.ColumnSpan="4" Margin="0,10,0,10" Grid.Row="2" Grid.Column="1" />
<local:BrushesList x:Name="RootsBrushComboBox" Margin="0,50,0,0" HorizontalAlignment="Right" Grid.Row="1" Grid.ColumnSpan="2" Height="30" VerticalAlignment="Top" Width="150" SelectedBrush="Indigo" Grid.Column="1"/>
<TextBox x:Name="RootsTextBox" TextWrapping="Wrap" Grid.ColumnSpan="4" Margin="0,10" Grid.Row="2" Grid.Column="1" />
<local:BrushesList x:Name="RootsBrushComboBox" HorizontalAlignment="Right" Grid.Row="1" Grid.ColumnSpan="2" Height="30" VerticalAlignment="Top" Width="150" SelectedBrush="Indigo" Grid.Column="1" Margin="0,104,0,0"/>
<TextBox x:Name="SolveStep" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="1e-3" VerticalAlignment="Top" Grid.Column="2" Margin="0,28,0,0"/>
<Label Content="Step" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Top" Width="105" FlowDirection="RightToLeft" Grid.Column="1" Height="26" Margin="0,28,0,0"/>
<TextBox x:Name="SolveIntervalLeft" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="-5" VerticalAlignment="Top" Grid.Column="2" Margin="0,56,0,0"/>
<Label Content="Interval" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Top" Width="105" FlowDirection="RightToLeft" Grid.Column="1" Height="26" Margin="0,56,0,0"/>
<TextBox x:Name="SolveIntervalRight" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="5" VerticalAlignment="Top" Grid.Column="4" Margin="0,56,0,0"/>
</Grid>
</TabItem>
</TabControl>
Expand Down
20 changes: 19 additions & 1 deletion MyExpression.Wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
{
pe = new PolynomialEquation(p, Double.Parse(SolveEpsilon.Text));
}
else if (last.Function is CodeDomEval f)
{
var l = Double.Parse(SolveIntervalLeft.Text);
var r = Double.Parse(SolveIntervalRight.Text);
var step = Double.Parse(SolveStep.Text);
var eps = Double.Parse(SolveEpsilon.Text);
pe = new FunctionEquation(f, new Interval(l, r), step, eps);
}
else
{
pe = (IEquation)last.Function;
Expand Down Expand Up @@ -279,7 +287,12 @@ private void FunctionsListView_SelectionChanged(object sender, SelectionChangedE
if (g.Function is CodeDomEval)
{
TangentLim.IsEnabled = true;
SolveButton.IsEnabled = false;
SolveButton.IsEnabled = true;
SolveStep.IsEnabled = true;
SolveIntervalLeft.IsEnabled = true;
SolveIntervalRight.IsEnabled = true;
MultipleRootsCheckBox.IsEnabled = false;
if (SolveEpsilon.Text == "1e-15") SolveEpsilon.Text = "1e-6";
}
else
{
Expand All @@ -293,6 +306,11 @@ private void FunctionsListView_SelectionChanged(object sender, SelectionChangedE
}
TangentLim.IsEnabled = false;
SolveButton.IsEnabled = true;
SolveStep.IsEnabled = false;
SolveIntervalLeft.IsEnabled = false;
SolveIntervalRight.IsEnabled = false;
MultipleRootsCheckBox.IsEnabled = true;
if (SolveEpsilon.Text == "1e-6") SolveEpsilon.Text = "1e-15";
}
}
}
Expand Down
1 change: 1 addition & 0 deletions MyExpression.Wpf/MyExpression.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
<ApplicationIcon>..\MyExpression.Wpf\Icons\Icon1.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down

0 comments on commit 4d8c257

Please sign in to comment.