ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 04 Oct 2016 13:08:28 -0500Bug with simplicial complexes or computing homology in Sage 7.3?http://ask.sagemath.org/question/35057/bug-with-simplicial-complexes-or-computing-homology-in-sage-73/I have the following code that has been running fine in Sage 6.5––Sage 7.2 on both my Mac OS 10.10.5 and a Unix system. I just tried upgrading to Sage 7.3 on my Mac, and it no longer works correctly. It doesn't crash, it just gives wrong answers.
I will copy the code below, but it reminds me of the issue I had when I posted the following question:
https://ask.sagemath.org/question/9943/homology-of-simplicial-complexes/
The code below is complicated, but the output of sampler(n) is supposed to be a random Q-acyclic simplicial complex with n vertices. So if I write
S=sampler(12)
S.homology()
I should expect to see something like
{0: 0, 1: 0, 1: 0}
or
{0: 0, 1: C2, 1: 0}
as outputs,
but definitely not
{0: 0, 1: Z, 2: Z}
The homology should be trivial or finite groups. But in Sage 7.3 on my Mac, I get Z parts in the homology. I thought my code was broken at first, but copying and pasting it back into Sage 7.2 on my Mac, it seems to work fine.
@cached_function
def f(X):
return (X.transpose() * X).inverse()
def ortho(X):
X.set_immutable()
return X*f(X)* X.transpose()
def TR(M):
D=M.diagonal()
X=GeneralDiscreteDistribution(D)
return X.get_random_element()
def shrink(X,Q,n):
W=Q[:,n]
W.set_immutable()
X2=X-W*f(W)*W.transpose()*X
X3=X2.transpose().echelon_form().transpose()
return X3[:,0:X3.ncols()-1]
def sampler(N):
S=range(1,N+1)
Z = SimplicialComplex(Set(range(1, N+1)).subsets(3))
L=list(Z.n_faces(2))
T=Z.n_skeleton(1)
C=Z.chain_complex()
M=C.differential(2).dense_matrix().transpose()
M2=M.transpose().echelon_form().transpose()
R=M2.rank()
X=M2[:,0:R]
Q=ortho(X)
n=TR(Q)
T.add_face(list(L[n]))
j = binomial(N,2) - N
for i in range(j):
X=shrink(X,Q,n)
Q=ortho(X)
n=TR(Q)
T.add_face(list(L[n]))
return(T)Mon, 03 Oct 2016 21:44:35 -0500http://ask.sagemath.org/question/35057/bug-with-simplicial-complexes-or-computing-homology-in-sage-73/Answer by John Palmieri for <p>I have the following code that has been running fine in Sage 6.5––Sage 7.2 on both my Mac OS 10.10.5 and a Unix system. I just tried upgrading to Sage 7.3 on my Mac, and it no longer works correctly. It doesn't crash, it just gives wrong answers.</p>
<p>I will copy the code below, but it reminds me of the issue I had when I posted the following question:
<a href="https://ask.sagemath.org/question/9943/homology-of-simplicial-complexes/">https://ask.sagemath.org/question/994...</a></p>
<p>The code below is complicated, but the output of sampler(n) is supposed to be a random Q-acyclic simplicial complex with n vertices. So if I write</p>
<pre><code>S=sampler(12)
S.homology()
</code></pre>
<p>I should expect to see something like</p>
<p>{0: 0, 1: 0, 1: 0}
or
{0: 0, 1: C2, 1: 0}
as outputs,</p>
<p>but definitely not
{0: 0, 1: Z, 2: Z}</p>
<p>The homology should be trivial or finite groups. But in Sage 7.3 on my Mac, I get Z parts in the homology. I thought my code was broken at first, but copying and pasting it back into Sage 7.2 on my Mac, it seems to work fine.</p>
<pre><code>@cached_function
def f(X):
return (X.transpose() * X).inverse()
def ortho(X):
X.set_immutable()
return X*f(X)* X.transpose()
def TR(M):
D=M.diagonal()
X=GeneralDiscreteDistribution(D)
return X.get_random_element()
def shrink(X,Q,n):
W=Q[:,n]
W.set_immutable()
X2=X-W*f(W)*W.transpose()*X
X3=X2.transpose().echelon_form().transpose()
return X3[:,0:X3.ncols()-1]
def sampler(N):
S=range(1,N+1)
Z = SimplicialComplex(Set(range(1, N+1)).subsets(3))
L=list(Z.n_faces(2))
T=Z.n_skeleton(1)
C=Z.chain_complex()
M=C.differential(2).dense_matrix().transpose()
M2=M.transpose().echelon_form().transpose()
R=M2.rank()
X=M2[:,0:R]
Q=ortho(X)
n=TR(Q)
T.add_face(list(L[n]))
j = binomial(N,2) - N
for i in range(j):
X=shrink(X,Q,n)
Q=ortho(X)
n=TR(Q)
T.add_face(list(L[n]))
return(T)
</code></pre>
http://ask.sagemath.org/question/35057/bug-with-simplicial-complexes-or-computing-homology-in-sage-73/?answer=35065#post-id-35065I think the issue is that between versions 7.2 and 7.3, Sage changed the way `n_faces` is implemented. If you care about the ordering of the results, use `n_cells` instead. That's what you should do here, because `n_cells` uses an ordering consistent with that of the matrices defining the chain complex. I think the fact that it worked before was a lucky accident -- the ordering in the output from `n_faces` was essentially random in 7.2 (since `n_faces` returns a set), but your code happened to work: I think the old Sage code used the same essentially random ordering when constructing the chain complex, whereas the new code is more careful to order things in a well-defined way and to always use that ordering.
Tue, 04 Oct 2016 13:08:28 -0500http://ask.sagemath.org/question/35057/bug-with-simplicial-complexes-or-computing-homology-in-sage-73/?answer=35065#post-id-35065