# Code printers

The most basic form of code generation are the code printers. The convert SymPy expressions into the target language. 

The most common languages are C, C++, Fortran, and Python, but over a dozen languages are supported. Here, we will quickly go over each supported language. 


```python
from sympy import *
init_printing()
```

Let us use the function $$|\sin(x^2)|.$$


```python
x = symbols('x')
expr = abs(sin(x**2))
expr
```


```python
ccode(expr)
```


```python
fcode(expr)
```


```python
julia_code(expr)
```


```python
jscode(expr)
```


```python
mathematica_code(expr)
```


```python
octave_code(expr)
```


```python
from sympy.printing.rust import rust_code
rust_code(expr)
```


```python
rcode(expr)
```


```python
from sympy.printing.cxxcode import cxxcode
cxxcode(expr)
```

## Exercise: Codegen your own function

Come up with a symbolic expression and try generating code for it in each language. Note, some languages don't support everything. What works and what doesn't? What things are the same across languages and what things are different?

**Reminder: If you click a cell and press `b` it will add a new cell below it.**


```python
# Write your answer here

```


```python

```


```python

```

## Exercise: Plotting SymPy Functions with JavaScript

One use case that works nicely with the Jupyter notebook is plotting mathematical functions using JavaScript plotting libraries. There are a variety of plotting libraries available and the notebook makes it relatively easy to use. Here we will use [Chart.js](http://www.chartjs.org/) to plot functions of a single variable. We can use the `%%javascript` magic to type JavaScript directly into a notebook cell. In this cell we load in the Chart.js library:


```javascript
%%javascript
require.config({
  paths: {
      'chartjs': '//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart'
  }
});
```

We've also prepared some Javascript to do the plotting. This code will take two mathematical expressions written in Javascript and plot the functions.


```python
from scipy2017codegen.plotting import js_template
```


```python
print(js_template.format(top_function='***fill me in!***',
                         bottom_function='***fill me in!***',
                         chart_id='***fill me in!***'))
```

Now SymPy functions can be plotted by filling in the two missing expressions in the above code and then calling the `Javascript` display function on that code.


```python
from IPython.display import Javascript
```


```python
x = symbols('x')
f1 = sin(x)
f2 = cos(x)
```


```python
Javascript(js_template.format(top_function=jscode(f1),
                              bottom_function=jscode(f2),
                              chart_id='sincos'))
```

## Exercise: Batman!

Plot the equations below for `top` and `bottom`.

There are all kind of functions that can be plotted, but one particularly interesting set of functions are called the [Batman Equations](http://mathworld.wolfram.com/BatmanCurve.html). We've provided the piecewise versions of these functions written in SymPy below. Try plotting these with the JS plotter we've created.


```python
from scipy2017codegen.plotting import batman_equations
```


```python
top, bottom = batman_equations()
```


```python
top
```


```python
bottom
```


```python
# Write your answer here

```

acf1c64a9e6c17f1de920366d2d5193b1ce9b8f4
ipynb
Jupyter Notebook
notebooks/02-code-printers.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-05-31T21:01:22.000Z
2022-02-14T04:26:01.000Z
notebooks/02-code-printers.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-06-06T19:05:04.000Z
2018-08-29T23:15:19.000Z
notebooks/02-code-printers.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-06-06T14:45:12.000Z
2021-09-25T14:11:06.000Z
Qwen/Qwen-72B
1. YES
2. YES