Sow the phase and Reap the sin -- why?

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Phases Sine
Click For Summary
SUMMARY

The discussion centers on the behavior of the Mathematica functions Reap and Sow in relation to the evaluation of sine values and phases. The provided code demonstrates that when a semicolon is omitted after the sine assignment, both the sine values and the phases are returned by Reap. This behavior is confirmed as beneficial, as it allows for the collection of sine values without additional assignments within the loop. The user concludes that removing the semicolon simplifies the code while achieving the desired output.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with functional programming concepts in Mathematica
  • Knowledge of trigonometric functions, specifically Sin
  • Basic comprehension of list manipulation in Mathematica
NEXT STEPS
  • Explore advanced usage of Reap and Sow in Mathematica
  • Learn about functional programming techniques in Mathematica
  • Investigate the implications of using semicolons in Mathematica code
  • Study list unpacking and manipulation in Mathematica for efficient data handling
USEFUL FOR

Mathematica users, programmers interested in functional programming, and anyone looking to optimize their code for mathematical computations.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
783
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   Reactions: 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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
6K
Replies
1
Views
814
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
860
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K