upd
This commit is contained in:
parent
d70b0390b7
commit
ee25655c8d
22 changed files with 2126 additions and 53 deletions
128
tests/GraphTUI.Tests/DoubleSidedEquationTests.cs
Normal file
128
tests/GraphTUI.Tests/DoubleSidedEquationTests.cs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
using GraphTUI.Core.Models;
|
||||
using GraphTUI.Rendering;
|
||||
|
||||
namespace GraphTUI.Tests;
|
||||
|
||||
public static class DoubleSidedEquationTests
|
||||
{
|
||||
public static void Register(TestRunner runner)
|
||||
{
|
||||
runner.Add("DoubleSided: y = f(x) definitions", () =>
|
||||
{
|
||||
var fn1 = new GraphFunction(1, "y = sin(x)", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.Cartesian, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.Equal(0.0, fn1.Evaluator!.Evaluate(0));
|
||||
Assert.Within(1.0, fn1.Evaluator.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// Inverted order: sin(x) = y
|
||||
var fn2 = new GraphFunction(2, "sin(x) = y", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.Cartesian, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
Assert.Within(1.0, fn2.Evaluator!.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// Plain expression sin(x) defaults to y = sin(x)
|
||||
var fn3 = new GraphFunction(3, "sin(x)", ColorRgb.Green);
|
||||
Assert.Equal(FunctionType.Cartesian, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: f(y) = x and x = f(y) definitions", () =>
|
||||
{
|
||||
// x = y^2
|
||||
var fn1 = new GraphFunction(1, "x = y^2", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.CartesianX, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.Equal(9.0, fn1.Evaluator!.EvaluateY(3));
|
||||
Assert.Equal(9.0, fn1.Evaluator.EvaluateY(-3));
|
||||
|
||||
// f(y) = x: y^2 = x
|
||||
var fn2 = new GraphFunction(2, "y^2 = x", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.CartesianX, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
Assert.Equal(16.0, fn2.Evaluator!.EvaluateY(4));
|
||||
Assert.Equal(16.0, fn2.Evaluator.EvaluateY(-4));
|
||||
|
||||
// sin(y) = x
|
||||
var fn3 = new GraphFunction(3, "sin(y) = x", ColorRgb.Magenta);
|
||||
Assert.Equal(FunctionType.CartesianX, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
Assert.Within(1.0, fn3.Evaluator!.EvaluateY(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// y^3 + 2y = x
|
||||
var fn4 = new GraphFunction(4, "y^3 + 2y = x", ColorRgb.Orange);
|
||||
Assert.Equal(FunctionType.CartesianX, fn4.Type);
|
||||
Assert.True(fn4.CompilationError == null);
|
||||
Assert.Equal(12.0, fn4.Evaluator!.EvaluateY(2)); // 2^3 + 2*2 = 8 + 4 = 12
|
||||
|
||||
// Plain expression with y and no x: y^2 defaults to x = y^2
|
||||
var fn5 = new GraphFunction(5, "y^2", ColorRgb.White);
|
||||
Assert.Equal(FunctionType.CartesianX, fn5.Type);
|
||||
Assert.True(fn5.CompilationError == null);
|
||||
Assert.Equal(4.0, fn5.Evaluator!.EvaluateY(2));
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: f(y) = g(x) and implicit definitions", () =>
|
||||
{
|
||||
// y^2 = sin(x)
|
||||
var fn1 = new GraphFunction(1, "y^2 = sin(x)", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.Implicit, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.True(fn1.EvaluatorLhs != null && fn1.EvaluatorRhs != null);
|
||||
Assert.Equal(4.0, fn1.EvaluatorLhs!.EvaluateY(2));
|
||||
Assert.Within(1.0, fn1.EvaluatorRhs!.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// sin(y) = cos(x)
|
||||
var fn2 = new GraphFunction(2, "sin(y) = cos(x)", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.Implicit, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
|
||||
// Elliptic curve: y^2 = x^3 - 3x
|
||||
var fn3 = new GraphFunction(3, "y^2 = x^3 - 3x", ColorRgb.Green);
|
||||
Assert.Equal(FunctionType.Implicit, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
|
||||
// Circle: x^2 + y^2 = 25
|
||||
var fn4 = new GraphFunction(4, "x^2 + y^2 = 25", ColorRgb.Magenta);
|
||||
Assert.Equal(FunctionType.Implicit, fn4.Type);
|
||||
Assert.True(fn4.CompilationError == null);
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: Rendering f(y) = x and f(y) = g(x) on Braille Canvas", () =>
|
||||
{
|
||||
var canvas = new BrailleCanvas(40, 20);
|
||||
var viewport = new Viewport { XMin = -10, XMax = 10, YMin = -10, YMax = 10 };
|
||||
|
||||
// 1. Test f(y) = x: y^2 = x
|
||||
var fnX = new GraphFunction(1, "y^2 = x", ColorRgb.Cyan);
|
||||
PlotRenderer.Render(canvas, viewport, [fnX], _ => 0);
|
||||
|
||||
// Count non-empty Braille cells
|
||||
int cellsWithDots = 0;
|
||||
for (int r = 0; r < canvas.CellHeight; r++)
|
||||
{
|
||||
for (int c = 0; c < canvas.CellWidth; c++)
|
||||
{
|
||||
var (ch, _, _) = canvas.GetCell(c, r);
|
||||
if (ch != ' ') cellsWithDots++;
|
||||
}
|
||||
}
|
||||
Assert.True(cellsWithDots > 5, $"Expected plotted dots for y^2=x, got {cellsWithDots}");
|
||||
|
||||
// 2. Test f(y) = g(x): y^2 = sin(x)
|
||||
var fnImplicit = new GraphFunction(2, "y^2 = sin(x)", ColorRgb.Yellow);
|
||||
PlotRenderer.Render(canvas, viewport, [fnImplicit], _ => 0);
|
||||
|
||||
int implicitDots = 0;
|
||||
for (int r = 0; r < canvas.CellHeight; r++)
|
||||
{
|
||||
for (int c = 0; c < canvas.CellWidth; c++)
|
||||
{
|
||||
var (ch, _, _) = canvas.GetCell(c, r);
|
||||
if (ch != ' ') implicitDots++;
|
||||
}
|
||||
}
|
||||
Assert.True(implicitDots > 5, $"Expected plotted dots for y^2=sin(x), got {implicitDots}");
|
||||
});
|
||||
}
|
||||
}
|
||||
215
tests/GraphTUI.Tests/FactorialVariantsTests.cs
Normal file
215
tests/GraphTUI.Tests/FactorialVariantsTests.cs
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
using GraphTUI.Core.Math;
|
||||
using GraphTUI.Core.Models;
|
||||
using GraphTUI.Rendering;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace GraphTUI.Tests;
|
||||
|
||||
public static class FactorialVariantsTests
|
||||
{
|
||||
public static void Register(TestRunner runner)
|
||||
{
|
||||
runner.Add("Factorials: x! postfix and fact(x)", () =>
|
||||
{
|
||||
// 0! = 1
|
||||
var eval0 = Evaluator.Compile("0!");
|
||||
Assert.Equal(1.0, eval0.Evaluate(0));
|
||||
|
||||
// 5! = 120
|
||||
var eval5 = Evaluator.Compile("5!");
|
||||
Assert.Equal(120.0, eval5.Evaluate(0));
|
||||
|
||||
// Variable x!
|
||||
var evalX = Evaluator.Compile("x!");
|
||||
Assert.Equal(6.0, evalX.Evaluate(3.0));
|
||||
Assert.Equal(24.0, evalX.Evaluate(4.0));
|
||||
Assert.Equal(120.0, evalX.Evaluate(5.0));
|
||||
|
||||
// Parenthesized (x + 1)!
|
||||
var evalParen = Evaluator.Compile("(x + 1)!");
|
||||
Assert.Equal(24.0, evalParen.Evaluate(3.0)); // (3+1)! = 4! = 24
|
||||
|
||||
// fact(x) equivalence
|
||||
var evalFunc = Evaluator.Compile("fact(4)");
|
||||
Assert.Equal(24.0, evalFunc.Evaluate(0));
|
||||
|
||||
// Implicit multiplication with factorial: 2x! at x=3 -> 2 * 6 = 12
|
||||
var evalImplicit = Evaluator.Compile("2x!");
|
||||
Assert.Equal(12.0, evalImplicit.Evaluate(3.0));
|
||||
|
||||
// Unary minus: -3! -> -(3!) = -6
|
||||
var evalUnary = Evaluator.Compile("-3!");
|
||||
Assert.Equal(-6.0, evalUnary.Evaluate(0));
|
||||
});
|
||||
|
||||
runner.Add("Double Factorials: x!! postfix and dfact(x)", () =>
|
||||
{
|
||||
// 0!! = 1, (-1)!! = 1
|
||||
var eval0 = Evaluator.Compile("0!!");
|
||||
Assert.Equal(1.0, eval0.Evaluate(0));
|
||||
|
||||
// 5!! = 5 * 3 * 1 = 15
|
||||
var eval5 = Evaluator.Compile("5!!");
|
||||
Assert.Equal(15.0, eval5.Evaluate(0));
|
||||
|
||||
// 6!! = 6 * 4 * 2 = 48
|
||||
var eval6 = Evaluator.Compile("6!!");
|
||||
Assert.Equal(48.0, eval6.Evaluate(0));
|
||||
|
||||
// 7!! = 7 * 5 * 3 * 1 = 105
|
||||
var eval7 = Evaluator.Compile("7!!");
|
||||
Assert.Equal(105.0, eval7.Evaluate(0));
|
||||
|
||||
// Variable x!!
|
||||
var evalX = Evaluator.Compile("x!!");
|
||||
Assert.Equal(15.0, evalX.Evaluate(5.0));
|
||||
Assert.Equal(48.0, evalX.Evaluate(6.0));
|
||||
|
||||
// dfact(x) function syntax
|
||||
var evalFunc = Evaluator.Compile("dfact(5)");
|
||||
Assert.Equal(15.0, evalFunc.Evaluate(0));
|
||||
|
||||
// (x + 1)!!
|
||||
var evalParen = Evaluator.Compile("(x + 1)!!");
|
||||
Assert.Equal(15.0, evalParen.Evaluate(4.0)); // (4+1)!! = 5!! = 15
|
||||
});
|
||||
|
||||
runner.Add("Super Factorials: x$ postfix and sfact(x)", () =>
|
||||
{
|
||||
// 1$ = 1! = 1
|
||||
var eval1 = Evaluator.Compile("1$");
|
||||
Assert.Equal(1.0, eval1.Evaluate(0));
|
||||
|
||||
// 2$ = 1! * 2! = 2
|
||||
var eval2 = Evaluator.Compile("2$");
|
||||
Assert.Equal(2.0, eval2.Evaluate(0));
|
||||
|
||||
// 3$ = 1! * 2! * 3! = 1 * 2 * 6 = 12
|
||||
var eval3 = Evaluator.Compile("3$");
|
||||
Assert.Equal(12.0, eval3.Evaluate(0));
|
||||
|
||||
// 4$ = 1! * 2! * 3! * 4! = 12 * 24 = 288
|
||||
var eval4 = Evaluator.Compile("4$");
|
||||
Assert.Equal(288.0, eval4.Evaluate(0));
|
||||
|
||||
// Variable x$
|
||||
var evalX = Evaluator.Compile("x$");
|
||||
Assert.Equal(12.0, evalX.Evaluate(3.0));
|
||||
Assert.Equal(288.0, evalX.Evaluate(4.0));
|
||||
|
||||
// sfact(x) function syntax
|
||||
var evalFunc = Evaluator.Compile("sfact(4)");
|
||||
Assert.Equal(288.0, evalFunc.Evaluate(0));
|
||||
});
|
||||
|
||||
runner.Add("Compound expressions with !, !!, and $", () =>
|
||||
{
|
||||
// 3! + 4!! = 6 + 8 = 14
|
||||
var eval1 = Evaluator.Compile("3! + 4!!");
|
||||
Assert.Equal(14.0, eval1.Evaluate(0));
|
||||
|
||||
// (3!)! = 6! = 720
|
||||
var eval2 = Evaluator.Compile("(3!)!");
|
||||
Assert.Equal(720.0, eval2.Evaluate(0));
|
||||
|
||||
// Taylor series using postfix x! in sum: sum(n=0, 4, x^n / n!)
|
||||
// For x=1: 1 + 1 + 1/2 + 1/6 + 1/24 = 65/24 ~ 2.708333
|
||||
var evalTaylor = Evaluator.Compile("sum(n=0, 4, x^n / n!)");
|
||||
Assert.Within(65.0 / 24.0, evalTaylor.Evaluate(1.0), 1e-4);
|
||||
});
|
||||
|
||||
runner.Add("Factorials: Edge cases, lookup tables, and limits", () =>
|
||||
{
|
||||
// 20! = 2,432,902,008,176,640,000
|
||||
var eval20 = Evaluator.Compile("20!");
|
||||
Assert.Equal(2432902008176640000.0, eval20.Evaluate(0));
|
||||
|
||||
// 170! is the maximum finite IEEE double factorial (~7.2574e306)
|
||||
var eval170 = Evaluator.Compile("170!");
|
||||
double f170 = eval170.Evaluate(0);
|
||||
Assert.True(!double.IsInfinity(f170) && f170 > 1e306, "170! should be finite and > 1e306");
|
||||
|
||||
// 171! overflows double to infinity
|
||||
var eval171 = Evaluator.Compile("171!");
|
||||
Assert.True(double.IsPositiveInfinity(eval171.Evaluate(0)), "171! should be PositiveInfinity");
|
||||
|
||||
// 300!! is finite
|
||||
var eval300 = Evaluator.Compile("300!!");
|
||||
double df300 = eval300.Evaluate(0);
|
||||
Assert.True(!double.IsInfinity(df300) && df300 > 1e300, "300!! should be finite");
|
||||
|
||||
// 302!! overflows double
|
||||
var eval302 = Evaluator.Compile("302!!");
|
||||
Assert.True(double.IsPositiveInfinity(eval302.Evaluate(0)), "302!! should be PositiveInfinity");
|
||||
|
||||
// Negative integer poles return NaN
|
||||
var evalNegInt = Evaluator.Compile("(-1)!");
|
||||
Assert.True(double.IsNaN(evalNegInt.Evaluate(0)), "(-1)! should be NaN");
|
||||
|
||||
var evalNegInt2 = Evaluator.Compile("(-2)!");
|
||||
Assert.True(double.IsNaN(evalNegInt2.Evaluate(0)), "(-2)! should be NaN");
|
||||
|
||||
// Non-integer factorial: (-0.5)! = Gamma(0.5) = sqrt(pi) ~ 1.77245385
|
||||
var evalHalf = Evaluator.Compile("(-0.5)!");
|
||||
Assert.Within(System.Math.Sqrt(System.Math.PI), evalHalf.Evaluate(0), 1e-5);
|
||||
});
|
||||
|
||||
runner.Add("Factorials: Performance stress test (zero lag on x!, x!!, x$)", () =>
|
||||
{
|
||||
var canvas = new BrailleCanvas(80, 25); // 160 x 100 subpixels
|
||||
var viewport = new Viewport
|
||||
{
|
||||
XMin = -10.0,
|
||||
XMax = 10.0,
|
||||
YMin = -5.0,
|
||||
YMax = 5.0
|
||||
};
|
||||
|
||||
// 1. Rendering y = x! over viewport: must complete in < 50ms (previously took seconds or froze)
|
||||
var fnFact = new GraphFunction(1, "y = x!", ColorRgb.Cyan);
|
||||
var sw = Stopwatch.StartNew();
|
||||
for (int frame = 0; frame < 5; frame++)
|
||||
{
|
||||
PlotRenderer.Render(canvas, viewport, [fnFact], _ => 0);
|
||||
}
|
||||
sw.Stop();
|
||||
long elapsedFact = sw.ElapsedMilliseconds;
|
||||
Assert.True(elapsedFact < 200, $"5 frames of y = x! took {elapsedFact}ms (expected < 200ms)");
|
||||
|
||||
// 2. Rendering y = x!! over viewport
|
||||
var fnDfact = new GraphFunction(2, "y = x!!", ColorRgb.Yellow);
|
||||
sw.Restart();
|
||||
for (int frame = 0; frame < 5; frame++)
|
||||
{
|
||||
PlotRenderer.Render(canvas, viewport, [fnDfact], _ => 0);
|
||||
}
|
||||
sw.Stop();
|
||||
long elapsedDfact = sw.ElapsedMilliseconds;
|
||||
Assert.True(elapsedDfact < 200, $"5 frames of y = x!! took {elapsedDfact}ms (expected < 200ms)");
|
||||
|
||||
// 3. Rendering y = x$ over viewport
|
||||
var fnSfact = new GraphFunction(3, "y = x$", ColorRgb.Green);
|
||||
sw.Restart();
|
||||
for (int frame = 0; frame < 5; frame++)
|
||||
{
|
||||
PlotRenderer.Render(canvas, viewport, [fnSfact], _ => 0);
|
||||
}
|
||||
sw.Stop();
|
||||
long elapsedSfact = sw.ElapsedMilliseconds;
|
||||
Assert.True(elapsedSfact < 200, $"5 frames of y = x$ took {elapsedSfact}ms (expected < 200ms)");
|
||||
|
||||
// 4. Batch mathematical evaluation of 100,000 points
|
||||
var eval = Evaluator.Compile("x!");
|
||||
sw.Restart();
|
||||
double sum = 0.0;
|
||||
for (int i = 0; i < 100_000; i++)
|
||||
{
|
||||
sum += eval.Evaluate(i % 172);
|
||||
}
|
||||
sw.Stop();
|
||||
long elapsed100k = sw.ElapsedMilliseconds;
|
||||
Assert.True(elapsed100k < 100, $"100,000 factorial evaluations took {elapsed100k}ms (expected < 100ms)");
|
||||
Assert.True(!double.IsNaN(sum), "Sum of evaluations should not be NaN");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,9 @@ public static class Program
|
|||
CalculusTests.Register(runner);
|
||||
BrailleCanvasTests.Register(runner);
|
||||
ExportSessionTests.Register(runner);
|
||||
DoubleSidedEquationTests.Register(runner);
|
||||
SummationProductIntegralTests.Register(runner);
|
||||
FactorialVariantsTests.Register(runner);
|
||||
|
||||
return runner.RunAll();
|
||||
}
|
||||
|
|
|
|||
98
tests/GraphTUI.Tests/SummationProductIntegralTests.cs
Normal file
98
tests/GraphTUI.Tests/SummationProductIntegralTests.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
using GraphTUI.Core.Math;
|
||||
|
||||
namespace GraphTUI.Tests;
|
||||
|
||||
public static class SummationProductIntegralTests
|
||||
{
|
||||
public static void Register(TestRunner runner)
|
||||
{
|
||||
runner.Add("Summation: sum(n=start, end, f(n)) and sum(n, start, end, f(n))", () =>
|
||||
{
|
||||
// sum(n=1, 5, n) = 1 + 2 + 3 + 4 + 5 = 15
|
||||
var eval1 = Evaluator.Compile("sum(n=1, 5, n)");
|
||||
Assert.Equal(15.0, eval1.Evaluate(0));
|
||||
|
||||
// sum(n=1, 4, n^2) = 1 + 4 + 9 + 16 = 30
|
||||
var eval2 = Evaluator.Compile("sum(n=1, 4, n^2)");
|
||||
Assert.Equal(30.0, eval2.Evaluate(0));
|
||||
|
||||
// sum with variable x: sum(n=1, 3, n*x) = 1x + 2x + 3x = 6x -> for x=2, result is 12
|
||||
var eval3 = Evaluator.Compile("sum(n=1, 3, n*x)");
|
||||
Assert.Equal(12.0, eval3.Evaluate(2.0));
|
||||
|
||||
// sum(k=0, 4, 2^k) = 1 + 2 + 4 + 8 + 16 = 31
|
||||
var eval4 = Evaluator.Compile("sum(k=0, 4, 2^k)");
|
||||
Assert.Equal(31.0, eval4.Evaluate(0));
|
||||
|
||||
// sum with comma syntax: sum(n, 1, 5, n)
|
||||
var eval5 = Evaluator.Compile("sum(n, 1, 5, n)");
|
||||
Assert.Equal(15.0, eval5.Evaluate(0));
|
||||
});
|
||||
|
||||
runner.Add("Product notation: prod(n=start, end, f(n)) and prod(n, start, end, f(n))", () =>
|
||||
{
|
||||
// prod(n=1, 5, n) = 1 * 2 * 3 * 4 * 5 = 120
|
||||
var eval1 = Evaluator.Compile("prod(n=1, 5, n)");
|
||||
Assert.Equal(120.0, eval1.Evaluate(0));
|
||||
|
||||
// prod with variable x: prod(n=1, 3, x + n) -> for x=1: 2 * 3 * 4 = 24
|
||||
var eval2 = Evaluator.Compile("prod(n=1, 3, x + n)");
|
||||
Assert.Equal(24.0, eval2.Evaluate(1.0));
|
||||
|
||||
// prod with comma syntax: prod(i, 1, 4, 2) = 2^4 = 16
|
||||
var eval3 = Evaluator.Compile("prod(i, 1, 4, 2)");
|
||||
Assert.Equal(16.0, eval3.Evaluate(0));
|
||||
});
|
||||
|
||||
runner.Add("Calculus Integrals: int(t=a, b, f(t)) and integral(t, a, b, f(t))", () =>
|
||||
{
|
||||
// int(t=0, x, 2t) = [t^2]_0^x = x^2 -> for x=3, result is 9
|
||||
var eval1 = Evaluator.Compile("int(t=0, x, 2*t)");
|
||||
Assert.Within(9.0, eval1.Evaluate(3.0), 1e-4);
|
||||
|
||||
// int(t=0, pi, sin(t)) = 2.0
|
||||
var eval2 = Evaluator.Compile("int(t=0, pi, sin(t))");
|
||||
Assert.Within(2.0, eval2.Evaluate(0), 1e-4);
|
||||
|
||||
// int(t=1, e, 1/t) = ln(e) - ln(1) = 1.0
|
||||
var eval3 = Evaluator.Compile("integral(t=1, e, 1/t)");
|
||||
Assert.Within(1.0, eval3.Evaluate(0), 1e-4);
|
||||
|
||||
// Gaussian integral error function proxy: int(t=0, 1, exp(-t^2))
|
||||
var eval4 = Evaluator.Compile("int(t=0, 1, exp(-t^2))");
|
||||
Assert.Within(0.746824, eval4.Evaluate(0), 1e-3);
|
||||
});
|
||||
|
||||
runner.Add("Calculus Derivatives: diff(t=x, f(t)) and diff(f(x), x)", () =>
|
||||
{
|
||||
// d/dx [x^3] at x=2 is 3*(2^2) = 12
|
||||
var eval1 = Evaluator.Compile("diff(t=x, t^3)");
|
||||
Assert.Within(12.0, eval1.Evaluate(2.0), 1e-4);
|
||||
|
||||
// d/dx [sin(x)] at x=0 is cos(0) = 1
|
||||
var eval2 = Evaluator.Compile("diff(sin(x), x)");
|
||||
Assert.Within(1.0, eval2.Evaluate(0.0), 1e-4);
|
||||
|
||||
// d/dx [exp(x)] at x=1 is e
|
||||
var eval3 = Evaluator.Compile("diff(exp(x), x)");
|
||||
Assert.Within(System.Math.E, eval3.Evaluate(1.0), 1e-4);
|
||||
});
|
||||
|
||||
runner.Add("Factorials and Gamma functions: fact(n) and gamma(x)", () =>
|
||||
{
|
||||
var evalFact = Evaluator.Compile("fact(x)");
|
||||
Assert.Equal(1.0, evalFact.Evaluate(0));
|
||||
Assert.Equal(1.0, evalFact.Evaluate(1));
|
||||
Assert.Equal(2.0, evalFact.Evaluate(2));
|
||||
Assert.Equal(6.0, evalFact.Evaluate(3));
|
||||
Assert.Equal(24.0, evalFact.Evaluate(4));
|
||||
Assert.Equal(120.0, evalFact.Evaluate(5));
|
||||
|
||||
var evalGamma = Evaluator.Compile("gamma(x)");
|
||||
// Gamma(5) = 4! = 24
|
||||
Assert.Within(24.0, evalGamma.Evaluate(5.0), 1e-4);
|
||||
// Gamma(1.5) = sqrt(pi) / 2 ~ 0.886227
|
||||
Assert.Within(0.886227, evalGamma.Evaluate(1.5), 1e-4);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue