5.4.20 · D1Scientific Computing (Python)

Foundations — SymPy — symbolic algebra, calculus, ODE solving

1,722 words8 min readBack to topic

Before you can read a single line of the parent note, you must own a handful of tiny ideas. This page builds every one of them from nothing — no notation is used before it is drawn and explained. We go slow on purpose.


1. Number vs. symbol — the deepest split

The picture. Imagine a number line. A number is a pin stuck at one exact spot. A symbol is a slider that can sit anywhere — you keep it as a slider precisely so you can reason about all positions at once.

Figure — SymPy — symbolic algebra, calculus, ODE solving

WHY the topic needs this. NumPy (see NumPy — numerical arrays) pins everything down to a number immediately. SymPy refuses: it keeps the slider, so it can factor, differentiate and solve before any value is chosen. This is the entire reason SymPy exists.


2. Operation — the verb that joins symbols

The picture. Think of as a machine with two funnels on top and one spout below. Pour two things in the funnels, one thing drops out. The funnels can be fed numbers, symbols, or the output of another machine — that stacking is what makes trees possible.


3. Expression tree — the shape of an idea

Reading the tree. The outermost machine is . Its two funnels are fed by (a power machine) and (a multiply machine) and (a plain number). Draw it top-down and you get a branching diagram — hence "tree".

Figure — SymPy — symbolic algebra, calculus, ODE solving

WHY the topic needs this. Every SymPy command is a rule for rewriting this tree:

  • expand pushes multiplications inward (opens brackets),
  • factor pulls common structure outward,
  • diff walks the tree applying the derivative rule at each node.

If you picture the tree, these stop being magic.


4. Equation, and "set to zero"

The picture. Plot the expression as a curve. The places where the curve crosses the horizontal axis (height zero) are exactly the solutions. Solving = finding the crossings.

Figure — SymPy — symbolic algebra, calculus, ODE solving

5. The three calculus symbols you'll meet

You do not need to master calculus here — the parent note derives it. You only need to recognise the three symbols so they don't ambush you.

WHY these three. The parent's Section 3 recovers the derivative from the limit, and pairs it with the integral. The Taylor and Maclaurin Series section then uses repeated derivatives. Recognising the symbols is enough for now.


6. Function object and the arbitrary constant

WHY the topic needs this. dsolve returns Eq(y(x), C1*sin(x) + C2*cos(x)) — the whole family. Initial conditions (ics) turn the dials to a single curve. If you didn't know was a shape and a dial, that output would look like noise.


7. Exact vs. decimal — the last gate

Picture sqrt(2) as a sealed envelope: SymPy carries it around unopened, doing algebra on the label. .evalf() is the act of tearing it open to read the (rounded) contents. The bridge to actual plotting numbers is Lambdify — bridging SymPy to NumPy for plotting.


Prerequisite map

Number vs Symbol

Operation plus minus times power

Expression tree

Equation and set to zero

solve

Exact vs decimal evalf

Limit derivative integral

diff integrate series

Function object y of x

arbitrary constant C1

dsolve with ics

SymPy topic


Equipment checklist

Test yourself — cover the right side, answer aloud, then reveal.

The difference between a number and a symbol
A number is a settled value (a pin on the line); a symbol is an unknown placeholder (a slider) kept so you can reason before choosing a value.
Why SymPy stores an expression tree instead of a value
Because with unknown symbols there is nothing to compute yet, so it keeps the structure — which lets it later factor, expand and differentiate.
How to write "x squared" in SymPy code
x**2 — never x^2, since ^ means xor in Python.
The universal form every equation is rewritten into
expression = 0, because , so solving = finding where the expression crosses zero.
Why (x+1)**2 == x**2+2*x+1 is False
== compares tree structure, not mathematical value; use simplify(a-b)==0 or a.equals(b).
What means in words
The value you approach as shrinks toward zero without ever reaching it.
What a derivative measures, as a picture
The steepness/slope of the curve at the point .
What a definite integral measures, as a picture
The signed area between the curve and the horizontal axis from to .
How differs from , and how it's created
is an unknown number; is an unknown function (a whole shape), made with Function('y').
What is in a dsolve answer
A dial selecting one curve from the whole family of solutions; ics sets it.
When a decimal finally appears
Only when you ask with .evalf() or N(...); until then everything stays exact.