1
$\begingroup$

I wrote some code where my output function is defined in terms of various functions, themselves defined in terms of various functions, schematically like $f(x) = g(h(x))$, $h(x) = k(m(x))$, etc., as you do.

For a given x, the evaluation of each of these functions might take a while, and I would like to keep an eye on the stage my computation is at, e.g. (temporarily) printing "evaluating h", when $h(k)$ starts being computed, similarly to how Monitor[] does this for e.g. the iterator in a table.

I am looking for tips on how to do this nicely.

Currently I am defining e.g.

h[x_] := h[x_] = { PrintTemporary["evaluating h"] , (actual expression for the function) }[[2]]

which does the job. The construction with the list seems rather roundabout, but I am not sure how else to make sure the print happens every time the function is called (with a distinct argument). Is there a better, optimal, or even built-in option for this?

$\endgroup$
5
  • 2
    $\begingroup$ Perhaps Progress Indicator for NestList is what you are looking for $\endgroup$ Aug 14 '20 at 13:07
  • 2
    $\begingroup$ Don't use List brackets, use parentheses to enclose the compound expression: h[x_] := h[x] = (PrintTemporary[StringForm["Evaluating h[``]", x]]; calculation ); $\endgroup$
    – Bob Hanlon
    Aug 14 '20 at 13:26
  • $\begingroup$ Thanks a lot @BobHanlon! That is the appropriate way to implement what I tried to do above. I had a feeling something was off, but could not remember for the life of me how to do it.. $\endgroup$
    – Stijn
    Aug 14 '20 at 13:44
  • $\begingroup$ Thanks also @UlrichNeumann. I think the NestList progress indicator would have been very useful if my nesting/functions were indexed, but that is not the structure I am currently dealing with. $\endgroup$
    – Stijn
    Aug 14 '20 at 13:47
  • $\begingroup$ I would of course still be interested in solutions that avoid defining my individual functions to signal themselves.. $\endgroup$
    – Stijn
    Aug 14 '20 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.