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: SageMath 8.1

Math 157: Intro to Mathematical Software

UC San Diego, winter 2018

January 22, 2018: SageMath (part 1 of 3)

Administrivia:

  • You should have a solution key for HW 1 in the folder solutions/2018-01-19. We will continue to provide solution keys throughout the course.

  • Homework 2 is now available; it is due Friday, January 26 at 8pm. If you do not have it, contact course staff immediately.

  • 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.

SageMath in Jupyter

As I mentioned in the initial discussion of Jupyter notebook, the backend computing is provided by a piece of software called a kernel. The Python interpreter is an example of a kernel; Sage, also called SageMath to distinguish it from this accounting software, is another example.

To switch kernels, go to the Kernel menu to select a new kernel; this will reset the state of the kernel, so at that point you will have no variables or functions declared. For this lecture, please make sure that you have "SageMath 8.1" as the chosen kernel: you should see this image at the top right of your window (below the CoCalc tab bar): SageMath 8.1 and logo

What is Sage?

From the SageMath home page:

SageMath is a free open-source mathematics software system licensed under the GPL.

Note the distinction between free and open-source. Here free means that there is no cost to use the software, while open-source means that anyone can see the source code, modify it, and distribute the results. The only restriction is that any modified version must be distributed under the same conditions; in order to enforce this, the software is not released into the public domain (without any copyright protection), but under a suitable license (in this case the GNU Public License) which enshrines both the protection of open access and the rules for redistribution.

A consequence of these design choices is that SageMath does not have a cadre of full-time programmers developing the software. Most of the code base has been contributed by math researchers in order to extend Sage for their own purposes. This includes me!

It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more.

One key feature of the open-source model is that it is very easy to incorporate existing software into new software. In particular, in lieu of developing many basic features from scratch, the SageMath developers have incorporated other mature, well-written software to accomplish these tasks. In most cases, these packages are themselves under active development; updates from "upstream" are periodically imported into Sage.

Access their combined power through a common, Python-based language or directly via interfaces or wrappers.

The underlying language of Sage is Python (with some minor modifications). Some of the underlying packages can also be accessed more directly; this is sometimes important for high-performance computations, but we will not deal with such subtleties in this course.

Mission: Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.

The creator and lead developer of Sage is William Stein. Stein is also the creator of CoCalc, but these are different projects (albeit closely related).

Getting started

Since the underlying language of Sage is adapted from Python (currently Python 2, although this may change someday), most simple Python code will run unchanged in Sage. However, behind the scenes there are some subtle changes.

print("Hello World") # Parentheses optional for now, but recommended for Python 3 compatibility
Hello World
[n^2 for n in range(10)] # In Sage, ^ is translated into ** before passing the code to Python.
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
u = 7 type(u) # It's not a Python integer; it's better!
<type 'sage.rings.integer.Integer'>
type(Integer(int(7)))
<type 'sage.rings.integer.Integer'>
ZZ
Integer Ring
type(ZZ)
<type 'sage.rings.integer_ring.IntegerRing_class'>
ZZ(int(7))
7
u = 57 u.is_prime() # Sage integers have more methods than Python integers, including some number theory functions.
False
u.prime_divisors()
[3, 19]
u.next_prime?
v = 5/4 print(v) # What do you expect to see here?
5/4
type(v) # And here?
<type 'sage.rings.rational.Rational'>
# What if I actually want a floating-point number?
w = v*1.0
type(w)
<type 'sage.rings.real_mpfr.RealNumber'>
RR
Real Field with 53 bits of precision
RR(5/4)
1.25000000000000
RF = RealField(200)
RF(5/4)
1.2500000000000000000000000000000000000000000000000000000000
2^100
1267650600228229401496703205376

Symbolic calculus

Some resources for this material:

Preview of this section:

  • defining symbolic variables

  • defining symbolic functions

  • plot

  • differente

  • integrate

  • finding zeros

reset()
type(x)
<type 'sage.symbolic.expression.Expression'>
x*x
x^2
y + theta
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /ext/sage/sage-8.1/local/lib/python2.7/site-packages/sage/all_cmdline.pyc in <module>() ----> 1 y + theta NameError: name 'y' is not defined
# x is predefined; can define other variables... var('y, theta, aleph, xi')
(y, theta, aleph, xi)

Note that these are symbolic variables rather than Python variables.

type(y)
<type 'sage.symbolic.expression.Expression'>
parent(y)
Symbolic Ring
show(x+y+theta +aleph+xi)

In addition to Python functions, Sage also has symbolic functions.

f(z, ucsd) = z*(z+1)*ucsd f()
ucsd*(z + 1)*z
z + ucsd
ucsd + z
type(ucsd)
<type 'sage.symbolic.expression.Expression'>
type(f)
<type 'sage.symbolic.expression.Expression'>
f.parent()
Callable function ring with arguments (z, ucsd)
f - f
(z, ucsd) |--> 0
show(f)
# GOTCHA! z = .5 f(z) = z*(z+1) # this line whacks the previous defn of z!! print f() print z
(z + 1)*z z
type(z)
<type 'sage.symbolic.expression.Expression'>
z = .5 def f(z): print z f(1) print z
1 0.500000000000000
f(z) = z*(z+1) g = plot(f, (0, 3), color='chartreuse', thickness=10) # has a bazillion options show(g + text('hi there class!', (2,6), zorder=2))
Image in a Jupyter notebook
plot(lambda x: cos(x)^2, (0, 2*pi))
Image in a Jupyter notebook
print(diff(x^5 + 2*x + pi, x)) print(diff(tan(x)^2, x))
5*x^4 + 2 2*(tan(x)^2 + 1)*tan(x)
f = sin(x)*cos(x)*tan(x) g = integrate(f, x) show(g)
h = g.differentiate(x) show(h)
k = h - f show(k)
k
-cos(x)*sin(x)*tan(x) - 1/2*cos(2*x) + 1/2
plot(k, 0, 1)
Image in a Jupyter notebook
k.simplify_trig()
0
k.simplify_full()
0
sin(x).find_root(-5,5)
0.0
sin(x).find_root(3,4)
3.141592653589793
N(sin(1/2), digits=50)
0.47942553860420300027328793521557138808180336794060
complex_plot(x, (-1,1), (-1,1)) complex_plot(x^2^2, (-1,1), (-1,1))
Image in a Jupyter notebook
f(x) = 1/sqrt(1-x^4) diff(f,x)

Resources for further study

Since Sage has been around for some time, a number of resources exist for new users.