Automatically generating sums in Mathematica

2019-05-28 21:28发布

问题:

This is a design issue I came across while working on implementation of Generalized Distributive Law. Suppose you need to automatically generate expressions of the following form

http://yaroslavvb.com/upload/sum-prod-formula.png

Terms inside the sum, fixed variables and "summed over" variables are automatically generated for each such expression, and "f" functions are defined separately. To generate expression above, I may need to call

sumProduct(factors,fixedVariables,fixedValues,freeVariables,freeRanges)

where

factors={{1,4},{3,4},{3,4,5}}
fixedVariables={1,3}
fixedValues={-1,9}
freeVariables={4,5}
freeRanges={Range[5],Range[6]}

and the output of that function will be equivalent to

Total[{f14[-1,1]f34[9,1]f345[9,1,1],f14[-1,2]f34[9,2]f345[9,2,1],....}]

Representation of f terms could be different, ie f[{1,4},{-1,1}] instead of f14[-1,1]. Also using Integer to refer to each variable is just one design choice.

Can anyone suggest an elegant approach to implementing sumProduct?

Edit 11/11 Janus' solution, rewritten for readability

factors = {{1, 4}, {3, 4}, {3, 4, 5}};
vars = {{1, {-1}}, {3, {9}}, {4, Range[5]}, {5, Range[6]}};

(* list of numbers => list of vars *)
arglist[factor_] := Subscript[x, #] & /@ factor;

(* list of factors => list of functions for those factors *)
terms = Apply[f[#], arglist[#]] & /@ factors;

(* {var,range} pairs for each variable *)
args = {Subscript[x, #1], #2} & @@@ vars;

Sum[Times @@ terms, Sequence @@ args]

回答1:

I would bunch together the fixed and free variables and specify them all in a list as

variables={{1,{-1}},{3,{9}},{4,Range[5]},{5,Range[6]}};

Then your sumProduct can be implemented quite concisely

sumProduct[f_, factors_, vars_] := Module[{x}, Sum[
   Times @@ ((Subscript[f, ##] @@ (Subscript[x, #] & /@ {##}) &) @@@ factors),
   Sequence @@ ({Subscript[x, #1], #2} & @@@ vars)]]

Which is called as sumProduct[f,factors,variables] to spit out a long thing:

Subscript[f, 1,4][-1,1] Subscript[f, 3,4][9,1] Subscript[f, 3,4,5][9,1,1]+....

Was this what you were after?