darboux_sum
Compute the upper or lower Darboux sum for a function over a specified interval, dividing it into subintervals to approximate the integral.
Instructions
Calculate the Darboux sum of a function
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | Lower limit of integration | |
| b | Yes | Upper limit of integration | |
| expression | Yes | Function to integrate | |
| n | Yes | Number of subintervals | |
| type | No | Type: upper or lower Darboux sum | upper |
| variable | Yes | Variable of integration |
Implementation Reference
- index.js:304-322 (registration)Registration of the 'darboux_sum' tool using ai.defineTool, including name, description, input/output schemas, and handler.ai.defineTool( { name: 'darboux_sum', description: 'Calculate the Darboux sum of a function', inputSchema: z.object({ expression: z.string().describe('Function to integrate'), variable: z.string().describe('Variable of integration'), a: z.number().describe('Lower limit of integration'), b: z.number().describe('Upper limit of integration'), n: z.number().describe('Number of subintervals'), type: z.enum(['upper', 'lower']).default('upper') .describe('Type: upper or lower Darboux sum') }), outputSchema: z.union([z.number(), z.string()]), }, async ({ expression, variable, a, b, n, type }) => { return darbouxSum(expression, variable, a, b, n, type); } );
- index.js:319-321 (handler)The handler function for the darboux_sum tool, which delegates to the darbouxSum helper.async ({ expression, variable, a, b, n, type }) => { return darbouxSum(expression, variable, a, b, n, type); }
- index.js:308-317 (schema)Input and output schema definitions for the darboux_sum tool using Zod.inputSchema: z.object({ expression: z.string().describe('Function to integrate'), variable: z.string().describe('Variable of integration'), a: z.number().describe('Lower limit of integration'), b: z.number().describe('Upper limit of integration'), n: z.number().describe('Number of subintervals'), type: z.enum(['upper', 'lower']).default('upper') .describe('Type: upper or lower Darboux sum') }), outputSchema: z.union([z.number(), z.string()]),
- index.js:254-277 (helper)Helper function implementing the core Darboux (Riemann) sum logic: partitions interval [a,b] into n parts, evaluates expression at endpoints, uses max/min for upper/lower sums.const darbouxSum = (expr, variable, a, b, n, type = 'upper') => { try { const deltaX = (b - a) / n; let sum = 0; const node = math.parse(expr); const scope = {}; for (let i = 0; i < n; i++) { const x1 = a + i * deltaX; const x2 = x1 + deltaX; scope[variable] = x1; const y1 = math.evaluate(node, scope); scope[variable] = x2; const y2 = math.evaluate(node, scope); const value = type === 'upper' ? Math.max(y1, y2) : Math.min(y1, y2); sum += value * deltaX; } return sum; } catch (e) { return `Error: ${e.message}`; } };