1
$\begingroup$

This question already has an answer here:

Description

I have using Mathematica to solve all kinds of problems that encountered in my study. However, the function that I define to solve specific problem is not always right, in other words, it will occur all kinds of unexpected error. So I think I can debug it by looking over middle values of function's running process.

Example

I write a functionGridLineDatato solve my data.(the ultimate right edition as below, but I takes much time to debug it many times)

GridLineData[data_List, Δ_] := Block[
 {sortData, length, OriginData, deleteData},
 sortData = DeleteDuplicates@(Sort@data);
 length = Length@sortData;
 OriginData =
  Append[
   Prepend[sortData, 2 sortData[[1]] - sortData[[2]]],
   2 sortData[[length]] - sortData[[length - 1]]];
 deleteData =
   List /@ (First /@ Select[
    Thread[List[Range[length - 1], Differences@sortData]],
    #[[2]] < Δ &]) + 1;
 Delete[Total@#/2 & /@ Partition[OriginData, 2, 1], deleteData]
]

Using this fuction

DebugData =
{{0.349661, 0.380297}, {0.858156, 0.8442}, {0.906745, 0.171579},
 {0.0847783, 0.277227}, {0.198453, 0.40206}, {0.941614, 0.347187}};
GridLineData[DebugData[[All, 1]], .1]
(*==>{0.027941, 0.141616, 0.274057, 0.603909, 0.959049}*)

Question

When I debug this function, I always want to know its middle value in running process. I use a tedious method that output the middle values as the return result of this function. So the function can be rewritten as below:

  GridLineData[data_List, Δ_] := Block[
 {sortData, length, OriginData, deleteData},
 sortData = DeleteDuplicates@(Sort@data);
 length = Length@sortData;
 OriginData =
  Append[
   Prepend[sortData, 2 sortData[[1]] - sortData[[2]]],
   2 sortData[[length]] - sortData[[length - 1]]];
 deleteData =
   List /@ (First /@ Select[
    Thread[List[Range[length - 1], Differences@sortData]],
    #[[2]] < Δ &]) + 1;
  {sortData,OriginData,deleteData,
   Delete[Total@#/2 & /@ Partition[OriginData, 2, 1], deleteData]}
]

Rerun the function that give middle values

GridLineData[DebugData[[All, 1]], .1]
(**==>{{0.0847783, 0.198453, 0.349661, 0.858156, 0.906745, 
       0.941614}, {-0.0288964, 0.0847783, 0.198453, 0.349661, 0.858156, 
       0.906745, 0.941614, 0.976483}, {{5}, {6}}, {0.027941, 0.141616, 
       0.274057, 0.603909, 0.959049}})

I know the Mathematica has the inner function Trace, but it gives complex transform process. So my question is How to extract middle value to debug in Mathematica enviroment.

$\endgroup$

marked as duplicate by Jens, ciao, Sjoerd C. de Vries, m_goldberg, Verbeia Jul 5 '14 at 22:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Have you tried the varieties of Monitor, such as Monitor, StepMonitor, and EvaluationMonitor? Maybe those will help you. $\endgroup$ – seismatica Jul 5 '14 at 1:52
  • $\begingroup$ @seismatica,No,I've never heard of Monitor, StepMonitor, and EvaluationMonitor $\endgroup$ – lavender Jul 5 '14 at 1:54
  • $\begingroup$ Time to try em out ;) $\endgroup$ – seismatica Jul 5 '14 at 2:00
  • $\begingroup$ @seismatica,+1,OK,I will have a try!Thanks $\endgroup$ – lavender Jul 5 '14 at 2:08
  • $\begingroup$ Also see Reap and Sow $\endgroup$ – Apple Jul 5 '14 at 3:15

Browse other questions tagged or ask your own question.