Skip to content

Commit 3315dfe

Browse files
committed
Added test utils for compiled math assembly.
Fixed error with nodes sorting. Small code refactoring.
1 parent 9c58754 commit 3315dfe

File tree

13 files changed

+237
-142
lines changed

13 files changed

+237
-142
lines changed

MathFunctions.GUI/frmMain.Designer.cs

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

MathFunctions.GUI/frmMain.resx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,10 @@ log(f(x), g(x))' = g'(x)/(g(x)*ln(f(x))) - (f'(x)*ln(g(x)))/(f(x)*ln(f(x))^2);
148148
<metadata name="clnDescription.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
149149
<value>True</value>
150150
</metadata>
151-
<metadata name="clnPos.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
152-
<value>True</value>
153-
</metadata>
154-
<metadata name="clnDescription.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
155-
<value>True</value>
156-
</metadata>
157151
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
158152
<value>17, 17</value>
159153
</metadata>
160154
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
161-
<value>107</value>
155+
<value>124</value>
162156
</metadata>
163157
</root>

MathFunctions.Tests/MathFuncCompilationTests.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
using System.Linq;
44
using System.Text;
55
using NUnit.Framework;
6+
using System.Reflection;
7+
using System.Security.Policy;
8+
using System.IO;
69

710
namespace MathFunctions.Tests
811
{
912
[TestFixture]
1013
public class MathFuncCompilationTests
1114
{
12-
//[Test]
15+
[SetUp]
16+
public void Init()
17+
{
18+
Helper.InitDefaultDerivatives();
19+
}
20+
21+
[Test]
1322
public double TestFunc(double x)
1423
{
1524
return Math.Pow((2 * -x + 1), (2 * x + 1)) * Math.Sin((2 * x + 1) * (2 * x + 1) * (2 * x + 1));
@@ -18,10 +27,37 @@ public double TestFunc(double x)
1827
[Test]
1928
public void FindIdenticalFuncsTest()
2029
{
21-
var mathFunc = new MathFunc("(2 * -x + 1) ^ (2 * x + 1) * Sin((2 * x + 1) ^ 3)");
22-
//var mathFunc = new MathFunc("(2 * x + 1) * (2 * x + 1) * (3 * (x + 5) + x)");
30+
double x = 3;
31+
double func, funcDer;
32+
CompileAndCalculate("Sin(x) + x ^ (Ln(5 * x) - 10 / x)", "x", x, out func, out funcDer);
33+
double expected = Math.Sin(x) + Math.Pow(x, Math.Log(5 * x) - 10 / x);
34+
Assert.AreEqual(expected, func);
35+
}
2336

24-
//mathFunc.Compile();
37+
private bool CompileAndCalculate(string expression, string variable, double x,
38+
out double funcResult, out double funcDerivativeResult)
39+
{
40+
funcResult = double.NaN;
41+
funcDerivativeResult = double.NaN;
42+
string tempDllName = "MathFuncLib.dll";
43+
try
44+
{
45+
var mathAssembly = new MathFuncAssemblyCecil();
46+
mathAssembly.CompileFuncAndDerivative(expression, variable, "", tempDllName);
47+
var domain = AppDomain.CreateDomain("MathFuncDomain");
48+
var pathToDll = tempDllName;
49+
var mathFuncObj = domain.CreateInstanceFromAndUnwrap(pathToDll, mathAssembly.NamespaceName + "." + mathAssembly.ClassName);
50+
var mathFuncObjType = mathFuncObj.GetType();
51+
funcResult = (double)mathFuncObjType.GetMethod(mathAssembly.FuncName).Invoke(mathFuncObj, new object[] { x });
52+
funcDerivativeResult = (double)mathFuncObjType.GetMethod(mathAssembly.FuncDerivativeName).Invoke(mathFuncObj, new object[] { x });
53+
AppDomain.Unload(domain);
54+
File.Delete(tempDllName);
55+
}
56+
catch
57+
{
58+
return false;
59+
}
60+
return true;
2561
}
2662
}
2763
}

MathFunctions.Tests/MathFuncDerivativeTest.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,9 @@ namespace MathFunctions.Tests
1010
public class MathFuncDerivativeTest
1111
{
1212
[SetUp]
13-
public void InitDerivatives()
13+
public void Init()
1414
{
15-
var derivatives = new StringBuilder();
16-
17-
derivatives.AppendLine("(f(x) / g(x))' = (f(x)' * g(x) + f(x) * g(x)') / g(x)^2;");
18-
derivatives.AppendLine("(f(x) ^ g(x))' = f(x) ^ g(x) * (f(x)' * g(x) / f(x) + g(x)' * ln(f(x)));");
19-
20-
derivatives.AppendLine("neg(f(x))' = neg(f(x)');");
21-
22-
derivatives.AppendLine("sin(f(x))' = cos(f(x)) * f(x)'; ");
23-
derivatives.AppendLine("cos(f(x))' = -sin(f(x)) * f(x)'; ");
24-
derivatives.AppendLine("tan(f(x))' = f(x)' / cos(f(x)) ^ 2; ");
25-
derivatives.AppendLine("cot(f(x))' = -f(x)' / sin(f(x)) ^ 2; ");
26-
27-
derivatives.AppendLine("arcsin(f(x))' = f(x)' / sqrt(1 - f(x) ^ 2); ");
28-
derivatives.AppendLine("arccos(f(x))' = -f(x)' / sqrt(1 - f(x) ^ 2); ");
29-
derivatives.AppendLine("arctan(f(x))' = f(x)' / (1 + f(x) ^ 2); ");
30-
derivatives.AppendLine("arccot(f(x))' = -f(x)' / (1 + f(x) ^ 2); ");
31-
32-
derivatives.AppendLine("sinh(f(x))' = f(x)' * cosh(x); ");
33-
derivatives.AppendLine("cosh(f(x))' = f(x)' * sinh(x); ");
34-
35-
derivatives.AppendLine("arcsinh(f(x))' = f(x)' / sqrt(f(x) ^ 2 + 1); ");
36-
derivatives.AppendLine("arcosh(f(x))' = f(x)' / sqrt(f(x) ^ 2 - 1); ");
37-
38-
derivatives.AppendLine("ln(f(x))' = f(x)' / f(x); ");
39-
derivatives.AppendLine("log(f(x), g(x))' = (ln(f(x)) * g(x)' / g(x) - f(x)' * ln(g(x)) / f(x)) / ln(f(x)) ^ 2;");
40-
41-
Helper.InitDerivatives(derivatives.ToString());
15+
Helper.InitDefaultDerivatives();
4216
}
4317

4418
[Test]

MathFunctions.Tests/MathFuncTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class MathFuncTests
1313
MathExprParser Parser;
1414

1515
[SetUp]
16-
public void InitParser()
16+
public void Init()
1717
{
1818
Parser = new MathExprParser();
1919
}
@@ -31,13 +31,13 @@ public void IsNotValueTest()
3131
}
3232

3333
[Test]
34-
public void IsValueTest2()
34+
public void IsCalculatedTest()
3535
{
3636
Assert.IsTrue(new MathFunc("3 + sin(5 + 7 ^ 0.342345 - sqrt(2)) * 3 * 1").IsCalculated);
3737
}
3838

3939
[Test]
40-
public void IsNotValueTest2()
40+
public void IsNotCalculatedTest()
4141
{
4242
Assert.IsFalse(new MathFunc("3 + f(5 + 7 ^ 0.342345 - sqrt(2) + x) * 3 * 1").IsCalculated);
4343
}

MathFunctions.v11.suo

0 Bytes
Binary file not shown.

MathFunctions/Helper.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ public static void InitDerivatives(string str)
3131
}
3232
}
3333

