子函数在 Matlab 中模糊地拆分表达式
children function splits expressions ambiguously in Matlab
Matlab 符号工具箱对 children 函数给出了模棱两可的答案。
假设以下示例:
>> syms a b
>> children(a+b)
ans = [ a, b]
>> children(a*b)
ans = [ a, b]
>> children(a^b)
ans = [ a, b]
该函数将不同的表达式拆分为相同的答案,更糟糕的是,Matlab 没有给出任何提示它进行了何种拆分。因此,需要确切地知道输入的串联以进行推理,如何应用子函数。有没有办法应用类似 children 函数的方法,让您知道术语是如何拆分的?
一个天真的解决方法是一个函数,它增强了 matlab 中的子函数。以下可能有效,但有可能并未涵盖所有情况。我愿意对此进行改进:
function [childs, issum, isprod, ispow, isfun] = children2(parent)
%CHILDREN2 Returns the children plus meta info. of math. concatenation
% Author: tkorthals@cit-ec.uni-bielefeld.de
% Input
% parent Symbolic expression
% Output
% childs Vector of symbolic expression
% issum True if parent is sum of children
% isprod True if parent is product of children
% ispow True if parent is power of children
% isfun True if parent is some function's argument
childs = children(parent);
issum = false; isprod = false; ispow = false; isfun = false;
if numel(childs) == 1
if ~isequaln(childs,parent) % functions were removed
isfun = true;
end
return
end
if numel(childs) == 2
if isequaln(childs(1)^childs(2),parent) % pow
ispow = true;
return
elseif isequaln(childs(1)/childs(2),parent) % div
childs = parent;
return
end
end
if isequaln(prod(childs),parent) % prod
isprod = true;
return
end
if isequaln(sum(childs),parent) % sum
issum = true;
return
end
error('children2: Undefined behaviour for more than two children')
end
Matlab 符号工具箱对 children 函数给出了模棱两可的答案。 假设以下示例:
>> syms a b
>> children(a+b)
ans = [ a, b]
>> children(a*b)
ans = [ a, b]
>> children(a^b)
ans = [ a, b]
该函数将不同的表达式拆分为相同的答案,更糟糕的是,Matlab 没有给出任何提示它进行了何种拆分。因此,需要确切地知道输入的串联以进行推理,如何应用子函数。有没有办法应用类似 children 函数的方法,让您知道术语是如何拆分的?
一个天真的解决方法是一个函数,它增强了 matlab 中的子函数。以下可能有效,但有可能并未涵盖所有情况。我愿意对此进行改进:
function [childs, issum, isprod, ispow, isfun] = children2(parent)
%CHILDREN2 Returns the children plus meta info. of math. concatenation
% Author: tkorthals@cit-ec.uni-bielefeld.de
% Input
% parent Symbolic expression
% Output
% childs Vector of symbolic expression
% issum True if parent is sum of children
% isprod True if parent is product of children
% ispow True if parent is power of children
% isfun True if parent is some function's argument
childs = children(parent);
issum = false; isprod = false; ispow = false; isfun = false;
if numel(childs) == 1
if ~isequaln(childs,parent) % functions were removed
isfun = true;
end
return
end
if numel(childs) == 2
if isequaln(childs(1)^childs(2),parent) % pow
ispow = true;
return
elseif isequaln(childs(1)/childs(2),parent) % div
childs = parent;
return
end
end
if isequaln(prod(childs),parent) % prod
isprod = true;
return
end
if isequaln(sum(childs),parent) % sum
issum = true;
return
end
error('children2: Undefined behaviour for more than two children')
end