1
$\begingroup$

I know there is a Timing approach, but I am wondering if there is a Mathematica command that will count the number of flops (floating point operations). For example, if I do:

A = RandomReal[9, {10, 10}]

How can I get a count of the number of flops required to compute:

MatrixForm[A = RandomReal[9, {10, 10}]]

Then, I created a function to compute the cofactor.

Cofactor[m_List?MatrixQ, {i_Integer, j_Integer}] := (-1)^(i + j) Det[
   Drop[Transpose[Drop[Transpose[m], {j}]], {i}]]

Now, I can create the cofactor matrix for A and take its transpose which gives me the adjugate matrix.

MatrixForm[
 adjA = Transpose[Table[Cofactor[A, {i, j}], {i, 1, 10}, {j, 1, 10}]]]

And I can compute the inverse of matrix A in this manner.

MatrixForm[adjA/Det[A]]

Is there a way to count the number of floating point operations in both methods?

$\endgroup$
2
  • 3
    $\begingroup$ Due to how BLAS and LAPACK (the linear algebra stuff used behind the scenes) are structured, flop-counting is often an inaccurate measure of the effort done. See also the remarks here. $\endgroup$ Jun 26, 2016 at 20:00
  • $\begingroup$ Some people use Mathematica to prototype software that they subsequently reimplement in lower level languages. In this use case, having a better idea of computational complexity than given by, for example, Mathematica Timing can be very helpful. $\endgroup$
    – mikado
    Jun 27, 2016 at 18:57

1 Answer 1

1
$\begingroup$

We can put a wrapper q around the numeric values in our matrices.

multcount = 0; addcount = 0;
q[a_] q[b_] ^:= (multcount++; q[a b])
q[a_] + q[b_] ^:= (addcount++; q[a + b])
a_ q[b_] ^:= (multcount++; q[a b])

Defining

A = RandomReal[9, {10, 10}];
qA = Map[q, A, {2}];

We can evaluate

adjqA = Transpose[Table[Cofactor[qA, {i, j}], {i, 1, 10}, {j, 1, 10}]];

and then examine the values of multcount and addcount.

This looks as if it would extend to a range of operations .

As @JM comments, this may not be an accurate reflection of the floating point operations that Mathematica would use for the computation, but is an indication of the number of floating point operations that a simple implementation could use.

$\endgroup$
1
  • 1
    $\begingroup$ This of course can be misleading, as Mathematica is known to employ different methods for inexact inputs and symbolic inputs. $\endgroup$ Jun 26, 2016 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.