Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11248 > unrolled thread

Re: Functions That Remember Values They Have Found

Started byBob Hanlon <hanlonr357@gmail.com>
First post2012-08-07 07:02 +0000
Last post2012-08-08 07:20 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Functions That Remember Values They Have Found Bob Hanlon <hanlonr357@gmail.com> - 2012-08-07 07:02 +0000
    Re: Functions That Remember Values They Have Found Esteban González Morales <yo8231@gmail.com> - 2012-08-08 07:20 +0000

#11248 — Re: Functions That Remember Values They Have Found

FromBob Hanlon <hanlonr357@gmail.com>
Date2012-08-07 07:02 +0000
SubjectRe: Functions That Remember Values They Have Found
Message-ID<jvqei4$gsd$1@smc.vnet.net>
Clear[Hgeom, Hgeom2, Hgeom3]

Hgeom[n_] := H[n] = Sum[
    (1 - 0.5)^i/(1 - 0.5^i) Hgeom[n - i],
    {i, 1, n}]/n
Hgeom[0] = 1;

Hgeom[4]

0.203175

The memory is not effective when stored under a separate name

?? Hgeom

Use the same function name for the memory

Hgeom2[n_Integer] := Hgeom2[n] =
   Sum[(1 - 0.5)^i/(1 - 0.5^i)
       Hgeom2[n - i], {i, 1, n}]/n;
Hgeom2[0] = 1;

Hgeom2[4]

0.203175

?? Hgeom2

You could also use exact values

Hgeom3[n_Integer] := Hgeom3[n] =
   Sum[2^-i/(1 - 2^-i)
       Hgeom3[n - i], {i, 1, n}]/n;
Hgeom3[0] = 1;

Hgeom3[4]

64/315

?? Hgeom3

Note the timing differences

m = 18;

{Timing[Hgeom[m];][[1]],
 Timing[Hgeom2[m];][[1]],
 Timing[Hgeom3[m];][[1]]}

{3.09407, 0.001196, 0.002241}

Demonstrating that the results are equal

And @@ Table[
  Hgeom[n] == Hgeom2[n] == Hgeom3[n],
  {n, 0, m}]

True


Bob Hanlon


On Mon, Aug 6, 2012 at 4:37 AM, Esteban Gonz=E1lez Morales
<yo8231@gmail.com> wrote:
> Hi, I was trying to create a recursive function and I read the help about=
 it, and wrote this code
>
> Hgeom[n_] :=
>  H[n] = Sum[(1 - 0.5)^i/(1 - 0.5^i) Hgeom[n - i], {i, 1, n}]/n
> Hgeom[0] = 1;
>
> However, when I calculate Hgeom[10] it gives me the right value, and then=
 I ask for the information about Hgeom and get that it has values calculate=
d.
>
> Have you any idea what could have gone wrong?
>

[toc] | [next] | [standalone]


#11272

FromEsteban González Morales <yo8231@gmail.com>
Date2012-08-08 07:20 +0000
Message-ID<jvt40o$m1h$1@smc.vnet.net>
In reply to#11248
On Tuesday, 7 August 2012 02:02:28 UTC-5, Bob Hanlon  wrote:
> Clear[Hgeom, Hgeom2, Hgeom3]
> 
> 
> 
> Hgeom[n_] := H[n] = Sum[
> 
>     (1 - 0.5)^i/(1 - 0.5^i) Hgeom[n - i],
> 
>     {i, 1, n}]/n
> 
> Hgeom[0] = 1;
> 
> 
> 
> Hgeom[4]
> 
> 
> 
> 0.203175
> 
> 
> 
> The memory is not effective when stored under a separate name
> 
> 
> 
> ?? Hgeom
> 
> 
> 
> Use the same function name for the memory
> 
> 
> 
> Hgeom2[n_Integer] := Hgeom2[n] =
> 
>    Sum[(1 - 0.5)^i/(1 - 0.5^i)
> 
>        Hgeom2[n - i], {i, 1, n}]/n;
> 
> Hgeom2[0] = 1;
> 
> 
> 
> Hgeom2[4]
> 
> 
> 
> 0.203175
> 
> 
> 
> ?? Hgeom2
> 
> 
> 
> You could also use exact values
> 
> 
> 
> Hgeom3[n_Integer] := Hgeom3[n] =
> 
>    Sum[2^-i/(1 - 2^-i)
> 
>        Hgeom3[n - i], {i, 1, n}]/n;
> 
> Hgeom3[0] = 1;
> 
> 
> 
> Hgeom3[4]
> 
> 
> 
> 64/315
> 
> 
> 
> ?? Hgeom3
> 
> 
> 
> Note the timing differences
> 
> 
> 
> m = 18;
> 
> 
> 
> {Timing[Hgeom[m];][[1]],
> 
>  Timing[Hgeom2[m];][[1]],
> 
>  Timing[Hgeom3[m];][[1]]}
> 
> 
> 
> {3.09407, 0.001196, 0.002241}
> 
> 
> 
> Demonstrating that the results are equal
> 
> 
> 
> And @@ Table[
> 
>   Hgeom[n] == Hgeom2[n] == Hgeom3[n],
> 
>   {n, 0, m}]
> 
> 
> 
> True
> 
> 
> 
> 
> 
> Bob Hanlon
> 
> 
> 
> 
> 
> On Mon, Aug 6, 2012 at 4:37 AM, Esteban Gonz=E1lez Morales
> 
> <yo8231@gmail.com> wrote:
> 
> > Hi, I was trying to create a recursive function and I read the help about=
> 
>  it, and wrote this code
> 
> >
> 
> > Hgeom[n_] :=
> 
> >  H[n] = Sum[(1 - 0.5)^i/(1 - 0.5^i) Hgeom[n - i], {i, 1, n}]/n
> 
> > Hgeom[0] = 1;
> 
> >
> 
> > However, when I calculate Hgeom[10] it gives me the right value, and then=
> 
>  I ask for the information about Hgeom and get that it has values calculate=
> 
> d.
> 
> >
> 
> > Have you any idea what could have gone wrong?
> 
> >

Thank you very much!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web