12 Mathematica Demystified General:wrsym -Wolfram Mathematica General: : wrsym W(lE List= Identity Set: wrsym: Symbol Ust is Protected. Ov Identity One of the attributes o List is the Protected attr but 2lttr⊥ butes[Lt] Out(-[Locked, Protected] Figure lI Trying to use the reserved word C as a variable causes a warning that leads to his page in the Help Files protected. Moreover, the double arrowhead, >> is actually a hyperlink to the on-line documentation, or Help Files. If we click on this link the window shown in Fig. 1.I pops up and explains the warning. We'll have a lot more to say about the Help Files as we go, starting a little later in this chapter. 1.9 Using comments After we start to do more complicated calculations, our input cells might start to have dozens of lines. When this happens, it can start to get hard to follow what is going on. Putting comments in our input cells, especially the more complicated
12 Mathematica Demystified Figure 1.1 Trying to use the reserved word C as a variable causes a warning that leads to this page in the Help Files. protected.” Moreover, the double arrowhead, >>, is actually a hyperlink to the on-line documentation, or Help Files. If we click on this link the window shown in Fig. 1.1 pops up and explains the warning. We’ll have a lot more to say about the Help Files as we go, starting a little later in this chapter. 1.9 Using Comments After we start to do more complicated calculations, our input cells might start to have dozens of lines. When this happens, it can start to get hard to follow what is going on. Putting comments in our input cells, especially the more complicated
CHAPTER 1 Getting Started input cells, is a great way to document our work. The following example illustrates the use of comments Example 1.9.1 In(43=(+ distance from sun to earth in meters * au=149597870691 ( speed of light in meters per second c=299792458 ( time for light to reach earth from sun in seconds * 43=149597870691 u4=299792458 45=499.005 The delimiters(and s)are used to enclose comments. Anything that appears between these delimiters is ignored by Mathematica when the cell is evaluated Learning to use comments well is a very good programming practice and writing good code can be a source of great pride. Later we'll be talking about the wolfram Demonstrations Project, a Web site that contains thousands of Mathematica note books that you can download for free. This is a great resource and someday you might find a notebook there that does almost exactly what you want to do. Excit edly, you'll download the notebook, open it up, see a hundred lines of mysterious code, and... What! No Comments! #@l &%* Good comments can make your code much better by making it readable by others(and by yourself after you have forgotten what you were thinking when you wrote it!) In addition to comments, we can also add whole paragraphs of text betwee put cells. We'll be talking about this in Chap l1 1.10 Suppressing Output If we compute something that produces a Lot of output, we may want to hide or suppress the output just because it takes up so much room
CHAPTER 1 Getting Started 13 input cells, is a great way to document our work. The following example illustrates the use of comments. Example 1.9.1 In[43]:= (* distance from sun to earth in meters *) au = 149 597 870 691 (* speed of light in meters per second *) c = 299 792 458 (* time for light to reach earth from sun in seconds *) N[au / c] Out[43]= 149 597 870 691 Out[44]= 299 792 458 Out[45]= 499.005 The delimiters (* and *) are used to enclose comments. Anything that appears between these delimiters is ignored by Mathematica when the cell is evaluated. Learning to use comments well is a very good programming practice and writing good code can be a source of great pride. Later we’ll be talking about the Wolfram Demonstrations Project, a Web site that contains thousands of Mathematica notebooks that you can download for free. This is a great resource and someday you might find a notebook there that does almost exactly what you want to do. Excitedly, you’ll download the notebook, open it up, see a hundred lines of mysterious code, and. . . What! No Comments! #@!&%* ! Good comments can make your code much better by making it readable by others (and by yourself after you have forgotten what you were thinking when you wrote it!). In addition to comments, we can also add whole paragraphs of text between our input cells. We’ll be talking about this in Chap. 11. 1.10 Suppressing Output If we compute something that produces a LOT of output, we may want to hide or suppress the output just because it takes up so much room
14 Mathematica Demystified Example 1.10.1 In(39=(+ a very large Mersenne prime * x=24253-1 o39=190797007524439073807468042969529173669∵ 356994749940177394741882673528979787005 053706368049835514900244303495954950709 725762186311224148828811920216904542206∵ 960744666169364221195289538436845390250∵ 168663932838805192055137154390912666527 533007309292687539092257043362517857366 624699975402375462954490293259233303137 330643531556539739921926201438606439020∵ 075174723029056838272505051571967594608 350063404495977660656269020823960825567 012344189908927956646011998057988548630∵ 107637380993519826582389781888135705408∵ 653045219655801758081251164080554609057∵ 468028203308718724654081055323215860189 611391296030471108443146745671967766308 925858547271507311563765171008318248647∵ 110097614890313562856541784154881743146 033909602737947385055355960331855614540∵ 900081456378659068370317267696980001187∵ 750995491090350108417050917991562167972 281070161305972518044872048331306383715 094854938415738549894606070722584737978 176686422134354526989443028353644037187∵ 375385397838259511833166416134323695660∵ 367676897722287918773420968982326089026 150031515424165462111337527431154890666 327374921446276833564519776797633875503∵ 548665093914556482031482248883127023777 039667707976559857333357013727342079099 064400455741830654320379350833236245819 348824064783585692924881021978332974949∵ 906122664421376034687815350484991 Out[ 40=True
14 Mathematica Demystified Example 1.10.1 In[39]:= ( * a very large Mersenne prime *) x = 24253 – 1 PrimeQ [ x] Out[39]= 190 797 007 524 439 073 807 468 042 969 529 173 669 … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … 356 994 749 940 177 394 741 882 673 528 979 787 005 053 706 368 049 835 514 900 244 303 495 954 950 709 725 762 186 311 224 148 828 811 920 216 904 542 206 960 744 666 169 364 221 195 289 538 436 845 390 250 168 663 932 838 805 192 055 137 154 390 912 666 527 533 007 309 292 687 539 092 257 043 362 517 857 366 624 699 975 402 375 462 954 490 293 259 233 303 137 330 643 531 556 539 739 921 926 201 438 606 439 020 075 174 723 029 056 838 272 505 051 571 967 594 608 350 063 404 495 977 660 656 269 020 823 960 825 567 012 344 189 908 927 956 646 011 998 057 988 548 630 107 637 380 993 519 826 582 389 781 888 135 705 408 653 045 219 655 801 758 081 251 164 080 554 609 057 468 028 203 308 718 724 654 081 055 323 215 860 189 611 391 296 030 471 108 443 146 745 671 967 766 308 925 858 547 271 507 311 563 765 171 008 318 248 647 110 097 614 890 313 562 856 541 784 154 881 743 146 033 909 602 737 947 385 055 355 960 331 855 614 540 900 081 456 378 659 068 370 317 267 696 980 001 187 750 995 491 090 350 108 417 050 917 991 562 167 972 281 070 161 305 972 518 044 872 048 331 306 383 715 094 854 938 415 738 549 894 606 070 722 584 737 978 176 686 422 134 354 526 989 443 028 353 644 037 187 375 385 397 838 259 511 833 166 416 134 323 695 660 367 676 897 722 287 918 773 420 968 982 326 089 026 150 031 515 424 165 462 111 337 527 431 154 890 666 327 374 921 446 276 833 564 519 776 797 633 875 503 548 665 093 914 556 482 031 482 248 883 127 023 777 039 667 707 976 559 857 333 357 013 727 342 079 099 064 400 455 741 830 654 320 379 350 833 236 245 819 348 824 064 783 585 692 924 881 021 978 332 974 949 906 122 664 421 376 034 687 815 350 484 991 Out[40]= True
CHAPTER 1 Getting Started In Example 1.10.1, we compute 24253-1 which just so happens is a prime number!6 Here we have also used the function PrimeQ which will test an integer There are two things we can do. First, we can simply hide the output by double- clicking on the cell bracket that surrounds both the input and output cells. If we double-click the bracket again it will redisplay the output. Try it! This way we can hide the output yet still have access to it if we need it. We wont be saying much more about cell brackets until we get to Chap. 11, but until then you should be comfortable with single-clicking a cell bracket to select that cell, and double-clicking brackets to hide or unhide large output cells. 7 Alternatively, if we follow any calculation with a semicolon, the output from the calculation will not be displayed at all. For example, the semicolon following a=5100 000 below will cause no output to be displayed, even though a will be given the value of 5 100000 Example 1. 10.2 In(38)=(* semicolons suppress output * a=5"100000; Using semicolons also will allow us to place more than one command on the same line in the input cell. Here is a simple example Example 1.10.3 In(34:=(* placing multiple commands on one line a=2;b=3; a+ b ou35=5 We could have placed the sum a+b on the same line too, but using two lines Important as we learn how to do more and more complicated calculationg can be makes for more readable input. Trying to develop good habits in style Suppose we do a calculation that produces a lot of output and we dont remember to, or dont want to, suppress the output? Fortunately, Mathematica will step in and pRimes of the form 2n-I Mersenne primes after Marin correct) list of them in the 17th Double-clicking the bracket of any cell that is part of a larger group of cells will hide all the other cells in the
CHAPTER 1 Getting Started 15 In Example 1.10.1, we compute 24253 − 1 which just so happens is a prime number!6 Here we have also used the function PrimeQ which will test an integer for primality. There are two things we can do. First, we can simply hide the output by doubleclicking on the cell bracket that surrounds both the input and output cells. If we double-click the bracket again it will redisplay the output. Try it! This way we can hide the output yet still have access to it if we need it. We won’t be saying much more about cell brackets until we get to Chap. 11, but until then you should be comfortable with single-clicking a cell bracket to select that cell, and double-clicking brackets to hide or unhide large output cells.7 Alternatively, if we follow any calculation with a semicolon, the output from the calculation will not be displayed at all. For example, the semicolon following a=5∧100 000 below will cause no output to be displayed, even though a will be given the value of 5100000. Example 1.10.2 In[38]:= (* semicolons suppress output *) a = 5 ^ 100 000 ; Using semicolons also will allow us to place more than one command on the same line in the input cell. Here is a simple example. Example 1.10.3 In[34]:= a = 2; b = 3; a + b Out[35]= 5 (* placing multiple commands on one line *) We could have placed the sum a+b on the same line too, but using two lines makes for more readable input. Trying to develop good habits in style can be important as we learn how to do more and more complicated calculations. Suppose we do a calculation that produces a lot of output and we don’t remember to, or don’t want to, suppress the output? Fortunately, Mathematica will step in and 6Primes of the form 2n − 1 are known as Mersenne primes after Marin Mersenne who compiled a (partially correct) list of them in the 17th century. So far, only 46 Mersenne primes have been found, with the largest having over 12 million digits! 7Double-clicking the bracket of any cell that is part of a larger group of cells will hide all the other cells in the group. Try it!
16 Mathematica Demystified save us from having to look at pages and pages of output. For example, suppose e compu Ite 12345 000000. Here is what happens Example 1. 10.4 In[36]=(* Mathematica abbreviates really large output * 123451000000 A very large output was generated. Here is a sample of it: 124241861881525819349735272741768996 457676520492834280006687987052188200 264979<4091336 ou36] 935453005290678044687344428392700699 560757815476108589791692793369293212 890625 Show Less show More show Full Output set Size Limit.. The beginning and end of the answer are displayed, with"<<4091336>> appearing to indicate that the middle 4091336 digits of the answer are not being displayed. The number 12345 000000 has 4091492 digits and would take a lot of space to display! We are also given the choice to see more or less of the output 1.11 Aborting a Calculation Sometimes we might want to interrupt a calculation. For example, if we unknow- ingly start a calculation that might take days to finish, we'll just be waiting and waiting wondering how long it is going to take! Or, after we learn how to program Mathematica, we might accidentally write a program that has a mistake in it that will cause the computer to run forever without ever completing what we wanted do. Rather than just quitting Mathematica and losing all of our work, we can usually abort the calculation by choosing Evaluation p Abort Evaluation from When Mathematica is doing a calculation it will say"Running.. "in the title bar of the window. If the calculation is really fast you wont even notice, but if the calculation lasts for several seconds you will see it. Try computing 5 0000000 This calculation should last long enough for you to see the title of your window change to include"Running.. This would be a great place to use a semicolon to suppress output! It is also a great place to use the Timing function so that we can
16 Mathematica Demystified save us from having to look at pages and pages of output. For example, suppose we compute 123451000000. Here is what happens. Example 1.10.4 In[36]:= 12345 ^ 1000000 Out[36]= A very large output was generated. Here is a sample of it: 124 241 861 881 525 819 349 735 272 741 768 996 … … … … 457 676 520 492 834 280 006 687 987 052 188 200 264 979 <<4 091 336>> 935 453 005 290 678 044 687 344 428 392 700 699 560 757 815 476 108 589 791 692 793 369 293 212 890 625 Show Less Show More Show Full Output Set Size Limit... (* Mathematica abbreviates really large output *) The beginning and end of the answer are displayed, with “<<4091336>>” appearing to indicate that the middle 4091336 digits of the answer are not being displayed. The number 123451000000 has 4091492 digits and would take a lot of space to display! We are also given the choice to see more or less of the output. 1.11 Aborting a Calculation Sometimes we might want to interrupt a calculation. For example, if we unknowingly start a calculation that might take days to finish, we’ll just be waiting and waiting wondering how long it is going to take! Or, after we learn how to program in Mathematica, we might accidentally write a program that has a mistake in it that will cause the computer to run forever without ever completing what we wanted it to do. Rather than just quitting Mathematica and losing all of our work, we can usually abort the calculation by choosing Evaluation Abort Evaluation from the menu bar. When Mathematica is doing a calculation it will say “Running . . . ” in the title bar of the window. If the calculation is really fast you won’t even notice, but if the calculation lasts for several seconds you will see it. Try computing 510000000. This calculation should last long enough for you to see the title of your window change to include “Running . . . .” This would be a great place to use a semicolon to suppress output! It is also a great place to use the Timing function so that we can