Connect symbols to numbersLesson 43 of 48 · 8 min
Turn a formula into a numerical function
Use lambdify with an explicit argument order and numerical backend.
Maths refresher: A rule and its implementation
A formula defines an input-output relationship. A numerical function implements that rule with a particular argument order and arithmetic.
Swapping the first two inputs gives . Keeping the constant 1 fixed isolates the effect of the argument order.
A symbolic expression is useful for derivation; a numerical function is convenient for repeated evaluation. lambdify translates a SymPy expression into a callable using a selected numerical library. It does not compile or prove the model automatically.
- Symbolic rule
- Input order (x, a, b)
- Swap x and a
State the argument order explicitly. Here the tuple (x,a,b) gives the order: x first, a second and the constant b third. Choose a backend such as math for ordinary scalar calls or numpy for array work. Keep the symbolic expression as the derivation and verification record.
Compare the numerical result with exact symbolic substitution at a check point. Both routes agree exactly here because the chosen inputs and operations give integers. For floating-point inputs the precision follows the numerical backend. Only lambdify expressions you trust; this translation uses generated executable code.
Will the callable infer argument roles from the numeric values passed to it?
Write a prediction (optional)
Symbolic formula and numerical evaluation
Select Run Python with the code unchanged.
Your saved draft is open. To use the published code, select Restore example in Tools. Use Recover draft to bring back your code.
Tools
About Python
Python and the course packages can load while you read. Each run starts with fresh user variables; imported modules stay loaded. Stop replaces the worker. Runtime details ↗
Ctrl+Enter or ⌘+Enter runs Python.
Execution error
Technical error details
Your result
Python text output
Compare with the worked example
Executed from the published Python during the site build. Your own run appears above. Build environment ↗
Worked text output
Symbolic formula: a*x**2 + b Inputs in order (x,a,b): (3, 2, 1) Numerical result: 19 Exact substitution: 19 Swapped x and a: 13
Read the starter result
Inputs in order (x,a,b) gives the order of the numerical function’s arguments. Numerical result and Exact substitution agree at that point. The swapped row changes the inputs.
Edit and compare
Replace inputs = (3,2,1) with inputs = (3,2,4). Select Run Python. Compare Numerical result with Exact substitution and Swapped x and a. Which tuple position controls the constant b?
Scope. lambdify uses code generation internally. Use trusted expressions and do not treat arbitrary external strings as safe executable mathematics.
Try a fresh case
What should accompany a lambdified function in a reproducible calculation?
Need a hint?
Hint 1
Write down what each positional input means.
Hint 2
Keep the generating expression and backend explicit.