I'm trying to define a function which modifies and sums over the elements of an array. Here is a very simplified version.
myF[kmax_] = Sum[v[[k]]/k, {k, 1, kmax}]
As you can see, each element between 1 and the function argument (kmax) is divided by its index and then summed with the others.
The problem is that mathematica isn't accepting that v[[k]] and complains that k isn't an integer.
Part::pspec: Part specification k is neither an integer nor a list of integers.
I tried replacing k with Round[k], but the problem remains. I even tried redefining the vector, where the elements are already divided by k, and then just summing over the first kmax elements:
myF[kmax_] = Total[Take[v, kmax]]
And I'm still getting error messages about the argument not being of the right type
Take::seqs: Sequence specification (+n, -n, {+n}, {-n}, {m, n}, or {m, n, s}) expected at position 2 in Take
So, am I doing something wrong? Is there any way to access array elements within a function definition?



myF[kmax_] := .... – b.gatessucks Dec 4 '12 at 14:51myF[kmax_Integer] := ...will give you some defence against real values creeping into your function. – image_doctor Dec 4 '12 at 15:01Set(=) for function definitions, and you should avoid defining functions which depend on variables implicitly (like on yourvhere). For the latter, you can read some relevant discussion here. – Leonid Shifrin Dec 4 '12 at 15:17