Are multiple hashtags allowed in NestList usage? If yes, how to implement it?
Example,
f[{x_, y_}] := {x + y, x - y};
NestList[f, {100, 75}, 5]
My attempt,
NestList[{#1+#2,#1-#2} &,{100,75},5]
It does not work.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityUse Apply
.
NestList[Apply[{#1 + #2, #1 - #2} &], {100, 75}, 5]
The argument being supplied to the function is a list. It's a single argument with head List. So, #2 cannot be bound to anything. You can either use Apply (per cvgmt's answer) or use Part:
NestList[{#[[1]] + #[[2]], #[[1]] - #[[2]]} &, {100, 75}, 5]
Use Function
and Apply
:
NestList[Apply[Function[{x, y}, {x + y, x - y}]], {100, 75}, 5]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)
Or following your attempt:
f := Function[{x, y}, {x + y, x - y}]
NestList[Apply[f], {100, 75}, 5]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)
Or you can define your function as a solution rule and use the resource function SolutionRulesToFunctions as follows:
ReplaceAll[ResourceFunction["SolutionRulesToFunctions"][f[{x, y}] -> {x + y, x - y}]][NestList[Apply[f], {100, 75}, 5]]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)