# Variables and Functions

This page will go through some of the differences between creating and using variables and functions in Sage versus Mathematica.

## Variables

### Naming Restrictions

#### Sage

Sage places the following restrictions on variable names:

• Variable names can contain only letters, numbers, and underscores
• Variable names must begin with a letter or underscore
• Variable names are case sensitive
• Variable names cannot contain any spaces

#### Mathematica

Mathematica is more flexible with variable names than Sage is and allows the use of subscripts and other math symbols to be used in variable names. Mathematica places the following restrictions on variable names:

• Variable names cannot contain spaces
• Variable names cannot begin with a number
• Variable names cannot be the name of a protected symbol
• Variable names are case sensitive

### Variable Types

#### Sage

Sage has support for two types of variables.

Standard Variables

The first type are standard python variables which may be integers, floats, string, objects, etc. Standard python variables are created when they are assigned a value. For example we can create a variable c and assign it to the string 'hello' by using the command c = 'hello' . c will then hold the value 'hello' until you change the value by assigning a new value to c.

Symbolic Variables

The second type of variable Sage supports are called symbolic variables. Symbolic variables are created without assigning them a value. Since support for symbolic variables is not builtin to python symbolic variables must be created using special syntax. These variables are used all over Sage for solving equations, plotting, creating symbolic arithmetic, to name a few instances.

• Syntax for creating symbolic variables x and y
• var('x y') or var('x,y')

The var function takes one string parameter s where s is the list of variable names to be created separated by either spaces or commas. Note: the same variable name restriction for standard variables apply to symbolic variables as well.

#### Mathematica

Mathematica has both standard variables and symbolic variables as well, but the use of them differs from Sage. The main difference is that Mathematica doesn't require you to explicitly specify a variable as symbolic, this is determined automatically by Mathematica. Any variable name that is used without having been assigned a value is automatically assumed to be a symbolic variable.

Standard Variables

Standard variables are declared the same way that they are in Sage by assigning them a value. For example if you enter the expression x = 5 then x is now an integer variable.

Symbolic Variables

As was stated above all variables are symbolic until they are assigned a value. So if x has never been assigned a value you can enter the expression Solve[x + 1 == 5,{x}] and x will be treated as a symbolic variable.

## Functions

### Sage

#### Defining Functions using def

The standard way to define a function in python is by using the def keyword. The syntax is as follows:

```def function_name(parameters separated by commas):
statement
statement
```

Example code for defining a function that adds two numbers and returns the result:

```def addTwo( numA, numB ):
return numA + numB
```

As you can see from the example above functions can return a value by using the keyword return. So you could use the example above to assign the result of the addTwo function to a variable x with the following syntax.

and x will now have the value of 11.

#### Defining Functions with Mathematical Syntax

Sage also allows you to define a function using mathematical syntax using symbolic expressions.

Syntax:

```function_name(parameters) = expression
```

Example - Adding two numbers together

```f(x,y) = x + y
```

Example - Using the function f

```v = f(5,6)
#v now has the value 11
```

Note: x and y are symbolic variables, but they do not need to be declared using the var function. In fact you can declare x and y with var and the x and y declared with var will be unrelated to the x and y parameters of the function f.

#### Piecewise Functions

Sage currently has basic support for Piecewise functions that work in conjunction with Python's lambda functions. For information on Python's lambda functions click here

Basic Syntax for Lambda Functions:

```variable_name = lambda parameters: expression
```

Creating a piecewise function in Sage:

```#Create a piecewise function that will return x for x between -1 and 0 and x + 1 for x between 0 and 1

f1 = lambda x: x
f2 = lambda x: x + 1
f = Piecewise([ [ (-1,0),f1 ], [ (0,1),f2 ] ] )
```

The constructor for the Piecewise class takes a list of of lists as a parameter. Each list will contain a tuple of the range e.g. (0,1) followed by a pointer to the lambda function e.g. the left hand side of the expression where the lambda function was created.

##### Current Issues with Piecewise

Unlike in Mathematica it is not possible to set an infinite range of values. E.G. in Mathematica it is possible to tell a piecewise function to use one definition when x < 0 and a different definition when x > 0. This isn't currently possible in Sage since only finite ranges can be specified. The best way to achieve this effect in Sage would be to use some basic flow control statements in a standard python function.

Example - Creating a Psuedo-Piecewise Functions

