> For the complete documentation index, see [llms.txt](https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/3.0-sympy_foundation/3.4-basic_mathematics.md).

# 3.4 基础数学

## 3.4.1 化简

使用simplify()函数可以对表达式进行化简，simplify()函数以字符表达式类型作为传入参数，以SymPy表达式类型返回表达式的化简结果。

例：化简多项式$$\frac{x^3 + x^2 -x -1}{x^2 + 2\*x +1}$$

\[]:expr=(x\*\*3 + x\*\*2 - x - 1)/(x\*\*2 + 2\*x + 1)

simplify(expr)

\[]: ![](/files/rbz56jCvtQcTg0kOjRxX)

## 3.4.2 展开

### 多项式展开

使用expand()函数可以对多项式进行展开，expand()函数以多项式类型作为传入参数，以SymPy表达式类型返回多项式的化简结果。

例：展开多项式$$(1+2x+4y)^3$$

```python
>>>   []:expr=(1+2\*x+4\*y)\*\*3
>>>   expand(expr)
```

\[]: ![](/files/0FNiZVXE4ipFUcMVPaWD)

### 三角展开

使用expand\_trig()函数可以对三角多项式进行展开，expand\_trig()函数以三角多项式类型作为传入参数，以SymPy表达式类型返回多项式的化简结果。

例：对多项式$$\sin{2x}+\cos{2x}$$进行三角展开

\[]:expr=sin(2\*x)+cos(2\*x)

expand\_trig(expr)

\[]: ![](/files/4ycOFl6fqrpLwxGu06vS)

## 3.4.3 因式分解

使用factor()函数可以对符号表达式进行因式分解，factor()函数以字符表达式类型作为传入参数，以字符表达式类型返回因式分解结果。

例：对多项式$$x^3-x^2+x-1$$进行因式分解

\[]:expr=x\*\*3-x\*\*2+x-1

factor(expr)

\[]: ![](/files/4nvkGeXqzH0w8bQ6fZ3L)

## 3.4.4 合并同类项

使用collect()函数可以对符号表达式进行同类项合并，collect()函数以字符表达式类型作为传入参数，以字符表达式类型返回同类项合并结果结果。使用expr.collect.coeff(expr, n)可以返回前的系数。

例：对多项式$$xy + x - 3 + 2x^2 - zx^2 + x^3$$进行因式分解

```python
[]:expr = x\*y + x - 3 + 2\*x\*\*2 - z\*x\*\*2 + x\*\*3

collected_expr=collect(expr, x)

collected_expr
```

\[]: ![](/files/rPEg56H5tAmJ5ZhVugU2)

```python
[]:collected\_expr.coeff(x,2)
```

\[]: ![](/files/Yy3mQpcpAKwMQPmJQzU1)

## 3.4.5 解一元方程

使用solveset()函数可以对一元方程进行求解，solveset()函数以字符表达式类型作为传入参数，以集合类型返回一元方程的解。

例：解一元方程$$(x+1)(x+3) = 15$$

```python
[]:solveset((x+1)\*(x+3)-15, x)
```

\[]: ![](/files/KIWQSmqIvKu8xi9RZYbV)

Eq()函数用来创建一个等式，eq()函数接受两个参数，分别为等号的左右两侧数学表达式。

\[]:Equ=Eq((x+1)\*(x+3), 15)

solveset(Equ, x)

\[]: ![](/files/KIWQSmqIvKu8xi9RZYbV)

## 3.4.6 解方程组

### 线性系统

使用linsolve()函数可以对线性系统方程组进行求解。

linsolve()函数的用法如下：

```python
sympy.solvers.solveset.linsolve(system, \*symbols)
```

其中system代之线性系统也即需要解的线性方程组。\*symbols为需要求解的变量。求解结果以集合的形式给出。

例：求解线性系统$$\begin{cases} 3x + 2y + z & = 39 \ 2x + 3y + z & = 34 \ x + 2y + 3z & = 26 \end{cases}

