MathGroup Archive 1999

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

Search the Archive

Re: newtons method, notation

  • To: mathgroup at
  • Subject: [mg21213] Re: [mg21084] newtons method, notation
  • From: BobHanlon at
  • Date: Fri, 17 Dec 1999 01:29:13 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Yes, there is a moderator.

Clear[f, g];

To simplify the expressions in the explanation I will use a recursive 
definition of the factorial function.

f[1] = 1;
f[n_] := n*f[n - 1];

A recursive definition is one that defines a function in terms of itself. 
Calling the function automatically causes it to call itself until it calls 
itself for a known value. For f[4] this would act like

f[4] = 4*f[3] = 4*3*f[2] = 4*3*2*f[1] = 4*3*2*1

It stops because it knows the value of f[1], which is to say that it stops 
calling itself.

Table[f[n], {n, 5}]

{1, 2, 6, 24, 120}

Now look at the definition of f

f[1] = 1
f[n_] := n*f[n - 1]

It is just as defined above. Now define the function with the added assignment

g[1] = 1;
g[n_] := g[n] = n*g[n - 1];

Table[g[n], {n, 5}]

{1, 2, 6, 24, 120}

The results are the same; however, checking the definition

g[1] = 1
g[2] = 2
g[3] = 6
g[4] = 24
g[5] = 120
g[n_] := g[n] = n*g[n - 1]

Note that with the revised definition that it has remembered the individual 
values that were calculated. Consequently, the recursion will find a stopping 
(known) value sooner. Checking the timing:

Timing[Table[f[n], {n, 100}]][[1]]

0.333333 Second


0.0166667 Second

Timing[Table[g[n], {n, 100}]][[1]]

0.0333333 Second


0. Second

This trades memory for speed.

Bob Hanlon

In a message dated 12/15/1999 2:32:40 AM, jcs3 at writes:

>thanks for writing to me...btw, my ng reader doesn't show my message, i'll
>check it again.  is there some sort of moderator?
>anyhow, i skimmed your message, sounds good.  i'm ok with newtons method,
>doing it pencil and what not.  it is more the notation, the symbols.
>the := followed by = is sort of like, define this function to equal
>this... but how does mathematica know to "run" it, so to speak, in

  • Prev by Date: Re: Partial evaluation
  • Next by Date: Re: newtons method, notation
  • Previous by thread: newtons method, notation
  • Next by thread: Re: newtons method, notation