Skip to content

add as_inexact option to _lib._util._asarray_validated #4869

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 3 commits into from
Jun 22, 2015

Conversation

argriffing
Copy link
Contributor

In general, SciPy linear algebra functions prefer input arrays that don't contain NaNs, that are not object arrays or masked arrays, and that are dense (not sparse). Currently _asarray_validated checks all of those properties. This PR adds the additional option to convert the input array to a dtype that is more palatable to BLAS/LAPACK functions, and it should address one of the complaints in #4866.

@ev-br
Copy link
Member

ev-br commented May 13, 2015

Maybe add a bit more flexibility by adding a dtype kwarg instead: _asarray_validated(..., dtype=None). Then dtype=None would mean to keep the input dtype (just like as_inexact=False is doing here), otherwise use toarray(.., dtype=dtype)?

@argriffing
Copy link
Contributor Author

@ev-br There is some discussion about that kind of thing at numpy/numpy#5634, especially regarding extensions for flexibility.

dtype=None would mean to keep the input dtype (just like as_inexact=False is doing here), otherwise use toarray(.., dtype=dtype)

As I understand it, this suggestion would need at least some modification. For example the code in this PR is intended to convert int but not complex to float when as_inexact=True.

The TravisCI tests are failing because of test timeouts and also because the code is incompatible with supported numpy 1.6.
asarray_chkfinite() got an unexpected keyword argument 'dtype'

@ev-br
Copy link
Member

ev-br commented May 13, 2015

Ah, OK. Indeed, it might be better to keep it simple for now and delegate additional sophistication to numpy. The numpy 1.6 limitation is I guess easy to work around with a try... except TypeError: (..).astype(np.float_) --- unless we want to bump the minimum numpy version again for scipy 0.17 :-).

@rgommers rgommers added the maintenance Items related to regular maintenance tasks label May 17, 2015
@ev-br
Copy link
Member

ev-br commented Jun 22, 2015

OK, this should not fall through the cracks. I've tested it locally and it seems to do what it says on the box:

>>> a
array([1, 1, 1])
>>> a.dtype
dtype('int64')
>>> from scipy._lib._util import _asarray_validated as av
>>> av(a, as_inexact=True).dtype
dtype('float64')
>>> 
>>> b = np.ones(3, dtype=np.complex128) * 2j
>>> b.dtype
dtype('complex128')
>>> av(b, as_inexact=True).dtype
dtype('complex128')
>>>
>>> c = np.ones(3)
>>> cc = av(c, as_inexact=True)
>>> np.may_share_memory(c, cc)
True

Until numpy grows the relevant functionality, this should do the it (and we'll see how much usage this actually gets).
Merging, thanks @argriffing.

ev-br added a commit that referenced this pull request Jun 22, 2015
add as_inexact option to _lib._util._asarray_validated
@ev-br ev-br merged commit 30880cb into scipy:master Jun 22, 2015
@ev-br ev-br added this to the 0.17.0 milestone Jun 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants