Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
liondog opened this issue Jan 27, 2015 · 5 comments
Open

Comments

@liondog
Copy link

liondog commented Jan 27, 2015

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'
@cbm755
Copy link
Collaborator

cbm755 commented Jan 29, 2015

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
Copy link
Collaborator

cbm755 commented Jan 29, 2015

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

@cbm755
Copy link
Collaborator

cbm755 commented Oct 4, 2016

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
Copy link
Collaborator

cbm755 commented Oct 4, 2016

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

@CarDeAlb
Copy link

CarDeAlb commented Feb 20, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants