{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SageMath Software\n", "\n", "KME Meeting -- February 25, 2019" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Brief history of SageMath\n", "\n", "- Originally called SAGE: **S**ofware for **A**lgebraic **G**eometry **E**xploration\n", " * It is a complete free and open-source distribution of mathematics software for OS X and Linux. (All under GPL compatible licenses.)\n", " * Now called SageMath.\n", "
\n", "
\n", "\n", "- SageMath is a computer algebra system based on the Python programming language as an open-source alternative designed to compete with the big-name M’s.\n", " * It was created in 2005 by William Stein and is now maintained and expanded with the help of the open-source community.\n", " * **Mission statement:** *Create a viable, free open-source alternative to Magma, Maple, Mathematica, and Matlab.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with numbers in SageMath" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "2 + 3" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "5 * 6" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "10 / 2" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "11 / 2" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "N(11 / 2)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "5 ^ 3" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "gcd(135, 75)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "lcm(135, 75)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "factor(37238117074)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "factorial(645)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "645!" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "e" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "N(e)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "pi" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "N(pi)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "log(45)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "log(16,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculus in SageMath" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f(x) = sin(3*x) + 2*exp(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.diff(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.diff(x)(3)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.integrate(x,0,3)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "integrate(f(x),x,0,3)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.integrate(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.integrate(y)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "var('t,y,z')" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.integrate(y)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f." ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "f.taylor(x,0,5)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "g(x,y) = sin(x-y)*y*cos(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "g.integrate(x).integrate(y)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "integrate( integrate( x * y, x, 0, y-3), y, 0, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting functions in SageMath" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "p = plot(f,-2,2)\n", "p.show()" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "p = plot(f,-2,2,frame=True, axes=False, gridlines=True, color='purple', thickness = 2)\n", "p.show()" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "implicit_plot(y^2 - x^3 - x^2, (x,-2,2), (y,-2,2),frame=True, axes=False, gridlines=True, color='red')" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "q = implicit_plot(y^2 - x^3 - x^2, (x,-2,2), (y,-2,2),frame=True, axes=False, gridlines=True, color='red')\n", "(p+q).show(aspect_ratio='automatic')" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "L = [(0,0)]\n", "for i in range(2000):\n", " last = L[-1]\n", " L.append((last[0]+(random()-.5), last[1]+(random()-.5)))\n", "\n", "g = line(L, thickness=.4, color='blue')\n", "g += points(L, pointsize=10, color='grey')\n", "g.show(aspect_ratio=1, figsize=10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f(x) = 3*sin(10*x) + 2*x\n", "t1 = f.taylor(x,pi/32,1)\n", "t2 = f.taylor(x,pi/32,2)\n", "pf = f.plot(color='red',ymin=-10,ymax=10,thickness=2)\n", "p1 = t1.plot(color='green')\n", "p2 = t2.plot(color='purple')\n", "pf + p1 + p2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@interact\n", "def my_taylor(degree=slider(0, 20, 1, 1)):\n", " p1 = plot(f, -2, 2, color='red', ymin=-10, ymax=10,thickness=2)\n", " p2 = plot(f.taylor(x, 0, degree), -2, 2, ymin=-10, ymax=10)\n", " show(p1 + p2, figsize=8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "frames = [pf]\n", "for k in range(1,20):\n", " frames.append(pf + plot(f.taylor(x,0,k),color='blue',ymin=-10,ymax=10))\n", "a = animate(frames)\n", "a.show(delay=60,iterations=2)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "P = plot3d(g,(x,-3,3),(y,-3,3), mesh=True)\n", "P.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What else?" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.7.beta5", "language": "", "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": 1 }