Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function call different result than inline, also not defined on one branch
function scale_sd!(A::SparseMatrixCSC; corrected=true)
n = size(A, 1)
if corrected
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / (n - 1) - (n / (n - 1)) * mn^2)
else
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / n - mn^2)
end
for j in 1:size(A, 2)
k = A.colptr[j]:(A.colptr[j+1] - 1)
if length(k) > 0
v = view(A.nzval, k)
mn = mean(v) * (length(k) / n)
sd = sdcall(v,n,mn)
sd2 = sqrt(sum(vi^2 for vi in v) / (n - 1) - (n / (n - 1)) * mn^2)
println("sdcall is $(sd)\tsdinline is $(sd2)")
A.nzval[k] .= v ./ sd
end
end
return A
end
A = sprand(10, 10, 0.1);
std(A, 1)
scale_sd!(copy(A)); # sdcall and sdinline different, sdinline correct
scale_sd!(copy(A), corrected=false); # ERROR: UndefVarError: sdcall not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment