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.

f(x,a,b)=ax2+b,f(3,2,1)=19f(x,a,b)=ax^2+b,\qquad f(3,2,1)=19
Worked example

Swapping the first two inputs gives f(2,3,1)=13f(2,3,1)=13. Keeping the constant 1 fixed isolates the effect of the argument order.

Maths prerequisite map ↗

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.

Argument order changes the value
  1. Symbolic rule
    ax2+ba x^2+b
  2. Input order (x, a, b)
    (3,2,1)↦19(3,2,1)\mapsto19
  3. Swap x and a
    (2,3,1)↦13(2,3,1)\mapsto13
Starter reference. Swapping the first two arguments changes the assignments to x and a. Keeping b fixed isolates the effect of the argument order.

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.

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.

Compare with the worked example

Executed from the published Python during the site build. Your own run appears above. Build environment ↗

Symbolic formula
ax2+ba x^{2} + b
Inputs in order (x,a,b)
(3, 2, 1)\left( 3, \ 2, \ 1\right)
Numerical result
1919
Exact substitution
1919
Swapped x and a
1313
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?

Return to the editor

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?

Choose an answer
Need a hint?
Hint 1

Write down what each positional input means.

Hint 2

Keep the generating expression and backend explicit.

Your notesNotes save in this browser when storage is available.