Runoob Tutorial ArchiveEnglish

Symbolic computation ability

Symbolic computation ability

In previous chapters, we learned various rules and concepts; in this chapter, we will see everyone's proficiency in whether they can derive formulas themselves.

This ability directly determines whether you can follow the derivations in the paper instead of getting stuck where the author skips steps. Every knowledge point is accompanied by an interactive demonstration, especially the geometric dissection animation of (a+b)², which allows you to understand where the "cross term 2ab" comes from just by watching it.


Why "symbolic computation ability" is important

When reading papers, you often encounter this situation: the author writes, "After simplification, it can be obtained...", skipping three or five steps in between.

If your symbolic computation is not proficient enough, these jumps will directly block you.

If you are proficient, you can fill in these steps yourself and truly "keep up" with the author's thinking, rather than passively accepting the conclusion.

The same rule: "knowing" and "being able to use proficiently with your eyes closed" are two different levels of ability. This chapter trains the latter.


From (a+b)² to multivariate expansion

The most basic identity

Geometric perspective: why there is an extra "2ab"

If you interpret it as "the area of a square with side length a+b," you can cut this square into 4 pieces:

Region Shape Area
Top left corner Square with side length a
Top right corner Rectangle with length b and width a
Bottom left corner Rectangle with length a and width b
Bottom right corner Square with side length b

The sum of the four areas.

This "2ab" is not arbitrary, but comes from two intersecting rectangles.Click the "Expand" button below to see how the four areas are separated.

Interactive Demonstration: Geometric dissection of (a+b)²

Drag the slider to change the ratio of a and b (with a fixed total side length of 4), click "Expand" to see the square divided into four parts, and then click "Restore" to put it back together.

a² ab ab b² a = 2.5 b = 1.5 A large square with side length (a+b) is cut into 4 pieces: a² + ab + ab + b² The cross term 2ab comes from two green rectangles

Extend to three variables

Following the pattern that "every pair of variables generates a cross-term," it can be written directly as:

Derivation logic: Treat as , and when expanded, every term is multiplied by every other term.

Multiplying the same variable yields a squared term (), and multiplying different variables results in two instances (for example, and ), which merge to form .

Generalization to vectors

This pattern appears repeatedly in vector operations in another form, such as calculating the squared norm of the difference between two vectors:

Essentially, replacing the "number" in with "vector" and replacing "multiplication" with "dot product."

When you see similar expansions in a paper, you can directly connect it to the rules learned here—new formulas are often old rules wearing a new guise, and this is how "symbolic computation ability" helps you "see through" new formulas.

Example

Numerical verification identity: a good habit to check if a formula is written correctly

Approach: substitute some numbers and see if both sides of the equation are equal

for

a

,

b

in

[

(

2

,

3

)

,

(

5

,

1

)

,

(

0.5

,

2.5

)

,

(

1

,

4

)

]

:

lhs

=

(

a + b

)

**

2

               

Left: (a+b)²

rhs

=

a **

2

2

2

 

Right: a² + 2ab + b²

   

print

(

a

,

b

,

abs

(

lhs - rhs

)

<

1e-12

)

import

math

x

,

y

=

(

3

,

4

)

,

(

1

,

2

)

lhs

=

(

x

[

0

]

-y

[

0

]

)

**

2

(

x

[

1

]

-y

[

1

]

)

**

2

rhs

=

(

x

[

0

]

**

2

[

1

]

**

2

)

2

(

x

[

0

]

*y

[

0

]

[

1

]

*y

[

1

]

)

(

y

[

0

]

**

2

[

1

]

**

2

)

print

(

abs

(

lhs - rhs

)

<

1e-12

)

The output of the above code is:

2 3 True

5 1 True

0.5 2.5 True

-1 4 True

True

This "random substitution verification" method is very practical: when you forget the formula details, substituting a few numbers can confirm if the version you remember is correct.

Practice: Expand and .

Click to view answers

Substituteandintoit.


Simplification, transformation, and formula

Method of completion: Converting the general form to the "vertex form"

The goal of the method of completion is to complete the square for this "missing piece" equation:

Example: Completing the formula yields .

Verification: Expanding shows they are indeed equal.

Two typical uses of the method of completion:

Use Method Example
Finding the vertex of a quadratic function Directly observing the vertex (-3, -7), no need to draw a graph or take the derivative
Completing the square to simplify derivation Forming a perfect square from a complex polynomial Intermediate steps for least squares method and maximum likelihood estimation of normal distribution

Interactive demonstration: Completing the square and the vertex

