# Solve equation systems with SymPy
Once an a while you need to solve simple equation systems, I have found that using SymPy for this is a much better option than using pen and paper, where I usually make mistakes. Here is some short examples...


```python
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import sympy as sp

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
```

## Linear system


```python
x,y,z = sp.symbols('x y z')
```


```python
eq_1 = sp.Eq(lhs=y,
             rhs=2*x)
eq_1
```


```python
eq_2 = sp.Eq(lhs=z,
             rhs=3*y)
eq_2
```


```python
eq_2.subs(y,sp.solve(eq_1,y)[0])
```

We try to use *solve* to get the expression for **z**:


```python
eqs = (
    eq_1,
    eq_2,
)

solution = sp.solve(eqs, [z])
solution
```

...so this is giving us **eq_2** which we kind of know already.
So we must add a list [y,z] to solve both equations.


```python
eqs = (
    eq_1,
    eq_2,
)

solution = sp.solve(eqs, [y,z])
solution
```


```python
solution[z]
```

## Quadratic


```python
eq_3 = sp.Eq(lhs=z**2,
             rhs=2*y)
eq_3
```


```python
eq_3.subs(y,sp.solve(eq_1,y)[0])
```


```python
eqs = (
    eq_1,
    eq_3,
)

solution = sp.solve(eqs, [y,z])
solution
```

Since there is now two solutions these are given in a list where each item contain the solution for **y** and **z**.


```python
solution[0][-1]
```


```python
solution[1][-1]
```

But in order to make this less confusing the *dict* flag can be set to True:


```python
eqs = (
    eq_1,
    eq_3,
)

solution = sp.solve(eqs, [y,z], dict=True)
solution
```

## Changing the order of symbols:


```python
eqs = (
    eq_1,
    eq_3,
)

solution = sp.solve(eqs, [z,y])
solution
```

... this is swapping the order of solutions

## Changing the order of equations:


```python
eqs = (
    eq_3,
    eq_1,
)

solution = sp.solve(eqs, [y,z])
solution
```

...this has no effect.

d41450a8092d350f579b02e2d7ee253c28a6a459
ipynb
Jupyter Notebook
kernels/sympy-solve/sympy-solve.ipynb
martinlarsalbert/kaggle
5f75b0b7bf6adf1f5c9c20c2c3d4e1f6670716ac
MIT
kernels/sympy-solve/sympy-solve.ipynb
martinlarsalbert/kaggle
5f75b0b7bf6adf1f5c9c20c2c3d4e1f6670716ac
MIT
kernels/sympy-solve/sympy-solve.ipynb
martinlarsalbert/kaggle
5f75b0b7bf6adf1f5c9c20c2c3d4e1f6670716ac
MIT
Qwen/Qwen-72B
1. YES
2. YES