Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Puzzled by Sum

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124627] Re: Puzzled by Sum
  • From: Dana DeLouis <dana01 at me.com>
  • Date: Fri, 27 Jan 2012 06:10:28 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

> F[X_] := (Sum[A[i - j, j], {j, 1, i - 1}]) /. i -> X

Hi. I believe the problem is that the Sum is evaluated First, and doesn't really have an idea what the variable i is.
As Bob mentioned, using =91x in the Sum function works:

a[i_,j_]:=i/j+j/i
f[x_]:=(Sum[a[x-j,j],{j,1,x-1}])

f[2]
2

Perhaps an alternative to your 2 separate functions:

g[n_]:= 2 n (HarmonicNumber[n]-1)


Table[f[j],{j,10}]
{0,2,5,26/3,77/6,87/5,223/10,962/35,4609/140,4861/126}

Table[g[n],{n,10}]
{0,2,5,26/3,77/6,87/5,223/10,962/35,4609/140,4861/126}

%==%%
True

If the input is large, then it appears faster by not having to loop (By an apx. factor of 21)

f[1000]//N//Timing
{0.009008,12970.9}

g[1000]//N//Timing
{0.000427,12970.9}

%% / %
{21.096,  1.}

= = = = = = = = = = = = = =
HTH  ;>)
Dana DeLouis
Mac, with Math 8
= = = = = = = = = = = = = =



On Jan 25, 6:06 am, Themis Matsoukas <tmatsou... at me.com> wrote:
> I am trying to define a function F[X] whose argument is the upper limit in a summation:
>
> A[i_, j_] := i/j + j/i
> F[X_] := (Sum[A[i - j, j], {j, 1, i - 1}]) /. i -> X
>
> but when I evaluate, say F[2], I get
>
> -2 DifferenceRoot[{\[FormalY],\[FormalN]}\[Function]{(\[FormalN]-2) \[FormalY](\[FormalN])+(3-2 \[FormalN]) 
\[FormalY](\[FormalN]+1)+(\[FormalN]-1) 
\[FormalY](\[FormalN]+2)=0,\[FormalY](0)=0,\[FormalY](1)=-(1/2)}][2]-1
>
> where the result should have been 2:
>
> ii = 2;
> Sum[A[ii - j, j], {j, 1, ii - 1}]
>
> 2
>
> If I use A[i,j]=i j or other simple functions, everything works as expected.
>
> Themis





  • Prev by Date: Re: Plotting in nested Manipulates
  • Next by Date: Re: Mapping a curve to a surface using Manipulate
  • Previous by thread: Re: Puzzled by Sum
  • Next by thread: Re: Puzzled by Sum