"Nest[f,expr,n]
gives an expression with f applied n times to expr."
Nest
takes
a function,
an expression,
an n of positive Integers.
No more, no less.
Nest
is somehow outdated.
It is superseded by Composition
.
Composition
is the mathematical elementary term from with Nest
is derived.
There is an example in the documentation of Composition for a sum:
Composition[HoldForm, Plus] @@ Range[20]
___
\!\(
TagBox[
RowBox[{"1", "+", "2", "+", "3", "+", "4", "+", "5", "+", "6", "+",
"7", "+", "8", "+", "9", "+", "10", "+", "11", "+", "12", "+",
"13", "+", "14", "+", "15", "+", "16", "+", "17", "+", "18", "+",
"19", "+", "20"}],
HoldForm]\)
This clears that Sum and Nest are rather different.
Sum
is derived from Plus
in the above manner. The documentation page of Plus
shows some alternative to Sum
.
To built up complicated products Mathematica offers the built-in Product
. There is neither a line with Nest
on the documentation page of Product
nor vice versa.
What does that imply for Your question?
Now at first nothing. But it is a strong hint.
While Nest
is iterative with n, the "times" constant at the third argument position, Product
does not require a x" but an iterator i with start and end. That is what Your summands represent. I accept the examples in the documentation page for Product
are far to easy or much the specialized.
There are some nice examples and methods, how to make this more efficient:
∑𝑖=2𝑁cos𝜃𝑖cos𝜃′𝑖∏𝑗=𝑖+1𝑀sin𝜃𝑗𝜃′𝑗
NSum[Cos[θ[[i]]] Cos[Θp[[i]]] Product[ Sin[θ[[j]]] Sin[θp[[j]]], {j, i + 1, d - 1}], {i, 2, d - 1}]
f[M_, n_] := Reverse[Table[Cos[θ[i]] Cos[θ'[i]], {i, 2, n}]].PadLeft[FoldList[
Sin[θ[M - #2] θ'[M - #2]] # &, Sin[θ[M] θ'[M]], Range[M - 3]], Max[n - 1, 0], 1]
This question is already concerned about sum or product with exclusions.
Sum is more essential for getting closed formulars like in this example:
Sum[Product[i^2, {i, 1, n}], {i, 1, n}]
n (n!)^2
n = 4;
Times @@ Flatten@Table[f[a[i] - a[j]], {i, 1, n - 1}, {j, i + 1, n}]
or
With[{n = 6}, Times @@ f /@ Subtract @@@ Subsets[Array[a, n], {2}]]
can be either done with an iterator or a list. The iterator needs the coefficients list to be already defined and iterates over it in a linear fashion. In more modern Mathematica versions the second version shall be faster in most contexts.
The formula makes use of different operators @
, @@
and @@@
that are different to Composition
@*
.
This is a highly rated answer about scan vs map vs apply. This answer explains some differences between Composition and Apply. This answers go much deeper in the topics related: v10s operator forms what are they good for?
Some common misconception is addressed in this answers: how do i designate arguments in a nested map.
ClearAll[list1, list2, a, b, c, x, y, z, f]
list1 = {a, b, c}
list2 = {x, y, z}
___
Map[Map[f[#1, #2] &, list1] &, list2]
__
list2
___
Map[Function[x, Map[f[#1, x] &, list1]], list2]
___
list2
But the desired result is this
Outer[f, list1, list2]
(*
{{f[a, x], f[a, y], f[a, z]},
{f[b, x], f[b, y], f[b, z]},
{f[c, x], f[c, y], f[c, z]}}
*)
Map[Function[p2, Map[Function[p1, f[p1, p2]], list1]], list2]
(*
{{f[a, x], f[b, x], f[c, x]},
{f[a, y], f[b, y], f[c, y]},
{f[a, z], f[b, z], f[c, z]}}
*)
If f in not listable this can be too written this way:
Distribute[f[{a, b, c}, {x, y, z}], List]
(*
{{f[a, x], f[b, x], f[c, x]},
{f[a, y], f[b, y], f[c, y]},
{f[a, z], f[b, z], f[c, z]}}
*)
The next alternative is
Tuples[{{a, b, c}, {x, y, z}}]
({{a, x}, {a, y}, {a, z}, {b, x}, {b, y}, {b, z}, {c, x}, {c, y}, {c,
z}})
Apply[f, Tuples[{{a, b, c}, {x, y, z}}], {1}]
({f[a, x], f[a, y], f[a, z], f[b, x], f[b, y], f[b, z], f[c, x],
f[c, y], f[c, z]})
And this, in turn, allows for the desired Nest
:
Nest[f, #, 1] & /@ Tuples[{{a, b, c}, {x, y, z}}]
({f[{a, x}], f[{a, y}], f[{a, z}], f[{b, x}], f[{b, y}], f[{b, z}],
f[{c, x}], f[{c, y}], f[{c, z}]})
This question about nest-fold-is-there-an-extension-for-more-than-2-arguments refers to a chapter 5.5.3 Restriction of Fold-ed function to two arguments is spurious of an online book by Leonid Shifrin and an example with three slots:
multiFoldList[f_, start_, args__List] :=
FoldList[f @@ Prepend[#2, #] &, start, {args}\[Transpose]]
____
multiFoldList[#1 (1 + #2) - #3 &, 1000, {.01, .02, .03}, {100, 200,
300}]
___
{1000, 910., 728.2, 450.046}
These are very special but these make the trick and the extensions are already included.
For now finally, I like to refer to this overview article
alternatives-to-procedural-loops-and-iterating-over-lists-in-mathematica/
that includes some examples using Fold and Nest and compare this in different situations to alternative built-ins. This is all quite nice and offers deeper knowledge into what Nest
does and can do and what not. I compare the built-in Nest
to other built-ins and combinations and Composition
s.
Search the Mathematica documentation for Iterator to get this as the better definition for the input value n and some explanation for the Mathematica paradigm selection about that.
There are two definitions for Expression in the Mathematica documentation one for the cells and one for the Wolfram Language interpreter. So such a search guide into inputs dedicated for the helpfulness of WolframAlpha
Have a look at FixedPoint a built-in historically grouped with Nest and for the generation of Mathematica users as the limiting built-in of Nest for infinite iterations, applications. The famous tutorial was Applying functions repeatedly.
defines the ranges for the indices that can Mathematica based on the Wolfram Language cope with.
So that is what Nest and alike is lacking and Prodcut has.
n
by usingUnitStep[i[2]-i[1],i[3]-i[2],...]
. $\endgroup$Nest
. UsingUnitStep
would depend on minimally two iterators each and hence can't be nested $\endgroup$Nest[]
for this application, mostly because it is more efficient to haveSum[expr, r1, r2, ...]
rather than the nestedSum[expr1 Sum[expr2 Sum[...], r2], r1]
. $\endgroup$