How Can I Define Tags Within My Code Using Sow/Reap in Mathematica?

  • Thread starter Thread starter Lucid Dreamer
  • Start date Start date
  • Tags Tags
    Mathematica Tags
AI Thread Summary
The discussion centers on the challenges of using the Sow/Reap functions in Mathematica, specifically regarding the definition of tags within the code. The initial attempt to define tags inside the Reap function does not yield the expected results, leading to errors indicating that there are no matching Reap calls for the Sow functions. In contrast, defining the tags outside of Reap allows the code to function correctly. The user explores the behavior of the Trace function to understand the underlying issues, noting differences in output and warning messages between the two approaches. The conversation highlights the importance of tag scope in the Sow/Reap mechanism and suggests that defining tags externally is a more reliable method.
Lucid Dreamer
Messages
25
Reaction score
0
I am trying to use Sow/Reap in Mathematica to replace Append in my code. The problem is that I need to define the tags within my code, but doing so doesn't reap the desired results. A simplified version of my code is

Code:
Clear[tag];
Reap[tag = {a,b}; Sow[1, a];Sow[2,b];, tag]
However, if I define my tag outside of Reap, then it works

Code:
Clear[tag];
tag = {a,b};
Reap[Sow[1, a];Sow[2,b];, tag]

Any ideas as to how I can define tags within my code?
 
Technology news on Phys.org
Trace gives some hints about what is going on

In[1]:= Clear[tag]; Trace[Reap[tag={a,b};Sow[1,a];Sow[2,b];,tag]]

Out[2]= {Reap[tag={a,b};Sow[1,a];Sow[2,b];,tag], {tag={a,b};Sow[1,a];Sow[2,b];,{tag={a,b},{a,b}},{Sow[1,a],{Message[Sow::enclt,a,Sow[1,a]],{Sow::enclt,$Off[Tag `1` in `2` has no matching Reap.]},Null},1},{Sow[2,b],{Message[Sow::enclt,b,Sow[2,b]],{Sow::enclt,$Off[Tag `1` in `2` has no matching Reap.]},Null},2},Null},{Null,{}}}

Now kill the kernel and restart with your alternate code

In[1]:= Clear[tag];tag={a,b}; Trace[Reap[Sow[1,a];Sow[2,b];,tag]]

Out[3]= {{tag,{a,b}},Reap[Sow[1,a];Sow[2,b];,{a,b}],{Sow[1,a];Sow[2,b];,{Sow[1,a],1},{Sow[2,b],2},Null},{Null,{{{1}},{{2}}}}}
 
Trace gives some hints about what is going on

In[1]:= Clear[tag]; Trace[Reap[tag={a,b};Sow[1,a];Sow[2,b];,tag]]

Out[2]= {Reap[tag={a,b};Sow[1,a];Sow[2,b];,tag], {tag={a,b};Sow[1,a];Sow[2,b];,
{tag={a,b},{a,b}},
{Sow[1,a],{Message[Sow::enclt,a,Sow[1,a]],{Sow::enclt,$Off[Tag `1` in `2` has no matching Reap.]},Null},1},
{Sow[2,b],{Message[Sow::enclt,b,Sow[2,b]],{Sow::enclt,$Off[Tag `1` in `2` has no matching Reap.]},Null},2},Null},
{Null,{}}}

Now kill the kernel and restart with your alternate code

In[1]:= Clear[tag];tag={a,b}; Trace[Reap[Sow[1,a];Sow[2,b];,tag]]

Out[3]= {{tag,{a,b}},Reap[Sow[1,a];Sow[2,b];,{a,b}],{Sow[1,a];Sow[2,b];,{Sow[1,a],1},{Sow[2,b],2},Null},
{Null,{{{1}},{{2}}}}}

This is interesting, notice the comma I removed from your nonworking example and the missing warning messages

In[1]:= Clear[tag];Trace[Reap[tag={a,b};Sow[1,a];Sow[2,b];tag]]

Out[2]= {Reap[tag={a,b};Sow[1,a];Sow[2,b];tag],{tag={a,b};
Sow[1,a];Sow[2,b];tag,{tag={a,b},{a,b}},
{Sow[1,a],1},{Sow[2,b],2},{tag,{a,b}},{a,b}},
{{a,b},{{1},{2}}}}

But that raises at least as many questions as your original
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top