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

promote fails with BitArrays #43551

Open
cossio opened this issue Dec 26, 2021 · 1 comment · Fixed by #44096
Open

promote fails with BitArrays #43551

cossio opened this issue Dec 26, 2021 · 1 comment · Fixed by #44096

Comments

@cossio
Copy link
Contributor

cossio commented Dec 26, 2021

promote(randn(4,5), bitrand(2,2))

ERROR: promotion of types Matrix{Float64} and BitMatrix failed to change any arguments
Stacktrace:
[1] error(::String, ::String, ::String)
@ Base ./error.jl:42
[2] sametype_error(input::Tuple{Matrix{Float64}, BitMatrix})
@ Base ./promotion.jl:374
[3] not_sametype(x::Tuple{Matrix{Float64}, BitMatrix}, y::Tuple{Matrix{Float64}, BitMatrix})
@ Base ./promotion.jl:368
[4] promote(x::Matrix{Float64}, y::BitMatrix)
@ Base ./promotion.jl:351
[5] top-level scope
@ REPL[5]:1

Contrast that with,

julia> promote(randn(4,5), rand(-3:3, 2,2))

([1.0404679330906446 0.13605200544037485 … 0.6584649350909583 1.759298096581398; 0.8577584331870316 -0.9385873914104255 … -0.7270472742833658 0.42998694669894727; -1.3226103979280828 1.2291060826052171 … 2.4340481349552263 -0.3980550678017092; 0.3515606302511447 -0.2598112417308294 … -0.2447667146035612 -1.6238446588858528], [2.0 2.0; 0.0 0.0])
@JakobSachs
Copy link
Contributor

JakobSachs commented Jan 3, 2022

I think its failing because theres no definition of a Promotion rule (promote_rule()) for BitMatrix, Matrix.
Something i tried and that seems to work is to create a new rule like:

julia> using Random

julia> import Base.promote_rule

julia> promote_rule(::Type{BitMatrix},::Type{Matrix{T}}) where {T} = Matrix{T}
promote_rule (generic function with 125 methods)

julia> promote(bitrand(2,2),randn(2,2))
([1.0 0.0; 0.0 1.0], [1.3555790148282878 1.1034170972100035; 0.2637702233227674 -0.8688494447101851])

julia> 

This is just the first thing i tried, so there has to be a better way to write it and make it more general (including BitArray/BitVector).

So one fix would be to include a promote_rule for BitMatrix and BitArray in base/bitarray.jl.

Maybe i'm also missing something but thats what i came up with by quickly looking over it.

Edit:

I tried using a rule like:

julia> promote_rule(::Type{BitArray},::Type{Array})= Array                                                              
promote_rule (generic function with 126 methods)                                                                                                                                                                                                                                                                                                                                                                                                                                          

in an attempt to make a generalized rule, but it doesn't seem to work:

julia> promote(BitArray([1,0,1]),Array([1,3,4]))                                                                        
ERROR: promotion of types BitVector and Vector{Int64} failed to change any arguments                                    
Stacktrace:                                                                                                              
 [1] error(::String, ::String, ::String)                                                                                   
  @ Base .\error.jl:42                                                                                                 
 [2] sametype_error(input::Tuple{BitVector, Vector{Int64}})                                                              
  @ Base .\promotion.jl:316                                                                                            
 [3] not_sametype(x::Tuple{BitVector, Vector{Int64}}, y::Tuple{BitVector, Vector{Int64}})                                
  @ Base .\promotion.jl:310                                                                                            
 [4] promote(x::BitVector, y::Vector{Int64})                                                                             
  @ Base .\promotion.jl:293                                                                                            
 [5] top-level scope                                                                                                     
  @ REPL[22]:1                                                                                                                                

JakobSachs added a commit to JakobSachs/julia that referenced this issue Jan 3, 2022
vtjnash added a commit that referenced this issue Feb 9, 2022
Fix #43551
Co-authored-by: Jakob Sachs <[email protected]>
vtjnash added a commit that referenced this issue Feb 13, 2022
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Fix #43551
Co-authored-by: Jakob Sachs <[email protected]>
vtjnash added a commit that referenced this issue Feb 22, 2022
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Add a few good promote_rules too.

Fix #43551
Co-authored-by: Jakob Sachs <[email protected]>
vtjnash added a commit that referenced this issue Mar 1, 2022
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Add a few good promote_rules too.

Fix #43551

Co-authored-by: Jakob Sachs <[email protected]>
staticfloat pushed a commit to JuliaCI/julia-buildkite-testing that referenced this issue Mar 2, 2022
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Add a few good promote_rules too.

Fix JuliaLang#43551

Co-authored-by: Jakob Sachs <[email protected]>
@KristofferC KristofferC reopened this Dec 20, 2022
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 a pull request may close this issue.

3 participants