Next: Symbolic Lisp Functions, Previous: Computational Lisp Functions, Up: Internals

The functions described here perform various operations on vectors and matrices.

— Function: **math-concat**` x y`

Do a vector concatenation; this operation is written ‘

’ in a symbolic formula. See Building Vectors.x|y

— Function: **vec-length**` v`

Return the length of vector

v. Ifvis not a vector, the result is zero. Ifvis a matrix, this returns the number of rows in the matrix.

— Function: **mat-dimens**` m`

Determine the dimensions of vector or matrix

m. Ifmis not a vector, the result is an empty list. Ifmis a plain vector but not a matrix, the result is a one-element list containing the length of the vector. Ifmis a matrix withrrows andccolumns, the result is the list ‘(’. Higher-order tensors produce lists of more than two dimensions. Note that the object ‘rc)[[1, 2, 3], [4, 5]]’ is a vector of vectors not all the same size, and is treated by this and other Calc routines as a plain vector of two elements.

— Function: **dimension-error**

Abort the current function with a message of “Dimension error.” The Calculator will leave the function being evaluated in symbolic form; this is really just a special case of

`reject-arg`

.

— Function: **build-vector**` args`

Return a Calc vector with

argsas elements. For example, ‘(build-vector 1 2 3)’ returns the Calc vector ‘[1, 2, 3]’, stored internally as the list ‘(vec 1 2 3)’.

— Function: **make-vec**` obj dims`

Return a Calc vector or matrix all of whose elements are equal to

obj. For example, ‘(make-vec 27 3 4)’ returns a 3x4 matrix filled with 27's.

— Function: **row-matrix**` v`

If

vis a plain vector, convert it into a row matrix, i.e., a matrix whose single row isv. Ifvis already a matrix, leave it alone.

— Function: **col-matrix**` v`

If

vis a plain vector, convert it into a column matrix, i.e., a matrix with each element ofvas a separate row. Ifvis already a matrix, leave it alone.

— Function: **map-vec**` f v`

Map the Lisp function

fover the Calc vectorv. For example, ‘(map-vec 'math-floor v)’ returns a vector of the floored components of vectorv.

— Function: **map-vec-2**` f a b`

Map the Lisp function

fover the two vectorsaandb. Ifaandbare vectors of equal length, the result is a vector of the results of calling ‘(’ for each pair of elementsfaibi)aiandbi. If eitheraorbis a scalar, it is matched with each value of the other vector. For example, ‘(map-vec-2 'math-add v 1)’ returns the vectorvwith each element increased by one. Note that using ‘'+’ would not work here, since`defmath`

does not expand function names everywhere, just where they are in the function position of a Lisp expression.

— Function: **reduce-vec**` f v`

Reduce the function

fover the vectorv. For example, ifvis ‘[10, 20, 30, 40]’, this calls ‘(f (f (f 10 20) 30) 40)’. Ifvis a matrix, this reduces over the rows ofv.

— Function: **reduce-cols**` f m`

Reduce the function

fover the columns of matrixm. For example, ifmis ‘[[1, 2], [3, 4], [5, 6]]’, the result is a vector of the two elements ‘(f (f 1 3) 5)’ and ‘(f (f 2 4) 6)’.

— Function: **mat-row**` m n`

Return the

nth row of matrixm. This is equivalent to ‘(elt m n)’. For a slower but safer version, use`mrow`

. (See Extracting Elements.)

— Function: **mat-col**` m n`

Return the

nth column of matrixm, in the form of a vector. The arguments are not checked for correctness.

— Function: **mat-less-row**` m n`

Return a copy of matrix

mwith itsnth row deleted. The numbernmust be in range from 1 to the number of rows inm.

— Function: **flatten-vector**` v`

Flatten nested vector

vinto a vector of scalars. For example, ifvis ‘[[1, 2, 3], [4, 5]]’ the result is ‘[1, 2, 3, 4, 5]’.

— Function: **copy-matrix**` m`

If

mis a matrix, return a copy ofm. This maps`copy-sequence`

over the rows ofm; in Lisp terms, each element of the result matrix will be`eq`

to the corresponding element ofm, but none of the`cons`

cells that make up the structure of the matrix will be`eq`

. Ifmis a plain vector, this is the same as`copy-sequence`

.

— Function: **swap-rows**` m r1 r2`

Exchange rows

r1andr2of matrixmin-place. In other words, unlike most of the other functions described here, this function changesmitself rather than building up a new result matrix. The return value ism, i.e., ‘(eq (swap-rows m 1 2) m)’ is true, with the side effect of exchanging the first two rows ofm.