MathGroup Archive 2005

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

Search the Archive

Re: Do loops in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55177] Re: Do loops in Mathematica
  • From: Jon Harrop <usenet at jdh30.plus.com>
  • Date: Wed, 16 Mar 2005 05:35:53 -0500 (EST)
  • References: <d15s6o$9l2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

dumb_founded wrote:
> I am having trouble setting up Do loops in Mathematica.  Even when I
> put in a return statement, I get no output.  Below is a concrete
> example of code I am putting in:
> 
> Do[{p = Solve[x^2 + 3*x + 1 == 0, x], Return[2*p]}, {i, 1}]
> 
> I get nothing out of it.

Yes, by using "Do" you are asking Mathematica to repeatedly do something and
_not_ return the answer. You want "Table" instead of "Do":

In[1]:= Table[{p = Solve[x^2 + 3*x + 1 == 0, x], Return[2*p]}, {i, 1}]

                 -3 - Sqrt[5]         -3 + Sqrt[5]
Out[1]= {{{{x -> ------------}, {x -> ------------}},
                      2                    2

                       -3 - Sqrt[5]             -3 + Sqrt[5]
>     Return[{{2 (x -> ------------)}, {2 (x -> ------------)}}]}}
                            2                        2

In computer science terminology, it could be said that "Do" is an imperative
construct and "Table" is a functional construct.

> I am having a headache figuring out this simple chore.  Mathematica
> simply refuses to behave.

Mathematica is doing precisely what you ask it to do.

-- 
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com


  • Prev by Date: Re: Surface Normal
  • Next by Date: Re: Surface Normal
  • Previous by thread: Re: Do loops in Mathematica
  • Next by thread: Re: Do loops in Mathematica