darboux_sum
Calculate upper or lower Darboux sums to approximate definite integrals by dividing functions into subintervals. Specify function, limits, and number of intervals for numerical integration.
Instructions
Calculate the Darboux sum of a function
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Function to integrate | |
| variable | Yes | Variable of integration | |
| a | Yes | Lower limit of integration | |
| b | Yes | Upper limit of integration | |
| n | Yes | Number of subintervals | |
| type | No | Type: upper or lower Darboux sum | upper |
Implementation Reference
- index.js:254-277 (handler)The darbouxSum function is the core handler that computes the Darboux (upper or lower) sum approximation for the definite integral by parsing the expression with mathjs, evaluating at interval endpoints, selecting max/min values based on type, and summing with deltaX.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}`; } };
- index.js:308-317 (schema)Zod schema defining the input parameters (expression, variable, a, b, n, type) and output as number or string (for errors).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:304-322 (registration)ai.defineTool call that registers the 'darboux_sum' tool with Genkit, including description, schema, and async handler wrapper that delegates to darbouxSum function.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); } );