Mathematica Sow the phase and Reap the sin -- why?

  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Phases Sine
AI Thread Summary
The discussion revolves around a Mathematica code snippet that utilizes the Reap function to collect values generated in a loop. The user expected to only receive the sown phases, but the output included both the phases and their corresponding sine values. The confusion stems from the behavior of the Reap function, which returns a list containing the evaluated expression and the sown elements. The user realizes that adding a semicolon after the sine assignment leads to a Null result, which is a common point of confusion. Ultimately, the user decides to remove the semicolon and assignments to simplify the code and effectively unpack the reaped list containing both phases and sine values. The discussion highlights the nuances of using Reap in Mathematica and the importance of understanding how semicolons affect code execution.
Swamp Thing
Insights Author
Messages
1,028
Reaction score
767
Mathematica code :

Code:
Reap[     (
              sin = (
                           (
                             phase = 2 Pi #/32; Sow[phase]; Sin[phase]
                           ) & /@ Range[32]
                    )
           )
     ]

The output looks like this:
1739760534140.png



But I would have expected only the phases, i.e. the second sublist only. Why do the sine values get reaped along with the phases that are actually sown?
 
Physics news on Phys.org
Reap returns a list with two elements. The first is the value of the expression evaluated. The second is the list of sown elements.
 
  • Informative
Likes Swamp Thing
Ok... a semicolon after the "sin = (...)" changes the sin arrray into a Null. In the past I have sometimes wondered about the Null... I suppose I used to put the semicolon in without realizing its role, and forgot to do so this time. Also this time, the reaping of the sin values is actually better than assigning them inside the loop. So I will remove the semicolon and the assignments and then unpack the big list that gets reaped, containing the phases as well as the sin terms.

Thank you.
 
Back
Top