Fisheries and Oceans Canada / Pêches et Océans Canada - Government of Canada / Gouvernement du Canada Fisheries and Oceans Canada / Pêches et Océans Canada - Government of Canada / Gouvernement du Canada
 
Français Contact Us Help Search Canada Site
Home What's New DFO National Site Map Media

Fisheries & Oceans
 
 
Maritimes Region
Fishing Industry
General Public
Marine & Oceans Industry
Media
Students and Teachers
Scientists and Researchers
 
AconIcon ACON       Home/Topics   |   Commands

Vector / Matrix Functions


The vector/matrix functions include: Ascending, Ave, BIndex, Cum Prod, Cum Sum, Descending, Disclose, Distinct, Divide, Drop, Enclose, Expand, Hessenberg, Hessenberg_QR, Householder, In Prod, Index, Insert, Inverse, Jacobi, Len, Linear Interpolate, Linear Regression, Linear Solve, Median, Member, Minus, Norm Dist, Out GE, Out GT, Out EQ, Out LE, Out LT, Out NE, Out Prod, Out Pow, Out Sum, Outer Join, Rank, Ravel, Reverse, Rot, Scalar, Select, Seq, Shape, Sqz Sum, StdDev, Sum, SV_Decomp, Take, Times, Transpose, Tridiagonal_QL, and the date maniupulation functions: Julian, and YMD.

These routines allow you to manipulate vectors and matrices. They are separated from the other related functions (see Math functions and String functions), as they are a conceptually more complex set of functions. A number of the functions described elsewhere in this manual also accept vectors or matrices as arguments; e.g. Data_Points(xyzmatrix). None of the vector or matrix functions have an initial state.


Indexing Vectors and Matrices

In addition to being able to assign values to, and obtain values from scalar variables, the user may create and use both vectors and matrices.

Simple vectors may be defined by specifying a sequence of scalar values separated by blanks;
v = 1 2 3 4 5 8
There are other ways, see the Seq command.

Matrices may be defined using the shape() command;

/* create a 2 row x 3 col matrix with elements 1,2,...6 */
m = shape(2 3,1 2 3 4 5 6)
m
1 2 3
4 5 6

There are other ways, see the Matrix Read command.

Elements of vectors and matrices may be addressed by using indexing. Vector indexes are specified by a "[", a selection vector, and a terminating "]";

v = 10 20 30 40 50 60
/* select the 1st, 2nd, and 3rd elements of vector v */
v[1 2 6]
10 20 60

Matrix indexes are specified by a "[", an optional row index, ";", an optional column index, and a terminating "]";

/* create a 2 row x 3 col matrix with elements 10,20,...60 */
m = shape(2 3,10 20 30 40 50 60)
m
10 20 30
40 50 60
m[1;2] /* select element from row 1 and col 2 of matrix m */
20
m[1;1 2] /* select cols 1 and 2 from row 1 of matrix m */
10 20

If a row or column index is missing, all rows (or columns) are included.

/* select all elements form the 1st and 2nd cols of matrix m */
m[;1 2]
10 20
40 50
/* select all elements from the 2nd row of matrix m */
m[2;]
40 50 60

A special indexing mode exists (different from that of APL) where if 2 equal length vectors are supplied for both the row and column indices, then the matching elements of each vector are paired to specify a single address within the matrix; e.g

/* select row 1 col 1 element, and row 1 col 3 element */
m[1 2;1 3]
10 60

Compare this with unequal indices, where 3 values could also generate 2 values as well;

/* select from 1st row, the 1st and 3rd cols from matrix m */ m[1;1 3]
10 30

Vectors and matrices may be written to or read from using indexing;

/* assign element in 2nd row and 3rd col to value in 1st row and 2nd col */
m[2;3] = m[1;2]
m
10 20 30
40 50 20

3 dimensional matrices are supported;

/* create matrix m, 2 pages by 3 rows by 4 cols using sequence of #´s 1 to 24 */
m = shape(2 3 4,seq(1,24));
m
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24

Ascending

This function evaluates the sorted order of its argument and returns a sorted list of indices in ascending order, as the result. The list of indices which is returned would, if used to index the original argument, generate the sorted list.

There is 1 parameter:
object - the vector for which the sorted index is to be generated.

Ascending(object);

x = 10 20 80 40
/* in is assigned the sorted list of indices of vector x */
in = ascending(x)
in
1 2 4 3
/* the vector x indexed by the ascending index, gives the sorted list */
x[in]
10 20 40 80
Hint: index is a reserved word for the index command, don´t use it as a variable name.

Ave

This function calculates the average value of its argument(s), and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the average of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the average of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object for which the average is to be calculated.
object n- an object for which the average is to be calculated.
when the 1st parameter is a vector or matrix
object - the object for which the average is to be calculated.
dimension- an optional integer specifying the dimension over which the average is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

ave(object,object2[,objectn]);
ave(object[,dimension]);

/* average of a list of scalar values */
ave(1,2,3,4,5,6)
3.5

v=seq(1,5)
v
1 2 3 4 5
/* average of vector v */
ave(v)
3
ave(v,1)
3
ave(v,2)
3

m=shape(2 3,seq(1,6)) /* create a 2 x 3 matrix */
m
1 2 3
4 5 6
/* average of entire matrix */
ave(m,0)
3.5
/* average over rows of matrix gives the column averages) */
ave(m,1)
2.5 3.5 4.5
/* average over columns of matrix gives the row averages) */
ave(m,2)
2 5

Bindex

This function returns the numeric indices of the true (1) values within a boolean vector. These values can be used to subscript vectors and matrices.

There is 1 parameters:
selection - the boolean vector to indicate which elements to select (0 = ignore, 1 = select).

subscripts = Bindex(selection);

/* select 1st, 2nd, and last elements of vector v */
i = Bindex(1 1 0 0 1)
print(i)
1 2 5

A useful example of this is to select subsets of data:

/* species must have the same # of elements or rows as catch */
/* x = any value to be selected */
i = Bindex(species == x); subset = catch[i];

Cum Prod

This function calculates the cumulative product of its argument, and returns the result as a scalar or vector. If the 1st argument is a matrix, a 2nd agument may be specified which determines which dimension the summation should occur.

There are 1 or more parameters:
object - the object for which the product is to be calculated.
dimension- an optional integer specifying the dimension over which the total is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

cum_prod(object[,dimension]);

/* cumulative product of the given vector */
cum_prod(1 2 3 4 5)
1 2 6 24 120

m=shape(2 3,seq(1,6)) /* create a 2 x 3 matrix */
m
1 2 3
4 5 6

/* cumulative product of the entire matrix */
cum_prod(m,0)
1 8 120
4 40 720

/* cumulative product over the rows of matrix m */
cum_prod(m,1)
1 2 3
4 10 18

/* cumulative product over the columns of matrix m */
cum_prod(m,2)
1 2 6
4 20 120

Cum Sum

This function calculates the cumulative sum of its argument, and returns the result as a scalar or vector. If the 1st argument is a matrix, a 2nd agument may be specified which determines which dimension the summation should occur.

There are 1 or more parameters:
object - the object for which the total is to be calculated.
dimension- an optional integer specifying the dimension over which the total is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

cum_sum(object[,dimension]);

cum_sum(1 2 3 4 5) /* cumulative sum of vector 1 2 3 4 5 */
1 3 6 10 15

m=shape(2 3,seq(1,6)) /* create a 2 x 3 matrix */
m
1 2 3
4 5 6

/* cumulative sum over the entire matrix */
cum_sum(m,0)
1 7 15
5 12 21

/* cumulative sum over the rows */
cum_sum(m,1)
1 2 3
5 7 9

/* cumulative sum over the columns */
cum_sum(m,2)
1 3 6
4 9 15

Descending

This function evaluates the sorted order of its argument and returns a sorted list of indices in descending order, as the result. The list of indices which is returned would, if used to index the original argument, generate the sorted list.

There is 1 parameter:
object - the vector for which the sorted index is to be generated.

Descending(object);

x = 10 20 80 40
/* create descending index of list */
in = Descending(x)
in
3 4 2 1
/* index original vector by index to give descending list */
x[in]
80 40 20 10

Hint: index is a reserved word for the index command, don´t use it as a variable name.


Disclose

This function discloses an object from an enclosed vector and returns the result as a new object.

This command is useful when you would like to return more than 1 object from a user-defined function.

There are 2 parameters:
object - the enclosed vector.
index - the index of the object to be disclosed from the enclosed vector.

obj = Disclose(object,index);

x = 1 2 3; // test data
y = sin(x);
enc = Enclose(x,y,"Test"); // Enclose 3 objects

z = Disclose(enc,1)
z
1 2 3

Distinct

This function evaluates a vector and returns the list of distinct values in ascending order, as the result. This command also accepts character matrices.

There is 1 parameter:
object - the vector for which the distinct values are to be generated.

Distinct(vector);

x = 10 20 20 20 80 80 40
y = Distinct(x)
y
10 20 40 80

Divide

This function calculates the value of its argument(s) divided together, and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the result of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the result of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object which is to be divided by the other objects.
object n- an object which is to be divided by the other objects.
when the 1st parameter is a vector or matrix
object - the object for which the result is to be calculated.
dimension- an optional integer specifying the dimension over which the result is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

divide(object,object2[,objectn]);
divide(object[,dimension]);

see Times for examples of a similar function.


Drop

This function drops elements from a vector or matrix. The 1st argument specifies how many elements to drop. Then 2nd argument is the object to be acted upon.

There are at least 2 parameters:
when the 1st parameter is a scalar
ndrop - the number of elements to drop (if negative, drop from the end of the vector).
object - an vector from which elements are to be dropped.
when the 1st parameter is a 2 element vector
ndrop vector - the number of rows and columns to drop
0 = do not drop any elements,
+ive = drop from the first,
-ive = drop from the last.
object - an matrix from which elements are to be dropped.

When more than 2 parameters exist, all but the last parameter are considered to comprise the ndrop vector.

Drop(ndrop,object);

If the 1st argument is a scalar then the 2nd argument must be a vector from which the elements will be dropped.

v
1 2 3 4 5
/* drop the 1st 2 elements */
drop(2,v)
3 4 5
/* drop the last 2 elements */
drop(-2,v)
1 2 3

Alternately, if the 1st argument is a vector (it must be a vector of length 2) then the 2nd argument must be a matrix from which the elements will be dropped.

m
1 2 3
4 5 6
/* drop no rows and 1st column */
drop(0 1,m)
2 3
5 6
/* drop no rows and last column */
drop(0 -1,m)
1 2
4 5
/* drop 1 rows and no columns */
drop(1 0,m)
4 5 6

Enclose

This function encloses its arguments as a vector of objects and returns the result as vector. Any variables may be enclosed which creates a copy of the objects in the enclosed vector. The elements of the enclosed vector may be accessed using an index expression or through the Disclose() command.

This command is useful when you would like to return more than 1 object from a user-defined function.

There are 1 or more parameters:
object - the object(s) to be enclosed.

enclosedVector = Enclose(object[,object]);

