控制符号表达式的输出顺序

Control the order of output for symbolic expressions

我可以控制 MATLAB 符号表达式的输出顺序吗?

例如:

syms x y  
f=y*x   

MATLAB 通常会 return:

f=  
x*y  

但我想得到:

f=  
y*x 

我正在使用 MATLAB R2012b。我该怎么做?

新编辑 2016/11/9
我昨天发现了这些,我觉得很奇怪:
根据 R2008a

%%feedback.m
function H=feedback(G1,G2,key)  
if nargin==2
    key=-1;
end
H=G1/(sym(1)-key*G1*G2);
H=simple(H);

%%matlab command window
syms G1 G2 G3 G4 G5 G6 H1 H2 H3 H4
c1=feedback(G4*G5,H3);
c2=feedback(G2*G3,H2);
c3=feedback(c1*c2,H4/G2/G5);
G=feedback(c3*G1*G6,H1);
pretty(G)

MATLAB returns

G=
G3 G2 G4 G5 G1 G6/(1 + G2 G3 H2 + G4 G5 H3 + G4 G5 H3 G2 G3 H2 + G4 G3 H4 + G3 G2 G4 G5 G1 G6 H1)

不幸的是,我的R2008a没有符号数学工具箱(我已经尝试重装很多次了,都不行)。因此我无法验证它是否属实。我觉得2008a能做到,为什么2012b不能。
我希望这可能对我上面提出的问题有所帮助。

我实际上发现 an answer by @horchler 似乎完全回答了这个问题。尽管它是作为对不同问题的回答给出的。

请注意,我引用了答案,但我不会将其放在引用块中以增强可读性:


我相信它是根据方程中变量名称的 ASCII 值按字母顺序排列的。根据 documentation for solve, sym/symvar is used to parse the equations in the case where you don't supply the names of output variables. The help for sym/symvar indicates that it returns variables in lexicographical order, i.e. alphabetical (symvar does the same, even though it doesn't say so, by making calls to setdiff). If you look at the actual code for solve.m (type edit solve in your command window) and examine the sub-function called assignOutputs (line 190 in R2012b) you'll see that it makes a call to sort 并且有关于字典顺序的评论。

在 R2012b(可能更早)中,文档与 R2013a 的文档有所不同,这似乎与您的问题相关。在 R2013a 中,this sentence is added:

If you explicitly specify independent variables vars, then the solver uses the same order to return the solutions.

我还是 运行 R2012b,所以我无法确认这种不同的行为。

不确定这是否适用于 R2012b,但在我的 R2010a 上,class sym 有一个 disp 方法,在显示您的小功能时是 运行 (当然还有所有 sym 个对象)。这是相关代码:

allstrs = mupadmex(X.s,0);
allstrs = strrep(allstrs,'MLVar','');
disp(allstrs);

其中Xsym对象,s是其中的private属性。

函数 mupadmex() 顾名思义,是一个 MEX 二进制文件。相应的 M 代码包含:

%   MUPADMEX(STMT) executes STMT in MuPAD. STMT must be a string or cell
%   array of strings. A cell array is converted into a MuPAD matrix or array.
%   Y = MUPADMEX(STMT) executes STMT in MuPAD and returns the result as
%   sym object Y. STMT must be a string or cell array of strings. If STMT
%   is a cell Y is a string reference instead of a sym.
%   Y = MUPADMEX(FCN,ARG1,ARG2, ...) evaluates FCN(ARG1,ARG2,...). The inputs
%   must be strings.
%   Y = MUPADMEX(... ,0) returns Y as a string instead of a sym.
%   Y = MUPADMEX(REF ,1) adds REF to the garbage list.
%   Y = MUPADMEX(STMT,2) frees any garbage.
%   Y = MUPADMEX(VAL ,3) formats VAL as 'symr'.
%   Y = MUPADMEX(VAL ,4) formats VAL as 'symfl'.
%   Y = MUPADMEX(VAL ,5) toggles the trace feature. VAL must be 'on' or 'off'.
%   Y = MUPADMEX(STMT,6) resets MuPAD.
%   Y = MUPADMEX(VAL ,7) toggles the pretty-print feature.
%   Y = MUPADMEX(VAL ,8) sets the complex unit. VAL is 'I' or 'sqrtmone'.
%   Y = MUPADMEX(... ,9) returns Y as a logical instead of a sym.
%   Y = MUPADMEX(VAL ,10) toggles the synchronous evaluation mode (out-of-process kernel only).
%   Y = MUPADMEX(... ,11) returns Y as a string reference.
%   Y = MUPADMEX(VAL ,12) print out memory usage
%   Y = MUPADMEX(VAL ,13) toggles lazy evaluation mode
%   Y = MUPADMEX(VAL ,14) evaluates all the lazy statements
%   [Y,STATUS] = ... sets STATUS to 0 if the command completes without error
%   and otherwise sets STATUS to 1 and Y to the error string.

如您所见,它没有说明 格式化 字符串。

所以,简而言之,您可以执行以下操作:

  1. 您必须更改工具箱代码
  2. 像这样写一个显示包装器:

    function disp_sym(X)
    
        str = evalc('X');
        str = regexp(str, '=', 'split');
        fml = strtrim(str{2});
        eq  = str{1};
    
        fml = regexp(fml, '*', 'split');
        fml = fml(end:-1:1);
    
        disp( [eq '=' sprintf('%s*', fml{1:end-1}) fml{end}]);
    
    end
    

    但是,这很快就会变得很糟糕、过于具体且不可移植。