Skip to content

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

Merged
merged 4 commits into from
May 15, 2017
Merged

Conversation

ShikharJ
Copy link
Member

@ShikharJ ShikharJ commented May 9, 2017

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?

@ShikharJ
Copy link
Member Author

Ping @isuruf.

_classes = (symengine.RealDouble,)

def __new__(cls, num, dps=None, prec=None, precision=None):
if prec is not None:
Copy link
Member

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

@isuruf
Copy link
Member

isuruf commented May 11, 2017

What are you planning to do next?

@ShikharJ
Copy link
Member Author

ShikharJ commented May 11, 2017

@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 SymEngine in sympy/parsing and sympy/physics modules (I'll continue on with this till the official coding period begins). Everything as previously planned in my proposal.

@isuruf
Copy link
Member

isuruf commented May 11, 2017

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

@ShikharJ
Copy link
Member Author

ShikharJ commented May 11, 2017

That would be one of my doubts. We're currently using RealMPFR for sympy.Float objects when prec > 53. Should I try interfacing RealMPFR in place of mpmath objects (in the constructor)? Would that be a viable plan?

@isuruf
Copy link
Member

isuruf commented May 11, 2017

Yes, Float class should return a RealDouble if the precision is <= 53 bits and a RealMPFR if not.

@isuruf isuruf modified the milestone: 0.3.0 May 11, 2017
dps = max(dps, len(str(num).lstrip('-')))
dps = max(15, dps)
'''
precision = mlib.libmpf.dps_to_prec(dps)
Copy link
Member Author

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?

Copy link
Member

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

return num
if isinstance(num, str) and _literal_float(num):
try:
Num = decimal.Decimal(num)
Copy link
Member

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.

@isuruf
Copy link
Member

isuruf commented May 14, 2017

I think you are trying too much to do what sympy does.
Do something like this,

  1. convert dps to prec
  2. If prec <= 53 return RealDouble(str(num))
  3. Else return RealMPFR(str(num), prec))

@ShikharJ ShikharJ changed the title [WIP] Additions to symengine/sympy_compat.py Additions to symengine/sympy_compat.py May 14, 2017
if dps is None and precision is None:
dps = 15
if isinstance(num, Float):
return num
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

@ShikharJ ShikharJ May 15, 2017

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?

Copy link
Member

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.

class Float(Number):
_classes = (symengine.RealDouble, symengine.RealMPFR)

def __new__(cls, num, dps=None, prec=None, precision=None):
Copy link
Member

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

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.')
Copy link
Member

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.

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 == '':
Copy link
Member

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

else:
return RealDouble(float(str(num)))
else:
RealDouble(float(str(num)))
Copy link
Member

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

if precision > 53:
return RealMPFR(str(num), precision)
else:
return RealDouble(float(str(num)))
Copy link
Member

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

else:
if isinstance(num, RealDouble):
return RealDouble(num.__float__())
elif isinstance(num, RealMPFR):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is impossible

if precision == num.get_prec():
return num
else:
return RealMPFR(str(num.__float__()), precision)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num.__float__() loses precision

if HAVE_SYMENGINE_MPFR:
if precision > 53:
if isinstance(num, RealDouble):
return RealMPFR(str(num.__float__()), precision)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove __float__() here

if isinstance(num, RealDouble):
return num
elif isinstance(num, RealMPFR):
return RealDouble(num.__float__())
Copy link
Member

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__()

raise ValueError('RealMPFR unavailable for high precision numerical values.')
else:
if isinstance(num, RealDouble):
return RealDouble(num.__float__())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return num here.

@ShikharJ
Copy link
Member Author

@isuruf The builds are failing because the HAVE_SYMENGINE_FLAG is not available to sympy_compat.py and the test_sympy_compat.py. What can be done here?

@isuruf
Copy link
Member

isuruf commented May 15, 2017

@ShikharJ, use have_mpfr in tests

@isuruf isuruf merged commit 6c0febf into symengine:master May 15, 2017
@isuruf
Copy link
Member

isuruf commented May 15, 2017

Thanks for the PR

@ShikharJ
Copy link
Member Author

Thanks for the extensive reviewing @isuruf. You're the best!

@ShikharJ ShikharJ deleted the SympyCompat branch May 15, 2017 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants