Day 11 — Variables & expressions
Day 11 — Variables & expressions
Stage II · concept day
Goal: Distinguish atoms from expressions; evaluate by substitution; combine like terms; apply distributive law; preview algebraic identities; connect free vs bound variables to programming scope.
Why this matters
Programming and mathematics both name unknown or varying quantities. Algebraic expressions are structured recipes; evaluating them is substitution into a tree of operations. If you cannot parse \(3(x-2)^2-4x\), later factoring and functions feel like symbol soup. Compilers see expressions as ASTs—same mental model as Day 1.
Theory
Variables, constants, atoms
A variable is a symbol standing for an element of a stated set (often \(\mathbb{R}\), \(\mathbb{Q}\), or \(\mathbb{Z}\)).
A constant is a fixed value (literals and fixed parameters).
An atom (atomic expression) is a variable or a constant numeral—leaves of the expression tree.
Expressions vs equations vs identities
- Expression: a syntactic form denoting a value once variables are fixed — e.g. \(3x+1\), \(\dfrac{x}{x+1}\).
- Equation: a claim that two expressions are equal — e.g. \(3x+1=7\). May be true for some, all, or no substitutions.
- Identity: an equation true for all allowed values — e.g. \((x+1)^2=x^2+2x+1\).
- Contradiction (as equation): never true — e.g. \(x=x+1\) over \(\mathbb{R}\).
Today’s focus is expressions and their evaluation/simplification.
Free vs bound (analogy)
In \(2n+1\), the variable \(n\) is free—its value is not fixed by the expression alone.
In “for all integers \(n\), \(n+(n+1)\) is odd,” the \(n\) is bound by the quantifier (Stage III).
In code, a function parameter is bound in the function body; a free name must come from outer scope. Same discipline: know what is fixed vs open.
Anatomy of polynomial expressions (preview)
A monomial in \(x\): \(c x^k\) with coefficient \(c\) and degree \(k\) (nonnegative integer for polynomials).
Like terms share the same variable powers: \(3x^2\) and \(-5x^2\) are like; \(3x^2\) and \(3x\) are not.
Combining like terms: \(3x^2-5x^2=(3-5)x^2=-2x^2\) (distributivity / field axioms).
Polynomial (univariate): finite sum of monomials \(a_n x^n+\cdots+a_1 x+a_0\).
Distributive law
For all numbers \(a,b,c\) in a ring/field such as \(\mathbb{R}\):
\[a(b+c)=ab+ac,\qquad (b+c)a=ba+ca.\]
Also: \(-(b+c)=-b-c\) because \(-1\) distributes.
FOIL is distributivity twice: \((a+b)(c+d)=ac+ad+bc+bd\).
Evaluating expressions
Substitution: replace each free variable by a value (with parentheses when needed), then apply order of operations.
Domain issues: \(\frac{1}{x-1}\) undefined at \(x=1\). Track excluded values even before formal function domains (Day 18).
Algebraic identities (preview)
These will be proved by expansion (and used constantly):
\[ \begin{align*} (a+b)^2 &= a^2+2ab+b^2,\\ (a-b)^2 &= a^2-2ab+b^2,\\ (a+b)(a-b) &= a^2-b^2,\\ (a+b)^3 &= a^3+3a^2b+3ab^2+b^3. \end{align*} \]
Difference of squares \((a+b)(a-b)=a^2-b^2\) is the most used factoring preview.
Nested structure and expression trees
\(3x+(2(x-1))^2\) has main operation \(+\) at the root.
Evaluation order = post-order on the tree = standard precedence on the string.
Equality of expressions
Two expressions \(E\) and \(F\) are identically equal (on a domain \(D\)) if \(E(x)=F(x)\) for all \(x\in D\).
Expanding and combining like terms is a standard way to prove identity on \(\mathbb{R}\).
Worked examples
Example 1 — Like terms
Simplify \(5x-3+2x+7-x\).
\((5x+2x-x)+(-3+7)=6x+4\).
Example 2 — Distribute
Expand \(3(2x-5)-(x+1)=6x-15-x-1=5x-16\).
Example 3 — FOIL
\((2x+3)(x-4)=2x\cdot x+2x(-4)+3x+3(-4)=2x^2-8x+3x-12=2x^2-5x-12\).
Example 4 — Evaluate
\(E(x)=3(x-2)^2-4x\) at \(x=5\): \(3(3)^2-20=27-20=7\).
At \(x=-1\): \(3(-3)^2-4(-1)=27+4=31\).
Example 5 — Identity check
Expand \((x+1)^2=x^2+2x+1\). At \(x=10\) both sides \(121\).
Example 6 — Difference of squares
\((x+5)(x-5)=x^2-25\). Evaluate both at \(x=5\): \(0=0\).
Example 7 — Excluded value
\(E(x)=\dfrac{x^2-1}{x-1}\) for \(x\neq 1\). At \(x=3\): \(\frac{8}{2}=4\).
Algebraically \(E(x)=x+1\) for \(x\neq 1\) (Day 17 cancels carefully).
Example 8 — Nested
Simplify \(2\bigl(3-(4-x)\bigr)=2(3-4+x)=2(x-1)=2x-2\).
Example 9 — Multi-variable
\(3ab+2a- ab+5a= (3ab-ab)+(2a+5a)=2ab+7a\).
Example 10 — Cube preview
\((x+1)^3=x^3+3x^2+3x+1\) by \((x+1)(x^2+2x+1)\).
Example 11 — Free vs bound
In \(\sum_{i=1}^{n} i\), the \(i\) is bound; \(n\) is free (parameter). Changing dummy \(i\to k\) does not change the sum.
Example 12 — Common factor
\(6x+9=3(2x+3)\); \(x^2+x=x(x+1)\).
Example 13 — Substitution pitfall
\(x=-3\) into \(x^2\): \((-3)^2=9\), not \(-3^2=-9\).
Example 14 — Prove small identity
Show \((a+b)^2- (a-b)^2=4ab\):
Left: \((a^2+2ab+b^2)-(a^2-2ab+b^2)=4ab\).
Exercises
Easy
- Combine like terms: \(7y-2+3y+8-y\).
- Expand \(4(3x-2)\).
- Evaluate \(2x^2-3x+1\) at \(x=4\).
- Expand \((x+3)(x+5)\).
- Factor out common factor: \(10x-15\).
Medium
- Expand and simplify $ (2x-1)(x+4)-x(x-3) $.
- Evaluate \(\dfrac{x+2}{x-2}\) at \(x=0\), \(x=4\); for which \(x\) undefined?
- Expand \((a+b)^2\) and \((a-b)^2\); subtract to prove \(4ab\) identity.
- Show \((x+y)(x-y)=x^2-y^2\) by expansion.
- Simplify \(3(a+2b)-2(2a-b)+a\).
- Substitute \(x=-2\) into \(x^2-5x-1\) carefully.
- Are $ (x+1)^2 $ and \(x^2+1\) identical? Prove or give counterexample.
- Build an expression tree for \(2(x+1)^2-3x\) (describe nodes).
Hard / proof
- Prove by expansion that \((a+b)^3=a^3+3a^2b+3ab^2+b^3\).
- Prove: if \(E(x)=F(x)\) for all \(x\) and \(G\) is any expression, then \(E+G\) and \(F+G\) are identical.
- For which real \(c\) is \(x^2+c\) always positive? Always nonnegative?
- Expand \((x+y+z)^2\) completely.
- Show that \(n\) free in \(2n+1\) vs bound in “\(\exists n\in\mathbb{Z}\) such that \(n^2=2\)” — explain the difference in one paragraph.
- Prove \((a+b+c)^2=a^2+b^2+c^2+2ab+2ac+2bc\).
- Determine whether \(\frac{x^2-1}{x-1}\) and \(x+1\) are identical as expressions on \(\mathbb{R}\) (consider \(x=1\)).
Challenge / CS-flavored
- Loop index \(i\) from \(0\) to \(n-1\): write an expression for the last index; for the count of iterations.
- Array length \(n\), middle index \(\lfloor n/2\rfloor\) — expression in \(n\).
- Cost expression \(3n^2+2n+5\) at \(n=10\); which term dominates for large \(n\) qualitatively?
- In a pure function
f(x) = 3*x+1, isxfree or bound in the body? What about a global used inside? - Parse
a-b-cas expression tree under left associativity; evaluate \(a=10,b=3,c=2\).
Formalizing “like terms”
Two monomials \(c x_1^{e_1}\cdots x_k^{e_k}\) and \(d x_1^{f_1}\cdots x_k^{f_k}\) are like when \(e_i=f_i\) for all \(i\). Only then \(c\) and \(d\) combine: \((c+d)\) times the common power product.
Ring axioms used constantly
When simplifying, you silently use associativity, commutativity of \(+\) and \(\times\), distributivity, and additive inverses. Writing \(a-b\) as \(a+(-b)\) prevents sign errors.
Polynomial vs non-polynomial expressions
\(x+\frac{1}{x}\), \(\sqrt{x}\), \(2^{x}\) are expressions but not polynomials. Polynomial restriction: finitely many nonnegative integer powers, coefficients from the base ring/field.
Evaluation homomorphism idea
Substituting \(x\mapsto c\) respects operations: \(\widehat{E+F}=\hat E+\hat F\), etc. That is why algebraic identities remain true after substitution (when defined).
Worked identity table
| Expand | Result |
|---|---|
| \((x+y+z)^2\) | \(x^2+y^2+z^2+2xy+2xz+2yz\) |
| \((x+y)^3\) | \(x^3+3x^2y+3xy^2+y^3\) |
| \((x-y)(x^2+xy+y^2)\) | \(x^3-y^3\) |
| \((x+y)(x^2-xy+y^2)\) | \(x^3+y^3\) |
Extra exercises
- Expand \((2x-3y)^2\).
- Prove \((x+y)^2-(x-y)^2=4xy\) without picking numbers.
- For \(E(x)=x^{2}-1\) and \(F(x)=(x-1)(x+1)\), show identical as polynomials by expansion.
- Substitute \(x=y=z=1\) into \((x+y+z)^2\) expansion both ways.
- Expression tree for \(\frac{a+b}{c+d}\): what is the root operation?
CS connection
Expression trees are ASTs; constant folding combines like constants at compile time. Free vs bound variables are scope and lambda calculus. Algebraic simplification is peephole optimization. Identities like difference of squares appear in algorithmic rewrites and in closed forms.
Common pitfalls
| Pitfall | What to do instead |
|---|---|
| Combining \(3x^2+3x\) into \(6x^2\) | Only like powers |
| \(- (x-3)=-x-3\) | Distribute the minus: \(-x+3\) |
| Substituting negatives without parentheses | Write \((-3)^2\) |
| Treating expressions as equations | Expressions do not “solve”; they evaluate/simplify |
| Canceling terms over addition | Factor first |
| Claiming identity after checking one \(x\) | Must hold for all \(x\) in domain |
Fully worked extra examples
E15 — Multi-step expand.
\((x+2)(x-2)(x+3)=(x^{2}-4)(x+3)=x^{3}+3x^{2}-4x-12\).
E16 — Nested evaluation with negatives.
\(E=2-3\bigl(1-(x-4)\bigr)\) at \(x=2\): inner \(1-(2-4)=1-(-2)=3\); \(2-3\cdot 3=2-9=-7\).
E17 — Identity failure counterexample.
Claim “\((x+y)^{2}=x^{2}+y^{2}\)”: at \(x=y=1\), left \(4\), right \(2\). False. Missing \(2xy\).
E18 — Parameter expression.
Cost \(C(n)=12n+50\). Incremental cost of one more unit: \(C(n+1)-C(n)=12\) (discrete derivative of linear).
E19 — Like terms multivariable.
\(3xy^{2}-5x^{2}y+2xy^{2}+x^{2}y=(3+2)xy^{2}+(-5+1)x^{2}y=5xy^{2}-4x^{2}y\).
End-of-day synthesis problems
S1. Expand and simplify \((2x-1)(x+3)-(x-2)^{2}\).
S2. Evaluate \(E(x)=x^{2}-5x+6\) at \(x=0,\pm 1,\pm 2\); relate to factors.
S3. Prove \((a+b)^{2}=a^{2}+2ab+b^{2}\) by distributivity only.
S4. Are \(x^{2}-1\) and \((x-1)(x+1)\) identical as polynomials? As functions on \(\mathbb{R}\)?
S5. Expression tree + evaluate \(3^{2+1}\) vs \(3^{2}\cdot 3\) vs \((3^{2})^{1}\) carefully with precedence.
S6. Free vs bound: rewrite \(\sum_{i=1}^{n}(2i+1)\) with index \(k\); what stays free?
S7. For which \(x\) is \(\frac{x^{2}-4}{x-2}\) defined? Simplify on its domain.
S8. Combine \(4a^{2}b-2ab^{2}+a^{2}b\) like terms in two variables.
Checkpoint
- Define expression vs equation vs identity
- Combine like terms and distribute reliably
- Evaluate nested expressions with negatives
- Expand \((a\pm b)^2\) and \((a+b)(a-b)\)
- Explain free vs bound with a math and a code analogy
- Spot excluded values in rational expressions
- Complete S1 and S3
Write two takeaways in your own words.
Deep dive — expressions, identities, evaluation
D1 — Multi-step expand and simplify.
\((2x-1)(x+3)-(x-2)^{2}= (2x^{2}+6x-x-3)-(x^{2}-4x+4)=2x^{2}+5x-3-x^{2}+4x-4=x^{2}+9x-7\).
Expand each product fully before combining; watch the minus before \((x-2)^{2}\).
D2 — Identity proof by distributivity only.
\((a+b)^{2}=(a+b)(a+b)=a(a+b)+b(a+b)=a^{2}+ab+ba+b^{2}=a^{2}+2ab+b^{2}\) (using \(ab=ba\)).
D3 — Difference of squares and cubes links.
\((x-y)(x^{2}+xy+y^{2})=x^{3}-y^{3}\); \((x+y)(x^{2}-xy+y^{2})=x^{3}+y^{3}\).
Verify by FOIL/distribution; store as expansion/factoring duals (Day 16).
D4 — Evaluation with nested negatives.
\(E=2-3\bigl(1-(x-4)\bigr)\) at \(x=2\): inner \(x-4=-2\), \(1-(-2)=3\), \(2-3\cdot 3=-7\).
Parentheses beat mental shortcuts when signs stack.
D5 — Free vs bound contrast.
In \(2n+1\), \(n\) is free. In \(\sum_{i=1}^{n}(2i+1)\), \(i\) is bound (dummy), \(n\) remains free.
Renaming \(i\to k\) does not change the sum; renaming \(n\) would change the meaning.
D6 — Excluded values before “cancel.”
\(\dfrac{x^{2}-4}{x-2}\) equals \(x+2\) for \(x\neq 2\), but as raw expressions on all of \(\mathbb{R}\) they differ at \(x=2\) (undefined vs \(4\)). Polynomial identity after clearing the hole is a separate statement.
Extra practice set
- Expand \((2x-3y)^{2}\) and \((x+y+z)^{2}\).
- Prove \((x+y)^{2}-(x-y)^{2}=4xy\) by expansion only.
- Are \((x+1)^{2}\) and \(x^{2}+1\) identical? Counterexample.
- Expression tree for \(\dfrac{a+b}{c+d}\): root operation? Evaluate \(a=1,b=2,c=3,d=1\).
- Combine like terms: \(4a^{2}b-2ab^{2}+a^{2}b+5ab^{2}\).
Synthesis checklist (expressions day)
Syn-A. Expand and simplify one multi-factor expression (include a squared binomial).
Syn-B. Prove \((a+b)^{2}=a^{2}+2ab+b^{2}\) using only distributivity and \(ab=ba\).
Syn-C. Evaluate a nested expression at a negative input with full parentheses.
Syn-D. State one free-vs-bound example (math sum or code parameter).
Syn-E. Name an excluded value for a rational expression and simplify on the domain.
Syn-F. Distinguish expression / equation / identity with one example each.
Complete Syn-A–C before moving to linear equations.
Tomorrow
Day 12 — Linear equations. Axioms of equality; multi-step solving; identities vs contradictions vs conditionals; word problems; formula rearrangement.