symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

Rational with symbol as numerator not working?

Open anthonyquizon opened this issue 6 years ago • 7 comments

Hello,

I'm testing out the library and tried to create a rational with a symbol as the numerator:

import symengine
A = symengine.Symbol("A")
B = symengine.Rational(A,10)

However, when I try this, I get the following error:

Traceback (most recent call last):                                                                       
  File "<stdin>", line 1, in <module>                                                                    
  File "symengine_wrapper.pyx", line 1586, in symengine.lib.symengine_wrapper.Rational.__new__           
  File "symengine_wrapper.pyx", line 1642, in symengine.lib.symengine_wrapper.Integer.__new__            
  File "symengine_wrapper.pyx", line 1078, in symengine.lib.symengine_wrapper.Basic.__int__              
  File "symengine_wrapper.pyx", line 1072, in symengine.lib.symengine_wrapper.Basic.__float__
  File "symengine_wrapper.pyx", line 921, in symengine.lib.symengine_wrapper.Basic.n                     
  File "symengine_wrapper.pyx", line 4033, in symengine.lib.symengine_wrapper.evalf
RuntimeError: Symbol cannot be evaluated.

II noticed in this issue that evalf for rationals with symbols has been excluded and thatthe error above runs evalf.

So is something like x/1 intentionally restricted from this library?

However, I'm able to create a rational if the symbol is the denominator:

B = symengine.Rational(10,A)

anthonyquizon avatar Oct 28 '19 00:10 anthonyquizon

I always thought Rational is meant for just numbers. You can create x/1 directly using the division / operator.

certik avatar Oct 28 '19 00:10 certik

Oh great, thats good to know!

anthonyquizon avatar Oct 28 '19 00:10 anthonyquizon

Give this works in sympy, should it not also work in symengine?

eric-wieser avatar Dec 18 '19 14:12 eric-wieser

Changing https://github.com/symengine/symengine.py/blob/740b26fb5832e155d8e1f6d289863a17763b3523/symengine/lib/symengine_wrapper.pyx#L1592 to _sympify(p)/q should work.

isuruf avatar Dec 18 '19 15:12 isuruf

@isuruf should we submit a PR?

certik avatar Dec 18 '19 17:12 certik

Sure. If someone can send a PR, I'll review.

isuruf avatar Dec 18 '19 22:12 isuruf

@eric-wieser do you want to send a PR for this?

certik avatar Dec 18 '19 22:12 certik