Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

This repository contains the course materials from Math 157: Intro to Mathematical Software.

Creative Commons BY-SA 4.0 license.

Views: 3037
License: OTHER
Kernel: Octave

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.

prime_range?

But you can do even more: you can type two question marks to get a look at the source code of an individual function!

prime_range??

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.

for p in range(10): print p.is_prime() # Fails because range returns Python integers, which don't have this method.
--------------------------------------------------------------------------- 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.

for p in range(10): print Integer(p).is_prime() # Force conversion to Sage integers.
False False True True False True False True False False
for p in range(10): print is_prime(p) # This works because is_prime also exists as a globally defined command. # When p is passed to is_prime, it automatically converts to a Sage integer.
False False True True False True False True False False
for p in srange(10): print p.is_prime() # Use srange, which returns Sage integers.
False False True True False True False True False False

Repeated squaring

Python includes a command pow which you may find useful.

pow?

To compute pow(x,y,z), Python does not compute x(modz),x2(modz),x3(modz),x4(modz)x \pmod{z}, x^2 \pmod{z}, x^3 \pmod{z}, x^4 \pmod{z}, etc. Rather, it computes x(modz),x2(modz),x4(modz),x8(modz)x \pmod{z}, x^2 \pmod{z}, x^4 \pmod{z}, x^8 \pmod{z}, and so on. It then determines xy(modz)x^y \pmod{z} by writing yy 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.)

var('x,y') plot3d(x^2+y^2,(x,-2,2),(y,-2,2))

Much of what we saw last time about 2D plots extends to 3D plots.

var('x,y,z') implicit_plot3d(z^2 == 3*x^2 + (y-1)^2, (x,-5,5), (y,-5,5), (z,-5,5), axes=True)

... plotting lists of points...

sum(point([random(), random(), random()]) for _ in range(50))

... and adding plots to superimpose them.

var('x,y,z') a = implicit_plot3d(x^2+y^2 == 1, (x,-5,5),(y,-5,5),(z,-5,5), color='blue') b = implicit_plot3d(x^2+y^2+z^2==4, (x,-5,5),(y,-5,5), (z,-5,5), color='red') a+b
MIME type unknown not supported

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):

syms x y
warning: the 'syms' function belongs to the symbolic package from Octave Forge which you have installed but not loaded. To load the package, run 'pkg load symbolic' from the Octave prompt. Please read <http://www.octave.org/missing.html> to learn how you can contribute missing functionality. error: 'syms' undefined near line 1 column 1
pkg load symbolic
syms x y
OctSymPy v2.6.0: this is free software without warranty, see source. Initializing communication with SymPy using a popen2() pipe. Some output from the Python subprocess (pid 19511) might appear next. Python 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> OctSymPy: Communication established. SymPy v1.1.1.

Reminder: SymPy also provides symbolics functionality to Sage.

factor(x^3 + 3*x^2*y + 3*x*y^2 + y^3)
ans = (sym) 3 (x + y)
diff(sin(x+3*y))
ans = (sym) cos(x + 3⋅y)

MATLAB assignments, section 1.6: Matrices in MATLAB

A = [2 1; 4 3] B = [2,1; 4 3]
A = 2 1 4 3 B = 2 1 4 3

MATLAB assignments, section 1.7: Matrix operations and manipulation

Fibonacci = [1 1 2 3; 5 8 13 21; 34 55 89 144; 233 377 610 987]
Fibonacci = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
A(1,2)
ans = 1
A(2,:)
ans = 4 3
Fibonacci(2:4,1)
ans = 5 34 233

MATLAB assignments, section 1.8: Random matrices

RMatrix1 = rand(3)
RMatrix1 = 0.68327 0.81027 0.98054 0.35526 0.21193 0.24415 0.82799 0.94380 0.41820

MATLAB assignments, section 2.1: Solving linear systems

A = [2 -1 1; 1 2 3; 3 0 -1] b = [8; 9; 3] x = A\b
A = 2 -1 1 1 2 3 3 0 -1 b = 8 9 3 x = 2 -1 3

MATLAB assignments, section 2.3: Computer science: web search

L = [0,1/3,1/3,1/2; 1/2,0,1/3,0; 1/2, 1/3, 0, 1/2; 0, 1/3, 1/3, 0] rref([L-eye(4)])
L = 0.00000 0.33333 0.33333 0.50000 0.50000 0.00000 0.33333 0.00000 0.50000 0.33333 0.00000 0.50000 0.00000 0.33333 0.33333 0.00000 ans = 1.00000 0.00000 0.00000 -1.50000 0.00000 1.00000 0.00000 -1.31250 0.00000 0.00000 1.00000 -1.68750 0.00000 0.00000 0.00000 0.00000

...

MATLAB assignments, section 5.3: Least squares

B = [1 75; 1 100; 1 128; 1 159; 1 195] d = [15; 23; 26; 34; 38] [b, se_b, mse] = lscov(B, d, eye(5))
B = 1 75 1 100 1 128 1 159 1 195 d = 15 23 26 34 38 b = 2.35959 0.18904 se_b = 2.617713 0.018959 mse = 3.2298