Attachment 'chap1-first_steps.rst'

Download

Chapter 1: First steps in Python and Sage

Authors: Vincent Delecroix
Nadia Lafrenière
Thierry Monteil
License:CC BY-NC-SA 3.0

The three golden rules

Recall from the introduction the three most import things.

First, to execute a cell on which you have the focus press Shift-Enter.

Second, to access the documentation add a question mark ? at the end and press enter:

sage: is_prime?

Third, to obtain the list of methods that starts with a given prefix, press the tab key:

sage: is_pr

Objects, functions, methods

Most names in Sage are either in snake case like bernoulli, is_prime, cos, ... or camel case like VectorSpace, PolynomialRing, Matrix, ... The snake case notation is mostly reserved to functions

sage: bernoulli(13)
sage: is_prime(73)
sage: cos(pi/3)

While camel case are often objects, that is to say after calling it you obtain an object that you can query further:

sage: K = GF(5)           # the field with 5 elements
sage: K.cardinality()     # size of K
sage: V = VectorSpace(K, 3)   # vector space K^3
sage: V.cardinality()         # size of V

Note that in the above example we used K.cardinality() that actually meant cardinality(K). Though in many programming languages like Python functions are bind to objects. In this situation they are named methods. Most method names are in lower case. Tab-completion also applies to methods. For example if you write V. in a cell and press the tab key you will get all methods appliable to V.

While if you write V.card and press tab, you will magically obtain V.cardinality because this is the unique method of V starting with card

Exercise 1.1

A partition of a nonnegative integer n is a non-increasing list of positive integers with total sum n.

Does Sage have a command for defining a partition ?

(hint: type Part and then hit the <TAB> key)

Create the partition with list [4, 3, 1, 1] and assign it to the Python name p:

(hint: to see documentation and examples for the Permutation command, type Partition?)

Find the conjugate of p (and name it q):

Which of p and q dominates the other ?

(hint: to see the methods available to the partition named p, you can type p. and hit <TAB>)

How did Sage decide whether p dominates q ?

(hint: to access the source code, use p??)

Exercise 1.2

Find out what is the name of the function to construct a matrix

Compute the rank, the determinant and the Hermite normal form of the following 5x5 integer matrix

8 0 7 5 6 9 0 6 9 4 4 9 5 3 0 6 9 4 9 3 0 8 4 5 6

Obtain a latex code for this matrix using the function latex

Playing with integers

To create an integer, simply write it in base 10 as:

sage: 12395851

To assign the integer 14585 to a Python variable named n use:

sage: n = 14585

Once the variable is created, you can access all its methods using tab-completion. Write n. in the following cell and press the tab key

Exercise 1.3

Find out the name of the method on integers that give the list of prime factors of an integer n

(note: to do that, you first need to assign an integer to a Python variable and use tab-completion on this variable)

Solve Euler problem 3: what is the largest prime factor of the number 600851475143

Exercise 1.4

Find the name of the function that given an integer n returns the n-th prime number

Solve Euler problem 7: what is the 100001-th prime number

Exercise 1.5

Solve Euler problem 10: what is sum of prime numbers below two milions?

(hint: You can use the function sum to make the sum of elements in a list.)

Exercise 1.6

What is the name of the method on integer that provides the list of digits?

Check that the sum of digits of 215 = 32768 is 3 + 2 + 7 + 6 + 8 = 26

Solve Euler problem 16 and Euler problem 20

Note that the comparison operators are == (for equality), != (for difference). You already used the sign = but for another purpose: assignment of value to a variable.

Graphics and Symbolic functions

It is often helpful to make pictures in mathematics. Sage comes with a lot of graphical capabilities. The main commands for 2d plotting are

  • plot: plot an object
  • point2d: plot a list of points
  • line2d: draw a broken line between points
  • polygon2d: draw a polygon

For example:

sage: plot(sin(x), (x, 0, 2*pi))
sage: point2d([(0,0), (1,1), (2,0), (3,1)], color='red')

Graphics can be assigned to variables and it is possible to superimpose them using addition:

