生成 n 个变量的 m 阶幂级数项
考虑这样一种情况,其中数据的列表形式
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 @ruebenko 的解决方案是完全正确的,但我想提一下,由于元组的复杂性和更高的幂/更大数量的变量的复杂性和大量的重复,它会非常慢。权力。这是一种在这些情况下具有更好性能的代数方法(运行时和内存方面):
这是对大量变量的比较:
在这种情况下,我们观察到 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):Here is a comparison for large number of variables:
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.
使用 @ruebenko 的简洁解决方案,
您可以通过 varsList[x, 4, 2] 生成所需的列表。
Using @ruebenko's neat solution,
you can generate desired list via
varsList[x, 4, 2]
.这是我认为值得了解的另一种方法:
Here is another method that I believe is worth knowing:
这是你想要的吗?
Does this do what you want?