-
Notifications
You must be signed in to change notification settings - Fork 67
Additions to symengine/sympy_compat.py #140
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
Conversation
Ping @isuruf. |
symengine/sympy_compat.py
Outdated
_classes = (symengine.RealDouble,) | ||
|
||
def __new__(cls, num, dps=None, prec=None, precision=None): | ||
if prec is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this. It's already deprecated in sympy
What are you planning to do next? |
@isuruf My short term goal for the next 4-5 days would be to get this and #129 merged in. After that, I'd like to devote my attention towards porting |
Thanks for the update. I particularly meant what are you going to do next in this PR? Float class doesn't really do anything here |
That would be one of my doubts. We're currently using |
Yes, Float class should return a |
symengine/sympy_compat.py
Outdated
dps = max(dps, len(str(num).lstrip('-'))) | ||
dps = max(15, dps) | ||
''' | ||
precision = mlib.libmpf.dps_to_prec(dps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@isuruf Would you be fine with re-implementing dps_to_prec
in symengine.py
? Or would it cause a problem due to the licenses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think dps_to_prec
is a one liner and that's fine
symengine/sympy_compat.py
Outdated
return num | ||
if isinstance(num, str) and _literal_float(num): | ||
try: | ||
Num = decimal.Decimal(num) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need Decimal
support.
I think you are trying too much to do what sympy does.
|
symengine/sympy_compat.py
Outdated
if dps is None and precision is None: | ||
dps = 15 | ||
if isinstance(num, Float): | ||
return num |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, what if num
had a different precision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 57 takes that into account. Rest cases have been accounted for later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it doesn't. dps = 15
is not honoured here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SymPy
sets dps = 15
for cases where the argument is not an instance of Float
. If the num
is an instance of the Float
class and no dps
or precision
is provided, it simply returns that same object. Should I change this? dps = 15
evaluates to precision = 53
, so should I return a RealDouble
each time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be consistent. Also, you can just remove the 2 lines above and the rest of the code below should handle that case.
symengine/sympy_compat.py
Outdated
class Float(Number): | ||
_classes = (symengine.RealDouble, symengine.RealMPFR) | ||
|
||
def __new__(cls, num, dps=None, prec=None, precision=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prec
is not used. You can remove that
symengine/sympy_compat.py
Outdated
elif precision == '' and dps is None or precision is None and dps == '': | ||
if not isinstance(num, (str, unicode)): | ||
raise ValueError('The null string can only be used when ' | ||
'the number to Float is passed as a string or an integer.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what the purpose here is.
symengine/sympy_compat.py
Outdated
if not isinstance(num, (str, unicode)): | ||
raise ValueError('The null string can only be used when ' | ||
'the number to Float is passed as a string or an integer.') | ||
if precision is None or precision == '': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove precision == ''
part. It's unnecessary
symengine/sympy_compat.py
Outdated
else: | ||
return RealDouble(float(str(num))) | ||
else: | ||
RealDouble(float(str(num))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should throw an error if precision > 53
symengine/sympy_compat.py
Outdated
if precision > 53: | ||
return RealMPFR(str(num), precision) | ||
else: | ||
return RealDouble(float(str(num))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be RealDouble(float(num))
, sorry about that
symengine/sympy_compat.py
Outdated
else: | ||
if isinstance(num, RealDouble): | ||
return RealDouble(num.__float__()) | ||
elif isinstance(num, RealMPFR): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is impossible
symengine/sympy_compat.py
Outdated
if precision == num.get_prec(): | ||
return num | ||
else: | ||
return RealMPFR(str(num.__float__()), precision) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
num.__float__()
loses precision
symengine/sympy_compat.py
Outdated
if HAVE_SYMENGINE_MPFR: | ||
if precision > 53: | ||
if isinstance(num, RealDouble): | ||
return RealMPFR(str(num.__float__()), precision) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove __float__()
here
symengine/sympy_compat.py
Outdated
if isinstance(num, RealDouble): | ||
return num | ||
elif isinstance(num, RealMPFR): | ||
return RealDouble(num.__float__()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use float(num)
instead of num.__float__()
symengine/sympy_compat.py
Outdated
raise ValueError('RealMPFR unavailable for high precision numerical values.') | ||
else: | ||
if isinstance(num, RealDouble): | ||
return RealDouble(num.__float__()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return num
here.
@isuruf The builds are failing because the |
@ShikharJ, use |
Thanks for the PR |
Thanks for the extensive reviewing @isuruf. You're the best! |
Relevant: #136
@isuruf The constructor for
Float
is itself quite complex and many of the functionalities used are currently unavailable. I have commented the specific parts as of now. What should be done here?