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

piecewise: matlab compatibility issue for certain interval forms #1277

Open
NRJank opened this issue Oct 26, 2023 · 3 comments
Open

piecewise: matlab compatibility issue for certain interval forms #1277

NRJank opened this issue Oct 26, 2023 · 3 comments

Comments

@NRJank
Copy link
Collaborator

NRJank commented Oct 26, 2023

the following is matlab compatible but produces an error in Octave 8.3.0 with symbolic package 3.1.1:

>> pkg load symbolic
>> syms x
>> y = piecewise(-2<x<0, 2*x +1);
error: Python exception: TypeError: Can only compare inequalities with Expr
    occurred at line 14 of the Python code block:
    return _op(*_ins)
error: called from
    pycall_sympy__ at line 179 column 7
    elementwise_op at line 102 column 5
    ineq_helper at line 32 column 5
    lt at line 71 column 5
@NRJank NRJank changed the title piecewise: matlab compatibility for certain interval forms piecewise: matlab compatibility issue for certain interval forms Oct 26, 2023
@cbm755
Copy link
Collaborator

cbm755 commented Oct 28, 2023

The real trouble here is -2 < x < 0, which gives the same error on its own.

I know Python allows these sorts of constructions. But does Octave?

Ignoring Symbolic, I get this:

x = 10
-2 < x < 8
ans = 1

I think what its done there is -2 < x is true. true is 1. 1 is less than 8.

Perhaps that doesn't exactly preclude overloading @sym/lt.m to somehow allow -2 < x < 0 to DTRT, but I'm a bit skeptical.

@cbm755
Copy link
Collaborator

cbm755 commented Oct 28, 2023

references:

sympy/sympy#8541

https://docs.sympy.org/latest/modules/core.html#r137

(technically, it being impossible to support -2 < x < 0 directly in SymPy does not preclude us from doing it)


Workaround:

>> y = piecewise(and(-2 < x, x < 0), 2*x + 1)
y = (sym) {2⋅x + 1  for x > -2 ∧ x < 0

@NRJank
Copy link
Collaborator Author

NRJank commented Oct 28, 2023

not surprised that that's what happens in Octave. in Matlab you get the following for the lines that fail above:

>> syms x
>> y = piecewise(-2<x<0, 2*x +1)

y = 
piecewise(x in Dom::Interval(-2, 0), 2*x +1)

and for your interval test, i think your interpretation for non-symbolic is correct. I get the following if i play with the numbers:

>> x = 10;
>> class(x)
ans = 
     'double'
>> -2<x<8
ans = 
    logical
      1

>> x = -1;

>> -2<x<8
ans = 
    logical
      1

>> -2<x<0
ans = 
    logical
      0

not sure how, but it seems overloading the different equality operators may be necessary to enable that sort of interval notation.

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

2 participants