x = 1 2 3; // test data
y = sin(x);
enc = Enclose(x,y,"Test"); // Enclose 3 objects

enc[3]
Test

Disclose(enc,1)
1 2 3

Expand

This function inserts 0´s into a vector or matrix (or blanks into a character string). The 1st argument specifies the positions where expansion is to occur using a boolean vector. Then 2nd argument is the object to be acted upon. The 3rd optional argument specifies which dimension to apply the expansion to.

There are 3 parameters:
expansion vector - the boolean vector indicating where expansion is to occur. 1´s in the vector denote existing data elements, 0´s indicate where new values are to be inserted.
object - a vector or matrix to which 0´s are to be inserted (or a character string).
dimension- an optional integer specifying the dimension over which the expansion is to occur. The default is the 1st dimension (not used for character string expansion).

Expand(booleanvector,object [,dimension]);

v
1 2 3 4 5
/* insert 0´s after the 2nd and 4th elements of vector */
expand(1 1 0 1 1 0 1,v)
1 2 0 3 4 0 5

m
1 2 3
4 5 6
/* insert rows of 0´s after the 1st row */
expand(1 0 1,m,1)
1 2 3
0 0 0
4 5 6

Hessenberg

This function computes the upper Hessenberg matrix from a real symmetric matrix.

The algorithm for this function is from Chapter 11.5 Reduction of a General Matrix to Hessenberg Form pages 382-386 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There is 1 parameter:
symmetric matrix - the real symmetrix matrix for which the hessenberg form is returned (size n x n).

hmat = Hessenberg(symmetric matrix);

m = shape(3 3,2 3 6 4 8 32 1 5 5.);
hm = Hessenberg(m); /* hessenberg form of matrix */
hm
2 4.5 3
0 16.0 16
0 0.0 -3

Hessenberg_QR

This function computes the real and imaginary eigenvalues of an upper hessenberg matrix.

The algorithm for this function is from Chapter 11.6 The QL Algorithm for Real Hessenberg Matrices pages 387-394 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There are 3 parameters:
matrix - the upper hessenberg matrix for which the eigenvalues and eigenvectors are to be calculated (size n x n).
real vector - the vector to contain the real part of the eigenvalues (size n).
imaginary vector - the vector to contain the imaginary part of the eigenvalues (size n).
The number of rows and columns of the matrices must be equal, and equal to the number of elements of the eigenvalue vectors.

Hessenberg_QR(hmatrix,real eigenvalue vector,imaginary eigenvalue vector);

m = shape(3 3,2 3 6 4 8 32 1 5 5.);
hm = Hessenberg(m); /* hessenberg form of matrix */
wr = shape(3,0.); /* wr => real part of eigenvalues */
wi = shape(3,0.); /* wi => imaginary part of eigenvalues */
Hessenberg_QR(hm,wr,wi);

wr
2 16 -3

wi
0 0 0

Householder

This function reduces a symmetric matrix to tridiagonal form by orthogonal transformations.

The algorithm for this function is from Chapter 11.2 Reduction of a Symmetric Matrix to Tridiagonal Form: Givens and Househoulder Reductions pages 367-374 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There are 3 parameters:
matrix - the real symmetric matrix which is replaced by the orthogonal matrix (size n x n).
diagonal vector - the vector to contain the diagonal elements of the tridiagonal matrix (size n).
off-diagonal vector - the vector to contain the off-diagonal elements of the tridiagonal matrix (size n).
The number of rows and columns of the matrices must be equal, and equal to the number of elements of the vectors.

Householder(matrix,dvector,odvector);

m = shape(3 3,2 3 6 4 8 32 1 5 5.);
dv = odv = shape(3,0.0);
Householder(m,dv,odv);

m
1 3.000000 0
4 1.000000 32
0 10.099020 1

dv

0.980581 1 1

odv
0 4 10.099

In Prod

This function computes the inner product of 2 matrices by multiplying each element of one matrix with the elements of the other matrix , and summing the result to perform standard matrix multiplication. A matrix is returned.

There are 2 parameters:
matrix1 - the 1st matrix for which the inner product is to be calculated.
matrix2 - the 2nd matrix for which the inner product is to be calculated.
The number of columns of matrix1 must equal the number of rows of matrix2.

in_prod(matrix1,matrix2);

m
1 2 3
4 5 6
n
1 1
2 2
3 3
in_prod(m,n)
14 14
32 32

Index

This function returns the index of the occurences of one vector within another vector. If an element of the comparison vector isn´t found in the reference vector a value of 0 is returned for that element of the comparison vector.

There are 2 parameters:
reference vector - the vector which is searched in and for which indices are returned.
comparison vector - the vector of values which are searched for. The result contains an index value of the position of each of these elements in the reference vector.

inx = index(reference vector,comparison vector);

a = 1 2 3 4 5
b = 2 3 6
/* index of vector b in vector a */
i = index(a,b)
i
2 3 0

/* index of vector a in vector b */
index(b,a)
0 1 2 0 0

Insert

This function replaces specific elements of a 2D or 3D numeric matrix with new values.

There are 4 or 5 parameters:
matrix - the matrix in which values are replaced.
page vector- the optional vector of page indices which are used to specify the page for which the corresponding z vector value is to be placed in 3D matrices.
row vector- the vector of row indices which are used to specify the row for which the corresponding z vector value is to be placed.
column vector- the vector of column indices which are used to specify the column for which the corresponding z vector value is to be placed.
z vector- the vector of new values which are used to replace existing values.

Note: The length of the page vector, row vector, column vector and z vector must have the same value.

Insert(matrix,[page vector,] row vector, column vector, z vector);

