integral
Calculate indefinite integrals of mathematical expressions like x^2, e^x, or sin(x) with respect to a specified variable. This tool performs symbolic calculus for integration problems.
Instructions
Calculate the indefinite integral of a mathematical expression
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Mathematical expression (e.g., "x^2", "e^x", "sin(x)") | |
| variable | No | Variable to integrate with respect to (default: x) |
Implementation Reference
- index.js:19-41 (handler)Core implementation of the integral computation logic, handling basic polynomials and predefined integration rules for common functions.const integral = (expr, variable = 'x') => { try { // For basic polynomials const powerMatch = expr.match(new RegExp(`${variable}\\^(\\d+)`)); if (powerMatch) { const n = parseInt(powerMatch[1]); return `${variable}^${n + 1}/${n + 1}`; } // For basic functions const rules = { 'e^x': 'e^x', '1/x': `ln|${variable}|`, [`sin(${variable})`]: `-cos(${variable})`, [`cos(${variable})`]: `sin(${variable})`, 'x': 'x^2/2' }; return rules[expr] || 'Cannot compute integral'; } catch (e) { return `Error: ${e.message}`; } };
- index.js:97-104 (schema)Tool schema definition including input parameters (expression and optional variable) and string output schema.name: 'integral', description: 'Calculate the indefinite integral of a mathematical expression', inputSchema: z.object({ expression: z.string().describe('Mathematical expression (e.g., "x^2", "e^x", "sin(x)")'), variable: z.string().optional().describe('Variable to integrate with respect to (default: x)') }), outputSchema: z.string(), },
- index.js:95-108 (registration)Registration of the 'integral' tool with Genkit's ai.defineTool, linking schema to the handler wrapper that invokes the core integral function.ai.defineTool( { name: 'integral', description: 'Calculate the indefinite integral of a mathematical expression', inputSchema: z.object({ expression: z.string().describe('Mathematical expression (e.g., "x^2", "e^x", "sin(x)")'), variable: z.string().optional().describe('Variable to integrate with respect to (default: x)') }), outputSchema: z.string(), }, async ({ expression, variable = 'x' }) => { return integral(expression, variable); } );