Skip to content

Commit 8989efd

Browse files
committed
Added precompile module with tests.
1 parent 6b9719b commit 8989efd

File tree

11 files changed

+239
-51
lines changed

11 files changed

+239
-51
lines changed

MathFunctions.GUI/frmMain.Designer.cs

Lines changed: 43 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MathFunctions.GUI/frmMain.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private void btnCalculate_Click(object sender, EventArgs e)
9898
{
9999
simplifiedFunc = new MathFunc(tbInput.Text, tbVar.Text).Simplify();
100100
tbSimplification.Text = simplifiedFunc.ToString();
101+
tbSimplifiedOpt.Text = simplifiedFunc.GetPrecompilied().ToString();
101102
}
102103
catch (Exception ex)
103104
{
@@ -132,6 +133,7 @@ private void btnCalculate_Click(object sender, EventArgs e)
132133
{
133134
derivativeFunc = new MathFunc(tbInput.Text, tbVar.Text).GetDerivative();
134135
tbDerivative.Text = derivativeFunc.ToString();
136+
tbDerivativeOpt.Text = derivativeFunc.GetPrecompilied().ToString();
135137
}
136138
catch (Exception ex)
137139
{
@@ -146,7 +148,7 @@ private void btnCalculate_Click(object sender, EventArgs e)
146148
{
147149
var compileDerivativeFunc = new MathFunc(tbDerivative.Text, tbVar.Text, true, true);
148150
compileDerivativeFunc.DerivativeDelta = double.Parse(tbDerivativeDelta.Text);
149-
compileDerivativeFunc.Compile(Assembly, "FuncDer");
151+
compileDerivativeFunc.Compile(Assembly, "FuncDerivative");
150152
var sb = new StringBuilder();
151153
compileDerivativeFunc.Instructions.ToList().ForEach(instr => sb.AppendLine(instr.ToString().Replace("IL_0000: ", "")));
152154

@@ -168,10 +170,5 @@ private void btnSave_Click(object sender, EventArgs e)
168170
Assembly.Finalize(Path.GetDirectoryName(saveFileDialog1.FileName), Path.GetFileName(saveFileDialog1.FileName));
169171
}
170172
}
171-
172-
private void tbResultExpression_Click(object sender, EventArgs e)
173-
{
174-
(sender as TextBox).SelectAll();
175-
}
176173
}
177174
}

MathFunctions.Tests/MathFuncDerivativeTest.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,47 @@ public void UnknownFuncDerivativeTest()
6767
public void Derivative1()
6868
{
6969
string expression = "x ^ 3 + sin(3 * ln(x * 1)) + x ^ ln(2 * sin(3 * ln(x))) - 2 * x ^ 3";
70-
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, new MathFunc(expression).GetDerivative().ToString()));
70+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
7171
}
7272

7373
[Test]
7474
public void Derivative2()
7575
{
7676
string expression = "x / sin(x) / cos(x) + ln(1 / sin(x))";
77-
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, new MathFunc(expression).GetDerivative().ToString()));
77+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
7878
}
7979

8080
[Test]
8181
public void Derivative3()
8282
{
8383
string expression = "ln(sin(x ^ x))";
84-
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, new MathFunc(expression).GetDerivative().ToString()));
84+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
8585
}
8686

