riemann_sum
Calculate the Riemann sum of a function to approximate definite integrals using left, right, midpoint, or trapezoid methods.
Instructions
Calculate the Riemann sum of a function using different methods
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 | |
| method | No | Method: left, right, midpoint, or trapezoid | midpoint |
Implementation Reference
- index.js:125-127 (handler)Handler function that receives tool inputs and delegates to the riemannSum helper function to compute the Riemann sum.async ({ expression, variable, a, b, n, method }) => { return riemannSum(expression, variable, a, b, n, method); }
- index.js:114-123 (schema)Zod schemas defining the input parameters and numeric output for the riemann_sum tool.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'), method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint') .describe('Method: left, right, midpoint, or trapezoid') }), outputSchema: z.number(),
- index.js:110-128 (registration)Complete registration of the 'riemann_sum' tool using Genkit's ai.defineTool, including name, description, schema, and handler.ai.defineTool( { name: 'riemann_sum', description: 'Calculate the Riemann sum of a function using different methods', 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'), method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint') .describe('Method: left, right, midpoint, or trapezoid') }), outputSchema: z.number(), }, async ({ expression, variable, a, b, n, method }) => { return riemannSum(expression, variable, a, b, n, method); } );
- index.js:43-76 (helper)Core helper function that performs the actual Riemann sum calculation using mathjs for expression parsing and evaluation, supporting left, right, midpoint, and trapezoid methods.const riemannSum = (expr, variable, a, b, n, method = 'midpoint') => { try { const deltaX = (b - a) / n; let sum = 0; const node = math.parse(expr); const scope = {}; if (method === 'left' || method === 'right') { const offset = method === 'right' ? 1 : 0; for (let i = 0; i < n; i++) { const x = a + (i + offset) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'midpoint') { for (let i = 0; i < n; i++) { const x = a + (i + 0.5) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'trapezoid') { for (let i = 0; i <= n; i++) { const x = a + i * deltaX; scope[variable] = x; const coef = (i === 0 || i === n) ? 0.5 : 1; sum += coef * math.evaluate(node, scope) * deltaX; } } return sum; } catch (e) { return `Error: ${e.message}`; } };