0

I have a function, nS[f_, xk_], which takes as arguments a function f and a vector xk. It works perfectly when I use it on its own.

nS[t, {1, 0, 0}]
{0.664063, 0.417969, 0.}

But when I try to use Nest, for some reason it won't evaluate the function:

Nest[nS, {t, {1, 0, 0}}, 1]
nS[{t, {1, 0, 0}}]

And if I increase the iterations on Nest I just get nS[nS[nS[...]]].

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 2
    You can add an additional definition for your function nS[{t_, list_}] := nS[t, list], your problem right now is that you have nothing defined for nS[{...}] only nS[..]. – C. E. May 08 '15 at 23:19
  • 2
    moreover, what is returned from first nest won't fit nS later for more repetitions. – Kuba May 09 '15 at 00:00

2 Answers2

1

Edit

Because you do not provide definitions for nS an t, I can not provide a solution based on your code, so I will contrive my own example.

f[fnc_, data_List] := fnc /@ data
sq[x_] := x^2

f[sq, Range @ 3]

{1, 4, 9}

Now to use f with Nest, I simply write

Nest[f[sq, #] &, Range @ 3, 3]
{1, 256, 6561}

The 1-st argument of Nest is a function of one variable, the list that f requires, so the 2-nd argument can be that list alone.

The auxiliary function sq makes the code easier to understand but isn't actually needed. The Nest expression can just as well be written with pure functions.

Nest[f[#^2 &, #] &, Range @ 3, 3]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
1

Since your examples shows your function outputting a simple numeric vector is not not make sense to apply your two-argument function to this result. Presumably you want to keep a constant f_ parameter for every application of nS. The best way to do that is to define a parameterized function:

nS[f_][xk_List] := Developer`PartitionMap[f, xk, 3, 1]

Basic use is almost the same:

nS[Total][Range@5]
{6, 9, 12}

And now you can just write:

NestList[nS[Total], Range[9], 4]
{
{1, 2, 3, 4, 5, 6, 7, 8, 9},
 {6, 9, 12, 15, 18, 21, 24},
  {27, 36, 45, 54, 63},
   {108, 135, 162},
    {405}
}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • is Developer'PartitionMap documentation easy to find? using phone without full range of characters. – ubpdqn May 09 '15 at 06:41
  • @ubpdqn Yes, if you mean is it easy to find after you know about it: just enter PartitionMap in the documentation search field. If you mean is it easy to find if you've never heard of it then it a bit more work but still not hard: from the main documentation page click Standard Extra Packages which takes you to a page that includes Developer Utilities of which PartitionMap is a member. – Mr.Wizard May 09 '15 at 06:46
  • ok stupid question but seems like a useful function I did not know about and some functions have variable quality documentation...am nowhere near computer so helpful answer wrt exploring other of the many functions unknown to me... – ubpdqn May 09 '15 at 06:50
  • 1
    @ubpdqn I believe it has uniformly the same syntax as Partition with the exception of the added first parameter that is a function to map over the partitions. – Mr.Wizard May 09 '15 at 06:53