Skip to content

Commit 2e805f6

Browse files
Integral optimized
1 parent 3b8aad2 commit 2e805f6

2 files changed

Lines changed: 13 additions & 24 deletions

File tree

AngouriMath/Functions/Algebra/Integration.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ public abstract partial class Entity
1010
/// <summary>
1111
/// Returns a value of a definite integral of a function. Only works for one-variable functions
1212
/// </summary>
13-
/// <param name="func">
14-
/// Function with only one variable
15-
/// </param>
1613
/// <param name="x">
1714
/// Variable to integrate over
1815
/// </param>
@@ -23,17 +20,14 @@ public abstract partial class Entity
2320
/// The up bound for integrating
2421
/// </param>
2522
/// <returns></returns>
26-
public Number DefiniteIntegral(Entity func, VariableEntity x, Number from, Number to)
23+
public Number DefiniteIntegral(VariableEntity x, Number from, Number to)
2724
{
28-
return Integration.Integrate(func, x, from, to, 100);
25+
return Integration.Integrate(this, x, from, to, 100);
2926
}
3027

3128
/// <summary>
3229
/// Returns a value of a definite integral of a function. Only works for one-variable functions
3330
/// </summary>
34-
/// <param name="func">
35-
/// Function with only one variable
36-
/// </param>
3731
/// <param name="x">
3832
/// Variable to integrate over
3933
/// </param>
@@ -47,9 +41,9 @@ public Number DefiniteIntegral(Entity func, VariableEntity x, Number from, Numbe
4741
/// Accuracy (initially, amount of iterations)
4842
/// </param>
4943
/// <returns></returns>
50-
public Number DefiniteIntegral(Entity func, VariableEntity x, Number from, Number to, int stepCount)
44+
public Number DefiniteIntegral(VariableEntity x, Number from, Number to, int stepCount)
5145
{
52-
return Integration.Integrate(func, x, from, to, stepCount);
46+
return Integration.Integrate(this, x, from, to, stepCount);
5347
}
5448
}
5549
public static class Integration
@@ -61,11 +55,12 @@ public static Number Integrate(Entity func, VariableEntity x, Number from, Numbe
6155
double ReTo = to.Re;
6256
double ImTo = to.Im;
6357
var res = new Number(0, 0);
58+
var cfunc = func.Compile(x.Name);
6459
for(int i = 0; i <= stepCount; i++)
6560
{
6661
var share = ((double)i) / stepCount;
6762
var tmp = new Number(ReFrom * share + ReTo * (1 - share), ImFrom * share + ImTo * (1 - share));
68-
res += func.Substitute(x, tmp).Eval().GetValue();
63+
res += cfunc.Eval(tmp);
6964
}
7065
return res / (stepCount + 1) * (to - from);
7166
}

Samples/Program.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@ class Program
1010
static void Main(string[] args)
1111
{
1212
var x = MathS.Var("x");
13-
14-
var expr = (x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))
15-
* x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))
16-
* x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))
17-
* x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))));//*/
18-
//var expr = MathS.Sqr(x - 4) + MathS.Log(3, MathS.Sin(x));
19-
var func = expr.Compile("x");
13+
var y = MathS.Var("y");
2014

21-
var iter = 10000000;
15+
var expr = MathS.Sin(x) * MathS.Sqrt(x + MathS.Cos(x)) / 3;
16+
//Console.WriteLine(func.Eval(1, 2));
17+
//return;
18+
19+
var iter = 1000;
2220
var stopWatch = new Stopwatch();
2321
stopWatch.Start();
24-
var a = 0.0;
25-
var cont = 3;
2622
for (int i = 0; i < iter; i++)
27-
func.Eval(3);
28-
//expr.Substitute(x, 3).Eval();
29-
//a = Math.Sqrt(cont - 4) + Math.Log(cont, Math.Sin(cont));
23+
expr.DefiniteIntegral(x, 1, 10);
3024
stopWatch.Stop();
3125
Console.Write(((double)stopWatch.ElapsedMilliseconds) / iter * 1000000);
3226
Console.WriteLine(" ns");

0 commit comments

Comments
 (0)