> 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.1-basic_manipulator.md).

# 3.1 基本操作

## 3.1.1 替换

使用数学表达式最常见的一种方法是替换。替换将表达式中某物的所有实例替换为另一物。这是使用symbols对象的subs方法完成的。

### 普通替换

例：替换$$cos(x)+sin(pi/2)$$中的$$x$$为 $$\frac{\pi}{2}$$

```python
[]:expr=cos(x)+sin(pi/2)

expr.subs(x, pi/2)
```

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

### 循环替换

例：对表达式$$1/(1+x)$$，执行5次将$$x$$替换为$$\frac{1}{1+x}$$的循环替换

```python
[]:expr=1/(1+x)

for i in range(5): expr=expr.subs(x, 1/(1+x))

expr
```

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

## 3.1.2 字符串转为SymPy表达式

使用simpify函数，可以将字符串类型的表达式转为SymPy表达式 例：将字符串类型表达式"x\*\*2+2\*x+1"转为SymPy表达式：

```python
[]:str_expr=’x\*\*2+2\*x+1’

expr=sympify(str_expr)

expr
```

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

## 3.1.3 表达式求值

使用exalf(precision)可以对数值表达式求值，precision为小数保留位数，precision默认为15。

例：对表达式$$\sqrt{13}$$求值：

```python
[]:expr=sqrt(13)

expr.evalf(13)
```

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

## 3.1.4 使用NumPy和SciPy计算

尽管subs和evalf可以很好的帮助我们进行计算，但是如果我们希望同时代入大量参数到一个公式进行计算，那么这时候你可能就忍受不了SymPy的运行效率了。

另外SymPy在计算精度上不如NumPy和SciPy，如果对机器精度有较高的要求的话，应该使用NumPy或SciPy这样的库。

将一个SymPy符号表达式转换成一个数值计算表达式最简单的方法是使用lambdify函数。lambdify的作用类似于lambda函数，只不过它将SymPy名称转换为特定数值库的名称，通常是NumPy。（？）

lambdify()函数用于转换表达式进行数值计算时需要指定三个参数，分别是变量，表达式和数值库。

例 将符号表达$$\sin x + cos x$$转为函数，并分别将0\~9十个整数代入计算。

```python
[]:import numpy

arr=numpy.arange(10)

expr=sin(x)+cos(x)

f=lambdify(x, expr, 'numpy')

type(f)

[]:function

[]:f(arr)

[]:array([ 1. , 1.38177329, 0.49315059, -0.84887249, -1.41044612, -0.67526209, 0.68075479, 1.41088885, 0.84385821, -0.49901178])
```


---

# 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, and the optional `goal` query parameter:

```
GET https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/3.0-sympy_foundation/3.1-basic_manipulator.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
