Use the functions and operators in this section to extract, concatenate, delete, insert, reverse, select, and sort elements of vectors.  They can also be used to perform these same operations on the characters of a string or the digits of an integer (see the String Processing FunctionsString_Processing_Functions).


Many of these functions and operators have arguments or operands that specify the index of a vector element.  If an index is a positive integer n, it specifies the nth element from the left end of the vector.  If an index is a negative integer n, it refers to the -nth element from the right end of the vector.


The dimension of a vector is the number of elements of the vector.  Use DIM to compute the dimension of a vector and the number of elements of a set.  Since a matrix is just a vector of vectors, the dimension of a matrix is the number of rows of the matrix.  For example,

DIM([[a, b, c], [1, 2, 3]])

simplifies to 2 since the matrix has two rows, and

DIM([[a, b, c], [1, 2, 3]] SUB 1)

simplifies to 3 since the first row of the matrix has three columns.


Use the SUB infix operator to extract the elements of a vector or matrix.  If v is a vector, v SUB n returns the nth element of v.  As an alternative to typing SUB on the expression entry line, it can be entered by clicking on the on the math symbol toolbar.  In the algebra window worksheet, subscripts are displayed using standard subscript notation.  For example,

[a, b, c, d] SUB 2

displays as

[a, b, c, d]  
            2 

and simplifies to b.  Use SUB twice to extract the elements of a matrix.  For example,

[a, b, c; d, e, f; g, h, i] SUB 2 SUB 3

displays as

a  b  c    
           
d  e  f    
           
g  h  i    
           2,3 

and simplifies to f.  If the right operand of SUB is a vector of indices, a vector of the specified elements is returned.  For example,

[a, b, c, d] SUB [4, 2]

simplifies to [d, b], and

[a, b, c; d, e, f; g, h, i] SUB [3, 1]

simplifies to

g  h  i
       
a  b  c


The SUB operator can be used in conjunction with the := assignment operator (see the Author > Variable Value command2SGW02B) to change designated elements of vectors assigned to variables.  If the value of the variable A is a vector and a subscripted assignment of the form

A SUB n := u

is simplified, the nth element of A is replaced by u.  If a subscripted assignment of the form

A SUB [n1, n2, ..., nm] := [u1, u2, ..., um]

is simplified, the elements n1, n2, ..., nm of A are replaced by u1, u2, ..., um, respectively.  Note that unlike regular assignments, subscripted assignments must be simplified in order to take effect.


Since matrices are stored as vectors of vectors, subscripted assignments can also be used to change designated elements of matrices assigned to variables.  If the value of the variable A is a matrix and a subscripted assignment of the form

A SUB n SUB m := u

is simplified, the mth element on the nth row of A is replaced by u.


The SUB operator can similarly be used in conjunction with the update operators :+, :-, :*, and :/ (for details see Procedural ProgrammingProcedural_Programming) to update the elements of vectors and matrices.


Use the SUB SUB operator to extract the columns of a matrix.  If A is a matrix, A SUB SUB n returns the nth column of A as a vector.  For example, A↓↓3 returns the 3rd column of A; whereas A3 returns the 3rd row of A.  This scheme naturally extends to higher dimensional matrices.  For example, three SUB operators extract a plane from a 3-dimensional matrix.


Finally, if the right operand of SUB SUB is a vector of indices, a matrix of the specified columns is returned.  This makes it easy to extract arbitrary sub-matrices of a matrix.  For example,

A [2, 3] ↓↓ [3, ..., 7]

returns the sub-matrix consisting of rows 2 and 3, and columns 3 through 7 of A.


The ELEMENT function is equivalent to the SUB operator but is entered using functional notation.  ELEMENT(v, n) simplifies to the nth element of the vector v.  For example,

ELEMENT([a, b, c, d], 2)

simplifies to b.  ELEMENT(m, j, k) simplifies to the element in row j and column k of the matrix m.  For example,

ELEMENT([a, b, c; d, e, f; g, h, i], 2, 3)

simplifies to f.  Since the SUB operator is displayed using standard subscript notation, we recommend using it rather than the ELEMENT function.


Use the APPEND function to concatenate two or more vectors.  APPEND(v1, v2, ..., vn) simplifies to a vector consisting of the elements of the vectors v1 through vn.  For example,

APPEND([a, b], [c, d], [e, f])

simplifies to [a, b, c, d, e, f].  If A is a matrix, APPEND(A) simplifies to a vector consisting of the elements of A.  For example,

APPEND([[a, b], [c, d], [e, f]])

