This repository contains the course materials from Math 157: Intro to Mathematical Software.
Creative Commons BY-SA 4.0 license.
License: OTHER
Math 157: Intro to Mathematical Software
UC San Diego, winter 2018
January 29, 2018: Linear algebra (part 1 of 2)
Administrivia:
Homework 1 has been returned. Feedback has been added to your project. Grades are posted on TritonEd.
Homework 2 solutions have been distributed. We are expecting to have grades and feedback by Friday at noon.
Homework 3 is now posted. It is due Friday, February 2 at 8pm.
In addition, please fill out the week 4 questionnaire.
We are continuing to monitor the course waitlist. If you are on it and still want to join the course, please continue to keep up with lectures and homework, and watch your email for further instructions.
Added in class:
Some of you might find this documentary screening on Monday, February 5 of interest.
We are still working some kinks out of the script that computes participation grades. Let us know if you find any issues.
Vectors and matrices in Sage
A handy "cheat sheet" for matrices in Sage: the Sage Quick Reference for Linear Algebra. You might also find useful:
References for today's material:
Linear algebra reference manual: http://doc.sagemath.org/html/en/reference/index.html#linear-algebra
1.5, 4.4, 4.16, of Sage For Undergraduates: http://www.gregorybard.com/Sage.html
Try this now: solve the system of linear equations There are several ways to do this! How many can you find?
Matrix entries
Unlike a Python array, the entries of a matrix must all be of the same type. Moreover, this type must admit addition, subtraction, and multiplication (that is, it must be an element of a ring). For some operations (e.g., row reduction), one also needs division by nonzero elements (i.e., a field).
Examples:
Integers (parent ring
Integers() == IntegerRing() == ZZ
)Rational numbers (parent ring
Rationals() == QQ
)Real numbers (parent ring
RealField() == RR
)Complex numbers (parent ring
ComplexField() == CC
)Can also change precision:
RealField(200)
for 200 bits of precisionInterval arithmetic:
RealIntervalField(200), ComplexIntervalField(200)
Integers modulo
n
(parent ringIntegers(n) == IntegerModRing(n)
)For integers modulo a prime
p
, another synonym for the parent ring isGF(p)
Symbolic expressions (parent ring
SymbolicRing() == SR
)
To pass from one ring to another, use the change_ring
function.
Constructing a matrix from a formula
The matrix constructor accepts inputs of several different types. One particularly useful syntax is to specify the dimensions (rows, then columns) and then a function that takes a row and column index (which as usual start from 0) and returns the corresponding matrix entry. This function can either be an explicitly declared function or a lambda function.
Try this on your own: formulate a general conjecture based on this example, then prove it. Better yet, try variations of this construction to see if you can come with any other conjectures like it!
Numpy
The NumPy ("Numerical Python") project provides Python with functionality comparable to Fortran. From the home page:
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
a powerful N-dimensional array object
sophisticated (broadcasting) functions
tools for integrating C/C++ and Fortran code
useful linear algebra, Fourier transform, and random number capabilities
NumPy is a pure Python package, but requires installation. However, Sage provides NumPy by default.
The NumPy type array
plays the role of both vectors and matrices. In fact, arrays may have any number of dimensions.
Converting between Sage matrices and Numpy matrices is easy! This makes it possible to apply numpy functions to Sage matrices.
Here we use the NumPy cumulative sum function.
Some other examples of NumPy functions you might want to access from Sage:
least squares approximations (see homework);
matrix decompositions (e.g., singular value decomposition);
discrete Fourier transform.
NumPy and SciPy
A closely related project to NumPy is SciPy ("Scientific Python"). More precisely, I am talking about the SciPy library, which together with NumPy form two components of the SciPy ecosystem. (Other components of the ecosystem include IPython, the basis for the Jupyter notebook; Matplotlib and Sympy, used by Sage for plotting and symbolics; and pandas, more on which later.)
Some of the strengths of SciPy (the library):
low-rank matrix approximations (a/k/a compressed sensing);
interpolation and curve-fitting;
numerical integration (including numerical solution of differential equations);
image manipulation;
signal processing.
We will probably not cover much of this in this course, though. See the SciPy lectures for a number of worked examples.