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

more type-stable reductions #6116

Merged
merged 1 commit into from
Mar 12, 2014
Merged

Conversation

stevengj
Copy link
Member

This patch tries to make reductions, especially sum and prod, as type-stable as is feasible (#6069). Key strategies:

  • For reductions of two or more elements, always initialize the reduction variable as v = op(A[1], A[2]) rather than as v = A[1].
  • For summation of empty collections, return zero(T)+zero(T) if T is known, rather than zero(T). (Similarly for prod and one.)
  • For summations of single-element collections, return A[1] + zero(A[1]) rather than A[1]. (Similarly for prod and one.)

sum and prod of empty collections for which the eltype is unknown now throw an exception rather than returning 0 (which is the wrong answer for non-Number collections).

cc: @jiahao, @lindahua, @StefanKarpinski

@stevengj
Copy link
Member Author

(Note that this only affects reduce and friends. I haven't tried to patch up reducedim.)

@StefanKarpinski
Copy link
Member

Looks good to me. Thanks for doing this.

JeffBezanson added a commit that referenced this pull request Mar 12, 2014
@JeffBezanson JeffBezanson merged commit 8c4111c into JuliaLang:master Mar 12, 2014
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.

3 participants