Drag the slider to change b and observe how the vertex of the parabola y = x² + bx moves, and that the x-coordinate of the vertex is always equal to -b/2.

General simplification approach

When encountering a complex expression, usually try in this order:

Order Technique Explanation
1 Combine like terms Combine powers of the same variable
2 Factor out the common factor Extract the common factor of all terms
3 Use identities Apply ready-made formulas, etc.
4 Substitution Replace frequently occurring complex subexpressions with new variables

Substitution method examples: Simplify.

Original prompt (Chinese, left untranslated)



令 t = x² + 1

原式 = t² - 3t + 2 = (t-1)(t-2)

代回 t:

     = (x²+1-1)(x²+1-2)

     = x² · (x²-1)

     = x⁴ - x²


Whether you can derive the formula yourself is a watershed

"Understanding" and "being able to derive" are two different levels of ability

Hierarchy Performance
Can understand Can understand "why this step is valid" by looking at the derivation steps
Can derive Can retrace these steps by oneself after closing the book

Many people get stuck on reading papers, not on "new knowledge," but on this level: the formulas use rules taught in the first three chapters, but one needs to organize the correct sequence of steps by oneself without prompts.

Complete derivation practice: Minimum of Mean Squared Error

Problem: Given n data points, find a constant c that minimizes the "sum of squared distances" of all points to c, and find the optimal c.

Derivation process:

Original prompt (Chinese, left untranslated)



设 f(c) = Σᵢ (xᵢ - c)²



展开每一项(用到 (a-b)² 公式):

f(c) = Σᵢ (xᵢ² - 2cxᵢ + c²)

     = Σᵢ xᵢ² - 2c·Σᵢxᵢ + n·c²    (把 Σ 拆到每一项,c 是常数可以提出来)



这是关于 c 的二次函数,开口向上(c² 系数为 n > 0),

顶点(最小值点)在 c = -B/2A,这里 B = -2Σxᵢ,A = n



代入:c = -(-2Σxᵢ) / (2n) = Σxᵢ / n

Conclusion: The optimal c is exactly the average of all data points.

This derivation only uses the knowledge from this chapter and Chapter 1 (squared expansion, Σ summation, quadratic function vertex formula) to derive the important conclusion in statistics: "why the mean is the optimal point for minimizing squared error."

Manually verify by dragging the slider: how the sum of squared distances increases when c deviates from the average.

Interactive Demonstration: Why the Mean Minimizes Squared Error

Five data points are fixed at 1, 3, 4, 8, 9 (mean 5). Drag the slider to move c and observe the change in f(c) = Σ(xᵢ-c)², the error is smallest when c = 5.

Gray dots are the data points, the red parabola is f(c), and the blue dot is the current position of c. No matter which way c deviates, the error is greater than the minimum at c = 5.

Example

Numerical Verification: The mean is the c that minimizes Σ(xᵢ-c)²

data

=

[

1

,

3

,

4

,

8

,

9

]

mean

=

sum

(

data

)

/

len

(

data

)

def

sq_error

(

c

)

:

   

"""f(c) = Σ(xᵢ - c)²"""

   

return

sum

(

(

x - c

)

**

2

for

x

in

data

)

print

(

mean

)

           

mean = 5

print

(

sq_error

(

mean

)

)

 

c = error when at mean

Comparing several c deviating from the mean: the error is larger

for

c

in

[

0

,

3

,

5

,

7

,

10

]

:

   

print

(

c

,

sq_error

(

c

)

)

The output result of the code above is:

5.0

46.0

0 171

3 66

5 46

7 66

10 171

Note that the errors for c=3 and c=7 are equal (both are 66) -- they are the same distance from the mean of 5, which is a manifestation of the parabola being "symmetrical about its vertex."

Practice: Try to independently re-derive the process of "mean squared error minimization" without looking at the preceding text, using only three tools: the square expansion formula, the linearity property of $\Sigma$, and the vertex formula for quadratic functions.

Click to view the answer

Expand, identify it as an upward-opening quadratic function, the vertex, which is the mean.


Chapter Summary

Skills One-sentence core
Expansion of (a+b)² Geometrically it is a "tangent square", the cross term comes from the product of two directions, which can be generalized to multiple variables and vectors
Completing the square method Completing the expression that is "missing a piece" to form a perfect square, used to find the vertex and simplify derivation
Simplification idea Combine like terms $\rightarrow$ Factor out common terms $\rightarrow$ Apply identity $\rightarrow$ Substitution
Independent derivation Being able to re-derive without looking at the answer is the dividing line for understanding the paper's formulas