sage: P1 = plot(sin(x), (x, 0, 2*pi), color='blue')
sage: P2 = plot(cos(x), (x, 0, 2*pi), color='red')
sage: P = P1 + P2
sage: P.show()

Note also that many objects possess a plot method that allows to produce graphics:

sage: m = AlternatingSignMatrices(20).random_element()
sage: fpl = m.to_fully_packed_loop()
sage: fpl.plot(link_color_map='rainbow')

To learn more about graphics, have a look at http://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html and https://doc.sagemath.org/html/en/tutorial/tour_plotting.html

Exercise 1.7

Draw a graphics of the function f(x) = sin(x) + cos(x) − xexp(x) for x between 0 and 1

Using the function find_root, obtain an approximation of the unique solution r of f(r) = 0 with r ∈ (0, 1).

Draw a graphics of f together with a red vertical line between (r,  − 1) and (r, 1)

Exercise 1.8

What are the first 20 terms of the Taylor expansion at x = 0 of the function g(x) = 1 ⁄ (1 − 4*x)

Check on the taylor expansion that

g(x) = n ≥ 0(2nn)xn

Combinatorics

In Sage, it is possible to build set of objects such that the alternating sign matrices of size 100 × 100:

sage: A = AlternatingSignMatrices(100)
sage: A

Exercise 1.9

How many alternating sign matrices of size 100 × 100 are there?

How does it compare with the number of atoms in the universe?

Exercise 1.10

Solve Euler problem 24: what is the millionth lexicographic permutation of {0, 1, …, 9}?

Exercise 1.11

Solve Euler problem 5: what is the smallest positive number that is divisible by all of the numbers from 1 to 20?

More resources

On the Euler project website you can find mathematical challenges to be solved with a computer. You can challenge yourself with the following ones that do not require any programming but only to find the right Sage function

