def test_with(m): i = sage.all.identity_matrix(m.nrows()).column(1).column() assert i.nrows() == m.nrows() mZ = flint.fmpz_mat(m) mQ = flint.fmpq_mat((Integer(1), mZ)) iZ = flint.fmpz_mat(i) iQ = flint.fmpq_mat((Integer(1), iZ)) try: sS = m.solve_right(i) except: write("-") singular_test(mZ, mQ, iZ, iQ) return Xd, d = mZ.solve_right(iZ) S_by_d_sage = sage.all.matrix(ZZ, d * sS) S_by_d = flint.fmpz_mat(S_by_d_sage) if Xd != S_by_d: print "integer solve_right() failed, m=\n", m sys.exit(1) X = flint.fmpq_mat((d, Xd)) Y = mQ.solve_dixon(iQ) assert X == Y write("+") if watch_canonical: " check if raw_str() of X and Y are equal " Xr, Yr = X.raw_str(), Y.raw_str() if Xr != Yr: print "m=\n", m print "Xr=%s" % Xr print "Yr=%s" % Yr
def test_with( m ): ''' sage method only called once, do not need to clean cache ''' global t_sage, t__inv, t_matz, t_Dixon i=sage.all.identity_matrix( m.nrows() ) mZ=flint.fmpz_mat( m ) mQ=flint.fmpq_mat( (Integer(1), mZ) ) iZ=flint.fmpz_mat( i ) iQ=flint.fmpq_mat( (Integer(1),iZ) ) try: t0=time.time() sS=m.inverse() except: t_sage += time.time()-t0 # ups, this subroutine can be removed, so # the whole file could be a dozen lines shorter singular_test( mZ, mQ, iZ, iQ ) return t1=time.time() t_sage += t1-t0 Xd,d=mZ.solve_right(iZ) t2=time.time() t_matz += t2-t1 Y=mQ.solve_dixon( iQ ) t3=time.time() t_Dixon += t3-t2 W=mQ.inverse() t__inv += time.time()-t3
def test(a,b,loud): assert b>1 assert b<2**64 c=a % b n=flint.nmod_mat(flint.fmpz_mat(a),Integer(b)) d=n.export_nonnegative_fmpz_mat() if d != flint.fmpz_mat(c): print 'mismatch, sage matrice=\n',c print 'd=\n',d assert 0 if loud: print c
def test(a,loud): global t__ntl,t_mine b=abs( a.determinant() ) if b<2 or b>=2**64: print 'bad determinant, internal error' sys.exit(1) t=ntl_HNF_mirror(a) if loud: print 'a=\n',a # benchmark t0=time.time() nmod_r=flint.fmpz_mat_hermite_form( flint.fmpz_mat( a ), b ) t1=time.time() t=t.HNF(b) t2=time.time() # convert result nmod_r=nmod_r.export_sage() if loud: print 'hnf(a)=\n',nmod_r t = ntl_HNF_unmirror( t ) if nmod_r != t: print 'bad result: ntl\n',t sys.exit(1) # update time t__ntl += t2-t1 t_mine += t1-t0
def det_flint(a,way): global t1,tx aF=flint.fmpz_mat( a ) t0=time.time() one=flint.det(aF) t1[way] += time.time()-t0 if loud: print one b=sage.all.copy(a) b[0,0] += 1 bF=flint.fmpz_mat( b ) t0=time.time() not_one=flint.det(bF) tx[way] += time.time()-t0 if loud: print not_one
def random_data(dim): ' returns matrice and absolute value of its determinant ' global coin_fell_on_the_edge n = m = dim while 1: A=sage.all.random_matrix( ZZ, dim, x=-100, y=100 ) B = A.matrix_from_rows(range(m-2)).matrix_from_columns(range(n-1)) c = A.matrix_from_rows([m-2]).matrix_from_columns (range(n-1)) d = A.matrix_from_rows([m-1]).matrix_from_columns (range(n-1)) try: (d1,d2) = double_det (B,c,d, proof=True) except: print 'coin fell on the edge, point 0' coin_fell_on_the_edge[0] += 1 continue (g,k,l) = d1._xgcd (d2, minimal=True) assert g >= 0 if not shut_up: print "Stein double-det g=%s"%g CUTOFF = 2**30 # aint not interesting to count HNF of unimodular matrice, # so disallow g=1 if g==1 or 2*g > CUTOFF: if not shut_up: print 'bad det, going back' continue W = B.stack (k*c + l*d) not_g=abs( flint.det(flint.fmpz_mat( W )) ) if not_g != g: print 'coin fell on the edge, real det=%d != %d' (not_g,g) coin_fell_on_the_edge[1] += 1 return W,g
def do_with( m ): global t_flint,t_sage i=sage.all.identity_matrix( m.nrows() ) mF,iF=flint.fmpz_mat( m ),flint.fmpz_mat( i ) try: t0=time.time() s=m.solve_right(i) except: pass t1=time.time() t_sage += t1-t0 Xd,d=mF.solve_right(iF) t_flint += time.time()-t1 if dim == m[0,0] // 1000: # this never happens print s,d
def det_flint(a,way): global t1,tx,strange_det aF=flint.fmpz_mat( a ) t0=time.time() one=flint.det(aF) t1[way] += time.time()-t0 if abs(one) != 1: print 'bad determinant, flint' print 'a=\n',a b=sage.all.copy(a) b[0,0] += 1 bF=flint.fmpz_mat( b ) t0=time.time() not_one=flint.det(bF) tx[way] += time.time()-t0 if abs(not_one)==1: strange_det += 1
def test_m(m): a=flint.fmpz_mat( m ) dG = flint.det(a) dB = flint.det_20140704( a ) if dG==dB: #sys.stdout.write('.') return print 'test failed, det good/bad=%s/%s' % (dG,dB) print m assert 0
def test_m(m,s): a=flint.fmpz_mat( m ) i = is_singular(a) if s and i: return d = flint.det_20140704( a ) if d: assert i==0 else: assert i
def test(a,loud): b=abs( a.determinant() ) if b<2 or b>=2**63: print 'bad determinant, internal error' sys.exit(1) sage_r=a.hermite_form() for mult in 1,2: if loud: print '\n\n\n' test_dump(a,sage_r) print '\n\n' nmod_r=flint.fmpz_mat_hermite_form( flint.fmpz_mat( a ), b*mult ) if nmod_r != flint.fmpz_mat( sage_r ): if not loud: test_dump(a,sage_r) print 'mult=',mult nmod_r=nmod_r.export_sage() print 'nmod result:\n',nmod_r det_sage=mult_diag(sage_r) det_nmod=mult_diag(nmod_r) print 'b=%X det_sage=%X det_nmod=%X' % (b,det_sage,det_nmod) sys.exit(1)
def test_with( m ): i=sage.all.identity_matrix( m.nrows() ) mZ=flint.fmpz_mat( m ) mQ=flint.fmpq_mat( (Integer(1), mZ) ) iZ=flint.fmpz_mat( i ) iQ=flint.fmpq_mat( (Integer(1),iZ) ) try: sS=m.inverse() except: write('-') singular_test( mZ, mQ, iZ, iQ ) return Xd,d=mZ.solve_right(iZ) S_by_d_sage=sage.all.matrix( ZZ, d*sS ) S_by_d=flint.fmpz_mat( S_by_d_sage ) if Xd != S_by_d: print 'integer solve_right() failed, m=\n',m sys.exit(1) X=flint.fmpq_mat( (d, Xd) ) Y=mQ.solve_dixon( iQ ) W=mQ.inverse() assert X == Y assert Y == W write('+') if watch_canonical: ' check if raw_str() of X, Y and W differ ' Xr,Yr,Wr=X.raw_str(),Y.raw_str(),W.raw_str() m_printed=0 if Xr != Yr: print 'm=\n',m print 'Xr=%s' % Xr print 'Yr=%s' % Yr m_printed=1 if Yr != Wr: if not m_printed: print 'm=\n',m print 'Yr=%s' % Yr print 'Wr=%s' % Wr
def test_m(m,p,k,p_deg_k): dG = m.det() % p_deg_k if 0: print 'p=%s k=%s p**k=%s' % (p,k,p_deg_k) print 'm=\n',m dB = flint.det_mod_pk_3arg( flint.fmpz_mat(m), p, k ) if dG==dB: if 0: print 'test passed, det good/bad=%s/%s' % (dG,dB) return print 'p=%s k=%s p**k=%s' % (p,k,p_deg_k) print 'test failed, det good/bad=%s/%s' % (dG,dB) print m assert 0
def test_with(m, precook): i=sage.all.identity_matrix( m.nrows() ) mF=flint.fmpz_mat( m ) # leave i as-is or convert to fmpz_mat if precook: iF=flint.fmpz_mat( i ) else: iF=i try: s=m.solve_right(i) # yep, this line works twice for every matrice except: print '\n singular case, dim=%s, calling flint solve_right()' % m.nrows() try: Xd,d=mF.solve_right(iF) except: print 'singular matrice, exception in flint solve_right()' return print '\n singular case, no exception in flint solve_right()' assert d==0 write('0 ') return # matrix non-singular, can do arithmetic Xd,d=mF.solve_right(iF) # ups, where is that Matrix_integer_dense? #Sd=sage.all.Matrix_integer_dense( d*s ) Sd=sage.all.matrix( ZZ, d*s ) Sd=flint.fmpz_mat( Sd ) if Xd != Sd: print '\n test failed,\n m=\n%s' % m print 's=%s' % s print 'd=%d' % d print 'd=%Xd' % Xd sys.exit(1) if 0: write('+') else: write( ' %s' % d )
def test_with( W, g ): global t_sage,t_mine t0=time.time() sage = W._hnf_mod(2*g) t_sage += time.time()-t0 for i in range(3): t1=time.time() r=flint.fmpz_mat_hermite_form( flint.fmpz_mat( W ), g*(i+1) ) t_mine[i] += time.time()-t1 if( r.export_sage() != sage ): print 'result incorrect, g=%s mult=%s\n' % (g,i+1) print W.str() print '\n',sage.str() print '\n',r.export_sage().str() sys.exit(1)
def test_with(m): global t_sage, t_matz, t_matq i = sage.all.identity_matrix(m.nrows()).column(1).column() mZ = flint.fmpz_mat(m) mQ = flint.fmpq_mat((Integer(1), mZ)) iZ = flint.fmpz_mat(i) iQ = flint.fmpq_mat((Integer(1), iZ)) singular = 0 t0 = time.time() try: sS = m.solve_right(i) except: write("-") singular = 1 t1 = time.time() Xd, d = mZ.solve_right(iZ) t2 = time.time() Y = mQ.solve_dixon(iQ) t3 = time.time() t_sage += t1 - t0 t_matz += t2 - t1 t_matq += t3 - t2 if not singular: write("+")
def test_1( a, a1, p, k): p_deg_k=p**k if 0: print 'p=%s k=%s p**k=%s' % (p,k,p_deg_k) print 'a=\n',a,'\n' rez=flint.test_invert_4x4_corner( flint.fmpz_mat(a1), p, k ) if rez==None: a_det=a.determinant() % p if a_det==0: return print 'a=',a die('Empty result from test_invert_4x4_corner()') negate_det,det,i,u=rez i %= p_deg_k u %= p_deg_k try: iInv=i.change_ring(Zn(p_deg_k)).I except: test_1_failed( a, i, u, negate_det, 'i not invertible', p_deg_k ) a_p=a.change_ring(Zn(p_deg_k)) a_det=a.determinant() % p_deg_k det %= p_deg_k if negate_det&1: det=p_deg_k-det if det != a_det: print "good/bad determinant: %s / %s" % (a_det,det) test_1_failed( a, i, u, negate_det, 'bad determinant', p_deg_k ) if u[1,0]==2 and u[2,0]==3 and u[3,0]==4 and u[4,0]==5: if not iInv==a_p: print 'p**k=%s' % p_deg_k print 'a modulo p**k=\n',a % p_deg_k print "i' modulo p**k=\n",iInv % p_deg_k test_1_failed( a, i, u, negate_det, 'bad inverse, kind 0', p_deg_k ) else: a1_p=a1.change_ring(Zn(p_deg_k)) for v in range(4): for w in range(4): u[1+v,1+w]=iInv[v,w] u_p=u.change_ring(Zn(p_deg_k)) try: a_i=a1_p.I except: test_1_failed( a, i, u, negate_det, 'a1 is not invertible', p_deg_k ) v=u_p*a_i if not is_permutation(v): print 'u=\n',u test_1_failed( a, i, u, negate_det, 'a1 is not a row-swapped u', p_deg_k )
def det_flint(a,f,way): global t1 if t1[way]<-0.1: return aF=flint.fmpz_mat( a ) if loud: print 'starts subroutine %s' % f t0=time.time() signal.alarm(time_constraint) try: one=f(aF) t1[way] += time.time()-t0 if loud: print 'spent',t1[way] except: t1[way]=-1 if loud: print 'TiMeOuT'
def test_m(m,i_m_first): global t_FLINT, t_RAZIN a=flint.fmpz_mat( m ) if i_m_first: t0=time.time() dG = flint.det(a) t1=time.time() dB = flint.det_20140704( a ) t2=time.time() t_FLINT += t1-t0 t_RAZIN += t2-t1 else: t0=time.time() dB = flint.det_20140704( a ) t1=time.time() dG = flint.det(a) t2=time.time() t_FLINT += t2-t1 t_RAZIN += t1-t0 if dG==dB: return print 'test failed, det good/bad=%s/%s' % (dG,dB) print m.__repr__() assert 0
def sage_ZZ_to_flint_QQ( d, a ): return F( (Integer(d), flint.fmpz_mat( a ) ) )
import sage.all import flint_sage as flint import sys dim = 2 ZZ = sage.all.ZZ def straighten(xx): r = [] for i in xx: for j in i: r.append(j) return r def random_matrice(): return sage.all.random_matrix(ZZ, dim, dim + 2, x=-100, y=100) for i in range(10): a = random_matrice() # both forms of constructor work if i & 1: b = flint.fmpz_mat(a) else: b = flint.fmpz_mat((dim, dim + 2, straighten(list(a)))) assert a == b.export_sage() print "\ntest passed"