{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Chapter 1: First steps in Python and Sage\n", "\n", "Authors \n", "- Vincent Delecroix\n", "- Nadia Lafreni\u00e8re\n", "- Thierry Monteil\n", "\n", "License \n", "CC BY-NC-SA 3.0\n", "\n", "## The three golden rules\n", "\n", "Recall from the introduction the three most import things.\n", "\n", "First, to execute a cell on which you have the focus press Shift-Enter.\n", "\n", "Second, to access the documentation add a question mark $?$ at the end\n", "and press enter:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_prime?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Third, to obtain the list of methods that starts with a given prefix,\n", "press the tab key:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_pr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objects, functions, methods\n", "\n", "Most names in Sage are either in *snake case* like `bernoulli`,\n", "`is_prime`, `cos`, ... or *camel case* like `VectorSpace`,\n", "`PolynomialRing`, `Matrix`, ... The snake case notation is mostly\n", "reserved to functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bernoulli(13)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_prime(73)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cos(pi/3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While camel case are often objects, that is to say after calling it you\n", "obtain an object that you can query further:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "K = GF(5) # the field with 5 elements\n", "K.cardinality() # size of K" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V = VectorSpace(K, 3) # vector space K^3\n", "V.cardinality() # size of V" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in the above example we used `K.cardinality()` that actually\n", "meant `cardinality(K)`. Though in many programming languages like Python\n", "functions are bind to objects. In this situation they are named\n", "*methods*. Most method names are in lower case. Tab-completion also\n", "applies to methods. For example if you write `V.` in a cell and press\n", "the tab key you will get all methods appliable to `V`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While if you write `V.card` and press tab, you will magically obtain\n", "`V.cardinality` because this is the unique method of `V` starting with\n", "`card`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.1\n", "\n", "A *partition* of a nonnegative integer $n$ is a non-increasing list of\n", "positive integers with total sum $n$.\n", "\n", "Does Sage have a command for defining a partition ?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*hint*: type `Part` and then hit the `` key)\n", "\n", "Create the partition with list $[4, 3, 1, 1]$ and assign it to the\n", "Python name `p` :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*hint*: to see documentation and examples for the Permutation command,\n", "type `Partition?`)\n", "\n", "Find the conjugate of `p` (and name it `q`):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which of `p` and `q` dominates the other ?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*hint*: to see the methods available to the partition named `p`, you\n", "can type `p.` and hit ``)\n", "\n", "How did Sage decide whether `p` dominates `q` ?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*hint*: to access the source code, use `p??`)\n", "\n", "### Exercise 1.2\n", "\n", "Find out what is the name of the function to construct a matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the rank, the determinant and the Hermite normal form of the\n", "following 5x5 integer matrix\n", "\n", "$$\\begin{aligned}\n", "\\left(\\begin{array}{rrrrr}\n", "8 & 0 & 7 & 5 & 6 \\\\\n", "9 & 0 & 6 & 9 & 4 \\\\\n", "4 & 9 & 5 & 3 & 0 \\\\\n", "6 & 9 & 4 & 9 & 3 \\\\\n", "0 & 8 & 4 & 5 & 6\n", "\\end{array}\\right)\n", "\\end{aligned}$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Obtain a latex code for this matrix using the function `latex`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Playing with integers\n", "\n", "To create an integer, simply write it in base 10 as:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "12395851" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To assign the integer $14585$ to a Python variable named `n` use:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 14585" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once the variable is created, you can access all its methods using\n", "tab-completion. Write `n.` in the following cell and press the tab key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.3\n", "\n", "Find out the name of the method on integers that give the list of prime\n", "factors of an integer `n`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*note*: to do that, you first need to assign an integer to a Python\n", "variable and use tab-completion on this variable)\n", "\n", "Solve [Euler problem 3](https://projecteuler.net/problem=3): what is the\n", "largest prime factor of the number $600851475143$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.4\n", "\n", "Find the name of the function that given an integer `n` returns the\n", "`n`-th prime number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve [Euler problem 7](https://projecteuler.net/problem=7): what is the\n", "100001-th prime number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.5\n", "\n", "Solve [Euler problem 10](https://projecteuler.net/problem=10): what is\n", "sum of prime numbers below two milions?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*hint*: You can use the function `sum` to make the sum of elements in a\n", "list.)\n", "\n", "### Exercise 1.6\n", "\n", "What is the name of the method on integer that provides the list of\n", "digits?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check that the sum of digits of $2^{15} = 32768$ is\n", "$3 + 2 + 7 + 6 + 8 = 26$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve [Euler problem 16](https://projecteuler.net/problem=16) and [Euler\n", "problem 20](https://projecteuler.net/problem=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the comparison operators are `==` (for equality), `!=` (for\n", "difference). You already used the sign `=` but for another purpose:\n", "assignment of value to a variable.\n", "\n", "## Graphics and Symbolic functions\n", "\n", "It is often helpful to make pictures in mathematics. Sage comes with a\n", "lot of graphical capabilities. The main commands for 2d plotting are\n", "\n", "- `plot` : plot an object\n", "- `point2d` : plot a list of points\n", "- `line2d` : draw a broken line between points\n", "- `polygon2d` : draw a polygon\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot(sin(x), (x, 0, 2*pi))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "point2d([(0,0), (1,1), (2,0), (3,1)], color='red')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Graphics can be assigned to variables and it is possible to superimpose\n", "them using addition:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "P1 = plot(sin(x), (x, 0, 2*pi), color='blue')\n", "P2 = plot(cos(x), (x, 0, 2*pi), color='red')\n", "P = P1 + P2\n", "P.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note also that many objects possess a `plot` method that allows to\n", "produce graphics:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = AlternatingSignMatrices(20).random_element()\n", "fpl = m.to_fully_packed_loop()\n", "fpl.plot(link_color_map='rainbow')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To learn more about graphics, have a look at\n", "\n", "and \n", "\n", "### Exercise 1.7\n", "\n", "Draw a graphics of the function $f(x) = \\sin(x) + \\cos(x) - x \\exp(x)$\n", "for $x$ between $0$ and $1$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the function `find_root`, obtain an approximation of the unique\n", "solution $r$ of $f(r) = 0$ with $r \\in (0,1)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw a graphics of $f$ together with a red vertical line between\n", "$(r, -1)$ and $(r, 1)$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.8\n", "\n", "What are the first 20 terms of the Taylor expansion at $x = 0$ of the\n", "function $g(x) = 1 / \\sqrt{1 - 4*x}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check on the taylor expansion that\n", "\n", "$$g(x) = \\sum_{n \\geq 0} \\binom{2n}{n} x^n$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combinatorics\n", "\n", "In Sage, it is possible to build set of objects such that the\n", "[alternating sign\n", "matrices](https://en.wikipedia.org/wiki/Alternating_sign_matrix) of size\n", "$100\n", "\\times 100$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = AlternatingSignMatrices(100)\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.9\n", "\n", "How many alternating sign matrices of size $100 \\times 100$ are there?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How does it compare with the number of atoms in the universe?\n", "\n", "### Exercise 1.10\n", "\n", "Solve [Euler problem 24](https://projecteuler.net/problem=24): what is\n", "the millionth lexicographic permutation of $\\{0, 1, \\ldots, 9\\}$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.11\n", "\n", "Solve [Euler problem 5](https://projecteuler.net/problem=5): what is the\n", "smallest positive number that is divisible by all of the numbers from 1\n", "to 20?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More resources\n", "\n", "On the [Euler project](https://projecteuler.net) website you can find\n", "mathematical challenges to be solved with a computer. You can challenge\n", "yourself with the following ones that do not require any programming but\n", "only to find the right Sage function\n", "\n", "Some other tutorial are available on internet, in particular\n", "\n", "- [Official Python tutorial](https://docs.python.org/2.7/tutorial/)" ] } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } }, "nbformat": 4, "nbformat_minor": 2 }