{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# <center>Introduction to SageMath</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "SageMath is a **free open-source mathematics software system** licensed under the GPL. It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more. Access their combined power through a common, Python-based language or directly via interfaces or wrappers.\n",
    "\n",
    "Mission: *Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.*\n",
    "\n",
    "\\[ from www.sagemath.org \\]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Interfaces\n",
    "\n",
    "1. Command line\n",
    "2. Interactive shell (using IPython)\n",
    "3. *Notebook* (using Jupyter) running locally or on a remote server.\n",
    "4. SageMathCell\n",
    "5. CoCalc\n",
    "\n",
    "(The next few slides are based on [this](https://www.sagemath.org/library-press.html) and slides by N. Thiéry [here](http://nicolas.thiery.name/Talks/2018-10-08-CategoriesPyData.pdf) and [here](http://nicolas.thiery.name/Talks/2011-05-02-SageDays30.pdf))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Hello, world!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print \"Hello, world!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "[i^2 for i in range(20) if is_prime(i)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "factor(x^4 - 16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "preparse(\"2^3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "pi.n(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Lots of mathematics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "show([42, 1234/6789, I + sqrt(3)*e, QQbar(2).sqrt()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "var(\"n,k\")\n",
    "show(sum(binomial(n,k), k, 0, n, hold=True) == sum(binomial(n,k), k, 0, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A = matrix(3, 3, range(1000, 1009))\n",
    "show(A, \"\\t\", A.echelon_form(), \"\\t\", LatexExpr(\"\\det(A)=\"), det(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "R.<q> = PolynomialRing(QQ)\n",
    "Rt.<t> = PowerSeriesRing(R)\n",
    "show(Rt.random_element(prec=7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(SkewPartition([[7,6,4,1],[3,3,1]]).diagram())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "D14 = DyckWords(14)\n",
    "w = D14.random_element()\n",
    "print(ascii_art(w))\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "And lots more..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Don't reinvent the wheel\n",
    "\n",
    "SageMath aims to provide everything mathematicians, researchers and students need to do their calculations. The basic concept is to combine [many established software packages](https://www.sagemath.org/links-components.html) under one umbrella. Even more than that, it provides powerful and unique algorithms in its own library.\n",
    "\n",
    "When possible, it uses existing tools to solve the problem and combine all of them in one unique interface. This concept not only exposes software packages to a wider audience, but also helps to increase the quality by submitting bugs upstream."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Open Source\n",
    "\n",
    "Sage is free software. Free in both senses of the word: **Libre** and **gratis**.\n",
    "\n",
    "Sage is distributed under the terms of the GNU General Public\n",
    "License version 2 (GPLv2+) which guarentees four types of freedoms:\n",
    "\n",
    "* The freedom to use the software\n",
    "* The freedom to read the source code.\n",
    "* The freedom to improve the software.\n",
    "* The freedom to redistribute the modified software to anyone."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Open Development\n",
    "\n",
    "SageMath loves curious students and researchers to examine its source code and it is possible to understand how each calculation is done. SageMath fosters a community of developers and encourages them to take part in its development. A vital community of people not only using but also participating in development is key to a healthy ecosystem in the field of mathematical software. Additionally, SageMath utilizes the scientific method of peer-review to double check each line of new source code in addition to its strict testing policies to ensure a certain level of quality."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Python\n",
    "\n",
    "SageMath uses Python as its \"glue language\" to interface with all its components. Python is also SageMath's primary interface language and hence SageMath does not invent a new programming language as other mathematical software systems do. Python is well established among research communities and makes interfacing even less complicated. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## RISE\n",
    "\n",
    "From its [homepage](https://rise.readthedocs.io/en/stable/index.html): Reveal.js - Jupyter Slideshow Extension"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Install by running\n",
    "\n",
    "`sage -pip install RISE`\n",
    "\n",
    "and you might need to restart the Jupyter kernel."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Customization\n",
    "\n",
    "RISE and Reveal.js have many customization options.\n",
    "\n",
    "In Jupyter you can go to Edit->\"Edit Notebook Metadata\". For example add\n",
    "\n",
    "    {\n",
    "     ...\n",
    "     \"rise\": {\n",
    "         \"transition\": \"zoom\",  # or \"none\", \"fade\", \"slide\", \"convex\", \"concave\"\n",
    "         \"theme\": \"sky\" # or \"white\", \"simple\", \"serif\"\n",
    "     }\n",
    "    }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "%%javascript\n",
    "document.getElementById(\"theme\").href = \"/nbextensions/rise/reveasl.js/css/theme/sky.css\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Customizing RISE seems to require halting the kernel. These are tests to change them dynamically\n",
    "from jupyter_core.paths import ENV_JUPYTER_PATH\n",
    "import os\n",
    "from IPython.display import Javascript, display\n",
    "\n",
    "path = os.path.join(ENV_JUPYTER_PATH[0], \"nbextensions/rise/reveasl.js/css/theme/sky.css\")\n",
    "print path\n",
    "display(Javascript('document.getElementById(\"theme\").href = \"%s\";' % (path,)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "%%javascript\n",
    "document.getElementById(\"theme\").href = \"/nbextensions/rise/reveasl.js/css/theme/simple.css\";\n",
    "/* change to the simple theme */"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Links and references\n",
    "\n",
    "General SageMath info can be found using search engine and in:\n",
    "* https://www.sagemath.org/ SageMath homepage\n",
    "* [SageMath online documentation](https://doc.sagemath.org/html/en/index.html)\n",
    "* [The Sage Tutorial](https://doc.sagemath.org/html/en/tutorial/index.html)\n",
    "* [The Sage Reference Manual](https://doc.sagemath.org/html/en/reference/index.html), e.g. [continued_fraction](https://doc.sagemath.org/html/en/reference/diophantine_approximation/sage/rings/continued_fraction.html)\n",
    "* [SageMathCell](https://sagecell.sagemath.org/)\n",
    "* [CoCalc](https://cocalc.com/)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "SageMath 8.8.rc1",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
