ODCalc
Functions

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.

total = sum( 1:10 )
55

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

rand(2, 4)
[0.13618, 0.0917, 0.37877, 0.78649; 0.18784, 0.2489, 0.553, 0.9679]

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

Number  rand( Number rowCount, Number columnCount )

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:

Number  rand( )
Number  rand( Number squareRowCount )
Number  rand( Number rowCount, Number columnCount )
The first two generating a single random number and a square matrix of random numbers, respectively, and the third a rectangular matrix as already described. Depending on the number and type of arguments used the calculator will find the correct function form
rand()
0.08029
rand(2)
[0.06757, 0.03726; 0.0472, 0.49818]
rand(1,5)
[0.10741, 0.86374, 0.42622, 0.2906, 0.49346]

Arithmetic

ceil

Number  ceil( Number x )
Number[ ]  ceil( Number[ ] x )

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.

ceil(5.5m)
6 m
ceil(0.5+1.5i)
1 +2i
ceil( [1.1, 5.5, 9.9] )
[2, 6, 10]

fix

Number  fix( Number x )
Number[ ]  fix( Number[ ] x )

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.

fix(5.5m)
5 m
fix(-5.5m)
-5 m
fix(0.5+1.5i)
1i
fix( [1.1, 5.5, 9.9] )
[1, 5, 9]

floor

Number  floor( Number x )
Number[ ]  floor( Number[ ] x )

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.

floor(5.5m)
5 m
floor(0.5+1.5i)
1i
floor( [1.1, 5.5, 9.9] )
[1, 5, 9]

round

Number  round( Number x )
Number[ ]  round( Number[ ] x )

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.

round(5.5m)
6 m
round(0.5+1.5i)
1 +2i
round([1.1, 5.5, 9.9])
[1, 6, 10]

Trigonometry

sin

Number  sin( Number a )
Number[ ]  sin( Number[ ] a )

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.

sin(45 deg)
0.70711
sin( pi/4 in rad)
0.70711
sin( pi/4 )
0.70711
sin( 1 revolution * (0:7)/8 )
[0, 0.70711, 1, 0.70711, 1.22465e-16, -0.70711, -1, -0.70711]

cos

Number  cos( Number a )
Number[ ]  cos( Number[ ] a )

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.

cos(45 deg)
0.7071
cos( pi/4 in rad )
0.70711
cos( pi/4 )
0.70711
cos( 1 revolution * (0:7)/8 )
[1, 0.70711, 6.12323e-17, -0.70711, -1, -0.70711, -1.83697e-16, 0.70711]

tan

Number  tan( Number a )
Number[ ]  tan( Number[ ] a )

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.

tan(45 deg)
1
tan( pi/4 in rad )
1
tan( pi/4 )
1
tan( 1 revolution * (0:7)/8 )
[0, 1, 1.63312e16, -1, -1.22465e-16, 1, 5.44375e15, -1]

csc

Number  csc( Number a )
Number[ ]  csc( Number[ ] a )

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.

csc(45 deg)
1.41421
csc( pi/4 in rad )
1.41421
csc( pi/4 )
1.41421
csc( 1 revolution *(0:7)/8 )
[Inf, 1.41421, 1, 1.41421, 8.16562e15, -1.41421, -1, -1.41421]

sec

Number  sec( Number a )
Number[ ]  sec( Number[ ] a )

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.

sec(45 deg)
1.41421
sec( pi/4 in rad )
1.41421
sec( pi/4 )
1.41421
sec( 1 revolution *(0:7)/8 )
[1, 1.41421, 1.63312e16, -1.41421, -1, -1.41421, -5.44375e15, 1.41421]

cot

Number  cot( Number a )
Number[ ]  cot( Number[ ] a )

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.

cot(45 deg)
1
cot( pi/4 in rad )
1
cot( pi/4 )
1
cot( 1 revolution *(0:7)/8 )
[Inf, 1, 6.12323e-17, -1, -8.16562e15, 1, 1.83697e-16, -1]

asin

Number  asin( Number x )
Number[ ]  asin( Number[ ] x )

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).

asin(1)
1.5708 rad
asin(0.5) in deg
30 º
asin( -1:0.5:1 ) in deg
[-90, -30, 0, 30, 90] º

acos

Number  acos( Number x )
Number[ ]  acos( Number[ ] x )

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).

acos(1)
0 rad
acos(0.5) in deg
60 º
acos( -1:0.5:1 ) in deg
[180, 120, 90, 60, 0] º

