Skip to main content

LLM Symbolic Math

This notebook showcases using LLMs and Python to Solve Algebraic Equations. Under the hood is makes use of SymPy.

from langchain.llms import OpenAI
from langchain.chains.llm_symbolic_math.base import LLMSymbolicMathChain

llm = OpenAI(temperature=0)
llm_symbolic_math = LLMSymbolicMathChain.from_llm(llm)

API Reference:

Integrals and derivates

llm_symbolic_math.run("What is the derivative of sin(x)*exp(x) with respect to x?")
    'Answer: exp(x)*sin(x) + exp(x)*cos(x)'
llm_symbolic_math.run(
"What is the integral of exp(x)*sin(x) + exp(x)*cos(x) with respect to x?"
)
    'Answer: exp(x)*sin(x)'

Solve linear and differential equations

llm_symbolic_math.run('Solve the differential equation y" - y = e^t')
    'Answer: Eq(y(t), C2*exp(-t) + (C1 + t/2)*exp(t))'
llm_symbolic_math.run("What are the solutions to this equation y^3 + 1/3y?")
    'Answer: {0, -sqrt(3)*I/3, sqrt(3)*I/3}'
llm_symbolic_math.run("x = y + 5, y = z - 3, z = x * y. Solve for x, y, z")
    'Answer: (3 - sqrt(7), -sqrt(7) - 2, 1 - sqrt(7)), (sqrt(7) + 3, -2 + sqrt(7), 1 + sqrt(7))'