Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sage beginners guide - Finch Kraigh copy.pdf
Скачиваний:
15
Добавлен:
10.02.2015
Размер:
6 Мб
Скачать
☆

Chapter 2

Downloadinganddecompressingsourcetarball

Download the latest source tarball from http://sagemath.org/download-source. html. Open a terminal, change to the directory where you saved the tarball, and decompress it with the following command:

$ tar -xvf sage-*.tar

BuildingSage

If you have a multi-core or multi-processor machine, you can speed up the build process by performing a parallel compilation. You can control this by setting the MAKE environment variable. For example, using Bash syntax, you can set the MAKE variable to use four cores:

$ export MAKE="make -j4"

Change to the Sage directory:

$ cd sage-*

Build Sage:

$ make

Sage may take a long time (1 hour to several days) to compile, depending on the speed of the machine.

Installation

When the compilation process is done, you should be able to run Sage from the build directory. If you want to move the Sage installation or make it available to other users on a shared Linux system, follow the directions in the previous sections.

Summary

At this point, you should have a functioning Sage installation on your machine. In the next chapter, you'll learn the basics of using Sage.

[ 39 ]

3

Getting Started with Sage

In this chapter, you will learn the basic ideas that will be the foundation for using all the features of Sage. We will start by learning how to use the command line and notebook interfaces efficiently. Then, we'll look at basic concepts like variables, operators, functions, and objects. By the end of the chapter, you should be able to use Sage like a sophisticated scientific graphing calculator.

Using the interactive shell

Using the notebook interface

Learning more about operators and variables

Defining and using callable symbolic expressions

Calling functions and making simple plots

Defining your own functions

Working with objects in Sage

HowtogethelpwithSage

The Sage documentation is accessible from the interactive shell and the notebook interface. Further help is available on the Web. The main Sage documentation page at http://www. sagemath.org/help.html provides links to dozens of resources. A few of these links are especially useful:

The official Sage tutorial can be found at http://www.sagemath.org/doc/ tutorial/, and it is also accessible from the Help link in the notebook interface. The Python language tutorial at http://docs.python.org/tutorial/ will also be helpful.

The reference manual at http://www.sagemath.org/doc/reference/ provides the full API documentation, along with many examples.

Getting Started with Sage

Thematic tutorials at http://www.sagemath.org/doc/thematic_tutorials/ index.html focus on specific topics.

The "Constructions" document at http://www.sagemath.org/doc/ constructions/index.html describes how to do specific things with Sage.

Interactive resources, including mailing lists, an IRC channel, and a discussion forum are available if you get stuck. A complete list of these resources can be found at http://www. sagemath.org/development.html. The mailing lists sage-support and sage-devel are intended for general questions. Other lists are available to discuss more specific topics.

StartingSagefromthecommandline

In the previous chapter, we learned how to start Sage. If you want to pass options to Sage when it starts, you can do so with command line arguments.

1.On OS X or a UNIX-like platform, start by opening a terminal window. On Windows, start the virtual machine and double-click the Terminal icon.

2.If you have added the Sage application directory to the PATH environment variable, you can just type sage at the command prompt. If not, you will need to enter the full path to the Sage binary.

3.Add command line arguments, separated by spaces, and press Enter. For example, type the following at the command prompt to start Sage and automatically launch the notebook interface:

$ /Applications/sage/sage -notebook

There are many command line arguments, but you probably won't need them right away.

Three of the most useful arguments are as follows:

Argument

Description

-help

Describe the most widely used command line arguments

-advanced

Describe more advanced command line arguments

-notebook

Start Sage and launch the notebook interface

[ 42 ]

Chapter 3

Usingtheinteractiveshell

The interactive shell provides a command-line interface to Sage. When you are done using Sage, remember to type quit or exit at the Sage prompt, or press Ctrl-D, to exit the interactive shell. In the following examples, lines which start with the Sage command prompt sage: indicate commands that you need to type in. Lines without the prompt indicate output from Sage. Although most of the examples in this book utilize the notebook interface, they will work in the interactive shell, unless noted otherwise.

Timeforaction–doingcalculationsonthecommandline

Let's say that you need to make some quick calculations to design a simple electrical circuit. We will use the example of a series RC circuit, which consists of a resistor and a capacitor connected in series:

The voltage across the capacitor can be described by a linear, first-order differential equation. Eventually we will learn to solve differential equations with Sage, but for now we will just use the solution, which is well known:

1. Define variables.

First, we'll define some variables to work with. Type in the following text at the command prompt, and press Enter after every line:

sage: R = 250e3 sage: C = 4e-6 sage: tau = R * C sage: tau 1.00000000000000

[ 43 ]

Getting Started with Sage

2.

3.

Sage doesn't give any output from variable definitions. You can type the variable name and press Enter to see what it contains.

Perform a calculation.

The voltage across the capacitor is a function of time. Let's set an initial voltage v0 and compute the voltage after one second:

sage: v0 = 100.0 sage: t = tau

sage: v0 * exp(-t / tau) 36.7879441171442

Here, we utilized the built-in function exp(x), which computes the value of e to the power x. After a period of time equal to the time constant has elapsed, the voltage is about 36% of its original value.

Change some values.

How does the voltage decay as time advances? sage: t = 4 * tau

sage: v0 * exp(-t / tau) 1.83156388887342

After a length of time equal to four time constants, the voltage is less than two per cent of its original value. Now, let's see how the value of tau impacts the voltage at a fixed time. Pressing the Up arrow allows you to scroll through the commands that you recently entered, in reverse order. To scroll the other way, press the Down arrow. Use the Up and Down arrows to get v = v0 * exp(-t / tau) on the command line, and edit it to match the following. Press Enter to see the result.

sage: t = tau

sage: v0 * exp(-t / (2 * tau)) 60.6530659712633

Now, let's see what happens if tau gets smaller. Press the Up arrow to edit the previous command:

sage: v0 * exp(-t / (tau / 4)) 1.83156388887342

[ 44 ]

Chapter 3

The time constant controls how quickly the voltage decays. If you are going to work on something else, but might want to come back to this calculation, you can save your session with this command:

sage: save_session('RC_circuit')

This will save your variables in a file called RC_circuit.sobj in the top level of your home directory. You can enter an absolute or relative path to save the file in other locations. You can load these variables at any time in the future using the following command:

sage:

load_session('RC_circuit')

sage:

who

 

C

t

tau

v v0

The who command lists the variables that you have already defined. This is very handy if you have a lot of variables and don't remember one of the names.

What just happened?

This example demonstrated some basic principles of command-line interaction with Sage. To perform a calculation, type an expression on the command line, and press Enter to see the result. If the result of a calculation is stored in a variable, it won't be shown. Typing the name of a variable and pressing Enter shows its value. You can use the Up and Down arrow keys to scroll through previous commands, edit a command, and run it again.

Gettinghelp

There are three ways to get help from the Sage command line. To see the documentation for a command or function, type a ? on the command line after the command. For example, to learn about the exp function type following:

sage: exp?

[ 45 ]

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]