Connect symbols to numbersLesson 47 of 48 · 8 min

Export an expression as code

Inspect generated C code and separate printing from compilation and validation.

Maths refresher: One formula, several representations

Mathematical notation, a SymPy expression and C source can describe the same rule using different syntax and arithmetic conventions.

f(x)=x2+13,f′(x)=2xf(x)=x^2+\frac13,\qquad f^{\prime}(x)=2x
Worked example

A C printer may emit pow(x, 2)+1.0/3.0. That source text still needs compilation before it becomes an executed C calculation.

Maths prerequisite map ↗

A symbolic derivation can produce an implementation in another language. SymPy code printers translate supported expressions into source text. The output remains a representation that needs a host function, declarations, libraries and tests in the target environment.

Code generation stops at source text
  1. Derive in SymPy
    x2+13x^2+\frac13
  2. Generate C text
    pow(x, 2) + 1.0/3.0\texttt{pow(x, 2) + 1.0/3.0}
  3. Next, outside this lab
    compile and validate\text{compile and validate}
Starter reference. The build executes Python and prints C source. The C is not compiled, run or benchmarked here.

The example generates a C expression for a simple formula and its derivative. The browser is running Python here; it is not compiling or benchmarking C. The printed code may use functions such as pow whose declarations and linking requirements belong to the target toolchain.

Retain the symbolic expression, its assumptions and reference values alongside generated code. Before deploying it, compile in the intended environment and compare numerical behaviour over the supported range. Code generation transfers a formula; it does not establish units, input validity or numerical stability automatically.

Does seeing generated C text mean that C has been compiled or run in this lesson?

Write a prediction (optional)

Symbolic calculation with generated C source

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 ↗

Source expression
x2+13x^{2} + \frac{1}{3}
Symbolic derivative
2x2 x
Exact reference at x=2
133\frac{13}{3}
Worked text output
GENERATED C ONLY; not compiled or executed:
value: pow(x, 2) + 1.0/3.0
derivative: 2*x
Source expression: x**2 + 1/3
Symbolic derivative: 2*x
Exact reference at x=2: 13/3

Read the starter result

Find the generated C in Python text output. The experiment only executed the SymPy derivation and printer in Python. Use the labelled symbolic rows to verify the expression and derivative independently of the generated text.

Edit and compare

Replace expression = x**2+sp.Rational(1,3) with expression = sp.exp(-x). Select Run Python and read Python text output. Which C math function appears in the generated value and derivative lines? Verify the symbolic derivative separately.

Return to the editor

Scope. This lesson does not compile C or make performance claims; complex code-generation systems require more tooling and tests.

Try a fresh case

Which step is still needed after printing target-language source?

Choose an answer
Need a hint?
Hint 1

Distinguish a string of source from a running program.

Hint 2

Validate with the compiler and libraries you intend to use.

Your notesNotes save in this browser when storage is available.