$$$
`python []:linsolve([3*x+2*y+z-39, 2*x+3*y+z-34, x+2*y+3*z-26], (x, y, z)) ` \[]: ![](../media/7667729dc84ce930c6f3b0f36698ad25.png) ### 非线性系统 使用nonlinsolve()函数可以对线性系统方程组进行求解。 nonlinsolve()函数的用法如下： `python sympy.solvers.solveset.linsolve(system, \*symbols) ` 其中system代之线性系统也即需要解的非线性方程组。\*symbols为需要求解的变量。求解结果以集合的形式给出。 例：求解非线性系统 `python []:nonlinsolve([x\*\*2+4\*y\*\*2-5, x+2\*y-1], x, y) ` \[]: ![](../media/7e3a25639cd09e0e676a25235e00424b.png) ## 3.4.7 解不等式 ### 解带有有理系数的有理不等式 针对有带有有理系数的有理不等式，SymPy在sympy.solvers.inequalities模块中封装了solve\_poly\_inequality()函数来求解此类不等式。 使用如下语句导入solve\_poly\_inequality()函数： `python from sympy.solvers.inequalities import solve\_rational\_inequality ` 例：解不等式组 `python []:solve\_rational_inequalities([[ ... ((Poly(-x + 1), Poly(1, x)), '\>='), ... ((Poly(-x + 1), Poly(1, x)), '\<=')]]) ` 例：解不等式组 `python []:solve\_rational_inequalities([[ ... ((Poly(x), Poly(1, x)), '!='), ... ((Poly(-x + 1), Poly(1, x)), '\>=')]]) ` ### 解带有理数系数的多项不等式 在对使用SymPy解带有有理系数的多项不等式进行讲解之前，我们首先需要介绍一个特殊的函数——Ploy。 Ploy()用于表示多项式表达式并对其进行操作的泛型类，是Expr类的子类。 Ploy()函数的使用方式如下： `python Poly**(**rep**,** **\***gens**,** **\*\***args**)** ` 使用如下语句导入Poly函数： 针对有带有有理系数的有理不等式，SymPy在sympy.solvers.inequalities模块中封装了solve\_poly\_inequality()函数来求解此类不等式。 使用如下语句导入solve\_poly\_inequality()函数： `python from sympy.solvers.inequalities import solve_poly_inequality ` 例：解不等式组 `python []:>>> solve_poly_inequality(Poly(x\*\*2 - 1, x, domain='ZZ'), '!=') ` \[]: ![](../media/d38ad1634f9caf5497070bac686c648a.png) 例：解方程 `python []:solve\_rational_inequalities([[ ... ((Poly(-x + 1), Poly(1, x)), '!='), ... ((Poly(-x + 1), Poly(1, x)), '\<=')]]) ` \[]:![](../media/311cc66a57a62c6e28172a41f5d2423a.png) ### 解实数单变量不等式 针对实数单变量不等式，SymPy在sympy.solvers.inequalities模块中封装了solve\_ univariate \_inequality()函数来求解此类不等式。 使用如下语句导入solve\_ univariate \_inequality()函数： from sympy.solvers.inequalities import solve\_ univariate \_inequality 例：解不等式 $$ x^2 \leq 4 $$ `python []:solve\_univariate_inequality(x\*\*2 \>= 4, x, relational=False) Union(Interval(-oo, -2), Interval(2, oo)) ` \[]: ![](../media/60b20d62806d36a6f306b0881938396c.png) 例：解不等式 $$ x^3+2x^2 -3x \ge 4 $$ `python []:solve\_univariate_inequality(x\*\*3+2\*x\*\*2 -3\*x\>= 4, x, relational=False) ` \[]: ![](../media/d4be1383e32bd325353380d3d64bf6ff.png) 例：解不等式 $$ x^3+2x^2 -3x \ge 4 $$ `python []:domain = Interval(0, S.Infinity) solve_univariate\_inequality(x\*\*3+2\*x\*\*2 -3\*x\>= 4, x, False, domain) ` \[]: ![](../media/cb53c03cfae57e745d8d418e2a6a2cbb.png) 例：解三角不等式 $$ \sin x^2 \lt 0 $$ `python []:solve\_univariate_inequality(sin(x)\*\*2\> 0, x, relational=False) ` \[] ![](../media/c18b39ee0174bb6b63a4c56bffce5b74.png) ### 化简带有有理系数的有理数多项式 `python []:from sympy import Poly, Symbol from sympy.solvers.inequalities import reduce_rational_inequalities x = Symbol('x', real=True) ` 例：化简 `python []:reduce\_rational_inequalities([[x\*\*2 \<= 0]], x) ` 例：化简 `python []:reduce\_rational_inequalities([[x + 2 \> 0]], x) ` \[]:![](../media/ae138aa4226e3db35f5932d7640832aa.png) `python []:reduce\_rational_inequalities([[(x + 2, "\>")]], x) ` \[]:![](../media/5f41eb20d49398cc2233161f536c7c4d.png) `python []:reduce\_rational_inequalities([[x + 2]], x) ` \[]:![](../media/b967aaa96e79c4bdda7a3d542e790ba8.png) ### 化简带有绝对值的不等式 `python []:from sympy import Abs, Symbol from sympy.solvers.inequalities import reduce_abs_inequality x = Symbol('x', real=True) ` 例：化简不等式$$|x-5|-3 \lt 0 $$ `python []:reduce\_abs_inequality(Abs(x - 5) - 3, '\<', x) ` \[]:![](../media/1054165365a5201c79213c9b272c4e49.png) 例：化简不等式$$|x+2|\times 3-13< 0$$ `python []:reduce\_abs_inequality(Abs(x + 2)\*3 - 13, '\<', x) ` \[]:![](../media/c6ebdec6c964350e93bc6d36f53b35bf.png) ### 化简带有绝对值的不等式系统 `python []:from sympy import Abs, Symbol from sympy.solvers.inequalities import reduce_abs_inequality x = Symbol('x', real=True) ` 例：化简$$g(x)= \begin{cases} |3x-5|-7, & \lt 0 \ |x+25|-13 , & \gt 0 \end{cases}
$$$

\[]:reduce\_abs\_inequalities(\[(Abs(3\*x - 5) - 7, '<'),

... (Abs(x + 25) - 13, '>')], x)

\[]:![](/files/QhaedDLrgfWGUswMB6pZ)

例：化简 $$|x - 4| + |3x - 5| - 7 \lt 0$$

```python
[]:reduce\_abs_inequalities([(Abs(x - 4) + Abs(3\*x - 5) - 7, '\<')], x)
```

\[]:![](/files/l1AH6rdYesdulWcUb8z8)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/3.0-sympy_foundation/3.4-basic_mathematics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
