Skip to content

Expression Transformation (functions/simplification.py)

functions/simplification.py provides the unified entry simplifies, which selects a transformation by the integer method. method is an integer 0–12; zhuyuan / huanyuan / huanyuanshi are only used by some methods. The function returns a single SymPy expression (simplified by radsimp), not a list of strings.

simplifies — expression transformation

Purpose: algebraically transform in_expr in the way selected by method and return the simplified expression.

def simplifies(in_expr, method, zhuyuan, huanyuan, huanyuanshi, fs):
    ...
Param Type Meaning
in_expr str expression to transform, e.g. "x**2 - 1"
method int transformation index 0–12 (see table below); selects the actual SymPy call
zhuyuan str main variable; used only by method=3 (collect) and method=12 (substitute)
huanyuan str substitution symbol (target symbol); only method=12
huanyuanshi str substitution expression (replaces zhuyuan); only method=12
fs dict function dictionary

Returns: a single SymPy expression from radsimp(...).

method index table

method name actual call params used
0 simplify simplify
1 expand expand
2 factor factor
3 collect (combine like terms) collect(expr, zhuyuan) zhuyuan
4 cancel cancel
5 apart (partial fractions) apart
6 trigsimp trigsimp
7 expand_trig expand_trig
8 powsimp powsimp
9 expand_power_exp expand_power_exp
10 expand_log expand_log
11 logcombine logcombine
12 substitute & expand expand(expr.subs(zhuyuan, solve(Eq(huanyuan, huanyuanshi), zhuyuan)[0])) zhuyuan, huanyuan, huanyuanshi

Warning

Except for method=3 and method=12, all other methods ignore the values of zhuyuan / huanyuan / huanyuanshi.

Example

python simplifies("x**2-1", 2, "x", "", "", {}) # -> (x - 1)*(x + 1) (factor) simplifies("x**2", 12, "x", "t", "y+1", {}) # -> (y + 1)**2 (substitute x with t=y+1) simplifies("1/sqrt(2)", 0, "x", "", "", {}) # -> sqrt(2)/2 (simplify)