atan

Number  atan( Number x )
Number[ ]  atan( Number[ ] x )

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).

atan(0)
0 rad
atan(1) in deg
45 º
atan( [-Inf, -1, 0, 1, Inf] ) in deg
[-90, -45, 0, 45, 90] º

Exponents and Logarithms

exp

Number  exp( Number n )
Number[ ]  exp( Number[ ] n )

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.

exp(1)
2.71828
exp(i*pi)
-1
exp( [-Inf, -1, 0, 1, Inf] )
[0, 0.36788, 1, 2.71828, Inf]

expm1

Number  expm1( Number n )
Number[ ]  expm1( Number[ ] n )

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.

expm1(0)
n
expm1(1e-15)
1e-15
exp(1e-15) -1
1.11022e-15
expm1( [-Inf, -1, 0, 1, Inf] )
[-1, -0.63212, 0, 1.71828, inf]

log

Number  log( Number x )
Number[ ]  log( Number[ ] x )

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.

log(1)
0
log( [0, 1, e, Inf] )
[-Inf, 0, 1, Inf]

log1p

Number  log1p( Number x )
Number[ ]  log1p( Number[ ] x )

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.

log1p(0)
0
log1p(1e-15)
1e-15
log(1+1e-15)
1.11022e-15
log1p( [0, 1, e, Inf] )
[0, 0.69315, 1.31326, Inf]

log10

Number  log10( Number x )
Number[ ]  log10( Number[ ] x )

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.

log10(100)
2
log10( [1u, 1m, 1, 1k, 1M] )
[-6, -3, 0, 3, 6]

log2

Number  log2( Number x )
Number[ ]  log2( Number[ ] x )

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.

log2(4)
2
log2( [0, 1, 2, 4, 8, 1ki, 1Mi, 1Gi] )
[-Inf, 0, 1, 2, 3, 10, 20, 30]

sqrt

Number  sqrt( Number x )
Number[ ]  sqrt( Number[ ] x )

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.

sqrt(9)
3
sqrt(-9)
3i
sqrt( 4m )
2 m1/2
sqrt( [1, 4, 9, 16, 25] )
[1, 2, 3, 4, 5]

cbrt

Number  cbrt( Number x )
Number[ ]  cbrt( Number[ ] x )

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.

cbrt(27)
3
cbrt(-2+2i)
1 +1i
cbrt( 8m )
2 m1/3
cbrt( [1, 8, 27, 64, 125] )
[1, 2, 3, 4, 5]

sqr

Number  sqr( Number x )
Number[ ]  sqr( Number[ ] x )

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.

sqr(2)
4
sqr(1+1i)
2i
sqr( 2m )
4 m2
sqr( [1, 2, 3, 4, 5] )
[1, 4, 9, 16, 25]

cube

Number  cube( Number x )
Number[ ]  cube( Number[ ] x )

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.

cube(2)
8
cube(1+1i)
-2 +2i
cube( 2m )
8 m3
cube( [1, 2, 3, 4, 5] )
[1, 8, 27, 64, 125]

Complex Numbers

abs

Number  abs( Number x )
Number[ ]  abs( Number[ ] x )

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.

abs(1)
1
abs(-1m)
1m
abs(1+i)
1.41421
abs( [1, 0.5+(sqrt(3)/2)*i, 0.5-(sqrt(3)/2)*i] )
[1, 1, 1]

angle

Number  angle( Number x )
Number[ ]  angle( Number[ ] x )

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).

angle(1)
0 rad
angle( [1, 1i, -1, -1i] ) in deg
[0, 90, 180, -90] º

conj

Number  conj( Number x )
Number[ ]  conj( Number[ ] x )

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.

conj(1+1i)
1 -1i
angle( conj( 1∠45 deg ) ) in deg
-45 º
conj( [1, i, -1, -i] )
[1, -1i, -1, 1i]

imag

Number  imag( Number x )
Number[ ]  imag( Number[ ] x )

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.

imag(1+2i)
2
imag( 2A ∠ 45deg )
1.41421 A
imag( [1, i, -1, -i] )
[0, 1, 0, -1]

real

Number  real( Number x )
Number[ ]  real( Number[ ] x )

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.

real(1+2i)
1
real( 2A ∠ 45deg )
1.41421 A
real( [1, i, -1, -i] )
[1, 0, -1, 0]

sign

Number  sign( Number x )
Number[ ]  sign( Number[ ]  )

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).

