matlab - 泰勒级数排序指数

标签 matlab sorting

我有一个 MATLAB 代码,可以生成 3 变量函数 f(x, y, z) 的一般泰勒级数的指数。在下面的示例中,我正在计算三阶泰勒级数。

order = 3;
nTuple = 3;
allExponents = [];
for n = 1 : order
    [~,x] = nsumk(nTuple, n); % nsumk can be downloaded from: https://www.mathworks.com/matlabcentral/fileexchange/28340-nsumk
    allExponents = [allExponents; x]
end

该函数生成以下系数:

0     0     1   % This means z with exponent 1; x and y with zero exponent.
0     1     0   % This means y with exponent 1; x and z with zero exponent.
1     0     0   % This means x with exponent 1; y and z with zero exponent.
0     0     2
0     1     1
0     2     0
1     0     1
1     1     0
2     0     0
0     0     3
0     1     2
0     2     1   % This means x with exponent 0, y with exponent 2 and z with exponent 1, i,e, y^2*z
0     3     0
1     0     2
1     1     1
1     2     0
2     0     1
2     1     0   % This means x with exponent 2 and y with exponent 1 and z with exponent 0, i,e, x^2*y
3     0     0

如何按以下形式对它们进行排序(为清楚起见,它们之间的空行):

1     0     0    % x
0     1     0    % y
0     0     1    % z

2     0     0    % x^2
0     2     0    % y^2
0     0     2    % z^2

3     0     0    % x^3
0     3     0    % y^3
0     0     3    % z^3

1     1     0    % x*y
1     0     1    % x*z
0     1     1    % y*z


1     2     0    % x*y^2
1     0     2    % x*z^2
0     1     2    % y*z^2

2     1     0    % x^2*y
2     0     1    % x^2*z
0     2     1    % y^2*z

1     1     1    % x*y*z

更新: 排序如下:

首先是x^n、y^n、z^n,其中n是从1到泰勒级数的阶数,在这个特定的例子中是3。

然后,每两个指数相加为 2 的变量(例如 xy、xz 和 yz)的交叉乘法。

然后每两个指数加为 3 的变量与第一个指数为 1 的变量进行交叉乘法,即 xy^2、xz^2、yz^2。

然后将指数加为 3 的每两个变量与第一个指数为 2 的变量进行叉乘,即 x^2y、x^2z、y^2z。

最后,指数加为 3 的所有三个变量的乘积,即 xyz。

最佳答案

我相信以下排序规则应该能满足您的需求。

按优先顺序...

  1. 按存在的变量数(非零幂数)降序。
  2. 按术语降序(幂和)。
  3. 非零次幂的字典顺序。
  4. 存在的变量的字典顺序。

实现这些规则有点困惑,但这是代码。

% 1.
num_vars = sum(allExponents~=0, 2);
% 2.
order = sum(allExponents, 2);
% 3. Implemented by pushing all zero-elements to end of the row
[~,j] = sort(allExponents == 0, 2);
[i,~] = ndgrid(1:size(allExponents, 1), 1:nTuple);
sub = sub2ind(size(allExponents), i, j);
squeezed = reshape(allExponents(sub), size(allExponents));
% 4.
lex = allExponents == 0;

% Construct a key and sort
sort_term = [num_vars, order, squeezed, lex];
[~, idx] = sortrows(sort_term);
allExponents = allExponents(idx,:);

附言我不确定这对更高阶或更多变量的概括程度如何。我在构建解决方案时试图牢记这一点,但没有进行测试。

关于matlab - 泰勒级数排序指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50110006/

相关文章:

algorithm - 将整数数组拆分为尽可能多的具有相同总和的子数组

python - PyQt 模型中按多列排序

arrays - 从索引数组创建二进制矩阵

matlab - 二次规划问题

PHP:按值长度对数组进行排序

c++ - cpp std::list 使用步进添加新元素

arrays - scala 中的 FP 排序函数解释

matlab - Matlab 的 perms 函数中的明显错误

matlab - Matlab中如何引用另一个文件

matlab - mtaylor MuPAD - Matlab