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

Support randmatrix() for mp.iv and mp contexts #527

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

MaxGaukler
Copy link
Contributor

Before, mpmath.iv.randmatrix() caused an error. Now it works correctly:

>>> import mpmath
>>> mpmath.iv.randmatrix(3)
matrix(
[['[0.66382280562735496154, 0.66382280562735496154]', '[0.016861908353603016764, 0.016861908353603016764]', '[0.73734056129022396142, 0.73734056129022396142]'],
 ['[0.36647301100616180847, 0.36647301100616180847]', '[0.62220372249504873885, 0.62220372249504873885]', '[0.48761902415903068952, 0.48761902415903068952]'],
 ['[0.81322606973113087392, 0.81322606973113087392]', '[0.15903271707414323721, 0.15903271707414323721]', '[0.090983058161586249923, 0.090983058161586249923]']])
>>> type(mpmath.fp.randmatrix(3)[0,0])
<type 'float'>
>>> type(mpmath.mp.randmatrix(3)[0,0])
<class 'mpmath.ctx_mp_python.mpf'>
>>> type(mpmath.iv.randmatrix(3)[0,0])
<class 'mpmath.ctx_iv.ivmpf'>
  • Is it okay that I moved import random to the top level? AFAIK it should even be faster this way.

@fredrik-johansson
Copy link
Collaborator

There is a small problem: random.random() returns a random double, so this will not give full precision random numbers.

@mgmax
Copy link

mgmax commented Mar 23, 2020

Should be better now. I added tests, but am not sure how workprec() is supposed to work for iv. Is that a bug or by design?

@MaxGaukler
Copy link
Contributor Author

By the way, please let me know as soon as you consider it ready for merging so that I can provide a rebased version of this pull request as a single commit.

@skirpichev
Copy link
Collaborator

@MaxGaukler could you please fix merge conflicts?

@MaxGaukler MaxGaukler force-pushed the randmatrix branch 2 times, most recently from d92aa5e to 7f5cf5e Compare May 11, 2023 17:57
@MaxGaukler
Copy link
Contributor Author

Now squashed and rebased. Happy to see that there is interest in my suggestion :-)

mpmath/ctx_fp.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

There are also merge conflicts. Please fix them.

Except for comment in the ctx_fp.py and above remark - seems ok for me.

To make this work correctly, rand() now supports arbitrary precision:

>>> x = mpmath.iv.rand() * mpmath.iv.convert(2**53)
>>> x - int(x)
mpi('0.0', '0.0')
>>> mpmath.iv.prec=1234
>>> x = mpmath.iv.rand() * mpmath.iv.convert(2**53)
>>> x - int(x)
mpi('0.05786680052076454587 ...
@skirpichev skirpichev merged commit 42a77be into mpmath:master Jun 26, 2023
7 checks passed
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.

None yet

4 participants