生成 n 个变量的 m 阶幂级数项

发布于 2024-12-21 20:28:11 字数 811 浏览 1 评论 0原文

考虑这样一种情况,其中数据的列表形式

data = {{x1, x2, x3, ..., xn, y}, {...}, ..., {...}}

为例如,

data = {{0, 2, 3, 2}, {0, 0, 1, 4}, {7, 6, 8, 3}}

我想将数据拟合到多元多项式,例如 2。 因此,3 变量函数值是:

{2, 4, 3}

在各自的点上,

{{0, 2, 3}, {0, 0, 1}, {7, 6, 8}}

我会说这样的话

Fit[data, {1, x, y, z, x^2, y^2, z^2, x y , x z, y z}, {x, y, z}]

:这一切都非常好,但我可能不仅有 3 变量数据,可能有任意数量的变量,而且我没有知道如何以编程方式生成所有线性、二次甚至高阶项,并将它们作为 Fit[] 的第二个参数插入。

对于 4 变量日期执行二阶操作,它会类似于:

{1, x1, x2, x3, x4, x1^2, x2^2, x3^2, x4^2, x1 x2, x1 x3, x1 x4, x2 x3, x2 x4, x3 x4}

有什么方法可以为 n 变量生成这样的列表,以 m 顺序? n 变量函数的 m 阶幂级数展开式中的类似项(不带系数)。

Consider a situation where you have data in a list of the form

data = {{x1, x2, x3, ..., xn, y}, {...}, ..., {...}}

For example,

data = {{0, 2, 3, 2}, {0, 0, 1, 4}, {7, 6, 8, 3}}

I'd like to fit the data to a multivariate polynomial of order, say, 2.
So, the 3-variable function values are:

{2, 4, 3}

in respective points

{{0, 2, 3}, {0, 0, 1}, {7, 6, 8}}

I'd say something like

Fit[data, {1, x, y, z, x^2, y^2, z^2, x y , x z, y z}, {x, y, z}]

This is all very nice, but i may not have only 3-variate data, there may be an arbitrary number of variables, and I don't know how to programmatically generate all the linear, quadratic or even higher-order terms, to insert them as the second argument of Fit[].

For 4-variate date do second order, it would be something like:

{1, x1, x2, x3, x4, x1^2, x2^2, x3^2, x4^2, x1 x2, x1 x3, x1 x4, x2 x3, x2 x4, x3 x4}

Is there any way I can generate such a list for n variables, to m-th order?
Like terms (without coefficients) in a m-order power series expansion of an n-variable function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-12-28 20:28:12

虽然 @ruebenko 的解决方案是完全正确的,但我想提一下,由于元组的复杂性和更高的幂/更大数量的变量的复杂性和大量的重复,它会非常慢。权力。这是一种在这些情况下具有更好性能的代数方法(运行时和内存方面):

List @@ Expand[(1 + x + y + z + t)^2] /. a_Integer*b_ :> b

这是对大量变量的比较:

In[257]:= (res1=Union[Times@@@Tuples[{1,x,y,z,t},9]])//Short//Timing
Out[257]= {19.345,{1,t,t^2,t^3,t^4,t^5,t^6,t^7,t^8,t^9,x,<<694>>,x^2 z^7,y z^7,
      t y z^7,x y z^7,y^2 z^7,z^8,t z^8,x z^8,y z^8,z^9}}

In[259]:= (res2=List@@Expand[(1+x+y+z+t)^9]/. a_Integer*b_:>b)//Short//Timing
Out[259]= {0.016,{1,t,t^2,t^3,t^4,t^5,t^6,t^7,t^8,t^9,x,<<694>>,x^2 z^7,y z^7,
      t y z^7,x y z^7,y^2 z^7,z^8,t z^8,x z^8,y z^8,z^9}}

In[260]:= res1===res2
Out[260]= True

在这种情况下,我们观察到 1000 倍的加速,但通常这两种方法只是具有不同的计算复杂度。上面的代码是一种通用且很好的方法的应用,称为代数编程。有关 Mathematica 背景下对此的有趣讨论,请参阅这篇 Mathematica Journal 论文 安杰伊·科兹洛斯基 (Andrzej Kozlowski)。

While the solution of @ruebenko is perfectly correct, I'd like to mention that it will be quite slow for higher powers / larger number of variables, because of the complexity of Tuples and lots of duplicates for higher powers. Here is an algebraic method with a much better performance for those cases (both run-time and memory-wise):

List @@ Expand[(1 + x + y + z + t)^2] /. a_Integer*b_ :> b

Here is a comparison for large number of variables:

In[257]:= (res1=Union[Times@@@Tuples[{1,x,y,z,t},9]])//Short//Timing
Out[257]= {19.345,{1,t,t^2,t^3,t^4,t^5,t^6,t^7,t^8,t^9,x,<<694>>,x^2 z^7,y z^7,
      t y z^7,x y z^7,y^2 z^7,z^8,t z^8,x z^8,y z^8,z^9}}

In[259]:= (res2=List@@Expand[(1+x+y+z+t)^9]/. a_Integer*b_:>b)//Short//Timing
Out[259]= {0.016,{1,t,t^2,t^3,t^4,t^5,t^6,t^7,t^8,t^9,x,<<694>>,x^2 z^7,y z^7,
      t y z^7,x y z^7,y^2 z^7,z^8,t z^8,x z^8,y z^8,z^9}}

In[260]:= res1===res2
Out[260]= True

In this case, we observe a 1000x speedup, but generally the two methods just have different computational complexities. The above code is an application of a general and nice method, called Algebraic Programming. For an interesting discussion of it in the context of Mathematica, see this Mathematica Journal paper by Andrzej Kozlowski.

云淡月浅 2024-12-28 20:28:12

使用 @ruebenko 的简洁解决方案,

varsList[y_, n_?IntegerQ, k_?IntegerQ] := 
Union[Times @@@ 
 Tuples[Prepend[Table[Subscript[y, i], {i, 1, n}], 1], k]]

您可以通过 varsList[x, 4, 2] 生成所需的列表。

Using @ruebenko's neat solution,

varsList[y_, n_?IntegerQ, k_?IntegerQ] := 
Union[Times @@@ 
 Tuples[Prepend[Table[Subscript[y, i], {i, 1, n}], 1], k]]

you can generate desired list via varsList[x, 4, 2].

黯淡〆 2024-12-28 20:28:12

这是我认为值得了解的另一种方法:

set = {1, x, y, z};

Union @@ Outer[Times, set, set]

Here is another method that I believe is worth knowing:

set = {1, x, y, z};

Union @@ Outer[Times, set, set]
时光礼记 2024-12-28 20:28:11

这是你想要的吗?

Union[Times @@@ Tuples[{1, x, y, z}, 2]]

Does this do what you want?

Union[Times @@@ Tuples[{1, x, y, z}, 2]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文