m = shape(5 5,seq(1,25))
m
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
r = 1 2 4 5
c = 1 1 3 2
z = 99 99 99 99

/* insert values of z at row and col positions of matrix m */
insert(m,r,c,z)
m
99 2 3 4 5
99 7 8 9 10
11 12 13 14 15
16 17 99 19 20
21 99 23 24 25

Inverse

This function calculates the inverse of a matrix, and returns the result.

There is 1 parameter:
object - the matrix for which the inverse is to be returned.

Inverse(matrix);

i
5.13871 1.75726 3.08634 5.34532
9.4763 1.71728 7.02231 2.26417
4.94766 1.24699 0.838954 3.8963
2.7723 3.68053 9.83459 5.35386
inverse(i)
0.117804 0.094633 0.168415 -0.0449689
2.32529 -0.18141 2.39557 0.65491
0.472216 0.0849891 -0.622941 -0.0540559
0.792108 -0.0804093 -0.58976 -0.140857

Jacobi

This function computes the eigenvalues and eigenvectors of a real symmetric matrix. The number of iterations is returned as an integer.

The algorithm for this function is from Chapter 11.1 Jacobi Transformations of a Symmetric Matrix pages 360-366 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There are 3 parameters:
symmetric matrix - the real symmetrix matrix for which the eigenvalues and eigenvectors are to be calculated (size n x n).
eigenvalue vector - the vector to contain the eigenvalues (size n).
eigenvector matrix - the matrix to contain the eigenvectors (size n x n).
The number of rows and columns of the matrices must be equal, and equal to the number of elements of the eigenvalue vector.

niter = Jacobi(symmetric matrix,eigenvalue vector,eigenvector matrix);

m = shape(3 3,2 3 6 4 8 32 1 5 5.);
d = shape(3,0.0);
v = shape(3 3,0.0);
Jacobi(m,d,v); /* d => eigenvalues, v => eigenvectors */
3
d
41.4378 -4.15159 -22.2862
v
-0.118839 0.745354 0.655991
0.810580 -0.308736 0.497637
0.573444 0.590872 -0.567479

Len

This function returns the number of elements in a numeric object or character string. If the object is a matrix, a 2 element vector is returned containing the number of rows and the number of columns in the matrix.

Note that the length of the string (or character matrix) when plotted (NDC units) may be calculated using the Len_NDC command.

There is 1 parameter:
object - the scalar, vector, matrix, character string, or character matrix for which the length is to be returned.

len(char string);
len(vector);
len(matrix);
len(character matrix);

len("test")
4
cmat = strfold("Test/Data","/")
len(cmat)
2 4
len(1 2 3 4 5)
5

m
1 2 3
4 5 6
len(m)
2 3

Linear Interpolate

This function linearly interpolates the y values for a set of x values using a reference set of x,y pairs of points.

There are 2 parameters:
xy matrix - the reference matrix containing 2 or more sets of x,y points (n x 2).
x vector- the x values for which corresponding y values are to be estimated.

y = Linear_Interpolate(xy matrix,x vector);

xy = shape(4 2,1 10 2 15 3 18 4 12)
xy
1 10
2 15
3 18
4 12
linear_interpolate(xy,3 3.5)
18 15

Linear Regression

This function computes a linear regression for a set of x and y vectors. The result is a vector containing the slope, intercept, variance, and r2.

There are 2 parameters:
x vector- the x values for which the regression parameters are to be estimated.
x vector- the y values for which the regression parameters are to be estimated.

result = Linear_Regression(x vector,y vector);

x = 1 2 3 4.
y = 10 12 15 16.
linear_regression(x,y)
2.1 8 0.35 0.969231

Linear Solve

This function solves a set of linear equations. The result is a vector or matrix containing the solution.

There are 2 parameters:
cmatrix- the coefficients of the equations to be solved.
x vector or matrix - the x estimates used to solve the equations.

result = Linear_Solve(cmatrix,xvector);

x = shape(3 3,4 6 0 3 2 2 1 3 4)
d = 1.1 0.59 .78
Linear_Solve(x,d)
0.05 0.15 0.07

Median

This function calculates the median value of its argument, and returns the result as a scalar or vector.

There are 1 or more parameters:
object - the object (vector, matrix, or 3D matrix) for which the median is to be calculated.
dimension - an optional integer specifying the dimension over which the median is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

median(object[,dimension]);

x
0 1 0 5 4
1 0 1 4 1

/* find median value over the entire matrix */
median(x,0)
1
/* find median value over the rows by column */
median(x,1)
0.5 0.5 0.5 4.5 2.5

/* find median value over the columns by row */
median(x,2)
1 1

Member

This function returns the boolean index of the occurences of one vector within another vector. If an element of the comparison vector is found in the reference vector a value of 1 is returned for that element of the comparison vector, otherwise its value is 0.

There are 2 parameters:
reference vector - the vector which is searched in and for which boolean indices are returned.
comparison vector- the vector of values which are searched for. The result contains a 1 when each of these elements are found anywhere in the reference vector.

inx = member(reference vector,comparison vector);

a = 1 2 3 4 5
b = 2 3 6
i = member(a,b)
i
1 1 0

/* find members of b in a */
member(b,a)
0 1 1 0 0

Minus

This function calculates the value of its argument(s) subtracted together, and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the result of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the result of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object which is to be subtracted by the other objects.
object n- an object which is to be subtracted by the other objects.
when the 1st parameter is a vector or matrix
object - the object for which the result is to be calculated.
dimension- an optional integer specifying the dimension over which the result is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

minus(object,object2[,objectn]);
minus(object[,dimension]);

see Times for examples of a similar function.


Norm Dist

This function returns the corresponding normal distribution of values.

There are 5 parameters:
x values - the vector or matrix which contains the x values for which y values are required.
mode - the value of the mode to be generated.
std dev - the value of the std dev associated with the normal distribution.
epsilon - the point at which low y values are set to 0.
dimension - the optional dimension of the x matrix over which this normal distribution is to be calculated.

y = Norm_Dist(<x vect>,<mode>,<std.dev>,<epsilon>,[,<dimension>])

Norm_Dist(1 2 3 4 5,3,1,.01)
0.142384 0.357616 0.357616 0.142384

Out EQ

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for equality. A boolean matrix is returned (an integer matrix containing 0´s or 1´s).

There are 2 parameters:
object1 - the 1st vector or matrix for which the outer equality is to be calculated. This may be a character matrix.
object2 - the 2nd vector or matrix for which the outer equality is to be calculated. This may be a character string.

out_EQ(vector1,vector2);

x = 1 2 3 4
y = 2 3 4 5 6
out_eq(x,y)
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0

m = shape(2 3,seq(1,6))
out_eq(m,y)
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0

0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
x = strfold("this is a test")
out_eq(x,"is")
0 0 1 0
1 0 0 0
0 0 0 0
0 0 0 0

Out GE

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for the boolean test ´>=´. A boolean matrix is returned.

out_GE(object1,object2);

see Out EQ


Out GT

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for the boolean test ´>´. A boolean matrix is returned.

out_GT(object1,object2);

see Out EQ


Out LE

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for the boolean test ´<=´. A boolean matrix is returned.

out_LE(object1,object2);

see Out EQ


Out LT

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for the boolean test ´<´. A boolean matrix is returned.

out_LT(object1,object2);

see Out EQ


Out NE

This function computes the outer product of 2 vectors (or a vector and a matrix) by testing each element of each vector with the elements of the other vector for the inequality ´!=´. A boolean matrix is returned.

out_NE(object1,object2);

see Out EQ


Out Pow

This function computes the outer product of 2 vectors (or a vector and a matrix) by calculating the power function pow(xi,yj) for each element of each vector with the elements of the other vector. A matrix is returned.

There are 2 parameters:
vector1 - the 1st vector for which the outer power is to be calculated.
vector2 - the 2nd vector containing the powers to which the 1st vector is raised.

out_pow(object1,object2);

x = 1 2 3 4
y = 2 3 4 5 6
/* outer product of vector x to the power y */
out_pow(x,y)
1 1 1 1 1
4 8 16 32 64
9 27 81 243 729
16 64 256 1024 4096

Out Prod

This function computes the outer product of 2 vectors (or a vector and a matrix) by multiplying each element of each vector with the elements of the other vector. A matrix is returned.

There are 2 parameters:
object1 - the 1st vector for which the outer product is to be calculated.
object2 - the 2nd vector for which the outer product is to be calculated.

out_prod(object1,object2);

v
1 2 3 4 5
out_prod(v,v)
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Out Sum

This function computes the outer sum of 2 vectors (or a vector and a matrix) by adding each element of each vector with the elements of the other vector. A matrix is returned.

There are 2 parameters:
object1 - the 1st vector for which the outer sum is to be calculated.
object2 - the 2nd vector for which the outer sum is to be calculated.

out_sum(object1,object2);

x = 1 2 3 4
y = 2 3 4 5 6
out_sum(x,y)
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10

Outer Join

This function performs the outer join of 1 or more vectors (or matrices) by matching records of a master key with matching detail keys. An outer join permits replication of missing data. Every master key element (which must be unique) will be tested (at least once) with the detail keys. When the keys match, the detail object is added to the output matrix, otherwise 0´s are added to the output matrix. A matrix is returned.

There are 3 or 4 parameters:
master key - Vector for which an optional matching master vector or matrix is also supplied.
detail key - Vector for which a matching detail vector or matrix is also supplied. Only where elements this detail key match the master key, will elements of the detail object be copied to the output matrix.
master object - Optional vector or matrix which corresponds with the master key. Every record of the master object will be replicated in the output (at least once) regardless of whether a match occurs between the master and detail keys.
detail object - Vector or matrix which corresponds with the detail key. When matching occurences of the master and detail keys are encountered, elements of the detail object are copyied to the output matrix.

Outer_Join(master key vector,detail key vector[, master vector or matrix], detail vector or matrix ).

key1 = seq(1,10);
key2 = 3 4 6 6 6 7;
x1 = key1 + 100;
x2 = key2 + 300;
print(x1)
101 102 103 104 105 106 107 108 109 110
print(x2)
303 304 306 306 306 307

outer_join(key1,key2,x1,x2);
101 0
102 0
103 303
104 304
105 0
106 306
106 306
106 306
107 307
108 0
109 0
110 0

Rank

This function returns the rank of a numeric object. The function returns 0, 1, and 2, for scalars, vectors, and matrices respectively.

There is 1 parameter:
object - the scalar, vector, or matrix, for which the rank is to be returned.

rank(scalar);
rank(vector);
rank(matrix);

rank(10)
0
rank(1 2 3 4 5)
1

m
1 2 3
4 5 6
rank(m)
2

Ravel

This function converts matrices to vectors by stringing together the elements along the rows. A vector is always returned.

There is 1 parameter:
object - the matrix to be ravelled.

ravel(matrix);

m
1 2 3
4 5 6
ravel(m)
1 2 3 4 5 6

Reverse

This function alters the sequence of elements of vectors and matrices by reversing the order of the elements.

