# Sage Intro

### From www.norsemathology.org

[Sage http://www.sagemath.org] is an open source mathematics application that combines numerous open source libraries and applications together. This section is designed to give a basic introduction to Sage, mainly in comparison to Mathematica.

## Contents |

### Language

While Mathematica uses it's own language developed for the application, Sage uses the popular open source language [Python http://www.python.org].

#### Notes on Python

Python is a cross platform open source programming language created by Guido Van Rossum and first released in 1991. Python is a dynamically typed language and it is strongly typed. Dynamically Typed means that every variable is typeless, but the object that variable points to is typed. Strongly typed means that python will not perform automatic conversions between types, so for example if x is the string 'hello ' and y is the int *5* then *x + y* is illegal, however *x + str(y)* is allowed and results in the string 'hello 5'.

##### Whitespace / Indentation

In traditional programming languages such as C or Java indenation is a matter of style and has no affect on the code. This is not the case for python. In python scope is determined by the indentation.

Example:

#Correct indentation for defining a function foo() def foo: x = 5 print x return x + 5 #incorrect indentation for defining a function foo() def foo: x = 5 print x #return x + 5 results in a syntax error because the scope of this #line is no longer in the function foo() return x + 5

### Getting Help

#### Sage

**Online Help:** Online help for Sage is available at http://www.sagemath.org/help.html

**Function Help:** Sage also provides inline documentation for individual functions using the syntax function_name?

Ex. Help with the arccos function - *arccos?*

Which gives you:

Type: <class 'sage.calculus.calculus.Function_arccos'> Definition: arccos( [noargspec] ) Docstring: The arccosine function EXAMPLES: sage: arccos(0.5) 1.04719755119660 sage: arccos(1/2) pi/3 sage: arccos(1 + I*1.0) 0.904556894302381 - 1.061275061905036*I

This can also be done with variables as well for example if *x* is a rational number you can type *x?* you get the documentation for Rational Numbers. Note: This only works with variables of a type defined as part of Sage and doesn't work with standard python variables.

**Package Help:** Sage also provides a help function to get the documentation for a particular package (For information on python packages click here).

For example if we wanted help with the package sage.calculus.all we would enter *help(sage.calculus.all)* and we would get a link to the documentation we can browse using our web browser.

#### Mathematica

**Online Help:** Online help for Mathematica is provided at http://reference.wolfram.com/mathematica/guide/Mathematica.html

**Function Help:** Inline function help is available in Mathematica using the syntax ?function_name

Ex. Help with the Apply function - *?Apply*

Which gives you

Apply[f, expr] or f @@ expr replaces the head of expr by f. Apply[f, expr, levelspec] replaces heads in parts of expr specified by levelspec. More...

This can also be done with variables as well as functions.

Mathematica also has nice feature not available in Sage where if you don't know the exact spelling of the function you want help with you can use the standard syntax, but type as much as you know and Mathematica will provide suggestions.

Ex. If you want to find help with the Sin function you could find it by typing *?Si*

**Category Help:** Mathematica has a Help Browser that organizes the documentation into categories which can be accessed through the help menu.

### Basic Conventions

#### Delimiters

##### Round Parentheses:

**Mathematica:**Round parentheses are used only for mathematical grouping

- ex. F(x + 3) is interpreted as the quantity F multiplied by the quantity (x + 3).

**Sage:**In Sage round parentheses are used for both mathematical grouping and to delimit the arugments of a function.

- Ex. F(x + 3) is interpreted as a call the function F with the parameter x+3.
- Ex2. F*(x + 3) is interpreted as the quantity F multiplied by the quantity x + 3

##### Square Brackets:

**Mathematica:**Square brackets are used only to delimit the arguments to a function.

- ex. F[x,y] is interpreted as a call to the function F with the parameters x and y.

**Sage:**Square brackets are used to subscript into several different Python data structures including lists, tuples, and dictionaries. Square brackets are also used to delimit a list of items. Python Data Structures

- Subscript ex. x[0] or y['key']

- List ex. x = [1,2,3,4,5]

##### Curly Braces:

**Mathematica:**Curly braces are used to delimit a list of items.

- ex. { 1 , 2}

**Sage:**Curly braces are used to delimit a dictionary. For information on python dictionaries click here

#### Other Syntax

##### Basic Arithmetic

**Addition and Subtraction:**In both Sage and Mathematica addition and substraction are done in the same way.

- ex. x + y or x - y

**Multiplication:**In both Sage and Mathematica multiplication is represented with the * character.

- ex. x * y or x * (5 + y)

*Mathematica Note:*In Mathematica multiplication can be done with by putting a space between two values or symbolic variables. ::In Sage you will get a syntax error if you attempt to do multiplication this way.

- Ex. x y = x times y

**Division:**In both Sage and Mathematica division is represented by the / character.

*Integer Division:*In both Sage and Mathematica the division of two integers values results in a rational number. (Note: This is :different from standard python where integer divison is truncated to an integer value) This also applies to division of symbolic :variables with other symbolic variables or integers.

**Powers:**

**Sage:**In Sage the expression*x*^{y}is written as x**y or x^y

**Mathematica:**In Mathematica the expression*x*^{y}is written as x^y

##### Trig Functions

###### Sage:

- Sine, Cosine, and Tangent functions are represented exactly as one would think.

- sin(x), cos(x), tan(x) where x is a numeric value or symbolic variable.

- The inverses are done very similarly where ArcSine, ArcCosine, and ArcTangent are represented as

- arcsin(x), arccos(x), arctan(x) where x is a numeric value or symbolic variable.

###### Mathematica:

- Sine, Cosine, and Tangent functions are represented in Mathematica as

- Sin[x], Cos[x], Tan[x] where x is a numeric value or symbol.

- The inverses are done very similarly where ArcSine, ArcCosine, and ArcTangent are represented as

- ArcSin[x], ArcCos[x], ArcTan[x] where x is a numeric value or symbol.

#### Solving Equations

##### Sage

- Solving an equation in Sage involves two steps.

**Step1:**__Creating Symbolic Variables__

- To create symbolic variables in Sage you use the function
*var(s)*where s is a string of variable names seperated by spaces.

- Example: Create the symbolic variables x,b, and c

*var('x b c')*

**Step2:**__Solve the Equation:__

- Next to solve and equation you use the function
*solve(f,*args)*where f is an equation or system of equations (given by a list or :tuple) and *args are the variables to solve for.

- Example. Solve
*x*^{2}+*b**x*+*c*= 0

*solve([x^2 + b*x + c == 0],x)*which returns

- [x == (-sqrt(b^2 - 4*c) - b)/2, x == (sqrt(b^2 - 4*c) - b)/2] (which is a list of the two solutions found)

##### Mathematica

Solving an equation in Mathematica is somewhat simpler because there is no need to explicitly create symbolic variables.

Using the same example from above *x*^{2} + *b**x* + *c* = 0 we would use the following statement in Mathematica.

*Solve[x^2 + bx + c == 0, x]* which returns

#### 2D Plotting Basics

##### Sage

2D Plotting in Sage is easy for example if you wanted to plot the cos function from -5 to 5 you would use the following command.

*plot(cos, (-5,5) )*

You can see that the first parameter to the plot function is the function you want to plot and the second parameter is the range specified as a tuple.

The result is the image below:

##### Mathematica

2D Plotting Mathematica is easy as well, using the same example as above if we want to plot the cos function from -5 to 5 we would use the following command.

*Plot[Cos[x],{x,-5,5}]*

The result is the image below: