symbolic icon indicating copy to clipboard operation
symbolic copied to clipboard

Assigning a symbolic expression to an element of a numerical matrix fails

Open liondog opened this issue 10 years ago • 5 comments

Once again this is a piece of code that I wrote in MATLAB and failed to execute in Octave with the most recent octsympy. Assume the following code

syms x
x = 2*pi/1853; % arbitrary symbolic expression that only contains symbolic variables that have a 
               % direct numerical equivalent, i.e. x = sym(0.113242); is also fine, or x = sym(e);

If one assigns

y = x;

y will become a symbolic variable, too. But the situation is different for numerical matrices. If you assign x to an arbitrary element of a numerical matrix, MATLAB automatically tries to convert the symbolic expression to a numerical value with the double function and throws an error if it fails ("Error in MuPAD command: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead."). For instance

(...)
syms t
y = zeros(1, 2);
y(1, 2) = x; % this works just fine in MATLAB
y(1, 2) = sym(pi*t); % this fails in MATLAB

in MATLAB the next-to-last line works and y is still of type double, in Octave both assignments to y fail with

error: operator =: no conversion for assignment of 'class' to indexed 'matrix'
error: assignment failed, or no method for 'matrix = class'

liondog avatar Jan 27 '15 16:01 liondog

Simpler example:

x = sym(2)
y = [0 0]
y(1) = x
error: operator =: no conversion for assignment of 'class' to indexed 'matrix'
error: assignment failed, or no method for 'matrix = class'

This works using octsympy on Matlab, so I think this is an Octave bug. Octave should try y(1) = double(x) automatically as Matlab does.

This is similar to https://savannah.gnu.org/bugs/?43097 where if (sym(1)) is false. It should do if (logical(sym(1))) internally.

cbm755 avatar Jan 29 '15 00:01 cbm755

AFAIK, nothing I can do about this, but I will comment on the upstream bug.

cbm755 avatar Jan 29 '15 00:01 cbm755

The logical thing was fixed in upstream Octave 4.2, thanks to @mtmiller.

But I think the double thing is still a bug. Leaving open, will chase upstream.

cbm755 avatar Oct 04 '16 16:10 cbm755

Filed new upstream bug: https://savannah.gnu.org/bugs/index.php?49267

cbm755 avatar Oct 04 '16 17:10 cbm755

Hi, try this: x = sym(2) y= sym([0 0]) % or sym(zeros(2,1)) y(1) = x I hope it works for you!

CarDeAlb avatar Feb 20 '18 09:02 CarDeAlb