Any calculation Mathcad can perform with single values, it can also perform with vectors or matrices of values. There are two ways to do this:
• By iterating over each element using range variables as described in Chapter 11, “Range Variables.”
• By using the “vectorize” operator described in this chapter.
Mathcad’s vectorize operator allows it to perform the same operation efficiently on each element of a vector or matrix.
Mathematical notation often shows repeated operations with subscripts. For example, to define a matrix P by multiplying corresponding elements of the matrices M and N, you would write
Note that this is not matrix multiplication, but multiplication element by element. It is possible to perform this operation in Mathcad using subscripts, as described in Chapter 11, “Range Variables,” but it is much faster to perform exactly the same operation with a vectorized equation.
How to apply the vectorize operator to an expression
Here’s how to apply the vectorize operator to an expression like M· N :
• Select the whole expression by clicking inside and pressing [Space] until the right-hand side is held between the editing lines
• Press [Ctrl]- to apply the vectorize operator. Mathcad puts an arrow over the top of the selected expression.
How the vectorize operator changes the meaning of an expression
The vectorize operator changes the meaning of the operators and functions to which it applies. The vectorize operator tells Mathcad to apply the operators and functions with their scalar meanings, element by element.
Here are some examples of how the vectorize operator changes the meaning of expressions with vectors and matrices:
• Ifv is a vector, sin(v) is an illegal expression. But if you apply the vectorize operator Mathcad applies the sine function to every element in v. The result is a new vector whose elements are the sines of the elements in v.
• If M is a matrix, JM is an illegal expression. But if you apply the vectorize operator, Mathcad takes the square root of every element of M and places the results in a new matrix.
• If v and w are vectors, then v . w means the dot product of v and w. But if you apply the vectorize operator, the result is a new vector whose ith element is obtained by multiplying vi and wi’ This is not the same as the dot product.
These properties of the vectorize operator let you use scalar operators and functions with array operands and arguments. In this User’s Guide, this is referred to as “vectorizing” an expression. For example, suppose you want to apply the quadratic formula to three vectors containing coefficients a, b, and c. Figure 10-18 shows how to do this when a, b, and c are just scalars. Figure 10-19 shows how to do the same thing when a, b, and c are vectors.
The vectorize operator appears as an arrow above the quadratic formula in Figure 10-19. Its use is essential in this calculation. Without it, Mathcad would interpret a . c as a vector dot product and also flag the square root of a vector as illegal. But with the vectorize operator, both a . c and the square root are performed element by element.
Here are the properties of the vectorize operator:
•The vectorize operator changes the meaning of the other operators and functions to which it applies. It does not change the meaning of the actual names and numbers. If you apply the vectorize operator to a single name, it simply draws an arrow over the name. You can use this arrow just for cosmetic purposes.
• Since operations between two arrays are performed element by element, all arrays under a vectorize operator must be the same size. Operations between an array and a scalar are performed by applying the scalar to each element of the array. For example, if v is a vector and n is a scalar, applying the vectorize operator to vn returns a vector whose elements are the nth powers of the elements of v.
• You cannot use any of the following matrix operations under a vectorize operator: dot product, matrix multiplication, matrix powers, matrix inverse, determinant, or magnitude of a vector. The vectorize operator will transform these operations into element-by-element scalar multiplication, exponentiation, or absolute value, as appropriate.
• The vectorize operator has no effect on operators and functions that require vectors or matrices: transpose, cross product, sum of vector elements, and functions like mean. These operators and functions have no scalar meaning.
•The vectorize operator applies only to the final, scalar arguments of interp and linterp. The other arguments are unaffected. See “Interpolation functions” in Chapter 14, “Statistical Functions.”