Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8199
| From | Dana DeLouis <dana01@me.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Puzzled by Sum |
| Date | 2012-01-27 11:12 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jfu0rg$c70$1@smc.vnet.net> (permalink) |
> 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...@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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Puzzled by Sum Dana DeLouis <dana01@me.com> - 2012-01-27 11:12 +0000
csiph-web