Skip to content

Commit

Permalink
Workaround JuliaLang/julia#22907, indmax no longer returning a linear
Browse files Browse the repository at this point in the history
index
  • Loading branch information
martinholters committed Sep 14, 2017
1 parent bd37cde commit ea29dfc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/ACME.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ else
end
end

function _indmax(a::AbstractMatrix)
ind = indmax(a)
if isa(ind, CartesianIndex) # since 0.7.0-DEV.1660
return (ind[1], ind[2])
else
return ind2sub(size(a), ind)
end
end

include("kdtree.jl")
include("solvers.jl")

Expand Down Expand Up @@ -550,7 +559,7 @@ function model_matrices(circ::Circuit, t::Rational{BigInt})
warn("State update depends on indeterminate quantity")
end
while size(nullspace, 2) > 0
i, j = ind2sub(size(nullspace), indmax(map(abs, nullspace)))
i, j = _indmax(map(abs, nullspace))
nullspace = nullspace[[1:i-1; i+1:end], [1:j-1; j+1:end]]
f = f[:, [1:j-1; j+1:end]]
for k in [:fv; :fi; :c; :fq]
Expand Down Expand Up @@ -587,7 +596,7 @@ function tryextract(fq, numcols)
for colcnt in 1:numcols
# determine element with maximum absolute value in unprocessed columns
# to use as pivot
i, j = ind2sub(size(fq), indmax(map(abs, fq[:,colcnt:end])))
i, j = _indmax(map(abs, fq[:,colcnt:end]))
j += colcnt-1

# swap pivot to first (unprocessed) column
Expand Down Expand Up @@ -1000,7 +1009,7 @@ function rank_factorize(a::SparseMatrixCSC)
nullspace = gensolve(a', spzeros(size(a, 2), 0))[2]
c = eye(eltype(a), size(a, 1))
while size(nullspace, 2) > 0
i, j = ind2sub(size(nullspace), indmax(map(abs, nullspace)))
i, j = _indmax(map(abs, nullspace))
c -= c[:, i] * nullspace[:, j]' / nullspace[i, j]
c = c[:, [1:i-1; i+1:end]]
nullspace -= nullspace[:, j] * vec(nullspace[i, :])' / nullspace[i, j]
Expand Down
4 changes: 2 additions & 2 deletions src/kdtree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function KDTree(p::AbstractMatrix, Np=size(p,2))
return KDTree{typeof(cut_val),typeof(p)}(cut_dim, cut_val, [1], p)
end

dim = indmax(var(p[:,1:Np],2))
dim = indmax(vec(var(p[:,1:Np],2)))
p_idx = sortperm(vec(p[dim,:]))

min_idx[1] = 1
Expand All @@ -56,7 +56,7 @@ function KDTree(p::AbstractMatrix, Np=size(p,2))
min_idx[n] = cut_idx[parent_n]+1
max_idx[n] = max_idx[parent_n]
end
dim = indmax(var(p[:,p_idx[min_idx[n]:max_idx[n]]],2))
dim = indmax(vec(var(p[:,p_idx[min_idx[n]:max_idx[n]]],2)))
idx = sortperm(vec(p[dim,p_idx[min_idx[n]:max_idx[n]]]))
p_idx[min_idx[n]:max_idx[n]] = p_idx[idx + min_idx[n] - 1]
cut_idx[n] = calc_cut_idx(min_idx[n], max_idx[n])
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end
let ps = rand(6, 10000)
t = ACME.KDTree(ps)
p = rand(6)
best_p = ps[:,indmin(sum(abs2, broadcast(-, ps, p),1))]
best_p = ps[:,indmin(vec(sum(abs2, broadcast(-, ps, p),1)))]
idx = ACME.indnearest(t, p)
@test sum(abs2, p - best_p) sum(abs2, p - ps[:, idx])
end
Expand Down

0 comments on commit ea29dfc

Please sign in to comment.