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

maximum gives surprising / wrong results #6724

Closed
mkriele opened this issue May 3, 2014 · 9 comments
Closed

maximum gives surprising / wrong results #6724

mkriele opened this issue May 3, 2014 · 9 comments

Comments

@mkriele
Copy link
Contributor

mkriele commented May 3, 2014

The following uses the current binary: Commit 7bb10f8 on Mac OSX.

julia> c = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> d = [3,4,2]
3-element Array{Int64,1}:
 3
 4
 2

julia> maximum(c,d)
3-element Array{Int64,1}:
 1
 2
 3

julia> maximum(d,c)
1-element Array{Int64,1}:
 4

julia> maximum([c,d])
4

I have three different results for the same operation. maximum(c,d) seems to just return c. I must have chosen unlucky numbers, because other choices of vectors give the correct result. In particular, maximum(d,c) is correct. Though I think it would be better if it returned an Int64 rather than an 1-element Array{Int64,1}. Only maximum([c,d]) returns what I expected. I experienced the same behavior on older versions including 0.2.0

Regards, Marcus

@porterjamesj
Copy link
Contributor

Those three calls aren't the same operation.

Remember that maximum(A, dims) means to get the maximum of an Array over the dimensions given.

So your first call maximum(c, d) means to get the maximum of [1,2,3] along dimensions 3,4, and 2. I would argue this should actually be an error since the object you're asking about doesn't have dimensions 3 or 4. Anyway apparently the way it is interpreted is getting the maximum along dimension 2 (i.e. row-wise), which is exactly what you get.

In the second call, you ask for the maximum of [3,4,2] along dimensions 1, 2, and 3, in other words all of them, which is exactly what you get back.

In the third call, you concatenate the arrays and the ask for the maximum, which is again what you get back.

@blakejohnson
Copy link
Contributor

@mkriele I think you are looking for max, not maximum.

@porterjamesj
Copy link
Contributor

@mkriele if you want to get the maximum of several arrays without allocating new memory to concatenate them, you can use Iterators.chain, e.g.

using Iterators
maximum(chain(c,d))  # 4

@mkriele
Copy link
Contributor Author

mkriele commented May 3, 2014

@porterjamesj many hanks, this clears things up for me and was very instructive. I will use Iterator.chain as suggested by you. I do not think any more that this is a bug. Since my array does not have dimensions 3, 4, we are operating over the empty set, so not doing anything would be okay in my book. I am closing the issue.

@mkriele mkriele closed this as completed May 3, 2014
@porterjamesj
Copy link
Contributor

@mkriele glad to help :)

@ivarne
Copy link
Member

ivarne commented May 3, 2014

This misunderstanding of the region argument to maximum and minimum seems to be a common problem. We should have some sort of error when the region isn't a sensible region (eg, <0, floats, etc...)

@JeffBezanson
Copy link
Member

Yes, that would be helpful. But help(maximum) also clarifies this.

@mkriele
Copy link
Contributor Author

mkriele commented May 3, 2014

I should have typed help(maximum) before posting. However, coming from R I thought I knew what I was doing. It was clear to me that maximum gives the maximum of a vector while max returns the component-wise maximum. Since in R both max(c,d) and max(c(c,d)) would have given the correct result, a corresponding entry in the manual section "Noteworthy differences from R" would also have helped in my case.

@ivarne
Copy link
Member

ivarne commented May 3, 2014

See also #4235 for a lengthy discussion that lead to the separation of max and maximum.

This is a noteworthy difference from R, so this should probably be mentioned there. If you have a good suggestion for the wording, you can actually just edit the file on github and submit a pull request to improve the document.

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

No branches or pull requests

5 participants