简体   繁体   English

如何在八度符号中接近零项

[英]how drop near zero terms in Octave symbolic

In this post I describe a symbolic computation I'm doing in Matlab that results in some terms remarkably small, near zero.这篇文章中,我描述了我在 Matlab 中进行的符号计算,它导致某些项非常小,接近于零。 I can run the same code in Octave (after loading the symbolic engine with pkg load symbolic in command window) and get the same results.我可以在 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

Is there a way to tell Octave to make those near-zero values actually zero?有没有办法告诉 Octave 使那些接近零的值实际上为零?

Sample result: Zaa*(1/3 + i/36028797018963968)样本结果: Zaa*(1/3 + i/36028797018963968)

Would like rounded to: Zaa*(1/3)希望四舍五入为: Zaa*(1/3)

Update 1 : My other post on this issue in Matlab has been answered - the solution being to use sqrt(sym(3)) in the a equation instead of just sqrt(3) .更新 1 :我在 Matlab 中关于此问题的一篇文章已得到解答 - 解决方案是在 a 方程中使用sqrt(sym(3))而不是sqrt(3) That solution doesn't work in Octave however.然而,该解决方案在 Octave 中不起作用。 If I run the code below in Octave I get the error blurb shown at bottom.如果我在 Octave 中运行下面的代码,我会得到底部显示的错误信息。

Code:代码:

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)

Here is the resulting error blurb:这是产生的错误简介:

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'

Final update: This code runs in both Octave and Matlab and gives identical results.最终更新:此代码在 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)

If a row contains all non-sym entries then you need to define such a row as a row vector to concatenate it with sym rows.如果一行包含所有非符号条目,那么您需要将这样的行定义为行向量以将其与符号行连接起来。 ie A should be this: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM