PHY 3101 - Modern Physics - Fall 2015


Programming and PHY 3101


Programming tools in PHY 3101

In this course I will introduce you to two free programming tools that are extensively used (and are similar to others that are heavily used) by physicists and engineers: python, a general purpose programming language, and one of several possible tools that can be used to manipulate and solve mathematical expressions in symbolic form. The tools are free (costly ones such as Mathematica Maple, etc. are also possible) and run on all popular operating systems (Windows, Mac, Linux).

  • Python: Python is a full-scale programming language (similar to C++ and Java) but has a much simpler syntax and runs directly without compiling (it's technically called a "scripting language"). Python is a powerful tool for solving problems where one has to use conditional logic, loops and external routines. It also has many libraries that extend its power. However, we will only use its most basic features in this course.
  • CAS software: You should also acquire a "computer algebra system" or CAS, capable of doing mathematics in symbolic form. A CAS allows you to manipulate mathematical expressions in symbolic form (trig functions, polynomials, special functions, etc), work with exact rational expressions like 2/5 + 1/3, differentiate and integrate complex expressions, compute Taylor series to arbitrarily high powers, solve equations and differential equations, and plot functions.

Substitutions

If you have MatLab, R, C++ or C you can use those instead of python

For the CAS, you have several choices.

  • Sympy: This was my original recommendation. However, further research has convinced me that this software is not easily downloaded and installed by people who are not Unix users. However, if you do install it, I have provided tutorials below.
  • wxMaxima: I have used wxMaxima for several years in another class with good results. I have provided tutorials for it below.
  • Sage: I have also used Sage in a previous class and have tutorials for it below. Note that it comes packaged for Windows and Mac OSX, but is a very large download and install.
  • Sagecloud: This Sage site is operated by the University of Washington under an NSF grant. It allows multiple users to access its resources. You can get a free account and start using it immediately. You can create, edit, execute and save programs at the site. You can also upload and download Sage programs to/from your local machine.
  • Mathematica: Mathematica is a commercial product that is expensive, though it is available as a relatively inexpensive student version. However, it can also be used for free at the UFApps site. Please check out this option.
  • Maple: I used Maple several years ago, but have not used it lately. It costs money but it is a nice program.
Python installation, tutorials and other resources
Download and install Standard install: This is done at the official python download site. You can click on the download button for 2.7.x (it is currently 2.7.10, but is updated occasionally). Once you download the package you can double click on it to install the system. There are binary versions for Mac and Windows so there is no need to download the source code and build it yourself.

Install using a python distribution: Another option is to use distributions such as Enthought Canopy or Anaconda. Both distributions include python plus a large number of related libraries such as numpy (numerical python), scipy (scientific python library), matplotlib (plotting in python) and sympy (CAS). A single installation guarantees that the packages are compatible. Note that these distributions are free only if you are a student or academic.

Note: I am using the 2.7.x version of python because most main science applications have not made the transition to version 3.x yet. However, you should be able to use either version since the differences are not that important at the elementary level. Be aware, though, that I have not tested my tutorials with the 3.x version.
Official tutorial This is a useful tutorial on the Python website, but you probably want to start at Chapter 3.
Simple tutorial Good for those of you new to programming. It's aimed at Python v2.6, but it's perfectly adequate for learning any version.


Optional CAS tool: Sage

Sage is another possibility as a computer algebra system. It runs on top of Python and thus most of its commands use underlying Python syntax and constructs. Sage has become somewhat complicated to install, and it doesn't appear to work with Windows, so I am providing a link to the cloud version at the University of Washington. Once you get an account, you can play with the system and try the tutorials.

Sage tutorials and resources
Cloud version of Sage This Sage site is operated by the University of Washington under an NSF grant. It allows multiple users to access its resources. You can get a free account and start using it immediately. You can create, edit, execute and save programs at the site. You can also upload and download Sage programs to/from your local machine.
Cloud Sage tutorial You have to create an account and login to test out the system. However, there does not seem to be a tutorial that teaches you how to use it.
avery tutorial 1
pdf output
Basic Sage, including syntax, exact and floating point math, arbitrary precision evaluation, basic expressions using polynomial, exponential and trig functions, factoring and expanding numbers and algebraic expressions, defining symbolic variables, defining functions, using substitution, trigonometric evaluation.
avery tutorial 2
pdf output
Derivatives, definite and indefinite integrals; taylor series; sums of powers of integers and expressions in closed form; using assume() to specify whether a variable is real, complex, integer, postive, etc.; special functions, paritcularly orthogonal polynomials.
avery tutorial 3
pdf output
2-D and 3-D plots.
Standard Sage tutorial This is from the Sage main page. It's useful, but tends to have too much information for users who don't care about the internals.


Optional CAS tool: wxMaxima

wxMaxima (a graphical version of Maxima) is another CAS program. Its documentation is not great, however, so I am providing you with tutorials that allow you to learn from examples, which is the way I learned it.

Below are some resources that you will find useful to come up to speed in wxMaxima.

wxMaxima tutorials and other resources
avery tutorial 1
pdf output
Basic wxMaxima, including syntax, exact and floating point math, basic expressions using polynomial, exponential and trig functions, factoring and expanding numbers and algebraic expressions, defining expressions and functions, substitution, trigonometric evaluation.
avery tutorial 2
pdf output
Derivatives, definite and indefinite integrals, taylor series, plotting functions
avery tutorial 3
pdf output
Sums of expressions in closed form, numerical integration, 2-D plots.
avery tutorial 4
pdf output
Not finished yet.
10 minute tutorial This is a short tutorial that you might find useful
William Stein tutorial A pdf file with Maxima examples
Ted Woollett page This site has several pdf files of increasing complexity that walk you through the Maxima language. I found it helpful when trying to understand some of the more arcane points of the language.


Optional CAS tool: Sympy

Sympy is written entirely in Python so after installation it is very easy to use. The problem, as noted above, is downloading and installing it. If you are proficient with python installations or use a python package distribution, this might not be a problem.

Sympy installation, tutorials and other resources
Download and install Warning: Unfortunately, downloading sympy turns out to be a more complex process than what an average person should have to deal with. Most documentation seems to assume that users are running Linux and are proficient with the operating system. I downloaded pip and used the "pip install" command on a Mac terminal. That's still not convenient for an average user who might not know how to run from a terminal window on Windows.

Install using a python distribution: A better option is to use a completely configured package such as the ones from Enthought or Anaconda, described above.

After you download and install sympy on your system, you use it by starting python and invoking the python import command.
Official tutorial Basic tutorial on sympy's capabilities.
avery tutorial 1
pdf output
Basic sympy, including syntax, exact and floating point math, basic expressions using polynomial, exponential and trig functions, factoring and expanding numbers and algebraic expressions, defining expressions and functions, substitution, trigonometric evaluation.
avery tutorial 2
pdf output
Calculus: Derivatives, definite and indefinite integrals, taylor series, sums, etc.
More tutorials to be added Need a tutorial where plotting is used. For that you will need matplotlib.