calc: Matrix Mode
7.4.6 Matrix and Scalar Modes
-----------------------------
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. Simply typing ‘C-u m v’ will get you a square Matrix mode, in
which matrices of unknown size are assumed to be square matrices of
unspecified size.
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. 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 (
Selecting Subformulas). 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.