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 particular, in the project period.
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. The following sections contain demos of the use of Sympy on the topics in Mathematics 1b.
Installations#
You can find an installation guide for Python and Sympy here. For installation of Jupyter Notebooks, see below.
Documentation#
If Sympy outputs an error or if you cannot remember the name of a command, then check the documentation: https://docs.sympy.org/latest/index.html. If you, for instance, wish to solve a differential equation, simply google solve differential equation site:https://docs.sympy.org. With the site:https://docs.sympy.org you search within the official documentation only. You can also find many other good tutorials there. The documentation for Python in general is found here: https://docs.python.org/3/.
Demos#
For Sympy demos on the material from Mathematics 1a, you can check out the demos on the Math1a course website. Below are listed demos on the use of Sympy in the topics from Mathematics 1b.
- Welcome to Sympy!
- Demo 1: Functions, Plotting, and Partial Derivatives
- Demo 2: Differentiability
- Demo 3: Inner-Product Spaces
- Demo 4: The Spectral Theorem, Diagonalization, and Hermitian Matrices
- Demo 5: Taylor Approximations
- Demo 6: Extremum Investigation and Optimization
- Demo 7: Riemann Integrals
- Demo 8: Parametric Representations
- Demo 9: Plane, Volume, Line, and Surface Integrals with The Change-of-Variables Theorem
- Demo 10: Vector Fields and their Integration along Curves (the Tangential Line Integral)
- Demo 11: Integration of Vector Fields through Surfaces (Flux)
Jupyter Notebooks#
These demos will present coding in Python/Sympy with the presentation program Jupyter Notebooks that allows for a mix of text and code on the same page/in one document. We advise you to use Jupyter yourself for your coding work. If you are not familiar with Jupyter then we advise you to complete this exercise:
Exercise: Get Started with Jupyter Notebooks#
This small exercise in Jupyter Notebooks will let you install necessary Jupyter extensions in your local Visual Studio (VS) Code software and train you in how to create a new Jupyter Notebook, create Markdown and Python cells in it, and execute (“run”) these cells.
In VS Code, click in the left column on the squares/blocks called Extensions.
Search for Jupyter and install the first result from the top, if not already installed. You are now ready to work with Jupyter Notebooks.
Now create a new Jupyter Notebook called something like
my_new_notebook.ipynb. To do so, run the commandCreate: New Jupyter Notebookfrom the Command prompt (use the keyboard shortcutCtrl+Shift+P) or by creating a new .ipynb file in your folder or by writingcode my_new_notebook.ipynbin the terminal in VS Code. Then choose a kernel using the kernel chooser in the upper right corner.Write
print("Hello World!")in the first cell, which by default should be a code 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.Create a markdown cell (click on +Markdown) and write text such as
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 checkmark symbol to the right to leave editor mode (alternatively, pressCtrl+Enter).
Note
If you already have an existing Jupyter file, you can open it by right-clicking the file and choosing to open it with VS Code or alternatively from within VS Code by navigating to File -> Open and there choose the file.
You can execute/run a code cell by pressing
Ctrl+Enter. To run the cell and move on to the next cell (if there is no next cell, then one will be created), pressShift+Enter. To leave editor mode without running the cell, simply pressesc.You can save your Jupyter Notebook by pressing
Ctrl+sor by navigating to File -> Save.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.