Arithmetic Operators


The following section summarizes all functions and operators that can be applied to numbers. There are also many other mathematical operations, and these can be found in the sections Vectors and Matrices, Geometric Operators, and Function Plotting.


Infix Operators


The elementary mathematical operators +, -, *, /, ˆ, are accessible in a straightforward manner. They can be applied to numbers and lists. Their particular meaning depends on the type of objects to which they are applied. For example, 5+7 evaluates to 12, while [2,3,4]+[3,-1,5] evaluates to [5,2,9].



The addition operator +


Description: Numbers (integers, real, complex) can be added with the + operator. Lists of the same shape can also be added. The addition is then performed componentwise.

Examples:

expression evaluates to
7 + 8 15
2.3 + 5.9 8.2
[2,3,4] + [3,4,6] [5,7,10]
[2,3,[1,2]] + [3,4,[1,3]] [5,7,[2,4]]


See also: String Operators



The subtraction operator -


Description: Numbers (integers, real, complex) can be subtracted with the - operator. Lists of the same shape can also be subtracted. The subtraction is then performed componentwise.
Furthermore, the - operator can be used as a unary minus.

Examples:

expression evaluates to
7 - 8 -1
8.3 - 5.9 2.4
[2,6,4] - [3,4,6] [-1,2,-2]
[5,3,[1,2]] - [3,4,[1,3]] [2,-1,[0,-1]]



See also: String Operators





The multiplication operator *


Description: Numbers (integers, real, complex) can be multiplied with the * operator. Lists that represent numerical vectors or numerical matrices can also be multiplied if the dimensions admit a reasonable mathematical operation. See the examples for further description.

Examples:

expression evaluates to comment
7 * 8 56 integer multiplication
(1+i) * (2+i) -+1+3*i multiplication of complex numbers
2 * [5,3,2] [10,2,4] scalar multiplication of number and vector
[5,3,2] * 2 [10,2,4] scalar multiplication of number and vector
[2,2,3] * [3,4,6] 32 scalar product of two vectors (x_1,x_2,…,x_n)*(y_1,y_2, …,y_n)=(x_1*x_2+…+x_n*y_n)
[[1,2],[3,4]] * [1,2] [5,11] matrix times vector
[1,2] * [[1,2],[3,4]] [7,10] vector times matrix
[[1,2],[3,4]] * [[1,2],[3,4]] [[7,10],[15,22]] product of two matrices


See also: Vectors and Matrices





The division operator /


Description: Numbers (integers, real, complex) can be divided with the / operator.
Also, a vector can be divided by a number.

Examples:

expression evaluates to
56 / 8 7
[6,8,4] / 2 [3,4,2]





The power operator ˆ


Description:

A number (integer, real, complex) can be taken to the power of another number (integer, real, complex). Note that not only integer powers are allowed. In aˆb the exponent b can
be an arbitrary real or complex number. Formally, the expression exp(b*ln(a)) is calculated.
Since ln(…) is defined only up to a period of 2*pi, the expression aˆb is in general multivalued. For noninteger values of b only one principal value of aˆb will be returned.

Examples:

expression evaluates to
5ˆ2 25
5ˆ(-1) 0.2
2ˆ(1/2) 1.4142…






The degree operator °

This operator multiplies any number by the constant pi/180. This makes possible angle conversion from degrees to radians.

Examples:

expression evaluates to
180° 3.1416…
cos(180°) -1








Functional Operators



The following operators can be applied to numbers (integer, real complex).
Some of them can also be applied to vectors.





Arithmetic Functions


These operators are binary functions equivalent to the operators +, -, *, /

add(_,_) addition binary
sub(_,_) subtraction binary
mult(_,_) product binary
div(_,_) quotient binary
pow(_,_) aˆb binary
mod(_,_) modulo binary



Examples:

expression evaluates to
add(5,6) 11
pow(6,2) 36
mod(23,4) 3








Standard Functions


These functions map numbers to numbers:

sqrt(_) square root unary
exp(_) exponential function unary
log(_) natural logarithm unary


Complex numbers are fully supported by these functions.





Trigonometric Functions


The standard trigonometric functions are available through the following operators:

sin(_) trigonometric sine function unary
cos(_) trigonometric cosine function unary
tan(_) trigonometric tangent function unary
arccos(_) trigonometric inverse sine function unary
arcsin(_) trigonometric inverse cosine function unary
arctan(_) trigonometric inverse tangent function unary
arctan2(_,_) arctan2(x,y) is the angle of the vector (x,y) binary


The arc operators are in principle multivalued. However, the operator represents only one principal value, for which the real value is between +pi and -pi.

Examples:

expression evaluates to
sin(pi) 0
arccos(-1) 3.1416…
arctan(1,1) 0.7854…
arctan(-1,-1) -2.3562…






Numeric Functions


The following unary operators can also be applied to lists:

abs(_) absolute value (for lists componentwise)
round(_) rounded value (for lists componentwise)
floor(_) largest integer less than or equal (for lists componentwise)
ceil(_) smallest integer greater than or equal (for lists componentwise)
re(_) real part of a complex number (for lists componentwise)
im(_) imaginary part of a complex number (for lists componentwise)
conjugate(_) conjugate of a complex number (for lists componentwise)


For complex numbers the operators round, floor, ceil are applied to the real and imaginary parts separately.

The operator abs calculates the norms of numbers, complex numbers, vectors, etc.

Examples:


expression evaluates to
round(4.3) 4
round([3.2,7.8,3.1+i*6.9]) [3,8,3+i*7]
abs([1,3,1,2,1]) 4
floor(4.8) 4






Random Operators


The following operators generate random numbers:

random() uniformly distributed random real number between 0 and 1 nullary
randomnormal() (0,1)-normally distributed real random number nullary
randombool() random Boolean value true+– or -+false nullary
random(<number>) uniformly distributed random real number between 0 and <number> unary
randomint(<number>) uniformly distributed random integer number between 0 and <number> unary
seedrandom(<number>) sets a seed for the random generator unary


The random generators also accept negative and complex numbers as arguments. For example, random(-5) generates a random number between -5 and 0; randomint(6+i*10) generates a random complex number for which the real part is an integer between 0 and 6 and the imaginary part is an integer between 0 and 10.

The (pseudo)random generator will always produce unforeseeable new random numbers. If for some reason one wants the same random numbers to be generated for different runs of a script, one can use the seedrandom(<number>) operator. After this function is invoked with a certain integer, the same sequence of random numbers will be deterministically generated. Each seeding integer produces a different sequence of random numbers.




Page last modified on Friday 26 of May, 2006 [21:14:40 UTC].
The original document is available at http://doc.cinderella.de/tiki-index.php?page=Arithmetic%20Operators