{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "
Sage is a computer algebra system (sometimes abbreviated as CAS). It can do all kinds of mathematics, ranging from plain computations to very abstract symbolic stuff. The model language for Sage is Python, so if you already know some Python you're ready to go.
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+3" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Helpfully, Sage does ordinary arithmetic. You can change the $2+3$ above to a different expression and hit the 'evaluate' button to get a new answer (try it!). (In order for the 'evaluate' button to appear, you need to click in/on the cell.) You can also use Shift-Enter to evaluate a cell once you've clicked in it.
\n", "Next you'll make a new cell: mouse just below this text until you get a horizontal blue line, and then click. The result should be a blank evaluation cell. Type something like 2^3*3 and evaluate it to see how this works. (Notice that Sage understands the usual order of operations.)
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# There are a couple of ways to write notes to yourself or the instructor.\n", "# The one you're reading now is a note inside an evaluation cell. Each line is preceded by\n", "# (the pound sign or number sign), and that tells Sage that you're making a comment rather than issuing a command.\n", "# The other one appears at the top of the page (and elsewhere), outside of evaluation cells. To create a text cell, use shift-click when creating a cell. To edit an existing text cell, double-click on it.\n", "# What do you think should happen if you evaluate this cell? Go ahead and hit 'evaluate' to see whether you're right." ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Evaluate the following two cells and notice the difference between the inputs and the outputs:
" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "284/16" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "284/16." ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "In the first case, Sage gives an exact answer, and in the second case Sage gives a decimal approximation. Why? When you give Sage exact numbers, it does symbolic calculations. When the number $16$ is written as $16.$, that's a cue to Sage that you want an approximate number, so the calculation should be done numerically instead of symbolically.
\n", "Make a new cell or two and evaluate log(3) exactly and also get a decimal approximation for log(3). (By the way, log(3) is the way Sage denotes $\\ln(3)$. To get a base-10 logarithm of 3, you'd write log(3,10).) Why not try to get a decimal approximation for $\\sqrt{2}$?
\n", "\n", "There are two other ways you can get decimal approximations:
" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n(pi)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi.n()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Those are lovely (and notice that Sage knows the value of $\\pi$), but what if you want more decimal places of accuracy? Here you go---and this slightly de-mystifies those empty parentheses...
" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi.n(digits=40)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "On the other hand, sometimes decimal approximations can get you in trouble. Check these two evaluations out:
" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(pi.n(digits=40))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(pi)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "A couple of other things Sage can do for you:
" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factor(2018)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var('x y')\n", "factor(x^2-y^2)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Notice that Sage treats variables differently than constants: it knows that 2018 is a number, but it doesn't know that $x$ and $y$ are variables until you say so. Similarly, $xy$ is viewed differently than $x*y$, as you will see if you evaluate the cells below:
" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xy" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x*y" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "If you assign variables integer values, you don't have to declare them as variables first:
" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = 3\n", "w = 4\n", "z*w" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Heck, let's do some calculus. (This is a calculus class...) Examine the following commands and figure out what they're doing; then figure out how to get a decimal approximation to an integral; then issue some similar commands of your own.
" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "derivative(x^2 +1,x)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integral(x^2 +1,x)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integral(x^2 +1,x,-1,1)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(x^2 +1,(x,-1,1))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "If you want to save your work (which is a good idea to do regularly!), then hit the 'Save' button near the top right of the page. If you want to send your work to someone else (like the instructor), then after saving your work, go under the 'File' menu (upper left of the page under the Sage logo) and choose 'Save worksheet to a file.' This will produce a file with extension .sws that you can email or save to a flash drive.
\n", "What if you need help with Sage? Well... Sage is open-source software, so documentation appears when someone on the project has time to volunteer to write some. With every passing semester, more and more documentation is available. Here are a few resources:
\n", "Finally, can you figure out how to get rid of the blank evaluation cell that appears below?
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.0", "language": "", "name": "sagemath" }, "language": "python", "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }