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

In summary, I am not getting the desired results when using Sow/Reap in Mathematica. I am not sure why it is not working, but tracing the Reap function gives some hints. I need to define my tags within my code, but doing so doesn't reap the desired results. If I define my tags outside of Reap, then it works.
  • #1
Lucid Dreamer
25
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
  • #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}}}}}
 
  • #3
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:

1. What is the purpose of using tags in Reap/Sow in Mathematica?

The tags in Reap/Sow in Mathematica are used to label and group the results of evaluations performed by the Sow function. This allows for easier organization and extraction of specific results from a larger set of data.

2. How do I create a tag in Reap/Sow?

To create a tag in Reap/Sow, you can use the syntax Reap[expr, tag], where "expr" is the expression to be evaluated and "tag" is the label you want to assign to the result. This will group all results with the same tag together.

3. Can I use multiple tags in Reap/Sow?

Yes, you can use multiple tags in Reap/Sow by using the syntax Reap[expr, {tag1, tag2, ...}]. This will group the results by each tag, allowing for more specific organization and extraction of data.

4. How do I extract data from a specific tag in Reap/Sow?

To extract data from a specific tag in Reap/Sow, you can use the syntax Sow[expr, tag] within the Reap function. This will return all results with the specified tag and can be assigned to a variable for further manipulation.

5. Can I use non-numeric tags in Reap/Sow?

Yes, you can use non-numeric tags in Reap/Sow in Mathematica. This allows for more flexibility in labeling and organizing results, as well as the ability to perform operations on non-numeric data.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
350
  • Introductory Physics Homework Help
Replies
23
Views
326
  • Programming and Computer Science
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
7
Views
686
  • Programming and Computer Science
Replies
6
Views
1K
  • Introductory Physics Homework Help
Replies
29
Views
874
  • Introductory Physics Homework Help
Replies
12
Views
717
Replies
10
Views
1K
Back
Top