Skip to content

Fix for cov() failing when a complex input matrix is given - #2716

Closed
vaamarnath wants to merge 3 commits into
numpy:masterfrom
vaamarnath:local
Closed

vaamarnath wants to merge 3 commits into
numpy:masterfrom
vaamarnath:local

Conversation

@vaamarnath

Copy link
Copy Markdown

Fix for issue #2680

@njsmith

njsmith commented Nov 8, 2012

Copy link
Copy Markdown
Member

(The travis failures are spurious.)

This needs tests added before we could commit it.

It seems like unconditionally casting input to complex will have nasty effects. Does this mean that cov() will always return a complex array, even for real inputs? It should really return an array of the same type as its input (modulo casting the two inputs onto each other, possible upcasting from integer -> float, that sort of thing).

@vaamarnath

Copy link
Copy Markdown
Author

@njsmith Yeah, I understand what I have done is not the optimum solution. I would like to get some help in this regard.

Could you give me some pointers to how to deal this case? Like how to find the highest type in the passed arrays. The only thing I can think of is iterating through the list and getting the highest type. Then I can cast to that type.

Yet another issue I face for this solution is that, I am not able to compare the types in python3 using >, < operators. In python2, I am able to do this.

@seberg

seberg commented Jan 19, 2013

Copy link
Copy Markdown
Member

@vaamarnath do you still want to fix this? I think a decent method is probably np.result_type (after the asarray, though), so you could do np.result_type(X, 1.) or if y is defined include that too. And then cast X to that dtype to allow those in-place operations. Though maybe someone else knows a better method to handle these cases...
(Object dtypes are not blantly cast to double like that, meaning also that there is no warning if someone puts in a complex number, maybe those should be cast explicitly to keep the old behaviour)

As for the result, I think it is always real (even with complex input), right? So I think cov should return .real. And you should extend the tests in numpy/lib/test/test_function_base.py.

(If you want, while you are at it, you could remove that empty array special case and fix the tests for cov and corrcoef, I am certain that everything in the function handles empty arrays correctly, but the result the special case gives – and that is tested – is incorrect)

@charris

charris commented Mar 2, 2013

Copy link
Copy Markdown
Member

ping travisbot.

@charris charris closed this Mar 2, 2013
@charris charris reopened this Mar 2, 2013
@charris

charris commented Mar 2, 2013

Copy link
Copy Markdown
Member

@seberg I'm tempted to close this for lack of activity. What do you think.

@seberg

seberg commented Mar 2, 2013

Copy link
Copy Markdown
Member

Agreed, though I would be happy to see it reopened.

@seberg seberg closed this Mar 2, 2013
@Nodd

Nodd commented Oct 8, 2013

Copy link
Copy Markdown
Contributor

Could you reopen this issue please ? I think the right fix is by using iscomplexobj:

from numpy.lib import iscomplexobj

# ...

    if iscomplexobj(m):
        X = array(m, ndmin=2, dtype=complex)
    else:
        X = array(m, ndmin=2, dtype=float)

A simple test case can be found in #597.

I will try to do a full PR soon if this one is completely abandoned.

@seberg

seberg commented Oct 8, 2013

Copy link
Copy Markdown
Member

Actually, I can't (not sure why), but since there is an issue about it and this was a pull request, I don't really see why it should be reopened, unless the original owner wants to resurrect it. This PR was closed because it seemed abandoned, so please do put together a new one.

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.

5 participants