There are 1 or 2 parameters:
object - the string, vector or matrix to be rotated
when the object is a matrix
dimension- an optional integer specifying the dimension over which the rotation is to be applied.
1 = over the rows of a 2D matrix or the pages of a 3D matrix
2 = over the columns of a 2D matrix or the rows of a 3D matrix.
3 = over the columns of a 3D matrix.

reverse(object,dimension);

v
1 2 3 4 5
/* reverse */
reverse(v)
5 4 3 2 1

m
1 2 3
4 5 6
/* reverse over the rows of the matrix */
reverse(m,1)
4 5 6
1 2 3
/* reverse over the columns of the matrix */
reverse(m,2)
2 3 1
5 6 4

Rot

This function alters the sequence of elements of vectors and matrices by removing elements from one end of an object and adding them to the other end (rotation).

There are 2 or 3 parameters:
rotation - the number of elements to be shifted (+ive = to the "left", -ive = to the "right").
object - the vector or matrix to be rotated when the object is a matrix
dimension- an optional integer specifying the dimension over which the rotation is to be applied.
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).
Note:
For 3D matrix dimension: 1 is pages, 2 - rows and 3 columns,
For 2D matrix: 1 is rows and 2 - columns.

rot(rotation,object,dimension);

v
1 2 3 4 5
/* rotate 1st element to the end */
rot(1,v)
2 3 4 5 1
/* rotate 1st 2 elements to the end */
rot(2,v)
3 4 5 1 2
/* rotate 1st 3 elements to the end */
rot(3,v)
4 5 1 2 3
/* rotate last element to the start */
rot(-1,v)
5 1 2 3 4

m
1 2 3
4 5 6
/* rotate 1st row of the matrix to the end */
rot(1,m)
/* rotate 1st row of the matrix to the end */
rot(1,m,1)
4 5 6
1 2 3
/* rotate 1st col of the matrix to the end */
rot(1,m,2)
2 3 1
5 6 4

Scalar

This function converts a vector or matrix to a scalar value.

There is 1 parameter:
object - the vector or matrix to be converted.

n = scalar(object);

m
1 2 3
4 5 6
scalar(m)

1

Select

This function selects elements of vectors and matrices by using a boolean list to flag those elements which should be selected.

