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


Groups > comp.soft-sys.math.mathematica > #11272

Re: Functions That Remember Values They Have Found

From Esteban González Morales <yo8231@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Functions That Remember Values They Have Found
Date 2012-08-08 07:20 +0000
Message-ID <jvt40o$m1h$1@smc.vnet.net> (permalink)
References <20120806083728.AC1FE683B@smc.vnet.net> <jvqei4$gsd$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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!

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web