Computes matrix powers, with optional application-specific callbacks. Accommodates (external) parallel multiplication mechanisms.
desired power. If NULL, it is expected that the
initialization portion of the user's callback function will set
if TRUE, saves time by first squaring
function to generate multiplication commands, in
quoted string form. For the ordinary R
function to make a deep copy of a matrix.
application-specific callback function.
Multiplication is iterated until the desired power
reached, with these exceptions: (a) If
squaring is TRUE,
k may be exceeded. (b) The callback function can set
ev, halting iterations; this is useful, for instance, if some
convergence criterion has been reached.
One key advantage of using
matpow rather than direct iteration
is that parallel computation can be accommodated, by specifying
genmulcmd. (The word "accommodated" here means the user must
have available a mechanism for parallel computation;
itself contains no parallel code.)
For instance, if one is using GPU with
gputools, one sets
genmulcmd.gputools, which calls
gpuMatMult() instead of the usual
%*%. So, one can
switch from serial to parallel by merely changing this one argument.
genmulcmd is not specified, the code attempts to sense the
proper function by inspecting
class(m), in the cases of
Of course, if the user's R is configured to use a parallel BLAS, such
as OpenBLAS, this is automatically handled via the ordinary R
Another important advantage of
matpow is the ability to write
a callback function, which enables much flexibility. The callback,
if present, is called by
matpow after each iteration, allowing
application-specific operations to be applied. For instance,
cgraph determines the connectivity of a graph, by
checking whether the current power has all of its entries nonzero.
The call form is
the R environment described above, and
init must be set to
TRUE on the first call, and FALSE afterward.
Since some types of matrix multiplication do not allow the product to
be in the same physical location as either multiplicand, a
"red and black" approach is taken to the iteration process: Storage
space for powers in
ev alternatives between
prod2, for odd-numbered and even-numbered iterations,
An R environment
ev, including the following components:
matrix, the final power.
boolean value, indicating whether the iterations were stopped before the final power was to be computed.
number of the last iteration performed.
Application-specific data, maintained by the callback function, can be stored here as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15