(minor changes by Bart Van Steirteghem, Fall 2015)

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.

︡a9f9fb95-faca-4551-a131-ac80c31b6d90︡{"html":"(minor changes by Bart Van Steirteghem, Fall 2015)

\nSage 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.

\n\n"}︡ ︠6af94dc4-02dc-4ee0-8f2b-ffcb039148b8︠ 2+3 ︡5208d9d3-5377-48bf-a0b9-e6b7b26b4751︡{"stdout": "5"}︡ ︠a54c634c-9c36-44f0-be9d-beaa11f54120i︠ %htmlHelpfully, Sage does ordinary arithmetic. You can change the $2+3$ above to a different expression and then hit Shift-Enter to 'evaluate' the cell. (You can also evaluate the cell by clicking in it, and then hitting 'Run' at the top of the window.)

Next you'll make a new cell: move the 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.)

︡e48855a5-e851-43e8-8b30-c38785b8473b︡{"html":"Helpfully, Sage does ordinary arithmetic. You can change the $2+3$ above to a different expression and then hit Shift-Enter to 'evaluate' the cell. (You can also evaluate the cell by clicking in it, and then hitting 'Run' at the top of the window.)

\nNext you'll make a new cell: move the 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.)

\n\n"}︡ ︠1aa61a47-2a6d-4206-8989-d5fa6a3f8e05︠ # There are a couple of ways to write notes to yourself or the instructor. # The one you're reading now is a note inside an evaluation cell. Each line is preceded by # (the pound sign or number sign), and that tells Sage that you're making a comment rather than issuing a command. # 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. # What do you think should happen if you evaluate this cell? Go ahead and hit 'evaluate' to see whether you're right. ︡cf668835-4e9b-4c6f-82d5-3b2fb53b6b2b︡︡ ︠eb29bf36-8b2e-4b4a-86dc-49ec199cc9e0i︠ %htmlEvaluate the following two cells and notice the difference between the inputs and the outputs. (Recall that to evaluate, you click in the cell and then hit Shift-Enter.)

︡b58f1761-48b7-498e-bd23-0a656188efdc︡{"html":"Evaluate the following two cells and notice the difference between the inputs and the outputs. (Recall that to evaluate, you click in the cell and then hit Shift-Enter.)

\n\n"}︡ ︠eea5a2d2-4c3a-4e38-8320-9ec4fe632591︠ 284/16 ︡86d504ea-dc66-4956-8806-5471e387c2f6︡︡ ︠71166127-447c-49bd-bfb8-77d88c6756b7︠ 284/16. ︡cf31637a-cf63-48c3-9d47-c2d0caa5a000︡︡ ︠5a14d072-d651-4865-a9cc-625ea9038c90i︠ %htmlIn 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.

There are two other ways you can get decimal approximations:

︡6a670c18-1b55-4496-8d93-3b4baaba434e︡{"html":"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.

\nThere are two other ways you can get decimal approximations:

\n\n"}︡ ︠db29cceb-ac79-4b9b-8016-2279d0a449b0︠ n(pi) ︡37b7342d-e599-4a68-86ab-804cceded3ff︡︡ ︠9f445201-0158-4660-9aaa-c79bdef0c336︠ pi.n() ︡7ade98c9-9421-4b2a-92f5-105adea494b6︡︡ ︠143b99e3-2462-4c7f-a43a-223b3187580ci︠ %htmlThose 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...

︡28c7dac9-aa20-4897-97fb-d04b91443e58︡{"html": "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...

"}︡ ︠c04fdc07-e71f-4ff1-b274-e0e1707dc89a︠ pi.n(digits=40) ︡8633c9c5-805e-42a5-9f2c-40d60e2b21f3︡︡ ︠995a5e92-6e89-464f-bd7b-2384910d06a0i︠ %htmlMake 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}$?

︡645736ca-07d5-403c-91a0-9b98f592bb4e︡{"html":"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"}︡ ︠2ed6b556-e9b4-48fc-b1d7-5cb18aa2d419i︠ %htmlOn the other hand, sometimes decimal approximations can get you in trouble. Check these two evaluations out:

︡ba238643-eddf-484f-b4f3-9b39f143854d︡{"html":"On the other hand, sometimes decimal approximations can get you in trouble. Check these two evaluations out:

\n\n"}︡ ︠9f89573e-accd-46f2-a4a3-2cef628a44f0︠ sin(pi.n(digits=40)) ︡b7438a28-fe03-455c-97db-0e4ea3334a08︡︡ ︠251eaec5-3696-4561-9fe7-f16bbcd637dd︠ sin(pi) ︡d613ff8c-31d3-4ce3-9567-914749c41dd2︡︡ ︠1b439db0-5a87-4e84-91ee-39d972745e0ai︠ %htmlA couple of other things Sage can do for you:

︡d9cab637-191a-4417-a065-d244848b6ca0︡{"html": "A couple of other things Sage can do for you:

"}︡ ︠4cd68573-b6a8-411d-b16a-83ffd34b8d37︠ factor(2013) ︡3ac2db0b-d019-47bc-a785-0ef55210859b︡︡ ︠00fc2be8-7d60-4d2a-929c-190e0029b664︠ var('x y') factor(x^2-y^2) ︡740ce784-db24-459d-b647-674d9fb7323b︡︡ ︠7131622e-b0f2-40aa-b366-9ce8ae96dd82i︠ %htmlNotice that Sage treats variables differently than constants: it knows that 2013 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:

︡c3c5b1dd-53b5-4838-aa01-1cad7d1786c4︡{"html": "Notice that Sage treats variables differently than constants: it knows that 2013 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:

"}︡ ︠6441f08b-b0e7-4532-aad5-9f4c33b34d30︠ xy ︡097b728e-7510-456d-b90f-e3e81959787a︡︡ ︠e3697fd7-8d64-4b51-afa1-c5839ddc7a40︠ x*y ︡5f70ee74-ef4d-41e9-9781-f1c82cca48a3︡︡ ︠0a45ba74-fa93-4674-90ba-c7612ee8b521i︠ %htmlIf you assign variables integer values, you don't have to declare them as variables first:

︡1cba8bd1-d40f-4987-b159-6d5ecff2ffad︡{"html": "If you assign variables integer values, you don't have to declare them as variables first:

"}︡ ︠b5ae64fd-3798-4e37-b107-9759fcfdcc41︠ z = 3 w = 4 z*w ︡2f1e28f4-8e47-4150-ad75-73fecde24777︡︡ ︠e8efeeb7-ffa1-4778-b858-aed0cc5664a9i︠ %htmlExamine the following command and figure out what it's doing; then issue some similar commands of your own.

︡d6b751eb-2c3b-4896-a281-64be09fd56ff︡{"html":"Examine the following command and figure out what it's doing; then issue some similar commands of your own.

\n\n"}︡ ︠b548d981-1912-4413-82dc-08dee43f2adf︠ plot(x^2 +1,(x,-1,1)) ︡95f45714-9e5c-4080-83be-6aac6820d2e0︡︡ ︠4b5bb2ce-5af3-46f5-b922-788c724460bfi︠ %htmlIf you want to save your work, 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 to 'Files' (upper left of the window), select your file and choose 'Download...' You can then download your file (with extension .sagews) to your computer or copy a link to share your file via 'the cloud.'

**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:

- http://www.sagemath.org/help.html is the Sage documentation list.
- https://doc.sagemath.org/html/en/tutorial/ is a tutorial.
- Use google to search for help, as there are lots of Sage examples and tutorials that are not part of the official Sage documentation.

If you want to save your work, 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 to 'Files' (upper left of the window), select your file and choose 'Download...' You can then download your file (with extension .sagews) to your computer or copy a link to share your file via 'the cloud.'

\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
- http://www.sagemath.org/help.html is the Sage documentation list. \n
- https://doc.sagemath.org/html/en/tutorial/ is a tutorial. \n
- Use google to search for help, as there are lots of Sage examples and tutorials that are not part of the official Sage documentation. \n