```python
import sympy as sp
import numpy as np

print(f"SymPy Version: {sp.__version__}")

# 数式をキレイに表示する
sp.init_printing()

# 乱数の種の固定しておく
np.random.seed(123)
```

    SymPy Version: 1.8


### C言語のコードを生成する。

- C言語のコードは、`sympy.ccode()`で出力することができる。


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


```python
sp.ccode(sp.exp(-x))
```




    'exp(-x)'




```python
distance = (x ** 2 + y ** 2 + z ** 2) ** 0.5
distance
```


```python
sp.ccode(distance)
```




    'sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))'




```python
x = sp.MatrixSymbol('x', 3, 1)

A = sp.Matrix(np.random.randint(15, size=(3, 3)))
A
```




$\displaystyle \left[\begin{matrix}14 & 13 & 14\\2 & 12 & 2\\6 & 1 & 3\end{matrix}\right]$




```python
Ax = A * sp.Matrix(x)
Ax
```




$\displaystyle \left[\begin{array}{c}14 x_{0, 0} + 13 x_{1, 0} + 14 x_{2, 0}\\2 x_{0, 0} + 12 x_{1, 0} + 2 x_{2, 0}\\6 x_{0, 0} + x_{1, 0} + 3 x_{2, 0}\end{array}\right]$




```python
n_rows = 3
for i in range(n_rows):
    code = sp.ccode(Ax[i], assign_to=f"y[{i}]")
    print(code)
```

    y[0] = 14*x[0] + 13*x[1] + 14*x[2];
    y[1] = 2*x[0] + 12*x[1] + 2*x[2];
    y[2] = 6*x[0] + x[1] + 3*x[2];



```python

```

aef0d7a5250fff50c9b399532c96d63b059e55f6
ipynb
Jupyter Notebook
notebooks/04-CodeGeneration.ipynb
codemajin/Introduction-to-SymPy
a403b50ba384260a8d5845be1fd1fbd0e5f6b807
MIT
notebooks/04-CodeGeneration.ipynb
codemajin/Introduction-to-SymPy
a403b50ba384260a8d5845be1fd1fbd0e5f6b807
MIT
notebooks/04-CodeGeneration.ipynb
codemajin/Introduction-to-SymPy
a403b50ba384260a8d5845be1fd1fbd0e5f6b807
MIT
Qwen/Qwen-72B
1. YES
2. YES