solve
Solve equations for specific variables using symbolic mathematics. Input an equation and target variable to get the solution.
Instructions
Solve an equation for a variable
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Equation to solve (e.g., "x^2 = 4") | |
| variable | Yes | Variable to solve for |
Implementation Reference
- index.js:350-364 (handler)Handler function that processes the input equation by normalizing it to zero form, parses it using math.parse, solves for the specified variable using math.solve, converts solutions to strings, and handles errors.async ({ expression, variable }) => { try { // Convert equation to standard form (expression = 0) const sides = expression.split('=').map(s => s.trim()); if (sides.length !== 2) { throw new Error('Invalid equation format. Use "expression = value"'); } const equationNode = math.parse(`${sides[0]}-(${sides[1]})`); const solutions = math.solve(equationNode, variable); return solutions.map(sol => sol.toString()); } catch (e) { return [`Error: ${e.message}`]; } }
- index.js:341-349 (schema)Schema definition for the 'solve' tool: input requires 'expression' (equation string) and 'variable' (string to solve for); output is an array of solution strings.{ name: 'solve', description: 'Solve an equation for a variable', inputSchema: z.object({ expression: z.string().describe('Equation to solve (e.g., "x^2 = 4")'), variable: z.string().describe('Variable to solve for') }), outputSchema: z.array(z.string()), },
- index.js:340-365 (registration)Registration of the 'solve' tool using ai.defineTool, providing name, description, input/output schemas, and inline handler implementation.ai.defineTool( { name: 'solve', description: 'Solve an equation for a variable', inputSchema: z.object({ expression: z.string().describe('Equation to solve (e.g., "x^2 = 4")'), variable: z.string().describe('Variable to solve for') }), outputSchema: z.array(z.string()), }, async ({ expression, variable }) => { try { // Convert equation to standard form (expression = 0) const sides = expression.split('=').map(s => s.trim()); if (sides.length !== 2) { throw new Error('Invalid equation format. Use "expression = value"'); } const equationNode = math.parse(`${sides[0]}-(${sides[1]})`); const solutions = math.solve(equationNode, variable); return solutions.map(sol => sol.toString()); } catch (e) { return [`Error: ${e.message}`]; } } );