Functions
Calling Functions
Functions are sets of operations that do a particular task, usually a task that needs to be done many times in different situations. For example it is often required to add up all the numbers in an array. It would be tedious to do this manually each time, so a built in function, sum, can be called to do this wherever it is required.
The above example shows how to call a function – use its name followed by brackets containing the thing it is to work on (its argument). Most functions return a value, in the case of sum the result of adding up all the elements. As with the operators this can be explicitly assigned to a variable (here to the total variable) or just be assigned to the default ans variable. Some functions have more than one argument in which case they are separated by commas, for example the rand function (which generates a matrix of random numbers) takes a first argument for the number of rows and a second for the number of columns
When a function is called, ODCalc tries to find a function that matches the signature of the function specified in the code. A function signature has two components – the name of the function and the types of its arguments. For example the rand function has the following signature
i.e. it takes two arguments, both of which are to be numbers, and returns a number after running the function. If you call rand with different arguments, for example with a string: rand(2, “four”), the calculator will generate an error because there is no built in function with the signature rand(number, string). In fact, like many functions, rand has a few signatures:
Arithmetic
ceil
Rounds the number argument x up to the nearest integer value (i.e. towards positive infinity), or if x is an array or matrix returns a copy of x in which each element is rounded up to the nearest integer. If x has a unit then the result will be a whole number multiple of that unit. If x is complex then both the real and imaginary parts are rounded up to the nearest integer value.
fix
Rounds the number argument x to the nearest integer value towards zero (i.e. down for positive numbers or up for negative numbers), or if x is an array or matrix returns a copy of x in which each element is rounded to the nearest integer closer to zero. If x has a unit then the result will be a whole number multiple of that unit. If x is complex then both the real and imaginary parts are rounded to the nearest integer value closer to zero.
floor
Rounds the number argument x down to the nearest integer value (i.e. towards negative infinity), or if x is an array or matrix returns a copy of x in which each element is rounded down to the nearest integer. If x has a unit then the result will be a whole number multiple of that unit. If x is complex then both the real and imaginary parts are rounded down to the nearest integer value.
round
Rounds the number argument x to the nearest integer value (either up or down), or if x is an array or matrix returns a copy of x in which each element is rounded to the nearest integer. If x has a unit then the result will be a whole number multiple of that unit. If x is complex then both the real and imaginary parts are rounded to the nearest integer value.
Trigonometry
sin
Calculates the sine of a, if a is an array or matrix it calculates the sine of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
cos
Calculates the cosine of a, if a is an array or matrix it calculates the cosine of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
tan
Calculates the tangent of a, if a is an array or matrix it calculates the tangent of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
csc
Calculates the cosecant of a (i.e. the reciprocal of the sine of a), if a is an array or matrix it calculates the cosecant of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
sec
Calculates the secant of a (i.e. the reciprocal of the cosine of a), if a is an array or matrix it calculates the secant of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
cot
Calculates the cotangent of a (i.e. the reciprocal of the tangent of a), if a is an array or matrix it calculates the cotangent of each element. The argument a should have units with dimensions of angle, for example radians or degrees. It can also be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.
asin
Calculates the inverse sine of the number x, if x is an array it calculates the inverse sine of each element. An angle is returned with the default angle unit as set up within the calculator (the returned angle can of course be converted to any other angle unit using the in operator).
acos
Calculates the inverse cosine of the number x, if x is an array it calculates the inverse cosine of each element. An angle is returned with the default angle unit as set up within the calculator (the returned angle can of course be converted to any other angle unit using the in operator).
atan
Calculates the inverse tangent of the number x, if x is an array it calculates the inverse tangent of each element. An angle is returned with the default angle unit as set up within the calculator (the returned angle can of course be converted to any other angle unit using the in operator).
Exponents and Logarithms
exp
Calculates Euler's number e raised to the power of n, or if n is an array or matrix returns a copy of that array or matrix in which each element is e raised to the power of the corresponding element in n. n must be dimensionless.
expm1
Calculates the en-1, or if n is an array or matrix returns a copy of that array or matrix in which each element is en-1 for the corresponding element in n. The result produced by this function is much more accurate for values of n close to zero than calling exp(n)-1. n must be dimensionless.
log
Calculates the natural logarithm of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the natural logarithm of the corresponding element of x. x must be dimensionless.
log1p
Calculates the natural logarithm of (x+1), or if x is an array or matrix returns a copy of that array or matrix in which each element is ln(x+1) of the corresponding element of x. This function produces much more accurate results for small values of x than using the log function. x must be dimensionless.
log10
Calculates the base 10 logarithm of x (the common logarithm), or if x is an array or matrix returns a copy of that array or matrix in which each element is the base 10 logarithm of the corresponding element of x. x must be dimensionless.
log2
Calculates the base 2 logarithm of x (the binary logarithm), or if x is an array or matrix returns a copy of that array or matrix in which each element is the base 2 logarithm of the corresponding element of x. x must be dimensionless.
sqrt
Calculates the square root of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the square root of the corresponding element of x.
cbrt
Calculates the cube root of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the cube root of the corresponding element of x.
sqr
Calculates the square of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the square of the corresponding element of x.
cube
Calculates the cube of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the cube of the corresponding element of x.
Complex Numbers
abs
Calculates the absolute value (magnitude) of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the absolute value of the corresponding element of x.
angle
Calculates the argument (angle) of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the argument of the corresponding element of x. An angle is returned with the default angle unit as set up within the calculator (the returned angle can of course be converted to any other angle unit using the in operator).
conj
Returns the complex conjugate of x (i.e. the sign of the complex part of x is swapped), or if x is an array or matrix returns a copy of that array or matrix in which each element is the complex conjugate of the corresponding element of x.
imag
Returns the imaginary part of x (as a real number), or if x is an array or matrix returns a copy of that array or matrix in which each element is the imaginary part of the corresponding element of x.
real
Returns the real part of x, or if x is an array or matrix returns a copy of that array or matrix in which each element is the real part of the corresponding element of x.
sign
Returns the sign of x (precisely the signum function), or if x is an array or matrix returns a copy of that array or matrix in which each element is the sign of the corresponding element of x. If x is a real number then the result is 1 if x is greater than zero, -1 if x is less than zero or 0 if x is zero. If x is a complex number then the result is x/abs(x).
Hyperbolics
sinh
Calculates the hyperbolic sine of x, if x is an array or matrix it calculates the hyperbolic sine of each element. x must be a dimensionless number.
cosh
Calculates the hyperbolic cosine of x, if x is an array or matrix it calculates the hyperbolic cosine of each element. x must be a dimensionless number.
tanh
Calculates the hyperbolic tangent of x, if x is an array or matrix it calculates the hyperbolic tangent of each element. x must be a dimensionless number.
Vector Creation Functions
eye
Create an identity matrix, that is a matrix with ones on its leading diagonal and zeros elsewhere. The zero argument form returns the number 1. The one argument form returns a square matrix with the argument determining the number of rows / columns / diagonal elements. All arguments must be positive real numbers.
ones
Create a matrix entirely populated with the number 1.0. The zero argument form returns the number 1. The one argument form returns a square matrix with the argument determining the number of rows / columns / diagonal elements. The form with two (or more) arguments creates a two (or more) dimensional matrix. All arguments must be positive real numbers.
rand
Create a matrix entirely populated with random numbers between 0.0 and 1.0. The zero argument form returns a single random number. The one argument form returns a square matrix with the argument determining the number of rows / columns / diagonal elements. The form with two (or more) arguments creates a two (or more) dimensional matrix. All arguments must be positive real numbers.
zeros
Create a matrix entirely populated with the number 0.0. The zero argument form returns 0.0. The one argument form returns a square matrix with the argument determining the number of rows / columns / diagonal elements. The form with two (or more) arguments creates a two (or more) dimensional matrix. All arguments must be positive real numbers.
Vector Functions
length
Returns the length of the array or matrix, that is the total number of elements it contains. Numbers up to length(n) can be used to access its elements when using linear indexing.
max
Returns the biggest elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function returns the largest elements in the array. If n is a matrix it finds the largest element in each column returning a column array of the values. For 3D matrices the result is a 2D matrix, etc. If the argument is an array of complex numbers then the function returns the value with the largest absolute value, for purely real arrays it is the most positive value.
min
Returns the smallest elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function returns the smallest elements in the array. If n is a matrix it finds the smallest element in each column returning a column array of the values. For 3D matrices the result is a 2D matrix, etc. If the argument is an array of complex numbers then the function returns the value with the smallest absolute value, for purely real arrays it is the most negative value.
size
Returns the size of the array or matrix, that is the number of elements in each of its rows, columns (and so on for higher dimension matrices. Numbers up to size(n)[d] can be used access the elements of n in its d dimension when using matrix indexing.
sum
Returns the sum of the elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function adds up all the elements in the array. If n is a matrix it adds up all the elements in each column returning a column array of the totals. For 3D matrices the result is a 2D matrix, etc.
prod
Returns the product of the elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function multiplies all the elements in the array. If n is a matrix it multiplies all the elements in each column returning a column array of the totals. For 3D matrices the result is a 2D matrix, etc.
any
Returns the logical or function of the elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function returns true if any of the elements in the array are true. If n is a matrix it returns a column array in which each element is true if any of the elements in the corresponding column of the matrix is true. For 3D matrices the result is a 2D matrix, etc.
all
Returns the logical and function of the elements in the array or matrix along the first non-singleton dimension. If n is a column or row array the function returns true if all the elements in the array are true. If n is a matrix it returns a column array in which each element is true if all the elements in the corresponding column of the matrix are true. For 3D matrices the result is a 2D matrix, etc.