# Define variables dynamically to compute a function

Let's say I have defined a function $$W(p, q)$$, where $$p$$ and $$q$$ are integers between (and including) $$0$$ and $$2$$ and the function takes me to a real number.

Upon an input $$n$$, where $$n$$ is a positive integer, I want to automate the process of computing the following quantity ($$|\cdot|$$ is absolute value):

$$$$\sum_{p_1, q_1, p_2, q_2, \ldots, p_n, q_n \in \{0, 1, 2\}}|W(p_1, q_1)|\cdot |W(p_2, q_2)| \cdots |W(p_n, q_n)|,$$$$

and then plot the variation of this quantity with $$n$$.

Additionally, I also want to find a symbolic expression for this quantity (that depends only on an integer $$n$$).

I do not know how to dynamically define $$2n$$ such variables, depending on the input $$n$$: my tries have involved defining the quantity by brute force for each particular $$n$$, which soon gets unwieldy.

• does ClearAll[sum]; sum[n_] := Total[Times @@@ Apply[Abs@*W, Tuples[{0, 1, 2}, {n, 2}], {2}]] give what you need?
– kglr
Commented Sep 6, 2021 at 13:12
• Thanks! Yes, it works and gives me the numerical value when I input an integer $n$. Is it also possible to get the symbolic expression for sum, for any integer $n$? If I write sum[n] to get such an expression, I get the following two errors: "Single or list of non-negative machine-sized integers expected at \position 2 of Tuples[{0,1,2},{n,2}]." and "Nonatomic expression expected at position 1 in Tuples[0,2 n]." Commented Sep 6, 2021 at 14:44
• I edited the question to add the part about a symbolic expression that only depends on $n$. Commented Sep 6, 2021 at 14:49
• do you want the symbolic sum just for formatting purposes, or do you want to be able to evaluate it later and get a numeric answer? Commented Sep 6, 2021 at 19:44
• I want to be able to evaluate later to get a numeric answer. Commented Sep 6, 2021 at 19:51

@kglr provided a nice way of calculating it when n is an explicit integer; to enforce that it is explicit, change n to n_Integer:

ClearAll[sum];
sum[n_Integer] :=   Total[Times @@@ Apply[Abs@*W, Tuples[{0, 1, 2}, {n, 2}], {2}]]


Here's a weird way of getting a symbolic version: integrate over a discrete region!

sum[n_Symbol] :=
Integrate[Product[Abs[W[p[[i]], q[[i]]]], {i, n}],
p \[Element] Point[Tuples[{0, 1, 2}, n]],
q \[Element] Point[Tuples[{0, 1, 2}, n]]]


(I would have liked to make this just one region to integrate over, but the simple way of doing that didn't seem to work. Another way would be to use )

ClearAll[n];
s = sum[n]


s /. n -> 1 (* or equivalently, n = 1; s *)

(* Out: Abs[W[0, 0]] + Abs[W[0, 1]] + Abs[W[0, 2]] +
Abs[W[1, 0]] + Abs[W[1, 1]] + Abs[W[1, 2]] +
Abs[W[2, 0]] + Abs[W[2, 1]] + Abs[W[2, 2]] *)


Here's a far simpler way: just define sum[n_Integer] as @kglr's expression, and don't define a value for sum[n_] or sum[n_Symbol]—that will leave sum[n] with n symbolic unevaluated until it becomes an explicit integer. :) (Maybe I should have led with this...)

You can also then give a value to Format[sum[n_Symbol]] := ..., so that it looks like the sum you wrote down, instead of a weird integral! E.g.

ClearAll[sum];

sum[n_Integer] := Total[Times @@@ Apply[Abs@*W, Tuples[{0, 1, 2}, {n, 2}], {2}]]

Format[sum[n_Symbol]] := HoldForm[Sum[Product[
BracketingBar@W[Subscript[p, i], Subscript[q, i]], {i, 1, n}],
SequenceForm[p, ",", q] \[Element] {0, 1, 2}^n]]

ClearAll[n];
sum[n]


The downside here is that now sum[n] has no computational content until we set n to be an explicit integer. In the former version, we could try simplifications and manipulations, e.g. multiplying by a scalar, and Mathematica would know what we were talking about. Here it's only formatting, nothing more.