Python and CAS demos#

In Mathematics 1b we will mainly use Python with the Python library SymPy for symbolic mathematics and Matplotlib for plotting (dtumathtools will handle some of the plotting for us), but we will also need other Python libraries such as NumPy and SciPy, in partiuclar, in the large group projects.

SymPy is a computer algebra system (CAS) written entirely in Python. It is easily extensible with other Python libraries such as NumPy, SciPy, and Matplotlib.

In the following sections you will see Demos of how SymPy can be used in Mathematics 1b.

Installations#

You can find an installation guide for Python SymPy here.

For installation of Jupyter Notebooks, see below.

Documentation#

If SymPy outputs and error or if you cannot remember the name of a command, then check the documentation: https://docs.sympy.org/latest/index.html. If one for instance wishes to solve a differential equation, one can simply google solve differential equation site:https://docs.sympy.org. Remember to use site:https://docs.sympy.org if you wish to search within the documentation. Also, there exist many other good tutorials there. The documentation for Python in general is found here: https://docs.python.org/3/. Remember to download documentations before the exam since there will only be local internet.

Demos#

In the following sections you will see Demos of how SymPy can be used in Mathematics 1b. Also, consider reviewing the demos from Math1a here for the basic introduction if you feel a little rusty.

Jupyter Notebooks#

We assume in this course that you are familiar with the use of Jupyter Notebooks – otherwise we advise you to complete this exercise:

Exercise: Get Started with Notebooks#

We will in this exercise start working with Jupyter Notebooks. When you are done, then you will have Jupyter extensions installed in VS Code and you can create a new Jupyter Notebook, write Markdown and Python cells in it, and you can “run” these cells.

  1. In VS Code, click in the left column on the squares/blocks called “Extensions”.

  2. Seach “Jupyter” and install the first result from the top, if it is not already installed. You are now ready to work with Juputer notebooks.

  3. Now create a new Jupyter Notebook my_new_notebook.ipynb. You can create a Jupyter Notebook by running the command Create: New Jupyter Notebook from the Command prompt (Ctrl+Shift+P) or by creating a new .ipynb file in your folder or by writing code my_new_notebook.ipynb in the terminal in VS Code. Then choose a kernel using the kernel chooser in the upper right corner.

  4. Write print("Hello World!") in the first cell. Now run the code cell by using the Run icon to the left of the cell, and check that the output is shown directly under the code cell.

  5. Create a Markdown cell (click on +Markdown) and write This cell will not be run by the Python interpreter and should be used for explanatory text and displayed mathematics, such as $4 x_1 + 5x_2 =7$ and $$\int x^2 dx.$$. Click on the check mark symbol to the right to leave editor mode (alternatively press ESC).

Note

  1. If you already have an existing Jupyter Notebook, you can open it by right-clicking on the file and opening it with VS Code or via the VS Code file browser.

  2. You can also run code by using keyboard shortcuts in both command mode and editor mode. To run it in the chosen (focused) cell you must press Ctrl+Enter. To run the chosen cell and move on to the next cell, press Shift+Enter.

  3. You can save your Jupyter Notebook by using the keyboard shortcut Ctrl+S or by navigating to File > Save.

  4. You can read more here: https://code.visualstudio.com/docs/datascience/jupyter-notebooks and find the most-used keyboard shortcuts here: https://bbyond.medium.com/vscode-jupyter-notebook-keyboard-shortcuts-31fab95fa301.