There are 2 or 3 parameters:
selection - the boolean vector to indicate which elements to select (0 = ignore, 1 = select).
object - the vector or matrix to be selected.
when the object is a matrix
dimension- an optional integer specifying the dimension over which the selection is to be applied.
0 = over the rows (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

select(selection,object[,dimension]);

v
1 2 3 4 5
/* select 1st, 2nd, and last elements of vector v */
select(1 1 0 0 1,v)
1 2 5

m
1 2 3
4 5 6
/* select 1st row of matrix m */
select(1 0,m)
1 2 3
/* select 1st, 2nd, and last columns of matrix m */
select(1 0 1,m,2)
1 3
4 6

A useful example of this is to select subsets of data:

/* species must have the same # of elements or rows as catch */
/* x = any value to be selected */
subset = select(species == x,catch);

Seq

This function returns a sequence of integers or real numbers as a vector. The beginning and ending values of the sequence, and optionally the increment between the steps, are specified. If all of the arguments are integers, the result will be an integer vector, otherwise a real vector is returned. An empty sequence returns a 0 length vector.

There are 2 or 3 parameters:
start - the initial value of the sequence.
end - the last value of the sequence.
increment - the optional increment between the values to progress from the starting to ending value (default is 1).

seq(start,end[,increment]);

/* create sequence from 1 to 6 in increments of 1 */
seq(1,6,1)
1 2 3 4 5 6
/* create sequence from 10 to 4 in increments of -1 */
seq(10,4,-1)
10 9 8 7 6 5 4
/* create sequence from 2 to 3 in increments of 0.2 */
seq(2,3,0.2)
2.0 2.2 2.4 2.6 2.8 3.0
seq(10,1) /* this is an empty sequence */


Shape

This function returns a vector or matrix by reshaping an input parameter. The shape of the resultant object, and the object supplying the values, are specified.

There are 2 parameters:
shape - the shape of the resultant object (if it is a scalar a vector will be created).
object - the object from which the values will be reshaped.

shape(shape,object);

/* create vector of 5 elements of value 1 */
shape(5,1)
1 1 1 1 1
/* create vector of 5 elements using 1 2 3 repetitively */
shape(5,1 2 3)
1 2 3 1 2
/* create matrix of 2 rows and 3 columns with value 1 */
shape(2 3,1)
1 1 1
1 1 1
/* create matrix of 2 rows and 3 columns with given values */
shape(2 3,10 9 8 7 6 5 4 3 2 1)
10 9 8
7 6 5

Sqz Sum

This function calculates the aggregated sum of a vector or matrix for fixed groups of data. The number of elements in per group is specified. The aggregation is always performed over the 1st dimension. The shape of the 1st dimension of the object to be squeezed must be a multiple of the overall group size.

There are 2 parameters:
object - the object for which the total is to be calculated.
groupsize - the number of elements squeezed into a new element.

sqz_sum(object,groupsize);

m = shape(8 8,seq(1,64))
/* do aggregated sum of matrix m by groups of 4 */
sqz_sum(m,4)
52 56 60 64 68 72 76 80
180 184 188 192 196 200 204 208

StDev

This function calculates the standard deviation of its argument(s), and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the average of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the average of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object for which the Standard Deviation is to be calculated.
object n- an object for which the Standard Deviation is to be calculated.
when the 1st parameter is a vector or matrix
object - the object for which the Standard Deviation is to be calculated.
dimension- an optional integer specifying the dimension over which the Standard Deviation is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

StDev(object,object2[,objectn]);
StDev(object[,dimension]);

/* calculate the std dev for the given values */
stdev(1,2,3,4.)
1.290994448736

m = shape(8 8,seq(1,64));
/* calculate the std dev for the entire matrix */
stdev(m)
18.618986725025
/* calculate the std dev over the rows, giving a std dev for each col */
stdev(m,1)
19.595917942265 19.595917942265 19.595917942265 19.595917942265
19.595917942265 19.595917942265 19.595917942265 19.595917942265
/* calculate the std dev over the cols, giving a std dev for each row */
stdev(m,2)
2.449489742783 2.449489742783 2.449489742783 2.449489742783
2.449489742783 2.449489742783 2.449489742783 2.449489742783

Sum

This function calculates the total value of its argument(s), and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the total of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the total of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object for which the total is to be calculated.
object n- an object for which the total is to be calculated.
when the 1st parameter is a vector or matrix
object - the object for which the total is to be calculated.
dimension- an optional integer specifying the dimension over which the total is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

sum(object,object2[,objectn]);
sum(object[,dimension]);

/* calculate the sum of the given values */
sum(1,2,3,4,5,6)
21

v=seq(1,5)
v
1 2 3 4 5
/* calculate the sum of vector v */
sum(v)
15
sum(v,1)
15
sum(v,2)
15

m = shape(2 3,seq(1,6)) /* create a 2 x 3 matrix*/
m
1 2 3
4 5 6
/* sum over the entire matrix */
sum(m,0)
21
/* sum over the rows of the matrix (gives column totals) */
sum(m,1)
5 7 9
/* sum over the columns of the matrix (gives row totals) */
sum(m,2)
6 15

SV_Decomp

This linear least squares approximation function computes a singular value decomposition matrix.

The algorithm for this function is from Chapter 2.9 Singular Value Decomposition pages 60-72 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There are 4 parameter:
a - the real matrix to be used as input (size m x n).
u - the real matrix to be used for output (size m x n).
v - the real matrix to be used for output of V (size n x n).
w - the real vector to be used for output of the diagonal (size n).

SV_Decomp(a,u,v,w);

Function test_SV_Decomp() {
// Tests SV_Decomp() Using example data Numerical Recipes
// This test code converted from XSVDCMP.C 11/13/87
// Singular Value Decomposition Algorithm from pages 68-71 of
// Numerical Recipes in C. The art of Scientific Computing.
// W.H.Press, B.P.Flannery, S.A.Teukolsky, W.T.Vetterline.
// 1988. Cambridge University Press. 735 p.
m = 5; n = 3;
a = shape(m,n,
1.0 2.0 3.0
2.0 3.0 4.0
3.0 4.0 5.0
4.0 5.0 6.0
5.0 6.0 7.0);
u = a;
if (n > m) {
for (k=m+1;k<=n;k++) {
for (l=1;l<=n;l++) {
a[k;l]=0.0;
u[k;l]=0.0;
}
}
m=n;
}
v = shape(n,n,0.0);
w = shape(len(u)[2],0.);
/* perform decomposition */
SV_Decomp(a,u,v,w);
/* write results */
print("Decomposition matrices:");
print("Matrix u");
for (k=1;k<=m;k++) {
$s = "";
for (l=1;l<=n;l++)
$s = cat($s,sprintf("%12.6f",u[k;l]));
print($s);
}
print("Diagonal of matrix w");
$s = "";
for (k=1;k<=n;k++)
$s = cat($s,sprintf("%12.6f",w[k]));
print($s);
print("Matrix v-transpose");
for (k=1;k<=n;k++) {
$s = "";
for (l=1;l<=n;l++)
$s = cat($s,sprintf("%12.6f",v[l;k]));
print($s);
}
print("Check product against original matrix:");
print("Original matrix:");
for (k=1;k<=m;k++) {
$s = "";
for (l=1;l<=n;l++)
$s = cat($s,sprintf("%12.6f",a[k;l]));
print($s);
}
print("Product u*w*(v-transpose):");
for (k=1;k<=m;k++) {
for (l=1;l<=n;l++) {
a[k;l]=0.0;
for (j=1;j<=n;j++)
a[k;l] += u[k;j]*w[j]*v[l;j];
}
$s = "";
for (l=1;l<=n;l++) $s = cat($s,sprintf("%12.6f",a[k;l]));
print($s);
}
};
test_SV_Decomp();
Decomposition matrices:
Matrix u
-0.219236 -0.742924 0.530877
-0.321266 -0.443608 -0.282197
-0.423296 -0.144292 -0.718999
-0.525326 0.155023 0.161080
-0.627356 0.454339 0.309239
Diagonal of matrix w
16.701031 1.037092 0.000000
Matrix v-transpose
-0.441275 -0.568002 -0.694730
0.799131 0.103473 -0.592185
-0.408248 0.816497 -0.408248
Check product against original matrix:
Original matrix:
1.000000 2.000000 3.000000
2.000000 3.000000 4.000000
3.000000 4.000000 5.000000
4.000000 5.000000 6.000000
5.000000 6.000000 7.000000
Product u*w*(v-transpose):
1.000000 2.000000 3.000000
2.000000 3.000000 4.000000
3.000000 4.000000 5.000000
4.000000 5.000000 6.000000
5.000000 6.000000 7.000000


Take

This function takes elements from a vector or matrix. The 1st argument specifies how many elements to take. Then 2nd argument is the object to be acted upon.

There are at least 2 parameters:
when the 1st parameter is a scalar
ntake - the number of elements to take (if -ive, take from the end of the vector).
object - an vector from which elements are to be taken.
when the 1st parameter is a 2 element vector
ntake vector - the number of rows and columns to take
0 = do not take any elements,
+ive = take from the first,
-ive = take from the last.
object - an matrix from which elements are to be taken.

When more than 2 parameters exist, all but the last parameter are considered to comprise the ntake vector. Take(ntake,object);

If the 1st argument is a scalar then the 2nd argument must be a vector from which the elements will be taken.

v
1 2 3 4 5
/* take the 1st 2 elements of vector v */
take(2,v)
1 2
/* take the last 2 elements of vector v */
take(-2,v)
4 5

Alternately, if the 1st argument is a vector (it must be a vector of length 2) then the 2nd argument must be a matrix from which the elements will be taken.

m
1 2 3
4 5 6
/* take 2 rows and 1st column from matrix m */
take(2 1,m)
1 4
/* take 2 rows and last column from matrix m */
take(2 -1,m)
3 6
/* take 1st row and 3 columns from matrix m */
take(1 3,m)
1 2 3

Times

This function calculates the value of its argument(s) multiplied together, and returns the result as a scalar or vector. If the arguments are scalar (simple integers or real numbers) then the result of all of the arguments is calculated. If the 1st argument is a matrix or a vector, then the result of the contents of the 1st argument only is calculated.

There are 1 or more parameters:
when the parameters are scalar values
object - an object which is to be multiplied by the other objects.
object n- an object which is to be multiplied by the other objects.
when the 1st parameter is a vector or matrix
object - the object for which the result is to be calculated.
dimension- an optional integer specifying the dimension over which the result is to be calculated.
0 = over the entire vector or matrix (the default),
1 = over the rows of the matrix (or over the entire vector),
2 = over the columns of the matrix (or over the entire vector).

times(object,object2[,objectn]);
times(object[,dimension]);

/* times product of the series */
times(1,2,3,4,5,6)
720

v=seq(1,6)
v
1 2 3 4 5 6
/* times product of vector v */
times(v)
720

m=shape(2 3,seq(1,6)) /* create a 2 x 3 matrix */
m
1 2 3
4 5 6
/* times product of the entire matrix */
times(m,0)
720
/* times product over the rows of matrix */
times(m,1)
4 10 18
/* times product over the columns of matrix */
times(m,2)
6 120

Transpose

This function transposes a matrix (switches the rows for the columns) and returns the result as a matrix.

There are 2 parameters:
object - the matrix to be transposed.
dimension - an optional integer (1-3) specifying the dimension which is not transposed if the object is 3 dimensional.

transpose(matrix);

m
1 2 3
4 5 6
transpose(m)
1 4
2 5
3 6

Tridiagonal_QL

This function computes the eigenvalues and eigenvectors of a real tridiagonal symmetric matrix.

The algorithm for this function is from Chapter 11.3 Eigenvalues and Eigenvectors of a Tridiagonal Matrix pages 374-381 of Numerical Receipes in C The Art of Scientific Computing. 1988. W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling. Cambridge University Press Cambridge, 735 p.
There are 3 parameters:
diagonal vector - the diagonal elements of the tridiagonal matrix for which the eigenvalues and eigenvectors are to be calculated. On output this vector contains the eigenvalues (size n).
subdiagonal vector - the vector containing the subdiagonal elements of the tridiagonal matrix (size n).
orthogonal matrix - the matrix to contain the eigenvectors (size n x n). If the eigenvectors of a tridiagonal matrix is desired, an identity matrix is supplied. If the eigenvectors of an orthogonal matrix created by the Householder() function is required, then the output matrix from the Householder() function is used. In either case, the ith column of this matrix contains the normalized eigenvector corresponding to the ith element of the diagonal vector.
The number of rows and columns of the matrices must be equal, and equal to the number of elements of the vectors.

Tridiagonal_QL(dvector,sdvector,orthogonal matrix);

m = shape(3 3,2 3 6 4 8 32 1 5 5.);
dv = odv = shape(3,0.0);
Householder(m,dv,odv);
m
0.980581 3.000000 0
4.000000 1.000000 32
0.000000 10.099020 1
dv
2 8 5
odv
0 4 -5.09902

Tridiagonal_QL(dv,odv,m);
dv
-1.19801 3.38854 12.8095
odv
0 4 -5.09902
m
-0.694742 0.655544 0.295966
0.555448 0.227562 0.799809
0.456959 0.720054 -0.522217


Julian

This function converts year, month, and day into a Julian date. NOTE: If you supply a 2 digit year value you are referring to the 1st century AD. You should supply 4 digit year values to allow for correct date calculations associated with dates spanning the millennium.

There are 3 parameters:
year - the scalar or vector of year values.
month - the scalar or vector of corresponding month values.
day - the scalar or vector of corresponding day values. or
an n x 3 matrix containing year month and day as the 1st 3 columns.

jul = Julian(year,month,day);

year = 1994 1995 1996
month = 1 1 1
day = 31 31 31
/* determine the julian dates for the given year,month,day values */
julian(year,month,day)
2449384 2449749 2450114

YMD

This function converts Julian Date into year, month, and day.

There is 1 parameter:
juliandate - the scalar or vector of year values.

ymd = YMD(juliandate);

year = 1994 1995 1996
month = 1 1 1
day = 31 31 31
/* determine the julian dates for the given year,month,day values */
x = julian(year,month,day)
/* convert from julian date to year, month, day */
YMD(x);
1994 1 31
1995 1 31
1996 1 31


AconIcon ACON       Home/Topics   |   Commands



Last Modified : 2005-11-14