-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,022 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ os: | |
- linux | ||
- osx | ||
julia: | ||
- 1.0 | ||
- 1.1 | ||
- 1.2 | ||
- 1.3 | ||
- nightly | ||
matrix: | ||
allow_failures: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
function broadcasted_type(::Broadcast.DefaultArrayStyle{N}, ::Type{Eltype}) where {N, Eltype} | ||
return Array{Eltype, N} | ||
end | ||
function broadcasted_type(::Broadcast.DefaultArrayStyle{N}, ::Type{Bool}) where N | ||
return BitArray{N} | ||
end | ||
|
||
# Same as `Base.Broadcast.combine_styles` but with types as argument. | ||
combine_styles() = Broadcast.DefaultArrayStyle{0}() | ||
combine_styles(c::Type) = Broadcast.result_style(Broadcast.BroadcastStyle(c)) | ||
combine_styles(c1::Type, c2::Type) = Broadcast.result_style(combine_styles(c1), combine_styles(c2)) | ||
@inline combine_styles(c1::Type, c2::Type, cs::Vararg{Type, N}) where N = Broadcast.result_style(combine_styles(c1), combine_styles(c2, cs...)) | ||
|
||
function promote_broadcast(op::Function, args::Vararg{Any, N}) where N | ||
# FIXME we could use `promote_operation` instead as | ||
# `combine_eltypes` uses `return_type` hence it may return a non-concrete type | ||
# and we do not handle that case. | ||
T = Base.Broadcast.combine_eltypes(op, args) | ||
return broadcasted_type(combine_styles(args...), T) | ||
end | ||
|
||
""" | ||
broadcast_mutability(T::Type, ::typeof(op), args::Type...)::MutableTrait | ||
Return `IsMutable` to indicate an object of type `T` can be modified to be | ||
equal to `broadcast(op, args...)`. | ||
""" | ||
function broadcast_mutability(T::Type, op, args::Vararg{Type, N}) where N | ||
if mutability(T) isa IsMutable && promote_broadcast(op, args...) == T | ||
return IsMutable() | ||
else | ||
return NotMutable() | ||
end | ||
end | ||
broadcast_mutability(x, op, args::Vararg{Any, N}) where {N} = broadcast_mutability(typeof(x), op, typeof.(args)...) | ||
broadcast_mutability(::Type) = NotMutable() | ||
|
||
""" | ||
mutable_broadcast!(op::Function, args...) | ||
Modify the value of `args[1]` to be equal to the value of `broadcast(op, args...)`. Can | ||
only be called if `mutability(args[1], op, args...)` returns `true`. | ||
""" | ||
function mutable_broadcast! end | ||
|
||
function mutable_broadcasted(broadcasted::Broadcast.Broadcasted{S}) where S | ||
function f(args::Vararg{Any, N}) where N | ||
return operate!(broadcasted.f, args...) | ||
end | ||
return Broadcast.Broadcasted{S}(f, broadcasted.args, broadcasted.axes) | ||
end | ||
|
||
# If A is `Symmetric`, we cannot do that as we might modify the same entry twice. | ||
# See https://github.com/JuliaOpt/JuMP.jl/issues/2102 | ||
function mutable_broadcast!(op::Function, A::Array, args::Vararg{Any, N}) where N | ||
bc = Broadcast.broadcasted(op, A, args...) | ||
instantiated = Broadcast.instantiate(bc) | ||
return copyto!(A, mutable_broadcasted(instantiated)) | ||
end | ||
|
||
|
||
""" | ||
broadcast!(op::Function, args...) | ||
Returns the value of `broadcast(op, args...)`, possibly modifying `args[1]`. | ||
""" | ||
function broadcast!(op::Function, args::Vararg{Any, N}) where N | ||
return broadcast_fallback!(broadcast_mutability(args[1], op, args...), op, args...) | ||
end | ||
|
||
function broadcast_fallback!(::NotMutable, op::Function, args::Vararg{Any, N}) where N | ||
return broadcast(op, args...) | ||
end | ||
function broadcast_fallback!(::IsMutable, op::Function, args::Vararg{Any, N}) where N | ||
return mutable_broadcast!(op, args...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
abstract type AbstractMutable end | ||
|
||
# Special-case because the the base version wants to do fill!(::Array{AbstractVariableRef}, zero(GenericAffExpr{Float64,eltype(x)})) | ||
_one_indexed(A) = all(x -> isa(x, Base.OneTo), axes(A)) | ||
function LinearAlgebra.diagm(x::AbstractVector{<:AbstractMutable}) | ||
@assert _one_indexed(x) # `LinearAlgebra.diagm` doesn't work for non-one-indexed arrays in general. | ||
ZeroType = promote_operation(zero, eltype(x)) | ||
return LinearAlgebra.diagm(0 => copyto!(similar(x, ZeroType), x)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.