DPUSER - The Next Generation: Operators
Author: Thomas Ott

# Operators

In DPUSER, operators are used to combine expressions. There are several types of operators:

- Assignment
- Parentesis
- Ranges
- Mathematical
- Boolean

## Assignment

To assign a value to a variable, the assignment operator is used. The general syntax of an assignment is:

> `VARIABLE = value`
> `VARIABLE += value`
> `...`

or, if you wish to assign a value to a subset of a matrix:

> `VARIABLE[range] = value`
> `VARIABLE[range] += value`
> `...`

Like in the C programming language, several assignment operators are possible.

Operator | Syntax | Operands
Assignment | `VARIABLE = value` | Number, matrix, string, string array
Increment | `VARIABLE++` | Number, matrix
Decrement | `VARIABLE--` | Number, matrix
Multiplication | `VARIABLE *= value` | Number, matrix
Division | `VARIABLE /= value` | Number, matrix
Addition | `VARIABLE += value` | Number, matrix, string, string array
Subtraction | `VARIABLE -= value` | Number, matrix

If the "`+=`" operator is applied to a string array, the string at the right hand side of the statement will be appended to the string array.
The assignment operator "`=`" can be used to write a matrix as a FITS file. The syntax for doing this is `'filename'=matrix`, i.e. you assign a matrix to a filename, which is in single quotes. Note that writing FITS files is also possible using the writefits procedure.

### Examples

`var = 6` | creates a new variable named `var` and assigns a value of 6
`'gauss.fits' = gauss(129, 129, 30)` | writes a 2-dimensional gaussian to a disk file named `gauss.fits`
`var[100:200, *]++` | Increments elements 100 through 200 in the first dimension and all elements in the second dimension

## Parentesis

Parentesis are used in two ways: To enclose arguments to a function, and to group expressions. Parentesis can be used to override operator precedence.

### Examples

`print sin(pi())` | prints out the sine of pi
`var = 2 + 3 * 5` | yields 17
`var = (2 + 3) * 5` | yields 25

## Ranges

With ranges, you can create matrices and string arrays, and you can access subscripts of arrays. To access a single elements of a matrix, supply the coordinates like `[5,3]`. To access a range, give the inclusive range, separated by a colon like `[100:300, 50:60]`. You can use the asterisk (`*`) to access all elements in an axes. Ranges in DPUSER are 1-based like in fortran. The arguments in a range can be the result of an expression.

### Examples

`mvar = [1, sin(pi()), 3+5]` | creates a 1-dimensional matrix with the values 1, 0, 8
`nvar = [-3600:3600] / 10` | creates a 1D matrix with the values -360, -359.9, ..., 360
`svar = ["test", "string"]` | creates a string array with 2 elements
`print mvar[2]` | prints the second element of `mvar`, which is 0
`print svar[1]` | prints out "test"
`print svar[2][strlen(svar[2])]` | prints out the last character of the second element of svar, which is a "g"
`newvar = nvar[3601:3700]` | creates a new matrix variable with 100 elements and assigns the values 0, 0.1, ..., 9.9

## Mathematical operators

The basic mathematical operators are used in DPUSER. Depending on the type of associated variables, different actions (described below) are taken. If a matrix is combined with another matrix or a number, the result will be a matrix in which the operation is performed on each element. The available operators in precedence order are:

Operator | Syntax | Arguments | Return Value
Exponentation | `x ^ y` | Number, Matrix | Number if both x and y are numbers Matrix if either x or y is a matrix
Multiplication | `x * y` | Number, Matrix | Number if both x and y are numbers Matrix if either x or y is a matrix
Matrix multiplication | `x # y` | Matrix or vector | Matrix
Division | `x / y` | Number, Matrix | Number if both x and y are numbers Matrix if either x or y is a matrix
Modulus | `x % y` | Integer number | Integer number
Addition | `x + y` | Number, Matrix | Number if both x and y are numbers Matrix if either x or y is a matrix
|  | Number, String | String if either x or y is a string
|  | String, String array | String array if either x or y is a string array
Subtraction | `x - y` | Number, Matrix | Number if both x and y are numbers Matrix if either x or y is a matrix

## Boolean operators

Conditional statements can be given using the following operators. The list is in precedence order:

Operator | Syntax | Arguments | Return Value
Not | `!x` | Boolean | Boolean
Not equals | `x != y` | Number, String | Boolean
Equals | `x == y` | Number, String | Boolean
Greater than | `x > y` | Number, String | Boolean
Greater or equal | `x >= y` | Number, String | Boolean
Less than | `x < y` | Number, String | Boolean
Less than or equal to | `x <= y` | Number, String | Boolean
And | `x && y` | Boolean | Boolean
Or | `x || y` | Boolean | Boolean