34+
public static void InitDefaultDerivatives()
35+
{
36+
var derivatives = new StringBuilder();
37+
derivatives.AppendLine("(f(x) / g(x))' = (f(x)' * g(x) + f(x) * g(x)') / g(x)^2;");
38+
derivatives.AppendLine("(f(x) ^ g(x))' = f(x) ^ g(x) * (f(x)' * g(x) / f(x) + g(x)' * ln(f(x)));");
39+
derivatives.AppendLine("neg(f(x))' = neg(f(x)');");
40+
derivatives.AppendLine("sin(f(x))' = cos(f(x)) * f(x)';");
41+
derivatives.AppendLine("cos(f(x))' = -sin(f(x)) * f(x)';");
42+
derivatives.AppendLine("tan(f(x))' = f(x)' / cos(f(x)) ^ 2;");
43+
derivatives.AppendLine("cot(f(x))' = -f(x)' / sin(f(x)) ^ 2;");
44+
derivatives.AppendLine("arcsin(f(x))' = f(x)' / sqrt(1 - f(x) ^ 2);");
45+
derivatives.AppendLine("arccos(f(x))' = -f(x)' / sqrt(1 - f(x) ^ 2);");
46+
derivatives.AppendLine("arctan(f(x))' = f(x)' / (1 + f(x) ^ 2);");
47+
derivatives.AppendLine("arccot(f(x))' = -f(x)' / (1 + f(x) ^ 2);");
48+
derivatives.AppendLine("sinh(f(x))' = f(x)' * cosh(x);");
49+
derivatives.AppendLine("cosh(f(x))' = f(x)' * sinh(x);");
50+
derivatives.AppendLine("arcsinh(f(x))' = f(x)' / sqrt(f(x) ^ 2 + 1);");
51+
derivatives.AppendLine("arcosh(f(x))' = f(x)' / sqrt(f(x) ^ 2 - 1);");
52+
derivatives.AppendLine("ln(f(x))' = f(x)' / f(x);");
53+
derivatives.AppendLine("log(f(x), g(x))' = (ln(f(x)) * g(x)' / g(x) - f(x)' * ln(g(x)) / f(x)) / ln(f(x)) ^ 2;");
54+
InitDerivatives(derivatives.ToString());
55+
}
56+
3457
public static void InitRools(string str)
3558
{
3659
Parser.Parse(str);

0 commit comments

Comments
 (0)