Created
March 22, 2012 15:58
-
-
Save carlobaldassi/2159174 to your computer and use it in GitHub Desktop.
A very silly Julia bug [NOW FIXED]
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
# Annotated version | |
function calc{T<:Float}(X::Matrix{T}, N...) # change the name of the second argument -> the bug goes away | |
# (type of second argument doesn't matter) | |
# we might even use a single argument N::Matrix{T} | |
# it doesn't matter what's left of the T, it might be | |
# T<:Float64 or T<:Any or even just T | |
#function calc(X::Matrix{Float64}, N::Int) # use this form -> the bug goes away | |
# except where noted | |
println("$(X * X')") # this does _not_ trigger the bug | |
# and gives correct results | |
Z = X # remove this -> the bug goes away | |
# (any assignment involving X triggers the bug) | |
return | |
end | |
function main() | |
L = 3 | |
M = 2 | |
srand(10) # seed is not important | |
X = rand(M, L) | |
calc(X, 0) # removing this -> bug _also_ with alternative getC declaration! | |
new_perml = cell(1) | |
#new_perml = Array(Array{Int64,1},1) # use this instead -> the bug goes away | |
new_perml[1] = randperm(L) # note: the order of these | |
p = randperm(L) # two assignments doesnt'matter | |
@assert length(p) == length(new_perml[1]) | |
@assert typeof(p) == typeof(new_perml[1]) | |
p = new_perml[1] # removing this -> the bug goes away | |
# (using p=copy(newperml[1]) doesn't help) | |
Xp = X[:, p] # use Xp = X instead -> the bug goes away | |
# (using Xp = copy(X[:, p]) doesn't help) | |
calc([X Xp], 0) # remove this (or don't use Xp) -> the bug goes away | |
end | |
main() |
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
# Minimal version | |
function calc{T}(N::Matrix{T}) | |
mN = N | |
#mX = X # this would also trigger the bug | |
return | |
end | |
function main() | |
X = rand(3, 2) | |
pl = cell(1) | |
pl[1] = randperm(3) | |
Xp = X[pl[1],:] | |
Y = [X Xp] | |
calc(Y) | |
end | |
main() |
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 is the same as minimal without main() | |
# and it works! | |
function calc{T}(N::Matrix{T}) | |
mN = N | |
return | |
end | |
X = rand(3, 2) | |
pl = cell(1) | |
pl[1] = randperm(3) | |
Xp = X[pl[1],:] | |
Y = [X Xp] | |
calc(Y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment