Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168737
Image: ubuntu2004

This worksheet is part of the Sage Tips series I'm writing for my Discrete Mathematics (MA267) class in Spring 2010.

We've talked about how to do one computation at a time, like this one.

sin(pi/4)
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, \sqrt{2}

If you've tried to do several computations together in one cell, you've noticed that Sage just reports the answer for the last one.

sin(pi/4) sin(pi/3) sin(pi/2)
\newcommand{\Bold}[1]{\mathbf{#1}}1

But sometimes you want to see the results of several computations summarized together.
The easiest way to do this is to put them in a list.

Lists

In Sage, lists can be made using commas, as long as you wrap the whole list in either parentheses ( ) or brackets [ ].

So one way to make a list is (1,2,3,4,5) and another way to make a list is [6,7,8,9,10].

(1,2,3,4,5)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1, 2, 3, 4, 5\right)
[6,7,8,9,10]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[6, 7, 8, 9, 10\right]

Although parentheses and brackets do mean different things, the distinction is not important for us here.
For now, use whichever you prefer.

You can therefore group computations together in a list as follows.

( sin(pi/4), sin(pi/3), sin(pi/2) )
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\frac{1}{2} \, \sqrt{2}, \frac{1}{2} \, \sqrt{3}, 1\right)

There!  All three of the answers together, as desired.

Automatically generated lists

Rather than make a list yourself by typing out all its entries, sometimes it's useful to have Sage build the list for you.
For instance, perhaps you want to compute sin(π/n)sin(\pi/n) for all the nn from 1 to 10.

This is very easy to do in Sage, but this is one situation in which you'll want to use brackets instead of parentheses.  Here is an example.

[ sin(pi/n) for n in range(1,10) ]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, \frac{1}{2} \, \sqrt{3}, \frac{1}{2} \, \sqrt{2}, \sin\left(\frac{1}{5} \, \pi\right), \frac{1}{2}, \sin\left(\frac{1}{7} \, \pi\right), \sin\left(\frac{1}{8} \, \pi\right), \sin\left(\frac{1}{9} \, \pi\right)\right]

You might notice, however, that it only went up to 9.  For reasons that aren't important here, the range command counts up to but not including the second number you give it.  So to get it to count from 1 to 10, you need to use range(1,11).  This can be very useful for experimenting to see what functions do.

Want to know what the first 100 cubes are?

[ n^3 for n in range(100) ]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648, 12167, 13824, 15625, 17576, 19683, 21952, 24389, 27000, 29791, 32768, 35937, 39304, 42875, 46656, 50653, 54872, 59319, 64000, 68921, 74088, 79507, 85184, 91125, 97336, 103823, 110592, 117649, 125000, 132651, 140608, 148877, 157464, 166375, 175616, 185193, 195112, 205379, 216000, 226981, 238328, 250047, 262144, 274625, 287496, 300763, 314432, 328509, 343000, 357911, 373248, 389017, 405224, 421875, 438976, 456533, 474552, 493039, 512000, 531441, 551368, 571787, 592704, 614125, 636056, 658503, 681472, 704969, 729000, 753571, 778688, 804357, 830584, 857375, 884736, 912673, 941192, 970299\right]

Or what about that homework problem that asked why n2n^2 is always a multiple of 3 or one more than a multiple of 3?  Let's look at the remainder when we divide n2n^2 by 3 for lots of different values of nn.

[ mod(n^2,3) for n in range(50) ]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1\right]

Well, at least we know the homework problem wasn't a trick question.  The remainder is always 0 or 1, never 2.

All sorts of interesting experiments can be done this way.  I'll leave you with a few examples, and leave you to think of some more.

[ factor(n) for n in range(200,220) ]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[2^{3} \cdot 5^{2}, 3 \cdot 67, 2 \cdot 101, 7 \cdot 29, 2^{2} \cdot 3 \cdot 17, 5 \cdot 41, 2 \cdot 103, 3^{2} \cdot 23, 2^{4} \cdot 13, 11 \cdot 19, 2 \cdot 3 \cdot 5 \cdot 7, 211, 2^{2} \cdot 53, 3 \cdot 71, 2 \cdot 107, 5 \cdot 43, 2^{3} \cdot 3^{3}, 7 \cdot 31, 2 \cdot 109, 3 \cdot 73\right]
point( [ (n,mod(n^2,11)) for n in range(55) ] )