和、固定变量和求和"变量内的项是为每个这样的表达式自动生成的,并且f"函数是单独定义的.要生成上面的表达式,我可能需要调用sumProduct(factors,fixedVariables,fixedValues,freeVariables,freeRanges)哪里factors={{1,4},{3,4},{3,4,5}}固定变量={1,3}固定值={-1,9}自由变量={4,5}freeRanges={Range[5],Range[6]}并且该函数的输出将等价于总计[{f14[-1,1]f34[9,1]f345[9,1,1],f14[-1,2]f34[9,2]f345[9,2,1],....}] f 项的表示可能不同,即 f[{1,4},{-1,1}] 而不是 f14[-1,1].同样使用 Integer 来引用每个变量只是一种设计选择.谁能建议一种优雅的方法来实现 sumProduct?编辑 11/11Janus 的解决方案,重写以提高可读性factors = {{1, 4}, {3, 4}, {3, 4, 5}};vars = {{1, {-1}}, {3, {9}}, {4, Range[5]}, {5, Range[6]}};(* 数字列表 => 变量列表 *)arglist[factor_] := 下标[x, #] &/@ 因素;(* 因子列表 => 这些因子的函数列表 *)条款 = Apply[f[#], arglist[#]] &//@因素;(* {var,range} 对每个变量 *)args = {Subscript[x, #1], #2} &@@@ 变量;Sum[时间@@ 项,序列@@ args] 解决方案 我会将固定变量和自由变量放在一起,并在列表中将它们全部指定为variables={{1,{-1}},{3,{9}},{4,Range[5]},{5,Range[6]}};那么你的sumProduct就可以非常简洁地实现sumProduct[f_, factor_, vars_] := Module[{x}, Sum[次@@((下标[f,##]@@(下标[x,#]&/@{##})&)@@@因素),序列@@ ({Subscript[x, #1], #2} & @@@ vars)]]被称为sumProduct[f,factors,variables]吐出一个长长的东西:下标[f, 1,4][-1,1] 下标[f, 3,4][9,1] 下标[f, 3,4,5][9,1,1]+....这就是你所追求的吗?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 formTerms 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 callsumProduct(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 toTotal[{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/11Janus' solution, rewritten for readabilityfactors = {{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] 解决方案 I would bunch together the fixed and free variables and specify them all in a list asvariables={{1,{-1}},{3,{9}},{4,Range[5]},{5,Range[6]}};Then your sumProduct can be implemented quite conciselysumProduct[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? 这篇关于在 Mathematica 中自动生成总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 20:21