derivative
Calculate derivatives of mathematical expressions to solve calculus problems, analyze functions, and find rates of change in variables.
Instructions
Calculate the derivative 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 differentiate with respect to (default: x) |
Implementation Reference
- index.js:9-17 (handler)Core handler function that parses the mathematical expression using mathjs and computes its derivative with respect to the specified variable.const derivative = (expr, variable = 'x') => { try { const node = math.parse(expr); const derivativeExpr = math.derivative(node, variable); return derivativeExpr.toString(); } catch (e) { return `Error: ${e.message}`; } };
- index.js:84-88 (schema)Defines the input schema (expression string required, variable string optional) and output schema (string) for the derivative tool.inputSchema: z.object({ expression: z.string().describe('Mathematical expression (e.g., "x^2", "e^x", "sin(x)")'), variable: z.string().optional().describe('Variable to differentiate with respect to (default: x)') }), outputSchema: z.string(),
- index.js:80-93 (registration)Registers the 'derivative' tool with Genkit using ai.defineTool, providing name, description, schema, and a wrapper handler that delegates to the core derivative function.ai.defineTool( { name: 'derivative', description: 'Calculate the derivative 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 differentiate with respect to (default: x)') }), outputSchema: z.string(), }, async ({ expression, variable = 'x' }) => { return derivative(expression, variable); } );
- index.js:90-92 (helper)Thin wrapper handler function that calls the core derivative helper with the input parameters.async ({ expression, variable = 'x' }) => { return derivative(expression, variable); }