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

Incorrect broadcasted Period subtraction behaviour? #12094

Closed
GordStephen opened this issue Jul 10, 2015 · 2 comments
Closed

Incorrect broadcasted Period subtraction behaviour? #12094

GordStephen opened this issue Jul 10, 2015 · 2 comments

Comments

@GordStephen
Copy link
Contributor

The behaviour for broadcasted Period subtraction is currently

julia> Day(1) - [Day(2), Day(3)]
2-element Array{Base.Dates.Day,1}:
 1 day 
 2 days

and

julia> Day(1) .- [Day(2), Day(3)]
2-element Array{Base.Dates.Day,1}:
 1 day 
 2 days

whereas presumably the desired output is:

julia> [Day(1) - Day(2), Day(1) - Day(3)]
2-element Array{Base.Dates.Day,1}:
 -1 day 
 -2 days

Looking at dates/periods.jl, the first two method definitions here work fine for + but not -:

for op in (:.+, :.-)
op_ = symbol(string(op)[2:end])
    @eval begin
        ($op){P<:Period}(x::P,Y::StridedArray{P}) = ($op)(Y,x)
        ($op_){P<:Period}(x::P,Y::StridedArray{P}) = ($op)(Y,x)
        ($op_){P<:Period}(Y::StridedArray{P},x::P) = ($op)(Y,x)
    end
end

One option might be to implement unary addition (as identity) and then do

for op in (:.+, :.-)
op_ = symbol(string(op)[2:end])
    @eval begin
        ($op){P<:Period}(x::P,Y::StridedArray{P}) = ($op_)(($op)(Y,x))
        ($op_){P<:Period}(x::P,Y::StridedArray{P}) = ($op_)(($op)(Y,x))
        ($op_){P<:Period}(Y::StridedArray{P},x::P) = ($op)(Y,x)
    end
end

or, just hardcode the definitions as

(.+){P<:Period}(x::P,Y::StridedArray{P}) = Y .+ x
(+){P<:Period}(x::P,Y::StridedArray{P}) = Y .+ x
(+){P<:Period}(Y::StridedArray{P},x::P) = Y .+ x
(.-){P<:Period}(x::P,Y::StridedArray{P}) = (-Y) .+ x
(-){P<:Period}(x::P,Y::StridedArray{P}) = (-Y) .+ x
(-){P<:Period}(Y::StridedArray{P},x::P) = Y .- x

which would actually be fewer LOC... maybe more readable too.

@GordStephen
Copy link
Contributor Author

I'm working on a PR to get addition/subtraction working with heterogeneous Period arrays right now, so I'm happy to fix this while I'm at it - is there a preference between the two approaches mentioned above?

@quinnj
Copy link
Member

quinnj commented Jul 13, 2015

Closed via #12115

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

2 participants