mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-25 20:33:50 +00:00
402 lines
24 KiB
C#
402 lines
24 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
using AIStudio.Tools.Mathematics;
|
|
using AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations.Mathematics;
|
|
|
|
using AngouriMath;
|
|
using AngouriMath.Core;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
namespace AIStudio.Tests.Tools.Mathematics;
|
|
|
|
/// <summary>
|
|
/// Checks compute_symbolically through the computation service, so it runs with the settings of the tools: explicit
|
|
/// parsing, and solving without Newton's method.
|
|
/// </summary>
|
|
[TestFixture]
|
|
public sealed class ComputeSymbolicallyToolTests
|
|
{
|
|
private static readonly MathComputationService SERVICE = new(NullLogger<MathComputationService>.Instance);
|
|
|
|
private static Task<JsonObject> Compute(SymbolicOperation operation, string expression, string? variable = null, int order = 1, string? lowerBound = null, string? upperBound = null, string? approach = null, ApproachFrom direction = ApproachFrom.BothSides) =>
|
|
SERVICE.ComputeAsync(_ => ComputeSymbolicallyTool.Compute(new SymbolicRequest(operation, expression, variable, order, lowerBound, upperBound, approach, direction)));
|
|
|
|
private static string Text(JsonObject json, string property) => json[property]!.GetValue<string>();
|
|
|
|
private static IEnumerable<string> Issues(JsonObject json) => json["issues"]?.AsArray().Select(issue => issue!.GetValue<string>()) ?? [];
|
|
|
|
private static JsonElement Arguments(string json)
|
|
{
|
|
using var document = JsonDocument.Parse(json);
|
|
return document.RootElement.Clone();
|
|
}
|
|
|
|
private static string RequestRefusal(string json) => Assert.Throws<ArgumentException>(() => ComputeSymbolicallyTool.ReadRequest(Arguments(json)))!.Message;
|
|
|
|
/// <summary>
|
|
/// Writes the solutions of an equation in x as the tool does, for a set of solutions given as text.
|
|
/// </summary>
|
|
private static Task<JsonObject> DescribeSolutions(string equation, string solutions) => SERVICE.ComputeAsync(_ =>
|
|
{
|
|
var statement = MathExpressionReader.Read(equation, "expression");
|
|
var json = new JsonObject();
|
|
var issues = new List<string>();
|
|
ComputeSymbolicallyTool.Solutions(json, issues, statement, statement, MathS.Var("x"), MathExpressionReader.Read(solutions, "solutions"));
|
|
json["issues"] = new JsonArray([..issues.Select(issue => JsonValue.Create(issue))]);
|
|
return json;
|
|
});
|
|
|
|
private static JsonNode Member(JsonObject json, int index) => json["solutions"]![index]!;
|
|
|
|
[Test]
|
|
public async Task SimplifiesAndStatesTheCondition()
|
|
{
|
|
var identity = await Compute(SymbolicOperation.SIMPLIFY, "sin(x)^2 + cos(x)^2");
|
|
var quotient = await Compute(SymbolicOperation.SIMPLIFY, "(x^2 - 1)/(x - 1)");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(identity, "result"), Is.EqualTo("1"));
|
|
Assert.That(Text(identity, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(identity["complete"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Text(quotient, "result"), Is.EqualTo("x + 1"));
|
|
Assert.That(Text(quotient, "condition"), Is.EqualTo("not x - 1 = 0"));
|
|
Assert.That(Text(quotient, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ExpandsAndFactors()
|
|
{
|
|
var expanded = await Compute(SymbolicOperation.EXPAND, "(x + 1)^3");
|
|
var unchanged = await Compute(SymbolicOperation.EXPAND, "(x + y + z + w)^60");
|
|
var factored = await Compute(SymbolicOperation.FACTOR, "x^4 - 1");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(expanded, "result"), Is.EqualTo("1 + 3 * x + 3 * x ^ 2 + x ^ 3"));
|
|
Assert.That(Text(expanded, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Issues(unchanged), Has.Some.StartsWith("The tool left the expression as it is"));
|
|
Assert.That(Text(factored, "result"), Is.EqualTo("(x + 1) * (x - 1) * (x ^ 2 + 1)"));
|
|
Assert.That(Text(factored, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task DifferentiatesAndChecksEachOrder()
|
|
{
|
|
var product = await Compute(SymbolicOperation.DIFFERENTIATE, "x^2*sin(x)");
|
|
var third = await Compute(SymbolicOperation.DIFFERENTIATE, "x^5", order: 3);
|
|
var parameter = await Compute(SymbolicOperation.DIFFERENTIATE, "a*x^2", "x");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(product, "result"), Is.EqualTo("x ^ 2 * cos(x) + 2 * x * sin(x)"));
|
|
Assert.That(Text(product, "variable"), Is.EqualTo("x"));
|
|
Assert.That(Text(product, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(third, "result"), Is.EqualTo("60 * x ^ 2"));
|
|
Assert.That(third["order"]!.GetValue<int>(), Is.EqualTo(3));
|
|
Assert.That(Text(third, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(parameter, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task IntegratesWithoutBounds()
|
|
{
|
|
var power = await Compute(SymbolicOperation.INTEGRATE, "x^2");
|
|
var withoutClosedForm = await Compute(SymbolicOperation.INTEGRATE, "sin(sin(x))");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(power, "result"), Is.EqualTo("x ^ 3 / 3 + C"));
|
|
Assert.That(Text(power, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Issues(power), Has.Member("C stands for any constant."));
|
|
Assert.That(withoutClosedForm["complete"]!.GetValue<bool>(), Is.False);
|
|
Assert.That(Text(withoutClosedForm, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Issues(withoutClosedForm), Has.Member("The tool found no antiderivative in closed form."));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task IntegratesWithBoundsAndChecksByQuadrature()
|
|
{
|
|
var polynomial = await Compute(SymbolicOperation.INTEGRATE, "x^2", lowerBound: "0", upperBound: "1");
|
|
var arctangent = await Compute(SymbolicOperation.INTEGRATE, "1/(1 + x^2)", lowerBound: "0", upperBound: "+oo");
|
|
var bose = await Compute(SymbolicOperation.INTEGRATE, "x^3/(e^x - 1)", lowerBound: "0", upperBound: "+oo");
|
|
var pole = await Compute(SymbolicOperation.INTEGRATE, "1/x", lowerBound: "-1", upperBound: "1");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(polynomial, "result"), Is.EqualTo("1/3"));
|
|
Assert.That(Text(polynomial, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(arctangent, "result"), Is.EqualTo("pi / 2"));
|
|
Assert.That(Text(arctangent, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(bose["complete"]!.GetValue<bool>(), Is.False, "AngouriMath finds no closed form for pi^4/15.");
|
|
Assert.That(Issues(bose), Has.Member("The tool found no closed form for this integral."));
|
|
Assert.That(Text(pole, "result"), Is.EqualTo("NaN"));
|
|
Assert.That(Issues(pole), Has.Some.StartsWith("The result is NaN: the integral has no value"));
|
|
Assert.That(Text(pole, "verification"), Is.EqualTo("not_possible"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task TakesLimits()
|
|
{
|
|
var sinc = await Compute(SymbolicOperation.LIMIT, "sin(x)/x", approach: "0");
|
|
var fromTheRight = await Compute(SymbolicOperation.LIMIT, "1/x", approach: "0", direction: ApproachFrom.Right);
|
|
var fromBothSides = await Compute(SymbolicOperation.LIMIT, "1/x", approach: "0");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(sinc, "result"), Is.EqualTo("1"));
|
|
Assert.That(Text(sinc, "direction"), Is.EqualTo("both"));
|
|
Assert.That(Text(sinc, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Text(fromTheRight, "result"), Is.EqualTo("+oo"));
|
|
Assert.That(Text(fromTheRight, "direction"), Is.EqualTo("right"));
|
|
Assert.That(Text(fromBothSides, "result"), Is.EqualTo("NaN"));
|
|
Assert.That(Issues(fromBothSides), Has.Some.StartsWith("The result is NaN, which the tool gives where the limit does not exist"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task SolvesAndChecksEverySolution()
|
|
{
|
|
var quadratic = await Compute(SymbolicOperation.SOLVE, "x^2 - 5*x + 6 = 0");
|
|
var complex = await Compute(SymbolicOperation.SOLVE, "x^2 + 1 = 0");
|
|
var periodic = await Compute(SymbolicOperation.SOLVE, "sin(x) = 1/2");
|
|
var withoutEquals = await Compute(SymbolicOperation.SOLVE, "x^2 - 4");
|
|
var tiny = await Compute(SymbolicOperation.SOLVE, "1.5e30*x = 3");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(quadratic["solutions"]!.AsArray().Select(solution => solution!["value"]!.GetValue<string>()), Is.EqualTo(new[] { "2", "3" }));
|
|
Assert.That(tiny["solutions"]![0]!["decimal"]!.GetValue<string>(), Is.EqualTo("2e-30"), "The decimal of a tiny solution keeps its digits, see NumericEvaluator.EvaluateAt.");
|
|
Assert.That(quadratic["solutions"]![0]!["real"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Text(quadratic, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(complex["solutions"]![0]!["real"]!.GetValue<bool>(), Is.False);
|
|
Assert.That(Text(complex, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(periodic, "verification"), Is.EqualTo("passed"), "The index takes integer values in the check.");
|
|
Assert.That(Member(periodic, 0)["holds_for"]!.GetValue<string>(), Is.EqualTo("every integer n"));
|
|
Assert.That(Member(periodic, 1)["at_index_0"]!["value"]!.GetValue<string>(), Is.EqualTo("5/6 * pi"));
|
|
Assert.That(Issues(withoutEquals), Has.Member("The expression has no '=', so it was solved for expression = 0."));
|
|
Assert.That(withoutEquals["solutions"]!.AsArray(), Has.Count.EqualTo(2));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ListsAndChecksTheMembersOfAFamilyOfSolutions()
|
|
{
|
|
var periodic = await DescribeSolutions("sin(x) = 1/2", "union({ pi/6 + 2*pi*n, pi - pi/6 + 2*pi*n }, n in ZZ)");
|
|
var exponential = await DescribeSolutions("2^x = 5", "union({ log(2, 5) + 2*pi*i*n/ln(2) }, n in ZZ)");
|
|
var wrong = await DescribeSolutions("sin(x) = 1/2", "union({ pi/5 + 2*pi*n }, n in ZZ)");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(periodic["solutions"]!.AsArray(), Has.Count.EqualTo(2));
|
|
Assert.That(Member(periodic, 0)["holds_for"]!.GetValue<string>(), Is.EqualTo("every integer n"));
|
|
Assert.That(Member(periodic, 0)["at_index_0"]!["value"]!.GetValue<string>(), Is.EqualTo("pi / 6"));
|
|
Assert.That(Member(periodic, 0)["at_index_0"]!["decimal"]!.GetValue<string>(), Does.StartWith("0.52359877559"));
|
|
Assert.That(Member(periodic, 0)["at_index_0"]!["real"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Member(periodic, 0).AsObject().ContainsKey("decimal"), Is.False, "A member with an index has no single value.");
|
|
Assert.That(Text(periodic, "verification"), Is.EqualTo("passed"), "The index takes integer values in the check.");
|
|
Assert.That(periodic["complete"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Member(exponential, 0)["at_index_0"]!["decimal"]!.GetValue<string>(), Does.StartWith("2.32192809488"), "The solution for the index 0 is the real one, which a model needs most.");
|
|
Assert.That(Text(exponential, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(wrong, "verification"), Is.EqualTo("failed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task GivesTheRealSolutionOfAnExponentialEquation()
|
|
{
|
|
// The decimals are those of Python's decimal module, to 15 significant digits:
|
|
var power = await Compute(SymbolicOperation.SOLVE, "2^x = 5");
|
|
var doubling = await Compute(SymbolicOperation.SOLVE, "1.05^x = 2");
|
|
var lambert = await Compute(SymbolicOperation.SOLVE, "x*e^x = 2");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Member(power, 0)["at_index_0"]!["decimal"]!.GetValue<string>(), Is.EqualTo("2.32192809488736"));
|
|
Assert.That(Text(power, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Member(doubling, 0)["at_index_0"]!["decimal"]!.GetValue<string>(), Is.EqualTo("14.2066990828905"), "The years in which 5 % interest doubles a sum.");
|
|
Assert.That(Member(doubling, 0)["at_index_0"]!["real"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Member(lambert, 0)["at_index_0"]!["value"]!.GetValue<string>(), Is.EqualTo("lambertw(2)"));
|
|
Assert.That(Member(lambert, 0)["at_index_0"]!["decimal"]!.GetValue<string>(), Is.EqualTo("0.852605502013725"));
|
|
Assert.That(Text(lambert, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task GivesSolutionsCalculateTakes()
|
|
{
|
|
foreach (var equation in new[] { "2^x = 5", "x*e^x = 2", "sin(x) = 1/2" })
|
|
{
|
|
var solution = Member(await Compute(SymbolicOperation.SOLVE, equation), 0)["at_index_0"]!;
|
|
var value = await SERVICE.ComputeAsync(token => CalculateTool.Calculate(solution["value"]!.GetValue<string>(), 15, token));
|
|
Assert.That(value["decimal"]!.GetValue<string>(), Is.EqualTo(solution["decimal"]!.GetValue<string>()), equation);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ChecksAnAntiderivativeFarLargerThanTheIntegrand()
|
|
{
|
|
// The antiderivative -gamma(41, x) is about 40!, its derivative x^40 * e^(-x) about 1e-9:
|
|
var integrated = await Compute(SymbolicOperation.INTEGRATE, "x^40*e^(-x)");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(integrated, "result"), Is.EqualTo("-gamma(41, x) + C"));
|
|
Assert.That(Text(integrated, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ChecksNoSamplePointWhichCouldNotBeStopped()
|
|
{
|
|
// At x = 0.61, z is 6,100, the factorial that of 610,000, and the power a fraction of 378,000 digits; each
|
|
// would take seconds or minutes without a way to stop it:
|
|
var incompleteGamma = await Compute(SymbolicOperation.SIMPLIFY, "gamma(1/2, 10000*x)");
|
|
var factorial = await Compute(SymbolicOperation.SIMPLIFY, "(10^6*x)!");
|
|
var derivative = await Compute(SymbolicOperation.DIFFERENTIATE, "(10^6*x)!", "x");
|
|
var power = await Compute(SymbolicOperation.SIMPLIFY, "x^100000 + 1");
|
|
|
|
// The terms of the expansion each have a few hundred digits, which is no reason to leave a point out:
|
|
var expansion = await Compute(SymbolicOperation.EXPAND, "(x + 1)^200");
|
|
var smallerFactorial = await Compute(SymbolicOperation.SIMPLIFY, "(1000*x)!");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Text(incompleteGamma, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Text(factorial, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Text(derivative, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Text(power, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(Text(expansion, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(smallerFactorial, "verification"), Is.EqualTo("passed"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnswersWhatRanPastTheBudgetBefore()
|
|
{
|
|
// Each of these took the whole budget of 10 seconds with version 2.6.0:
|
|
var mixedBases = await Compute(SymbolicOperation.SOLVE, "(-2)^x + 4^x = 0");
|
|
var quintic = await Compute(SymbolicOperation.SOLVE, "x^5 + x - sqrt(2) = 0");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(mixedBases["complete"]!.GetValue<bool>(), Is.True);
|
|
Assert.That(Text(mixedBases, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(quintic["complete"]!.GetValue<bool>(), Is.False);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task TakesFamiliesOfSolutionsApart()
|
|
{
|
|
var withoutZero = await DescribeSolutions("sin(x)/x = 0", "union({ pi*n }, n in ZZ \\ { 0 })");
|
|
var nested = await DescribeSolutions("e^(sin(x)) = 1", "union(union({ arcsin(2*pi*i*n) + 2*pi*m, pi - arcsin(2*pi*i*n) + 2*pi*m }, m in ZZ), n in ZZ)");
|
|
var joined = await DescribeSolutions("sin(x)*(x - 7) = 0", "{ 7 } \\/ union({ pi*n }, n in ZZ)");
|
|
var unsolved = await DescribeSolutions("sin(x) = x^5", "{ x : sin(x) = x^5 } \\/ union({ pi*n }, n in ZZ)");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(Member(withoutZero, 0)["holds_for"]!.GetValue<string>(), Does.StartWith("every n in ZZ"));
|
|
Assert.That(Member(withoutZero, 0).AsObject().ContainsKey("at_index_0"), Is.False, "0 is not among the values of the index.");
|
|
Assert.That(Text(withoutZero, "verification"), Is.EqualTo("passed"), "At 0, the index would take a value without a solution.");
|
|
Assert.That(Member(nested, 0)["holds_for"]!.GetValue<string>(), Is.EqualTo("every integer n and every integer m"), "The outer index comes first.");
|
|
Assert.That(Member(nested, 0)["at_index_0"]!["value"]!.GetValue<string>(), Is.EqualTo("0"));
|
|
Assert.That(Text(nested, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(joined["solutions"]!.AsArray(), Has.Count.EqualTo(2));
|
|
Assert.That(Member(joined, 0).AsObject().ContainsKey("holds_for"), Is.False);
|
|
Assert.That(Member(joined, 1)["holds_for"]!.GetValue<string>(), Is.EqualTo("every integer n"));
|
|
Assert.That(Text(joined, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(unsolved["complete"]!.GetValue<bool>(), Is.False, "A part without a closed form leaves the solutions incomplete.");
|
|
Assert.That(unsolved.ContainsKey("solutions"), Is.False);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task WorksWithSumsAndDivisors()
|
|
{
|
|
var gauss = await Compute(SymbolicOperation.SOLVE, "sum(k, k, 1, n) = 55", "n");
|
|
var derivative = await Compute(SymbolicOperation.DIFFERENTIATE, "sum(k*x^k, k, 1, 3)", "x");
|
|
var divisor = await Compute(SymbolicOperation.SIMPLIFY, "gcd(12, 18) * x");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(gauss["solutions"]!.AsArray().Select(solution => solution!["value"]!.GetValue<string>()), Is.EqualTo(new[] { "10" }), "The closed form holds for n >= 0 only, so -11 is no solution.");
|
|
Assert.That(Text(gauss, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(derivative, "result"), Is.EqualTo("1 + 4 * x + 9 * x ^ 2"));
|
|
Assert.That(Text(derivative, "verification"), Is.EqualTo("passed"));
|
|
Assert.That(Text(divisor, "result"), Is.EqualTo("6 * x"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task SaysWhenThereIsNoSolutionInClosedForm()
|
|
{
|
|
var quintic = await Compute(SymbolicOperation.SOLVE, "x^5 + 3*x + 1 = 0");
|
|
var none = await Compute(SymbolicOperation.SOLVE, "x + 1 = x");
|
|
var inequality = await Compute(SymbolicOperation.SOLVE, "x^2 < 4");
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(quintic["complete"]!.GetValue<bool>(), Is.False, "Without Newton's method, there are no approximate roots.");
|
|
Assert.That(Text(quintic, "verification"), Is.EqualTo("not_possible"));
|
|
Assert.That(none["solutions"]!.AsArray(), Is.Empty);
|
|
Assert.That(Issues(none), Has.Member("There is no solution."));
|
|
Assert.That(Text(inequality, "result"), Is.EqualTo("(-2; 2)"));
|
|
Assert.That(inequality.ContainsKey("solutions"), Is.False);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void RefusesWhatTheOperationCannotTake()
|
|
{
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(async () => await Compute(SymbolicOperation.SIMPLIFY, "x = 2"), Throws.InstanceOf<MathComputationException>().With.Message.EqualTo("Argument 'expression' is a statement, such as an equation or a comparison, which only the operation 'solve' takes."));
|
|
Assert.That(async () => await Compute(SymbolicOperation.DIFFERENTIATE, "a*x^2"), Throws.InstanceOf<MathComputationException>().With.Message.EqualTo("Argument 'expression' has the variables a, x, so argument 'variable' has to say which one to differentiate by."));
|
|
Assert.That(async () => await Compute(SymbolicOperation.INTEGRATE, "2"), Throws.InstanceOf<MathComputationException>().With.Message.EqualTo("Argument 'expression' has no variable to integrate over."));
|
|
Assert.That(async () => await Compute(SymbolicOperation.SOLVE, "x^2 = 2", "pi"), Throws.InstanceOf<MathComputationException>().With.Message.EqualTo("Argument 'variable' must name a variable, such as x, but was 'pi'."));
|
|
Assert.That(async () => await Compute(SymbolicOperation.EXPAND, "(x + y)^500"), Throws.InstanceOf<MathComputationException>().With.Message.Contains("raises a sum to the power of 500"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void RefusesArgumentsTheOperationDoesNotUse()
|
|
{
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(RequestRefusal("""{"operation": "simplify", "expression": "x", "variable": "x"}"""), Is.EqualTo("Argument 'variable' is not used by the operation 'simplify'. Leave it out."));
|
|
Assert.That(RequestRefusal("""{"operation": "integrate", "expression": "x", "order": 2}"""), Is.EqualTo("Argument 'order' is not used by the operation 'integrate'. Leave it out."));
|
|
Assert.That(RequestRefusal("""{"operation": "integrate", "expression": "x", "lower_bound": "0"}"""), Is.EqualTo("Argument 'lower_bound' needs argument 'upper_bound' as well, since a definite integral has two bounds. Leave both out for an antiderivative."));
|
|
Assert.That(RequestRefusal("""{"operation": "limit", "expression": "1/x"}"""), Does.StartWith("Argument 'approach' is required for the operation 'limit'"));
|
|
Assert.That(RequestRefusal("""{"operation": "solve", "expression": "x = 1", "direction": "left"}"""), Is.EqualTo("Argument 'direction' is not used by the operation 'solve'. Leave it out."));
|
|
Assert.That(RequestRefusal("""{"operation": "differentiate", "expression": "x", "order": 11}"""), Is.EqualTo("Argument 'order' must be at most 10, but was 11. Leave it out for the first derivative."));
|
|
Assert.That(RequestRefusal("""{"operation": "derive", "expression": "x"}"""), Is.EqualTo("Argument 'operation' must be one of simplify, expand, factor, differentiate, integrate, limit, solve, but was 'derive'."));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void ReadsTheArgumentsOfALimit()
|
|
{
|
|
var request = ComputeSymbolicallyTool.ReadRequest(Arguments("""{"operation": "limit", "expression": "1/x", "variable": null, "approach": "0", "direction": "left"}"""));
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(request.Operation, Is.EqualTo(SymbolicOperation.LIMIT));
|
|
Assert.That(request.Variable, Is.Null, "In strict mode, a model passes null for an argument it leaves out.");
|
|
Assert.That(request.Approach, Is.EqualTo("0"));
|
|
Assert.That(request.Direction, Is.EqualTo(ApproachFrom.Left));
|
|
Assert.That(request.Order, Is.EqualTo(1));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ExecutesThroughTheComputationService()
|
|
{
|
|
var tool = new ComputeSymbolicallyTool(null!, SERVICE);
|
|
var result = await tool.ExecuteAsync(Arguments("""{"operation": "factor", "expression": "x^2 - 5*x + 6"}"""), null!);
|
|
Assert.That(result.JsonContent!["result"]!.GetValue<string>(), Is.EqualTo("(x - 3) * (x - 2)"));
|
|
}
|
|
|
|
[Test]
|
|
public void TellsTheModelNothingAboutTheLibrary()
|
|
{
|
|
var definition = new ComputeSymbolicallyTool(null!, SERVICE).GetDefinition();
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(definition.SystemPromptInstructions, Does.Not.Contain("AngouriMath"), "A model would take the name for something it may call or look up.");
|
|
Assert.That(definition.Function.DescriptionForLLM, Does.Not.Contain("AngouriMath"));
|
|
Assert.That(definition.Function.Parameters.GetRawText(), Does.Not.Contain("AngouriMath"));
|
|
}
|
|
}
|
|
} |