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 26, 2018: SageMath (part 3 of 3)
Administrivia:
Homework 2 is due at 8pm.
Virtual office hours today 3-7pm in the master chat room (shared project).
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.
Introspection
As in Python, you can do introspection to do a quick documentation lookup on any Sage command. For instance, this is a very easy way to check the input and output syntax.
But you can do even more: you can type two question marks to get a look at the source code of an individual function!
In this case, we see that this function is partly based on code provided by the Pari package. When using specialized functionality in Sage, it may be important to know where the code ultimately comes from, in case you need to understand what it is doing on a deeper level.
Reminder: if introspection doesn't tell you what you need, try the Sage documentation. In particular, for problem 2, you might want to look at the Sage reference manual for the most detailed explanation of use of external components in various contexts.
Python integers versus Sage integers
Sage integers have more methods than Python integers; consequently, if you do something that produces a Python integer and try to call a Sage integer method, this may fail.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-9e88479f9212> in <module>()
1 for p in range(Integer(10)):
----> 2 print p.is_prime() # Fails because range returns Python integers, which don't have this method.
AttributeError: 'int' object has no attribute 'is_prime'
There are several ways to fix this.
Repeated squaring
Python includes a command pow
which you may find useful.
To compute pow(x,y,z)
, Python does not compute , etc. Rather, it computes , and so on. It then determines by writing as a sum of powers of 2 (binary expansion) and combining the previous results.
Sage has a command powermod
that does essentially the same thing.
3D plotting
The example from last time. (The positioning issue is an issue with CoCalc which I have reported.)
Much of what we saw last time about 2D plots extends to 3D plots.
... plotting lists of points...
... and adding plots to superimpose them.
Preview of linear algebra
As you may recall, UCSD's standard linear algebra course (Math 18) includes a MATLAB portion. Since MATLAB is not an open-source package, it is not available in CoCalc.
All of the functionality of MATLAB demonstrated in Math 18 (which is admittedly a rather small portion of what MATLAB is capable of) can be reproduced in Sage. We'll see that in the upcoming lectures and assignments.
But first, let me show you Octave, an open-source project which (almost) exactly replicates MATLAB's syntax and much of its basic functionality. Switch your kernel to "Octave" now.
MATLAB assignments, section 1.4 (computer algebra):
Reminder: SymPy also provides symbolics functionality to Sage.
MATLAB assignments, section 1.6: Matrices in MATLAB
MATLAB assignments, section 1.7: Matrix operations and manipulation
MATLAB assignments, section 1.8: Random matrices
MATLAB assignments, section 2.1: Solving linear systems
MATLAB assignments, section 2.3: Computer science: web search
...
MATLAB assignments, section 5.3: Least squares