sign(3)
1
sign(5km)
1 m
sign( [0, 100, 100i, -100, -100i] )
[0, 1, 1i, -1, -1i]

Hyperbolics

sinh

Number  sinh( Number x )
Number[ ]  sinh( Number[ ] x )

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.

sinh(0)
0
sinh( [-Inf, 0, 1, Inf, i, -i] )
[-Inf, 0, 1.1752, Inf, 0.84147i, -0.84147i]

cosh

Number  cosh( Number x )
Number[ ]  cosh( Number[ ] x )

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.

cosh(0)
q
cosh( [-Inf, -1, 0, 1, Inf, -i, i] )
[-Inf, 1.54308, 1, 1.54308, Inf, 0.5403, 0.5403]

tanh

Number  tanh( Number  )
Number[ ]  tanh( Number[ ] x )

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.

tanh(0)
0
tanh( [-Inf, -1, 0, 1, Inf, -i, i] )
[-1, -0.76159, 0, 0.76159, 1, -1.55741i, 1.55741i]

Vector Creation Functions

eye

Number  eye(  )
Number[ ]  eye( Number diagCount )

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.

eye()
1
eye(2)
[1, 0; 0, 1]

ones

Number  ones(  )
Number[ ]  ones( Number diagCount )
Number[ ]  ones( Number rowCount , Number columnCount...  )

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.

ones()
1
ones(2)
[1, 1; 1, 1]
ones(2,3)
[1, 1, 1; 1, 1, 1]

rand

Number  rand(  )
Number[ ]  rand( Number diagCount )
Number[ ]  rand( Number rowCount , Number columnCount...  )

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.

rand()
0.4164
rand(2)
[0.20758, 0.14484; 0.81804, 0.60078]
rand(2,3)
[0.721, 0.24574, 0.56769; 0.22827, 0.08006, 0.84662]

zeros

Number  zeros(   )
Number[ ]  zeros( Number diagCount )
Number[ ]  zeros( Number rowCount , Number columnCount...  )

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.

zeros()
0
zeros(2)
[0, 0; 0, 0]
zeros(2,3)
[0, 0, 0; 0, 0, 0]

Vector Functions

length

Number  length( Object x )
Number  length( Object[ ] x )

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.

length( [1,2,3] )
3
length( [1,2,3;4,5,6] )
6

max

Number  max( Number n )
Number[ ]  max( Number[ ] n )

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.

max( [2, 3, 5, 7, 11] )
11
max( [1, 2, 3; 7, 8, 9; 4, 5, 6] )
[7, 8, 9]
max( [1,2+2i,-3-3i] )
-3-3i

min

Number  min( Number n )
Number[ ]  min( Number[ ] n )

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.

min( [2, 3, 5, 7, 11] )
2
min( [1, 2, 3; 7, 8, 9; 4, 5, 6] )
[1, 2, 3]
min( [1,2+2i,-3-3i] )
1

size

Number[ ]  size( Object n )
Number[ ]  size( Object[ ] n )

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.

size( [1,2,3] )
[1, 3]
size( [1;2;3] )
[3, 1]
size( [1, 0; 0, 1] )
[2, 2]
size( [true, true, false, false] )
[1, 4]

sum

Number  sum( Number n )
Number[ ]  sum( Number[ ] n )

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.

sum( [1, 2, 3, 4] )
10
sum( [1; 2; 3; 4] mA)
10 mA
sum( [1,2,3;4,5,6] )
[5, 7, 9]
sum( sum( [1,2,3;4,5,6] ) )
21

prod

Number  prod( Number n )
Number[ ]  prod( Number[ ] n )

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.

prod( [1, 2, 3, 4] )
24
prod( [1; 2; 3; 4] mA)
24 mA4
prod( [1,2,3;4,5,6] )
[4, 10, 18]
prod( prod( [1,2,3;4,5,6] ) )
720

any

Boolean  any( Boolean b )
Boolean[ ]  any( Boolean[ ] b )

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.

any( [true, false, false] )
true
any( [false; false; false] )
false
any( [true, false, false; true, true, false] )
[true, true, false]
any( any( [true, false, false; true, true, false] ) )
true

all

Boolean  all( Boolean b )
Boolean[ ]  all( Boolean[ ] b )

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.

all( [true, false, false] )
false
all( [true; true; true] )
true
all( [true, false, false; true, true, false] )
[true, false, false]
all( all( [true, false, false; true, true, false] ) )
false

See Also

Getting Started Entering Data Units Operators Accessing Open Data Vectors Functions