8787
[Test]
8888
public void Derivative4()
8989
{
9090
string expression = "(2 * x ^ 2 - 1) / (2 * x ^ 2 + 1)";
91-
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, new MathFunc(expression).GetDerivative().ToString()));
91+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
92+
}
93+
94+
[Test]
95+
public void Derivative5()
96+
{
97+
string expression = "tan(1 / x) / 3 ^ sin(x)";
98+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
99+
}
100+
101+
[Test]
102+
public void Derivative6()
103+
{
104+
string expression = "arctan(sqrt(x)) * ln(x)";
105+
Assert.IsTrue(WolframAlphaUtils.CheckDerivative(expression, GetDerivativeExpression(expression)));
106+
}
107+
108+
private string GetDerivativeExpression(string expression)
109+
{
110+
return new MathFunc(expression).GetDerivative().GetPrecompilied().ToString();
92111
}
93112
}
94113
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading;
8+
9+
namespace MathFunctions.Tests
10+
{
11+
[TestFixture]
12+
public class MathFuncPrecompileTests
13+
{
14+
[SetUp]
15+
public static void Init()
16+
{
17+
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
18+
}
19+
20+
[Test]
21+
public static void FoldConstantsTest()
22+
{
23+
MathFunc func = new MathFunc("cos(ln(sin(2)))").GetPrecompilied();
24+
Assert.IsTrue(double.Parse(func.ToString()) == 0.995483012754421);
25+
}
26+
27+
[Test]
28+
public static void PrecompileTest2()
29+
{
30+
MathFunc func = new MathFunc("-(sin(x) ^ -2 * x) + -(sin(x) ^ -1 * cos(x)) + cos(x) ^ -2 * x + cos(x) ^ -1 * sin(x) ^ -1");
31+
Assert.IsTrue(WolframAlphaUtils.CheckEquality(func.ToString(), func.GetPrecompilied().ToString()));
32+
}
33+
}
34+
}

MathFunctions.Tests/MathFuncSample.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

MathFunctions.Tests/MathFunctions.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<ItemGroup>
5151
<Compile Include="MathFuncDerivativeTest.cs" />
5252
<Compile Include="MathFuncCompilationTests.cs" />
53-
<Compile Include="MathFuncSample.cs" />
53+
<Compile Include="MathFuncPrecompileTests.cs" />
5454
<Compile Include="MathFuncSimplificationTests.cs" />
5555
<Compile Include="MathFuncTests.cs" />
5656
<Compile Include="MiscTests.cs" />

MathFunctions.v11.suo

1 KB
Binary file not shown.

MathFunctions/MathFunc.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public MathFunc(string str, string v = null, bool simplify = true, bool precompi
5757
if (simplify)
5858
Root = Simplify(Root);
5959
if (precompile)
60-
Root = RationalToDouble(Root);
60+
Root = Precompile(null, Root);
6161
}
6262

6363
public MathFunc(MathFuncNode root,
@@ -88,7 +88,7 @@ public MathFunc(MathFuncNode left, MathFuncNode right,
8888
if (simplify)
8989
Root = Simplify(Root);
9090
if (calculateConstants)
91-
Root = RationalToDouble(Root);
91+
Root = Precompile(null, Root);
9292
}
9393

9494
public ValueNode SimplifyValues(KnownFuncType? funcType, IList<ValueNode> args)
@@ -323,6 +323,11 @@ public override string ToString()
323323
return Root.ToString();
324324
}
325325

326+
public string ToShortString()
327+
{
328+
return Root.ToString().Replace(" ", "");
329+
}
330+
326331
public override bool Equals(object obj)
327332
{
328333
return Root.Equals(obj);

MathFunctions/MathFuncCompilation.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ private bool EmitAddFunc(FuncNode funcNode)
295295

296296
private bool EmitSubFunc(FuncNode funcNode)
297297
{
298-
for (int i = 0; i < funcNode.Childs.Count; i++)
298+
EmitNode(funcNode.Childs[0]);
299+
for (int i = 1; i < funcNode.Childs.Count; i++)
299300
{
300301
EmitNode(funcNode.Childs[i]);
301302
IlInstructions.Add(new OpCodeArg(OpCodes.Sub));
@@ -346,7 +347,8 @@ private bool EmitMultFunc(FuncNode funcNode)
346347

347348
private bool EmitDivFunc(FuncNode funcNode)
348349
{
349-
for (int i = 0; i < funcNode.Childs.Count; i++)
350+
EmitNode(funcNode.Childs[0]);
351+
for (int i = 1; i < funcNode.Childs.Count; i++)
350352
{
351353
EmitNode(funcNode.Childs[i]);
352354
IlInstructions.Add(new OpCodeArg(OpCodes.Div));

0 commit comments

Comments
 (0)