Search code examples
octavesymbolic-math

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. 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.

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?

Sample result: Zaa*(1/3 + i/36028797018963968)

Would like rounded to: 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). That solution doesn't work in Octave however. If I run the code below in Octave I get the error blurb shown at bottom.

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.

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)

Solution

  • 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. i.e. A should be this:

    A = [[1 1 1]; 1 a^2 a; 1 a a^2];   %works in both Octave and MATLAB
    %    ↑     ↑