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.
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.
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.
- Derive in SymPy
- Generate C text
- Next, outside this lab
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.
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
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.
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?
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.