# piecewise 函数

按条件写出的分段取值，写作 value if condition，并可带一个 otherwise 分支。

## 怎么写

### piecewise(case, ...)

第一个条件成立的分支的值；以 value otherwise 结尾。

**case**

一个值及其成立的条件，写作 value if condition。接受带条件的值。

## 示例

- $\displaystyle \operatorname{subs}\left(\begin{cases} x & \text{当 } x > 0 \text{ 时} \\ -x & \text{其他} \end{cases}, x \to -3\right) = 3$

  `subs(piecewise(x if x > 0, -x otherwise), x -> -3)`

  在求解器中，`subs(piecewise(x if x > 0, -x otherwise), x -> -3)` 的结果是 `3`。

  [在求解器中打开](https://zh.hilberon.com/?q=subs%28piecewise%28x%20if%20x%20%3E%200%2C%20-x%20otherwise%29%2C%20x%20-%3E%20-3%29)

## 示例中用到的函数

- [subs](https://zh.hilberon.com/library/subs)

  在表达式中代入一条规则或规则列表。

## 相关函数

- [pre_decrement](https://zh.hilberon.com/library/pre_decrement)

  将变量就地减 1，并返回新值。
- [indicator](https://zh.hilberon.com/library/indicator)

  把条件表示为数：为真时为 1，为假时为 0。
- [pre_increment](https://zh.hilberon.com/library/pre_increment)

  把变量原地加 1 并返回新值。
- [increment](https://zh.hilberon.com/library/increment)

  将变量原地加 1，并返回它的旧值。
- [release_hold](https://zh.hilberon.com/library/release_hold)

  对已保持的表达式求值。
- [ifelse](https://zh.hilberon.com/library/ifelse)

  由测试条件选出的值。

---
Source: https://zh.hilberon.com/library/piecewise