also simplifies to [a, b, c, d, e, f].  To append the columns of two matrices, use the function APPEND_COLUMNS defined in the VECTOR.MTHI._DEZ utility file.


The functions FIRST, REST, and ADJOIN can be used to process the elements of vectors analogously to the way lists are processed in the LISP programming language (see Procedural ProgrammingProcedural_Programming).  FIRST(v) simplifies to the first element of the vector v.  REST(v) simplifies to a vector of all but the first element of v.  If n is a positive integer, REST(v,n) simplifies to a vector of all but the first n elements of v.  If n is a negative integer, REST(v,n) simplifies to a vector of all but the last n elements of v.  ADJOIN(u,v) simplifies to a vector whose first element is u and whose remaining elements are the same as v.  For example,

ADJOIN(FIRST([a, b, c]), REST([d, e, f]))

simplifies to [a, e, f].


Use the DELETE function to delete one or more elements from a vector.  If v is a vector and n is an integer, DELETE(v, n) simplifies to a copy of v but with the nth element deleted.  For example,

DELETE([a, b, c, d], 2)

simplifies to [a, c, d].  Note that if v is a matrix, the nth row of v is deleted.  If w is a collection (i.e. a vector or set) of integers, DELETE(v, w) simplifies to a copy of v but with the elements specified in w deleted.  For example,

DELETE([a, b, c, d], [2, -1])

simplifies to [a, c].


Use the INSERT function to insert an element into a vector.  INSERT(u, v, n) simplifies to a copy of vector v but with expression u inserted before the nth element.  For example,

INSERT(d, [a, b, c], 2)

simplifies to [a, d, b, c].  n defaults to 1.  If n is one more than the dimension of the vector, the new element is added to the end of the vector.


Use the REPLACE function to replace an element in a vector.  REPLACE(u, v, n) simplifies to a copy of vector v but with expression u replacing the nth element.  For example,

REPLACE(d, [a, b, c], 2)

simplifies to [a, d, c].  n defaults to 1.


Use the REVERSE function to reverse the elements of a vector.  REVERSE(v) simplifies to a copy of vector v but with the elements reversed.  For example,

REVERSE([a, b, c])

simplifies to [c, b, a].


Use the POSITION function to find the position (i.e. index) of an element in a vector.  If e is an expression that occurs in the vector v, POSITION(e, v) simplifies to the position of the first occurrence of e.  If e does not occur in v, POSITION(e, v) simplifies to false.  For example,

POSITION(c, [a, b, c, d, c, e])

simplifies to 3, and

POSITION(f, [a, b, c, d, c, e])

simplifies to false.  If n is an integer and e is an expression that occurs at or after the nth element of vector v, POSITION(e, v, n) simplifies to the position of the first such occurrence of e.  For example,

POSITION(c, [a, b, c, d, c, e], 4)

simplifies to 5.


Use the SELECT function to find those elements of a vector that satisfy some test criteria.  SELECT(u, k, v) simplifies to a vector of those elements k of v for which u(k) is true.  For example,

SELECT(PRIME(k), k, [3, 5, 7, 9, 11])

simplifies to [3, 5, 7, 11].  SELECT can also be used to test the elements of a sequence.  SELECT(u, k, m, n, s) simplifies to a vector of those k for which u(k) is true with the variable k stepping from m through n in steps of size s.  s defaults to 1.  For example,

SELECT(PRIME(k), k, 1, 100)

simplifies to a vector of all the prime numbers less than 100.


Use the SORT function to sort the elements of a vector or set, and return the result as a vector.  Given a vector or set of numbers, SORT returns a vector of the numbers sorted in ascending numerical order of their real parts with ties broken on their imaginary parts.  Variable names are sorted lexicographically after numbers.  For example,

SORT([3, 2, y, pi, #e, SQRT(2), z, x, 1, 0, 2, 0, 0, -3])

simplifies to

[-3, 0, 0, 0, 1, 2, 2, 2, ê, 3, π, x, y, z]


Given a vector or set of vectors, the vectors are sorted by comparing their first elements with ties being broken by comparing their second elements, etc.  For example,

SORT({­[2, 3], [3, 1], [2, 1], [1, 5]})

simplifies to

1  5
     
2  1
     
2  3
     
3  1


If, in addition to a vector or set, SORT is given a predicate and two variables, the ordering is based on the truth-value returned by the predicate.  For example,

SORT([-5, -2, 0, 3, 4], ABS(x)<ABS(y), x, y)

simplifies to

[0, –2, 3, 4, -5]


Other Vectors and MatricesVectors_and_Matrices topics

Created with the Personal Edition of HelpNDoc: Easily create Qt Help files