Calc sometimes makes assumptions during algebraic manipulation that are awkward or incorrect when vectors and matrices are involved. Calc has two modes, matrix mode and scalar mode, which modify its behavior around vectors in useful ways.
Press m v (calc-matrix-mode
) once to enter matrix mode.
In this mode, all objects are assumed to be matrices unless provably
otherwise. One major effect is that Calc will no longer consider
multiplication to be commutative. (Recall that in matrix arithmetic,
`A*B' is not the same as `B*A'.) This assumption affects
rewrite rules and algebraic simplification. Another effect of this
mode is that calculations that would normally produce constants like
0 and 1 (e.g., a - a and a / a, respectively) will now
produce function calls that represent "generic" zero or identity
matrices: `idn(0)', `idn(1)'. The idn
function
`idn(a,n)' returns a times an nxn
identity matrix; if n is omitted, it doesn't know what
dimension to use and so the idn
call remains in symbolic
form. However, if this generic identity matrix is later combined
with a matrix whose size is known, it will be converted into
a true identity matrix of the appropriate size. On the other hand,
if it is combined with a scalar (as in `idn(1) + 2'), Calc
will assume it really was a scalar after all and produce, e.g., 3.
Press m v a second time to get scalar mode. Here, objects are assumed not to be vectors or matrices unless provably so. For example, normally adding a variable to a vector, as in `[x, y, z] + a', will leave the sum in symbolic form because as far as Calc knows, `a' could represent either a number or another 3-vector. In scalar mode, `a' is assumed to be a non-vector, and the addition is evaluated to `[x+a, y+a, z+a]'.
Press m v a third time to return to the normal mode of operation.
If you press m v with a numeric prefix argument n, you get a special "dimensioned matrix mode" in which matrices of unknown size are assumed to be nxn square matrices. Then, the function call `idn(1)' will expand into an actual matrix rather than representing a "generic" matrix.
Of course these modes are approximations to the true state of affairs, which is probably that some quantities will be matrices and others will be scalars. One solution is to "declare" certain variables or functions to be scalar-valued. See section Declarations, to see how to make declarations in Calc.
There is nothing stopping you from declaring a variable to be scalar and then storing a matrix in it; however, if you do, the results you get from Calc may not be valid. Suppose you let Calc get the result `[x+a, y+a, z+a]' shown above, and then stored `[1, 2, 3]' in `a'. The result would not be the same as for `[x, y, z] + [1, 2, 3]', but that's because you have broken your earlier promise to Calc that `a' would be scalar.
Another way to mix scalars and matrices is to use selections (see section Selecting Sub-Formulas). Use matrix mode when operating on your formula normally; then, to apply scalar mode to a certain part of the formula without affecting the rest just select that part, change into scalar mode and press = to resimplify the part under this mode, then change back to matrix mode before deselecting.