Some other tutorial are available on internet, in particular

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-12 22:01:42, 8.3 KB) [[attachment:CollatzConjecture.ipynb]]
  • [get | view] (2018-08-12 22:01:45, 100.9 KB) [[attachment:CollatzConjecture.pdf]]
  • [get | view] (2018-08-12 22:01:49, 4.8 KB) [[attachment:CollatzConjecture.rst]]
  • [get | view] (2018-08-12 22:01:53, 12.3 KB) [[attachment:Dictionaries-GraphTheory.ipynb]]
  • [get | view] (2018-08-12 22:01:56, 202.6 KB) [[attachment:Dictionaries-GraphTheory_Solutions.pdf]]
  • [get | view] (2018-08-12 22:02:00, 9.1 KB) [[attachment:Dictionaries-GraphTheory_Solutions.rst]]
  • [get | view] (2018-08-15 12:32:58, 16.0 KB) [[attachment:Fields-2018-flatsurf_and_surface_dynamics_demo.ipynb]]
  • [get | view] (2018-08-11 19:25:37, 15.0 KB) [[attachment:S_2_1.svg]]
  • [get | view] (2018-08-12 22:02:10, 13.4 KB) [[attachment:Strings-BWT.ipynb]]
  • [get | view] (2018-08-12 22:02:14, 72.4 KB) [[attachment:Strings-BWT.pdf]]
  • [get | view] (2018-08-12 22:02:17, 7.0 KB) [[attachment:Strings-BWT.rst]]
  • [get | view] (2018-08-11 19:28:42, 16.7 KB) [[attachment:chap1-first_steps.ipynb]]
  • [get | view] (2018-08-11 19:28:46, 103.1 KB) [[attachment:chap1-first_steps.pdf]]
  • [get | view] (2018-08-11 19:28:53, 7.8 KB) [[attachment:chap1-first_steps.rst]]
  • [get | view] (2018-08-11 19:29:05, 24.6 KB) [[attachment:chap1-first_steps_solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:09, 108.7 KB) [[attachment:chap1-first_steps_solutions.pdf]]
  • [get | view] (2018-08-11 19:29:12, 12.0 KB) [[attachment:chap1-first_steps_solutions.rst]]
  • [get | view] (2018-08-11 19:29:17, 30.8 KB) [[attachment:chap2-list_and_for.ipynb]]
  • [get | view] (2018-08-11 19:29:23, 110.0 KB) [[attachment:chap2-list_and_for.pdf]]
  • [get | view] (2018-08-11 19:29:26, 17.6 KB) [[attachment:chap2-list_and_for.rst]]
  • [get | view] (2018-08-11 19:29:32, 42.0 KB) [[attachment:chap2-list_and_for_Solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:35, 116.1 KB) [[attachment:chap2-list_and_for_Solutions.pdf]]
  • [get | view] (2018-08-11 19:29:38, 25.6 KB) [[attachment:chap2-list_and_for_Solutions.rst]]
  • [get | view] (2018-08-11 19:29:51, 21.1 KB) [[attachment:chap3-if-solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:58, 95.4 KB) [[attachment:chap3-if-solutions.pdf]]
  • [get | view] (2018-08-11 19:29:55, 9.0 KB) [[attachment:chap3-if-solutions.rst]]
  • [get | view] (2018-08-11 19:29:41, 11.8 KB) [[attachment:chap3-if.ipynb]]
  • [get | view] (2018-08-11 19:29:45, 91.6 KB) [[attachment:chap3-if.pdf]]
  • [get | view] (2018-08-11 19:29:48, 5.7 KB) [[attachment:chap3-if.rst]]
  • [get | view] (2018-08-11 19:30:02, 4.4 KB) [[attachment:chap4-functions.ipynb]]
  • [get | view] (2018-08-11 19:30:06, 83.1 KB) [[attachment:chap4-functions.pdf]]
  • [get | view] (2018-08-11 19:30:09, 2.3 KB) [[attachment:chap4-functions.rst]]
  • [get | view] (2018-08-11 19:30:20, 3.2 KB) [[attachment:chap5-while.ipynb]]
  • [get | view] (2018-08-11 19:30:14, 62.1 KB) [[attachment:chap5-while.pdf]]
  • [get | view] (2018-08-11 19:30:39, 1.5 KB) [[attachment:chap5-while.rst]]
  • [get | view] (2018-08-11 19:30:47, 4.0 KB) [[attachment:chap6-advanced_exercises.ipynb]]
  • [get | view] (2018-08-11 19:30:53, 69.7 KB) [[attachment:chap6-advanced_exercises.pdf]]
  • [get | view] (2018-08-11 19:31:00, 2.0 KB) [[attachment:chap6-advanced_exercises.rst]]
  • [get | view] (2018-08-12 22:02:04, 90.9 KB) [[attachment:euler.png]]
  • [get | view] (2018-08-13 21:52:57, 1.1 KB) [[attachment:flipper_nf_conversion.py]]
  • [get | view] (2018-08-15 12:18:55, 55.4 KB) [[attachment:flipper_tutorial.pdf]]
  • [get | view] (2018-08-12 21:56:57, 16.1 KB) [[attachment:floating_point_and_stability.ipynb]]
  • [get | view] (2018-08-12 21:57:01, 78.0 KB) [[attachment:floating_point_and_stability.pdf]]
  • [get | view] (2018-08-12 21:57:04, 6.6 KB) [[attachment:floating_point_and_stability.rst]]
  • [get | view] (2018-08-12 22:02:07, 17.6 KB) [[attachment:graph0.png]]
  • [get | view] (2018-08-11 23:44:38, 23.2 KB) [[attachment:intro.en.ipynb]]
  • [get | view] (2018-08-11 23:44:45, 117.5 KB) [[attachment:intro.en.pdf]]
  • [get | view] (2018-08-11 23:44:53, 12.5 KB) [[attachment:intro.en.rst]]
  • [get | view] (2018-08-12 23:25:35, 62.3 KB) [[attachment:logistic_orbit_interact.png]]
  • [get | view] (2018-08-11 19:48:34, 9.6 KB) [[attachment:random_walk.ipynb]]
  • [get | view] (2018-08-11 19:48:38, 87.9 KB) [[attachment:random_walk.pdf]]
  • [get | view] (2018-08-11 19:48:44, 5.7 KB) [[attachment:random_walk.rst]]
  • [get | view] (2018-08-15 19:25:46, 67.3 KB) [[attachment:real_and_complex_numbers.ipynb]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.