[英]How to convert a symbolic expression to a Octave function from the Symbolic Package?
[英]how drop near zero terms in Octave symbolic
在这篇文章中,我描述了我在 Matlab 中进行的符号计算,它导致某些项非常小,接近于零。 我可以在 Octave 中运行相同的代码(在命令窗口中使用pkg load symbolic
加载符号引擎之后)并获得相同的结果。
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(3)/2*1i;
A = [1 1 1; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
有没有办法告诉 Octave 使那些接近零的值实际上为零?
样本结果: Zaa*(1/3 + i/36028797018963968)
希望四舍五入为: Zaa*(1/3)
更新 1 :我在 Matlab 中关于此问题的另一篇文章已得到解答 - 解决方案是在 a 方程中使用sqrt(sym(3))而不是sqrt(3) 。 然而,该解决方案在 Octave 中不起作用。 如果我在 Octave 中运行下面的代码,我会得到底部显示的错误信息。
代码:
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(sym(3))/2*1i; % the sym(3) helps as
A = [1 1 1; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
vpa(3*Z012,2) % to clean up the display (remember to put a 1/3 factor in front of result)
这是产生的错误简介:
Symbolic pkg v3.0.0: Python communication link active, SymPy v1.10.1.
Z = (sym 3x3 matrix)
[Zaa Zab Zac]
[ ]
[Zba Zbb Zbc]
[ ]
[Zca Zcb Zcc]
warning: passing floating-point values to sym is dangerous, see "help sym"
warning: called from
double_to_sym_heuristic at line 50 column 7
sym at line 379 column 13
plus at line 53 column 5
symbolic_similarity_transformation at line 11 column 3
error: octave_base_value::map_value(): wrong type argument 'scalar'
最终更新:此代码在 Octave 和 Matlab 中运行并给出相同的结果。
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(sym(3))/2*1i;
A = [[1 1 1]; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
vpa(3*Z012,2) % to clean up the display (remember to put a 1/3 factor in front of result)
如果一行包含所有非符号条目,那么您需要将这样的行定义为行向量以将其与符号行连接起来。 即A
应该是这样的:
A = [[1 1 1]; 1 a^2 a; 1 a a^2]; %works in both Octave and MATLAB
% ↑ ↑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.