MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Assertions in Mathematica?

  • To: mathgroup at
  • Subject: [mg113557] Re: Assertions in Mathematica?
  • From: magma <maderri2 at>
  • Date: Tue, 2 Nov 2010 05:03:01 -0500 (EST)
  • References: <iaj4ob$n70$> <iajgs3$qt7$> <iam35f$k25$>

> I said, great, let me look at Sow and Reap (which I never to this day
> figured how to use these 2 guys).
> The first thing I see about Sow[e] is:
> "Sow[e] specifies that e should be collected by the nearest enclosing
> Reap. "
> What does that mean? collected by the 'nearest' enclosing Reap?
> Reading more...I look at the first example,
> "Evaluate a sequence of expressions, sowing some to be collected by Reap"
> Sowing "some"? How this "some" come about? to be collected by Reap? in
> what sense? What does Sowing actually mean? as in sewing cloth? as in
> joining parts?
> Ok, trying to understand what the command does from looking at the
> output, since the description above made no sense to me. After looking
> at some examples, and still not clear how it works exactly, I looked at
> Reap, and the first thing it said is:
> "Reap[expr]
> gives the value of expr together with all expressions to which Sow has
> been applied during its evaluation. Expressions sown using Sow[e] or
> Sow[e, tagi] with different tags are given in different lists. "
> humm, looking at the next part, it says
> "Make a separate sublist for each tag being reaped"
> Ok, I give up.  Sow seems to be described in terms of Reap, and Reap
> seems to be described in terms of Sow. Like a cycle.
> I do wish these 2 command could have been presented in a simpler way.
> It would have been nice for example to show how to use Sow and Reap in
> place of Append. May be it is hidden there somewhere.
> But I will keep reading, I am sure I will figure these 2 commands out
> one day :)
> --Nasser

You are right Nasser, Sow and Reap could have been described better,
but they hadn't.
So let me do it instead.
First, let's clear the naming confusion.
According to Wolfram Alpha:
Sow in English means
place seeds in or on the ground.
It is a synonym of inseminate.

Sew means
create clothings with cloth
fasten by sewing; do needlework

Reap means
gather, as of natural products.

So Sow and Reap commands are an agricultural metaphor.
You "plant" expressions (Sow) and then you later gather them (Reap).

The command Sow[e] gives as result just e, BUT ALSO instructs Mathematica to
"tag" or "remember" expression e, so that it can be later collected in
a list created by the "nearest enclosing Reap".

In practice

Reap[expr] will return the value of expr PLUS a list of all "Sowed"
sub-expressions that are contained in expr

A couple of interesting examples:

Reap[Plot[Sin[x], {x, 0, 10}, EvaluationMonitor :> Sow[x]]] // Short

will Plot Sin[x] and create a list of the evaluated abscissas.

Reap[FindRoot[x^2 - 2, {x, 1}, EvaluationMonitor :> Sow[x]]]

will FindRoot and create a list of the trial x values.

You can also use optional "tags" in Sow and Reap, so that you can
"plant" different kinds of "crops" (expressions) and only gather those
matching specific tags.
I think you can by now easily read the doc and figure it out by

You can also check this tutorial, but probably by now you do not
really need it any more.

Note that Catch and Throw work similarly.

  • Prev by Date: Re: NDSolve and hybrid dynamics (Differential Algebraic Equation DAE)
  • Next by Date: Question about Mathematica benchmark
  • Previous by thread: Re: Assertions in Mathematica?
  • Next by thread: Re: Assertions in Mathematica?