# How

## How to create a symbolic numeric value

To create a symbolic numerical value use `sympy.S`.

````{tip}
```
sympy.S(a)
```
````

For example:


```python
import sympy

value = sympy.S(3)
value
```




$\displaystyle 3$



```{attention}
If we combine a symbolic value with a non symbolic value it will automatically
give a symbolic value:
```


```python
1 / value
```




$\displaystyle \frac{1}{3}$



## How to get the numerical value of a symbolic expression

We can get the numerical value of a symbolic value using `float` or `int`:

- `float` will give the numeric approximation in \\(\mathbb{R}\\)
  ````{tip}
  ```
  float(x)
  ```
  ````
- `int` will give the integer value
  ````{tip}
  ```
  int(x)
  ```
  ````

For example, let us create a symbolic numeric variable with value
\\(\frac{1}{5}\\):


```python
value = 1 / sympy.S(5)
value
```




$\displaystyle \frac{1}{5}$



To get the numerical value:


```python
float(value)
```




    0.2



If we wanted the integer value:


```python
int(value)
```




    0



```{attention}
This is not rounding to the nearest integer. It is returning the integer part.
```

## How to factor an expression

We use the `sympy.factor` tool to factor expressions.

````{tip}
```
sympy.factor(expression)
```
````

For example:


```python
x = sympy.Symbol("x")
sympy.factor(x ** 2 - 9)
```




$\displaystyle \left(x - 3\right) \left(x + 3\right)$



## How to expand an expression

We use the `sympy.expand` tool to expand expressions.

````{tip}
```
sympy.expand(expression)
```
````

For example:


```python
sympy.expand((x - 3) * (x + 3))
```




$\displaystyle x^{2} - 9$



## How to simplify an expression

We use the `sympy.simplify` tool to simplify an expression.

````{tip}
```
sympy.simplify(expression)
```
````

For example:


```python
sympy.simplify((x - 3) * (x + 3))
```




$\displaystyle x^{2} - 9$



```{attention}
This will not always give the expected (or any) result. At times it could be
more beneficial to use `sympy.expand` and/or `sympy.factor`.
```

## How to solve an equation

We use the `sympy.solveset` tool to solve an equation. It takes two values as
inputs. The first is either:

- An expression for which a root is to be found
- An equation

The second is the variable we want to solve for.

````{tip}
```
sympy.solveset(equation, variable)
```
````

Here is how we can use `sympy` to obtain the roots of the general quadratic:

\\[
a x ^ 2 + bx + c
\\]


```python
a = sympy.Symbol("a")
b = sympy.Symbol("b")
c = sympy.Symbol("c")
quadratic = a * x ** 2 + b * x + c
sympy.solveset(quadratic, x)
```




$\displaystyle \left\{- \frac{b}{2 a} - \frac{\sqrt{- 4 a c + b^{2}}}{2 a}, - \frac{b}{2 a} + \frac{\sqrt{- 4 a c + b^{2}}}{2 a}\right\}$



Here is how we would solve the same equation but not for \\(x\\) but for
\\(b\\):


```python
sympy.solveset(quadratic, b)
```




$\displaystyle \left\{- \frac{a x^{2} + c}{x}\right\}$



It is however clearer to specifically write the equation that we want to solve:


```python
equation = sympy.Eq(a * x ** 2 + b * x + c, 0)
sympy.solveset(equation, x)
```




$\displaystyle \left\{- \frac{b}{2 a} - \frac{\sqrt{- 4 a c + b^{2}}}{2 a}, - \frac{b}{2 a} + \frac{\sqrt{- 4 a c + b^{2}}}{2 a}\right\}$



## How to substitute a value in to an expression

Given a `sympy` expression it is possible to substitute values in to it using
the `.subs()` tool.

````{tip}
```
expression.subs({variable: value})
```
````

```{attention}
It is possible to pass multiple variables at a time.
```

For example we can substitute the values for \\(a, b, c\\) in to our quadratic:


```python
quadratic = a * x ** 2 + b * x + c
quadratic.subs({a: 1, b: sympy.S(7) / 8, c: 0})
```




$\displaystyle x^{2} + \frac{7 x}{8}$



8b2f76668c14164a7b1fe35f4cc8679ef917196c
ipynb
Jupyter Notebook
book/tools-for-mathematics/02-algebra/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-24T21:02:41.000Z
2020-10-14T08:37:21.000Z
book/tools-for-mathematics/02-algebra/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-21T15:54:23.000Z
2021-12-19T23:26:15.000Z
book/tools-for-mathematics/02-algebra/how/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-10-02T09:21:27.000Z
2021-07-08T14:46:27.000Z
Qwen/Qwen-72B
1. YES
2. YES