```def f(x):
if x < 0:
return x
elif x == 0:
return x + 1
else:
return x + 2
```

#### Differences with Mathematica

##### Delayed Assignment

Mathematica supports delayed assignment using the := operator when creating functions. In general functions in Sage use delayed assignment in the same sense as Mathematica. For functions defined with the def keyword assignment can always be thought of as delayed, since all variables and expressions in the function are local in scope and only evaluated during a function call. Functions defined using Mathematical syntax in Sage can have what I will call partially delayed assignment. All parameters to the function are evaluated at each function call and are unrelated to any variables with the same name elsewhere in the worksheet, but any constant calculation are done when the function is created.

Example where delayed assignment works:

```var('x y')
x = 5
y = 6
f(x,y) = x + y
c = f(1,2)
#the value of c is now 3
```

Example where delayed assignment doesn't work:

```f(x) = x + random()
c = f(1)
d = f(1)
#c and d will have the same value because random() is evaluated during assignment to f(x)
#not during each function call
```

It is possible to get a similar effect to delayed assignment using Python's lambda functions. Using the example above we can rewrite it as a lambda function.

```g = lambda x: x + random()
c = g(1)
d = g(1)
#c and d will not have the same value here because random() is evaluated separately during each function call
```
##### Functions with Multiple Definitions

While Mathematica supports having multiple definitions of a function (function overloading), Sage does not. This is due to Python's lack of support for function overloading. Python does allow for default parameters which can often be used to achieve a similar effect.

Example using default parameters:

```def func( x, y=0):
return x + y + 1

x = func(1)
y = func(1,2)
#x is now 2
#y is now 4
```

Note: Default parameters can only be used for functions defined using def not for functions defined with mathematical syntax.

### Mathematica

#### Defining a Function

To define a standard function in Mathematica you use the following syntax:

```variable_name = function_name[ parameter1,parameter2, etc ] = expression
```

As was Discussed in the Sage Intro section function parameters are delimited in Mathematica by using square brackets. Similar to the way static typed languages such as C use the data type to specify parameter, Mathematica uses a type of pattern matching to determine the type of a parameter.

Example:

```Defining a function that takes an integer parameter followed by a real parameter:

In C:
float f(int x, float y)
{
return x*x + y;
}
In Mathematica:
f[x_Integer,y_Real] = x^2 + y
```

Mathematica also allows you to specify that a parameter can be of any type:

```A function that takes one parameter of any type:

f[x_] = x^2

The _ after x is basically a wildcard telling Mathematica that x can be of any type.
```

#### Immediate and Delayed Assignment

When you assign a function to a variable by default the function definition is evaluated immediately. However this is not always the desired result.

The following example illustrates some of the issues with Immediate Assignment:

```x = 5
f[x_] = x^2 + 5

Output:
5
30

f
Output:
30

```

As you can see when the function f was created the value for x was evaluated at assignment and all future calls to the function f will return the result 30 regardless of the parameter that was passed to f.

So how can you prevent this from happening? The answer is by using something called delayed assignment. With delayed assignment the function right hand side of the equation doesn't get evaluated until the function call.

To use delayed assignment the syntax is f[parameters] := rhs

```x = 5
f[x_] := x + 10

Calling the function f:
f
Output:
25
```

#### Piecewise Functions

Mathematica allows you to create Piecewise Functions or in other words functions with multiple definitions where each definition has an associated condition. When the function is called Mathematica will determine which definition to use based on the value passed to the function.

Example:

```Syntax:
function_name[parameters] := expression /; condition

f[x_] := x /; x <= 1
f[x_] := x + 1 /; x <= 2
f[x_] := x^2 /; x >= 3

f
f
f
Output:
1
3
9
```

It is possible to use piecewise functions and both immediate and delayed assignment to create functions in the same way they are thought about in Mathematics.

Here is a simple example involving Fibonacci Numbers:

```F = 1
F = 1
F[x_] := F[x - 2] + F[x - 1]

Notice that immediate assignment is used for F and F, but delayed assignment is used for F[x_]
```

#### Functions with Multiple Definitions

Examples are shown above how functions can have multiple definitions by using piecewise functions. Note though that in the examples above the functions had the same parameters. It is possible in Mathematica to define multiple definitions of a function that have different parameters.

Example:

```f[x_] := x^2
f[x_,y_] := x^y

Note: Delayed assignment is being used in both function definitions.
```