calc: Building Vectors
10.2 Building Vectors
=====================
Vectors and matrices can be added, subtracted, multiplied, and divided;
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 (
Matrix Mode), 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’.