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

Deprecate sumabs, sumabs2, minabs, maxabs #19616

Merged
merged 4 commits into from
Dec 17, 2016

Conversation

ararslan
Copy link
Member

As suggested by @andreasnoack in #19598 (comment).

This PR deprecates these functions, as well as their in-place counterparts, in favor of sum, minimum, and maximum with function arguments.

maxabs{T<:Number}(x::AbstractSparseVector{T}) = maxabs(nonzeros(x))
minabs{T<:Number}(x::AbstractSparseVector{T}) = nnz(x) < length(x) ? abs(zero(T)) : minabs(nonzeros(x))
maximum{T<:Number}(f, x::AbstractSparseVector{T}) = maximum(f, nonzeros(x))
minimum{T<:Number}(f, x::AbstractSparseVector{T}) = nnz(x) < length(x) ? f(zero(T)) : minimum(f, nonzeros(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

this isn't always true for functions other than abs

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yeah. Could just do ::typeof(abs) and ::typeof(abs2) then, right?

@@ -1284,8 +1284,7 @@ end
### Reduction

sum(x::AbstractSparseVector) = sum(nonzeros(x))
sumabs(x::AbstractSparseVector) = sumabs(nonzeros(x))
sumabs2(x::AbstractSparseVector) = sumabs2(nonzeros(x))
sum(f, x::AbstractSparseVector) = sum(f, nonzeros(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

this isn't true for functions that aren't zero-preserving

@ararslan ararslan force-pushed the aa/deprecate-sumabs-et-al branch from 4c8f064 to 937c970 Compare December 15, 2016 23:31
Copy link
Contributor

@tkelman tkelman left a comment

Choose a reason for hiding this comment

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

nice code deletion

@@ -1305,8 +1303,14 @@ function minimum{T<:Real}(x::AbstractSparseVector{T})
min(zero(T), minimum(nonzeros(x))))::T
end

maxabs{T<:Number}(x::AbstractSparseVector{T}) = maxabs(nonzeros(x))
minabs{T<:Number}(x::AbstractSparseVector{T}) = nnz(x) < length(x) ? abs(zero(T)) : minabs(nonzeros(x))
for f in [:sum, :maximum, :minimum], op in [:abs, :abs2]
Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine for now since it covers what we already have but I was wondering if we could get something more general (probably not in this PR) with something like

mapreduce(f, op, x::AbstractSparseVector) = op(mapreduce(f, op, nonzeros(x)), repeat(op, f(zero(x[1])), countnz(x)))

where the repeat functions could get specialized, e.g.

repeat(::typeof(+) , x, n::Integer) = x*n

There are probably problems with this approach that I haven't seen yet but it seems nice that it can handle arbitrary return values of the zero input and be fast for the important cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm far from an expert on the intricacies of dealing with sparse structures, so if it's alright I think I'd prefer to punt that to a different PR assuming what I've done here is fine in the interim.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. I think that is the right approach. Just wanted to share my half-baked idea.

Copy link
Member

Choose a reason for hiding this comment

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

Could be a nice optimization, but needs op to commute, right?

Copy link
Member

Choose a reason for hiding this comment

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

@martinholters Right. The definition strengthens the assumptions which wouldn't be okay. I'll say the thing we always say in situations like these: traits could help.

@deprecate maxabs(x) maximum(abs, x)
@deprecate maxabs(A, region) maximum(abs, A, region)
@deprecate sumabs!(r, A) sum!(abs, r, A)
@deprecate sumabs!(r, A; init=false) sum!(abs, r, A; init=false)
Copy link
Contributor

Choose a reason for hiding this comment

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

guess the rhs here should just say init

Copy link
Contributor

Choose a reason for hiding this comment

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

init = init rather

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I knew there had to be a better way to do it than this. Thanks, I'll try that.

Copy link
Contributor

@tkelman tkelman Dec 16, 2016

Choose a reason for hiding this comment

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

This should be tested manually on some inputs where different init values would make a difference, but I think the way it should probably be is init=false init=true on the left, and init=init on the right

Copy link
Member Author

Choose a reason for hiding this comment

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

Why init=false on the left?

Copy link
Contributor

Choose a reason for hiding this comment

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

that's the default value, otherwise init is undefined

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought init=true was the default?

Copy link
Contributor

Choose a reason for hiding this comment

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

then I didn't read the deleted lines in the diff closely enough - the deprecation should try to preserve the signature and behavior of the old method

Copy link
Contributor

@tkelman tkelman left a comment

Choose a reason for hiding this comment

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

deprecation of init kwarg is not right

@kshyatt kshyatt merged commit 9a587b1 into JuliaLang:master Dec 17, 2016
@kshyatt
Copy link
Contributor

kshyatt commented Dec 17, 2016

Really nice pr, @ararslan :)

@ararslan
Copy link
Member Author

Thanks @kshyatt! Glad I can help.

@ararslan ararslan deleted the aa/deprecate-sumabs-et-al branch December 17, 2016 00:07
@KristofferC
Copy link
Member

Benchmarks should be updated to not call the deprecated methods, yes.

@KristofferC
Copy link
Member

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.

7 participants