9.2 Building Vectors

Vectors and matrices can be added, subtracted, multiplied, and divided; see Basic Arithmetic.

The | (calc-concat) [vconcat] command “concatenates” two vectors into one. For example, after [ 1 , 2 ] [ 3 , 4 ] |, the stack will contain the single vector ‘[1, 2, 3, 4]’. If the arguments are matrices, the rows of the first matrix are concatenated with the rows of the second. (In other words, two matrices are just two vectors of row-vectors as far as | is concerned.)

If either argument to | is a scalar (a non-vector), it is treated like a one-element vector for purposes of concatenation: 1 [ 2 , 3 ] | produces the vector ‘[1, 2, 3]’. Likewise, if one argument is a matrix and the other is a plain vector, the vector is treated as a one-row matrix.

The H | (calc-append) [append] command concatenates two vectors without any special cases. Both inputs must be vectors. Whether or not they are matrices is not taken into account. If either argument is a scalar, the append function is left in symbolic form. See also cons and rcons below.

The I | and H I | commands are similar, but they use their two stack arguments in the opposite order. Thus I | is equivalent to TAB |, but possibly more convenient and also a bit faster.

The v d (calc-diag) [diag] function builds a diagonal square matrix. The optional numeric prefix gives the number of rows and columns in the matrix. If the value at the top of the stack is a vector, the elements of the vector are used as the diagonal elements; the prefix, if specified, must match the size of the vector. If the value on the stack is a scalar, it is used for each element on the diagonal, and the prefix argument is required.

To build a constant square matrix, e.g., a 3x3 matrix filled with ones, use 0 M-3 v d 1 +, i.e., build a zero matrix first and then add a constant value to that matrix. (Another alternative would be to use v b and v a; see below.)

The v i (calc-ident) [idn] function builds an identity matrix of the specified size. It is a convenient form of v d where the diagonal element is always one. If no prefix argument is given, this command prompts for one.

In algebraic notation, ‘idn(a,n)’ acts much like ‘diag(a,n)’, except that ‘a’ is required to be a scalar (non-vector) quantity. If ‘n’ is omitted, ‘idn(a)’ represents ‘a’ times an identity matrix of unknown size. Calc can operate algebraically on such generic identity matrices, and if one is combined with a matrix whose size is known, it is converted automatically to an identity matrix of a suitable matching size. The v i command with an argument of zero creates a generic identity matrix, ‘idn(1)’. Note that in dimensioned Matrix mode (see Matrix and Scalar Modes), generic identity matrices are immediately expanded to the current default dimensions.

The v x (calc-index) [index] function builds a vector of consecutive integers from 1 to n, where n is the numeric prefix argument. If you do not provide a prefix argument, you will be prompted to enter a suitable number. If n is negative, the result is a vector of negative integers from n to -1.

With a prefix argument of just C-u, the v x command takes three values from the stack: n, start, and incr (with incr at top-of-stack). Counting starts at start and increases by incr for successive vector elements. If start or n is in floating-point format, the resulting vector elements will also be floats. Note that start and incr may in fact be any kind of numbers or formulas.

When start and incr are specified, a negative n has a different interpretation: It causes a geometric instead of arithmetic sequence to be generated. For example, ‘index(-3, a, b)’ produces ‘[a, a b, a b^2]’. If you omit incr in the algebraic form, ‘index(n, start)’, the default value for incr is one for positive n or two for negative n.

The v b (calc-build-vector) [cvec] function builds a vector of n copies of the value on the top of the stack, where n is the numeric prefix argument. In algebraic formulas, ‘cvec(x,n,m)’ can also be used to build an n-by-m matrix of copies of x. (Interactively, just use v b twice: once to build a row, then again to build a matrix of copies of that row.)

The v h (calc-head) [head] function returns the first element of a vector. The I v h (calc-tail) [tail] function returns the vector with its first element removed. In both cases, the argument must be a non-empty vector.

The v k (calc-cons) [cons] function takes a value h and a vector t from the stack, and produces the vector whose head is h and whose tail is t. This is similar to |, except if h is itself a vector, | will concatenate the two vectors whereas cons will insert h at the front of the vector t.

Each of these three functions also accepts the Hyperbolic flag [rhead, rtail, rcons] in which case t instead represents the last single element of the vector, with h representing the remainder of the vector. Thus the vector ‘[a, b, c, d] = cons(a, [b, c, d]) = rcons([a, b, c], d)’. Also, ‘head([a, b, c, d]) = a’, ‘tail([a, b, c, d]) = [b, c, d]’, ‘rhead([a, b, c, d]) = [a, b, c]’, and ‘rtail([a, b, c, d]) = d’.