I'm in a situation where I have to rewrite some Julia code into Python code and I cannot reproduce this line.
if 1 in array1[array2] || 1 in array1[array3]
In my understanding, this line comparing arrays array1
to array2
and array1
to array3
, to see if the index array2
of array1
is 1
or the index array3
of array1
is 1
.
So, I reproduced this code into python code with my understand,
for i, j in zip(array2, array3):
if array1[i] == 1 or array1[j] == 1:
But this code didn't work like an above code and I got a ValueError like below:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I'm not sure if this is my misunderstanding of the Julia's line or my Python code is wrong.
Could someone tell me what is wrong?
[edit]:
Here is Julia code of this problem. here I am using karate club network as an input matrix
dir = "to/your/path"
ln = "soc-karate.mtx"
mtx = MatrixMarketRead(string(dir,strip(ln)));
A = mtx - spdiagm(diag(mtx))
n = size(A,1);
A = speye(n) - A * spdiagm(1./vec(sum(A,1)));
println(A)
function findDegrees(Ac::SparseMatrixCSC)
degrees = zeros(Int,length(Ac.colptr)-1)
for i = 1:length(degrees)
degrees[i] = Ac.colptr[i+1]-Ac.colptr[i]-1
end
return degrees
end
function lowDegreeNodes(A::SparseMatrixCSC,At::SparseMatrixCSC,d::Int64,dout::Vector,din::Vector)
# 1: find low degree nodes
n = size(A,1)
U = collect(dout.==1)
println(U)
V = collect(din.==1)
Z = min((dout+din) .>= 1 , (dout+din) .<= 8 )
# 2: visited = 0 ==> NotVisited
# = 1 ==> FNode
# = 2 ==> NotEliminated
visited = zeros(length(U))
for u = 1:n
if Z[u]
if visited[u] == 0
Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]
Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
if 1 in visited[Au] || 1 in visited[Av]
visited[u] = 2
else
visited[Au] = 2
visited[u] = 1
end
end
end
if V[u]
if visited[u] == 0
Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]
Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
if 1 in visited[Au] || 1 in visited[Av]
visited[u] = 2
else
visited[Av] = 2
visited[u] = 1
end
end
end
if U[u]
if visited[u] == 0
Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]
Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
if 1 in visited[Au] || 1 in visited[Av]
visited[u] = 2
else
visited[Av] = 2
visited[u] = 1
end
end
end
end
return visited .== 1
end
dout = findDegrees(A)
din = findDegrees(A')
z = lowDegreeNodes(A, A', 3, dout, din)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…