rem
stringlengths
0
322k
add
stringlengths
0
2.05M
context
stringlengths
8
228k
sage: W = Words('012') sage: W('012012012').is_cube() True sage: W('01010101').is_cube() False sage: W().is_cube() True sage: W('012012').is_cube()
sage: Word('012012012').is_cube() True sage: Word('01010101').is_cube() False sage: Word().is_cube() True sage: Word('012012').is_cube()
def is_cube(self): r""" Returns True if self is a cube, and False otherwise. EXAMPLES:: sage: W = Words('012') sage: W('012012012').is_cube() True sage: W('01010101').is_cube() False sage: W().is_cube() True sage: W('012012').is_cube() False """ if self.length() % 3 != 0: return False l = self.length() / 3 return self[:l] == self[l:2*l] == self[2*l:]
sage: W = Words('123') sage: W('12312').is_cube_free() True sage: W('32221').is_cube_free() False sage: W().is_cube_free() True """ l = self.length() if l < 3:
sage: Word('12312').is_cube_free() True sage: Word('32221').is_cube_free() False sage: Word().is_cube_free() True TESTS: We make sure that sage: Word('111').is_cube_free() False sage: Word('2111').is_cube_free() False sage: Word('32111').is_cube_free() False """ L = self.length() if L < 3:
def is_cube_free(self): r""" Returns True if self does not contain cubes, and False otherwise. EXAMPLES:: sage: W = Words('123') sage: W('12312').is_cube_free() True sage: W('32221').is_cube_free() False sage: W().is_cube_free() True """ l = self.length() if l < 3: return True suff = self for i in xrange(0, l - 3): for ll in xrange(3, l-i+1, 3): if suff[:ll].is_cube(): return False suff = suff[1:] return True
suff = self for i in xrange(0, l - 3): for ll in xrange(3, l-i+1, 3): if suff[:ll].is_cube():
for start in xrange(0, L - 2): for end in xrange(start+3, L+1, 3): if self[start:end].is_cube():
def is_cube_free(self): r""" Returns True if self does not contain cubes, and False otherwise. EXAMPLES:: sage: W = Words('123') sage: W('12312').is_cube_free() True sage: W('32221').is_cube_free() False sage: W().is_cube_free() True """ l = self.length() if l < 3: return True suff = self for i in xrange(0, l - 3): for ll in xrange(3, l-i+1, 3): if suff[:ll].is_cube(): return False suff = suff[1:] return True
suff = suff[1:]
def is_cube_free(self): r""" Returns True if self does not contain cubes, and False otherwise. EXAMPLES:: sage: W = Words('123') sage: W('12312').is_cube_free() True sage: W('32221').is_cube_free() False sage: W().is_cube_free() True """ l = self.length() if l < 3: return True suff = self for i in xrange(0, l - 3): for ll in xrange(3, l-i+1, 3): if suff[:ll].is_cube(): return False suff = suff[1:] return True
return CRT_list(vec,mod)
return tuple(CRT_vectors(vec,mod))
def basiclemmavec(self,M): """ Finds a vector where the value of the quadratic form is coprime to M. EXAMPLES: sage: Q = QuadraticForm(ZZ, 2, [2, 1, 5]) sage: Q.basiclemmavec(10) (6, 5) sage: Q(_) 227 """ V=FreeModule(self.base_ring(),self.dim()) mat = self.matrix() vec = [] mod = [] M0 = abs(M) if M0 == 1: return V(0) for i in range(self.dim()): M1 = prime_to_m_part(M0, self[i,i]) if M1 != 1: vec.append(V.gen(i)) mod.append(M1) M0 = M0/M1 if M0 == 1: return CRT_list(vec,mod) for i in range(self.dim()): for j in range(i): M1 = prime_to_m_part(M0, self[i,j]) if M1 != 1: vec.append(V.i + V.j) mod.append(M1) M0 = M0/M1 if M0 == 1: return __crt_list(vec,mod) raise ValueError, "not primitive form"
p.add_constraint(v[x] + b[x][y] - v[y], min=0, max=0)
p.add_constraint(v[x] + b[x][y] - v[y], min=0)
def edge_cut(self, s, t, value_only=True, use_edge_labels=False, vertices=False, solver=None, verbose=0): r""" Returns a minimum edge cut between vertices `s` and `t` represented by a list of edges.
- ``catch` - a boolean (default: True)
- ``catch`` - a boolean (default: True)
def run(self, category = None, skip = [], catch = True, raise_on_failure = False, **options): """ Run all the tests from this test suite:
sage: ZZ._tester() Testing utilities for Integer Ring
sage: QQ._tester() Testing utilities for Rational Field
def instance_tester(instance, tester = None, **options): """ Returns a gadget attached to ``instance`` providing testing utilities. EXAMPLES:: sage: from sage.misc.sage_unittest import instance_tester sage: tester = instance_tester(ZZ) sage: tester.assert_(1 == 1) sage: tester.assert_(1 == 0) Traceback (most recent call last): ... AssertionError sage: tester.assert_(1 == 0, "this is expected to fail") Traceback (most recent call last): ... AssertionError: this is expected to fail sage: tester.assertEquals(1, 1) sage: tester.assertEquals(1, 0) Traceback (most recent call last): ... AssertionError: 1 != 0 The available assertion testing facilities are the same as in :class:`unittest.TestCase`, which see (actually, by a slight abuse, tester is currently an instance of this class). TESTS:: sage: instance_tester(ZZ, tester = tester) is tester True """ if tester is None: return InstanceTester(instance, **options) else: assert len(options) == 0 assert tester._instance is instance return tester
sage: ZZ._tester() Testing utilities for Integer Ring
sage: QQ._tester() Testing utilities for Rational Field
def __init__(self, instance, elements = None, verbose = False, prefix = "", **options): """ A gadget attached to an instance providing it with testing utilities.
sage: for lset in polytopes.cross_polytope(2).face_lattice().level_sets(): print lset[0] (None, (0, 1, 2, 3)) ((1,), (2, 3)) ((1, 2), (3,)) ((0, 1, 2, 3), None)
sage: level_sets = polytopes.cross_polytope(2).face_lattice().level_sets() sage: print level_sets[0], level_sets[-1] [(None, (0, 1, 2, 3))] [((0, 1, 2, 3), None)]
def face_lattice(self): """ Computes the face-lattice poset. Elements are tuples of (vertices, facets) - i.e. this keeps track of both the vertices in each face, and all the facets containing them.
""" Enumerates projective, rational points on scheme X of height up to bound B.
r""" Enumerates projective, rational points on scheme ``X`` of height up to bound ``B``.
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
- ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound
- ``X`` - a scheme or set of abstract rational points of a scheme; - ``B`` - a positive integer bound.
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
- a list containing the projective points of X of height up to B, sorted.
- a list containing the projective points of ``X`` of height up to ``B``, sorted.
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
of projective space to see if they lie on X.
of projective space to see if they lie on ``X``.
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
John Cremona and Charlie Turner (06-2010)
- John Cremona and Charlie Turner (06-2010)
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)]) pts =[] for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n+1)]): if gcd(c)==1 and c>zero:
n = X.codomain().ambient_space().ngens() zero = (0,) * n pts = [] for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n)]): if gcd(c) == 1 and c > zero:
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
except:
except TypeError:
def enum_projective_rational_field(X,B): """ Enumerates projective, rational points on scheme X of height up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme. - ``B`` - a positive integer bound OUTPUT: - a list containing the projective points of X of height up to B, sorted. EXAMPLES:: sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) sage: C = P.subscheme([X+Y-Z]) sage: from sage.schemes.generic.rational_point import enum_projective_rational_field sage: enum_projective_rational_field(C(QQ),6) [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), (6 : -5 : 1)] sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) True :: sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) sage: enum_projective_rational_field(P3,1) [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] ALGORITHM: We just check all possible projective points in correct dimension of projective space to see if they lie on X. AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 zero=tuple([0 for _ in range(n+1)])
Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B.
Enumerates affine rational points on scheme ``X`` (defined over `\QQ`) up to bound ``B``.
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
- ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound
- ``X`` - a scheme or set of abstract rational points of a scheme; - ``B`` - a positive integer bound.
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
- a list containing the affine points of X of height up to B, sorted.
- a list containing the affine points of ``X`` of height up to ``B``, sorted.
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010)
AUTHORS: - David R. Kohel <[email protected]>: original version. - Charlie Turner (06-2010): small adjustments.
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ]
Q = range(1, B + 1) R = [ 0 ] + [ s*k for k in range(1, B+1) for s in [1, -1] ]
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
P = [ 0 for _ in range(n) ]
P = [0] * n
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
except:
except TypeError:
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
[ iters[j].next() for j in range(n) ]
for it in iters: it.next()
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0)
def enum_affine_rational_field(X,B): """ Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. INPUT: - ``X`` - a scheme or set of abstract rational points of a scheme - ``B`` - a positive integer bound OUTPUT: - a list containing the affine points of X of height up to B, sorted. EXAMPLES:: sage: A.<x,y,z> = AffineSpace(3,QQ) sage: from sage.schemes.generic.rational_point import enum_affine_rational_field sage: enum_affine_rational_field(A(QQ),1) [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), (1, 1, 1)] :: sage: A.<w,x,y,z> = AffineSpace(4,QQ) sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) sage: enum_affine_rational_field(S(QQ),2) [] sage: enum_affine_rational_field(S(QQ),3) [(-2, 0, -3, -1)] :: sage: A.<x,y> = AffineSpace(2,QQ) sage: C = Curve(x^2+y-x) sage: enum_affine_rational_field(C,10) [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] AUTHOR: David R. Kohel <[email protected]> (small adjustments by Charlie Turner 06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens() if X.value_ring() is ZZ: Q = [ 1 ] else: # rational field Q = [ k+1 for k in range(B) ] R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] pts = [] P = [ 0 for _ in range(n) ] m = ZZ(0) try: pts.append(X(P)) except: pass iters = [ iter(R) for _ in range(n) ] [ iters[j].next() for j in range(n) ] i = 0 while i < n: try: a = ZZ(iters[i].next()) m = m.gcd(a) P[i] = a for b in Q: if m.gcd(b) == 1: try: pts.append(X([ num/b for num in P ])) except: pass i = 0 m = ZZ(0) except StopIteration: iters[i] = iter(R) # reset P[i] = iters[i].next() # reset P[i] to 0 and increment i += 1 pts.sort() return pts
Enumerates projective points on scheme X defined over a finite field
Enumerates projective points on scheme ``X`` defined over a finite field.
def enum_projective_finite_field(X): """ Enumerates projective points on scheme X defined over a finite field INPUT: - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme OUTPUT: - a list containing the projective points of X over the finite field, sorted EXAMPLES:: sage: F = GF(53) sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: from sage.schemes.generic.rational_point import enum_projective_finite_field sage: len(enum_projective_finite_field(P(F))) 2863 sage: 53^2+53+1 2863 :: sage: F = GF(9,'a') sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: C = Curve(X^3-Y^3+Z^2*Y) sage: enum_projective_finite_field(C(F)) [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] :: sage: F = GF(5) sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) sage: enum_projective_finite_field(P2F) [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), (4 : 4 : 1)] ALGORITHM: Checks all points in projective space to see if they lie on X. NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 F = X.value_ring() pts = [] for k in range(n+1): for c in cartesian_product_iterator([F for _ in range(k)]): try: pts.append(X(list(c)+[1]+[0]*(n-k))) except: pass pts.sort() return pts
- ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme
- ``X`` - a scheme defined over a finite field or a set of abstract rational points of such a scheme.
def enum_projective_finite_field(X): """ Enumerates projective points on scheme X defined over a finite field INPUT: - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme OUTPUT: - a list containing the projective points of X over the finite field, sorted EXAMPLES:: sage: F = GF(53) sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: from sage.schemes.generic.rational_point import enum_projective_finite_field sage: len(enum_projective_finite_field(P(F))) 2863 sage: 53^2+53+1 2863 :: sage: F = GF(9,'a') sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: C = Curve(X^3-Y^3+Z^2*Y) sage: enum_projective_finite_field(C(F)) [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] :: sage: F = GF(5) sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) sage: enum_projective_finite_field(P2F) [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), (4 : 4 : 1)] ALGORITHM: Checks all points in projective space to see if they lie on X. NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 F = X.value_ring() pts = [] for k in range(n+1): for c in cartesian_product_iterator([F for _ in range(k)]): try: pts.append(X(list(c)+[1]+[0]*(n-k))) except: pass pts.sort() return pts
- a list containing the projective points of X over the finite field, sorted
- a list containing the projective points of ``X`` over the finite field, sorted.
def enum_projective_finite_field(X): """ Enumerates projective points on scheme X defined over a finite field INPUT: - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme OUTPUT: - a list containing the projective points of X over the finite field, sorted EXAMPLES:: sage: F = GF(53) sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: from sage.schemes.generic.rational_point import enum_projective_finite_field sage: len(enum_projective_finite_field(P(F))) 2863 sage: 53^2+53+1 2863 :: sage: F = GF(9,'a') sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: C = Curve(X^3-Y^3+Z^2*Y) sage: enum_projective_finite_field(C(F)) [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] :: sage: F = GF(5) sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) sage: enum_projective_finite_field(P2F) [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), (4 : 4 : 1)] ALGORITHM: Checks all points in projective space to see if they lie on X. NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 F = X.value_ring() pts = [] for k in range(n+1): for c in cartesian_product_iterator([F for _ in range(k)]): try: pts.append(X(list(c)+[1]+[0]*(n-k))) except: pass pts.sort() return pts
NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010)
.. WARNING:: If ``X`` is defined over an infinite field, this code will not finish! AUTHORS: - John Cremona and Charlie Turner (06-2010).
def enum_projective_finite_field(X): """ Enumerates projective points on scheme X defined over a finite field INPUT: - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme OUTPUT: - a list containing the projective points of X over the finite field, sorted EXAMPLES:: sage: F = GF(53) sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: from sage.schemes.generic.rational_point import enum_projective_finite_field sage: len(enum_projective_finite_field(P(F))) 2863 sage: 53^2+53+1 2863 :: sage: F = GF(9,'a') sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: C = Curve(X^3-Y^3+Z^2*Y) sage: enum_projective_finite_field(C(F)) [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] :: sage: F = GF(5) sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) sage: enum_projective_finite_field(P2F) [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), (4 : 4 : 1)] ALGORITHM: Checks all points in projective space to see if they lie on X. NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 F = X.value_ring() pts = [] for k in range(n+1): for c in cartesian_product_iterator([F for _ in range(k)]): try: pts.append(X(list(c)+[1]+[0]*(n-k))) except: pass pts.sort() return pts
except:
except TypeError:
def enum_projective_finite_field(X): """ Enumerates projective points on scheme X defined over a finite field INPUT: - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme OUTPUT: - a list containing the projective points of X over the finite field, sorted EXAMPLES:: sage: F = GF(53) sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: from sage.schemes.generic.rational_point import enum_projective_finite_field sage: len(enum_projective_finite_field(P(F))) 2863 sage: 53^2+53+1 2863 :: sage: F = GF(9,'a') sage: P.<X,Y,Z> = ProjectiveSpace(2,F) sage: C = Curve(X^3-Y^3+Z^2*Y) sage: enum_projective_finite_field(C(F)) [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] :: sage: F = GF(5) sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) sage: enum_projective_finite_field(P2F) [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), (4 : 4 : 1)] ALGORITHM: Checks all points in projective space to see if they lie on X. NOTE: Warning:if X given as input is defined over an infinite field then this code will not finish! AUTHORS: John Cremona and Charlie Turner (06-2010) """ if is_Scheme(X): X = X(X.base_ring()) n = X.codomain().ambient_space().ngens()-1 F = X.value_ring() pts = [] for k in range(n+1): for c in cartesian_product_iterator([F for _ in range(k)]): try: pts.append(X(list(c)+[1]+[0]*(n-k))) except: pass pts.sort() return pts
sage: cmp(N3, 3) -1
sage: abs( cmp(N3, 3) ) 1
def __cmp__(self, right): r""" Compare ``self`` and ``right``.
``obj``, return a list of those names. If
``obj``, return a sorted list of those names. If
def sage_getvariablename(obj, omit_underscore_names=True): """ Attempt to get the name of a Sage object. INPUT: - ``obj`` - an object - ``omit_underscore_names`` (optional, default True) If the user has assigned an object ``obj`` to a variable name, then return that variable name. If several variables point to ``obj``, return a list of those names. If ``omit_underscore_names`` is True (the default) then omit names starting with an underscore "_". This is a modified version of code taken from http://pythonic.pocoo.org/2009/5/30/finding-objects-names, written by Georg Brandl. EXAMPLES:: sage: from sage.misc.sageinspect import sage_getvariablename sage: A = random_matrix(ZZ, 100) sage: sage_getvariablename(A) 'A' sage: B = A sage: sage_getvariablename(A) ['A', 'B'] If an object is not assigned to a variable, an empty list is returned:: sage: sage_getvariablename(random_matrix(ZZ, 60)) [] """ import gc result = [] for referrer in gc.get_referrers(obj): if isinstance(referrer, dict): for k, v in referrer.iteritems(): if v is obj: if isinstance(k, str): if (not omit_underscore_names) or not k.startswith('_'): result.append(k) if len(result) == 1: return result[0] else: return result
return result
return sorted(result)
def sage_getvariablename(obj, omit_underscore_names=True): """ Attempt to get the name of a Sage object. INPUT: - ``obj`` - an object - ``omit_underscore_names`` (optional, default True) If the user has assigned an object ``obj`` to a variable name, then return that variable name. If several variables point to ``obj``, return a list of those names. If ``omit_underscore_names`` is True (the default) then omit names starting with an underscore "_". This is a modified version of code taken from http://pythonic.pocoo.org/2009/5/30/finding-objects-names, written by Georg Brandl. EXAMPLES:: sage: from sage.misc.sageinspect import sage_getvariablename sage: A = random_matrix(ZZ, 100) sage: sage_getvariablename(A) 'A' sage: B = A sage: sage_getvariablename(A) ['A', 'B'] If an object is not assigned to a variable, an empty list is returned:: sage: sage_getvariablename(random_matrix(ZZ, 60)) [] """ import gc result = [] for referrer in gc.get_referrers(obj): if isinstance(referrer, dict): for k, v in referrer.iteritems(): if v is obj: if isinstance(k, str): if (not omit_underscore_names) or not k.startswith('_'): result.append(k) if len(result) == 1: return result[0] else: return result
sage: M=E.modular_symbol()
def modular_symbol(self, sign=1, use_eclib = False, normalize = "L_ratio"): r""" Return the modular symbol associated to this elliptic curve, with given sign and base ring. This is the map that sends `r/s` to a fixed multiple of the integral of `2 \pi i f(z) dz` from `\infty` to `r/s`, normalized so that all values of this map take values in `\QQ`.
1/5
2/5
def modular_symbol(self, sign=1, use_eclib = False, normalize = "L_ratio"): r""" Return the modular symbol associated to this elliptic curve, with given sign and base ring. This is the map that sends `r/s` to a fixed multiple of the integral of `2 \pi i f(z) dz` from `\infty` to `r/s`, normalized so that all values of this map take values in `\QQ`.
Returns the Duursama data `v` and `m` of this formally s.d. code `C`
Returns the Duursma data `v` and `m` of this formally s.d. code `C`
def sd_duursma_data(C, i): r""" Returns the Duursama data `v` and `m` of this formally s.d. code `C` and the type number `i` in (1,2,3,4). Does *not* check if this code is actually sd. INPUT: - ``i`` - Type number OUTPUT: - Pair ``(v, m)`` as in Duursama [D]_ REFERENCES: - [D] - I. Duursma, "Extremal weight enumerators and ultraspherical polynomials" """ n = C.length() d = C.minimum_distance() if i == 1: v = (n-4*d)/2 + 4 m = d-3 if i == 2: v = (n-6*d)/8 + 3 m = d-5 if i == 3: v = (n-4*d)/4 + 3 m = d-4 if i == 4: v = (n-3*d)/2 + 3 m = d-3 return [v,m]
- Pair ``(v, m)`` as in Duursama [D]_
- Pair ``(v, m)`` as in Duursma [D]_
def sd_duursma_data(C, i): r""" Returns the Duursama data `v` and `m` of this formally s.d. code `C` and the type number `i` in (1,2,3,4). Does *not* check if this code is actually sd. INPUT: - ``i`` - Type number OUTPUT: - Pair ``(v, m)`` as in Duursama [D]_ REFERENCES: - [D] - I. Duursma, "Extremal weight enumerators and ultraspherical polynomials" """ n = C.length() d = C.minimum_distance() if i == 1: v = (n-4*d)/2 + 4 m = d-3 if i == 2: v = (n-6*d)/8 + 3 m = d-5 if i == 3: v = (n-4*d)/4 + 3 m = d-4 if i == 4: v = (n-3*d)/2 + 3 m = d-3 return [v,m]
- [D] - I. Duursma, "Extremal weight enumerators and ultraspherical polynomials"
.. [D] I. Duursma, "Extremal weight enumerators and ultraspherical polynomials"
def sd_duursma_data(C, i): r""" Returns the Duursama data `v` and `m` of this formally s.d. code `C` and the type number `i` in (1,2,3,4). Does *not* check if this code is actually sd. INPUT: - ``i`` - Type number OUTPUT: - Pair ``(v, m)`` as in Duursama [D]_ REFERENCES: - [D] - I. Duursma, "Extremal weight enumerators and ultraspherical polynomials" """ n = C.length() d = C.minimum_distance() if i == 1: v = (n-4*d)/2 + 4 m = d-3 if i == 2: v = (n-6*d)/8 + 3 m = d-5 if i == 3: v = (n-4*d)/4 + 3 m = d-4 if i == 4: v = (n-3*d)/2 + 3 m = d-3 return [v,m]
- Coefficients `q_0, q_1, ...` of `q(T)` as in Duursama [D]_
- Coefficients `q_0, q_1, ...` of `q(T)` as in Duursma [D]_
def sd_duursma_q(C,i,d0): r""" INPUT: - ``C`` - sd code; does *not* check if `C` is actually an sd code - ``i`` - Type number, one of 1,2,3,4 - ``d0`` - Divisor, the smallest integer such that each `A_i > 0` iff `i` is divisible by `d0` OUTPUT: - Coefficients `q_0, q_1, ...` of `q(T)` as in Duursama [D]_ REFERENCES: - [D] - I. Duursma, "Extremal weight enumerators and ultraspherical polynomials" """ q = (C.base_ring()).order() n = C.length() d = C.minimum_distance() d0 = C.divisor() if i==1 or i==2: if d>d0: c0 = QQ((n-d)*rising_factorial(d-d0,d0+1)*C.spectrum()[d])/rising_factorial(n-d0-1,d0+2) else: c0 = QQ((n-d)*C.spectrum()[d])/rising_factorial(n-d0-1,d0+2) if i==3 or i==4: if d>d0: c0 = rising_factorial(d-d0,d0+1)*C.spectrum()[d]/((q-1)*rising_factorial(n-d0,d0+1)) else: c0 = C.spectrum()[d]/((q-1)*rising_factorial(n-d0,d0+1)) v = ZZ(C.sd_duursma_data(i)[0]) m = ZZ(C.sd_duursma_data(i)[1]) if m<0 or v<0: raise ValueError("This case not implemented.") PR = PolynomialRing(QQ,"T") T = PR.gen() if i == 1: coefs = PR(c0*(1+3*T+2*T**2)**m*(2*T**2+2*T+1)**v).list() qc = [coefs[j]/binomial(4*m+2*v,m+j) for j in range(2*m+2*v+1)] q = PR(qc) if i == 2: F = ((T+1)**8+14*T**4*(T+1)**4+T**8)**v coefs = (c0*(1+T)**m*(1+4*T+6*T**2+4*T**3)**m*F).coeffs() qc = [coefs[j]/binomial(6*m+8*v,m+j) for j in range(4*m+8*v+1)] q = PR(qc) if i == 3: F = (3*T^2+4*T+1)**v*(1+3*T^2)**v # Note that: (3*T^2+4*T+1)(1+3*T^2)=(T+1)**4+8*T**3*(T+1) coefs = (c0*(1+3*T+3*T**2)**m*F).coeffs() qc = [coefs[j]/binomial(4*m+4*v,m+j) for j in range(2*m+4*v+1)] q = PR(qc) if i == 4: coefs = (c0*(1+2*T)**m*(4*T**2+2*T+1)**v).coeffs() qc = [coefs[j]/binomial(3*m+2*v,m+j) for j in range(m+2*v+1)] q = PR(qc) return q/q(1)
img = self(letter)
img = self.image(letter)
def is_identity(self): r""" Returns ``True`` if ``self`` is the identity morphism. EXAMPLES::
gens = [ "()" ]
k = max(self.entries()) gens = [range(1,k+1)]
def row_stabilizer(self): """ Return the PermutationGroup corresponding to the row stabilizer of self. EXAMPLES:: sage: rs = Tableau([[1,2,3],[4,5]]).row_stabilizer() sage: rs.order() == factorial(3)*factorial(2) True sage: PermutationGroupElement([(1,3,2),(4,5)]) in rs True sage: PermutationGroupElement([(1,4)]) in rs False sage: rs = Tableau([[1],[2],[3]]).row_stabilizer() sage: rs.order() 1 """
- ``algorithm`` - string (default: 'recursive') specifying which algorithm to be used when computing the iterated palindromic closure. It must be one of the two following values: - ``'definition'`` means that the iterated right palindromic closure is computed using the definition. - ``'recursive'`` is based on an efficient formula that recursively computes the iterated right palindromic closure without having to recompute the longest `f`-palindromic suffix at each iteration [2].
- ``algorithm`` - string (default: ``'recursive'``) specifying which algorithm to be used when computing the iterated palindromic closure. It must be one of the two following values: - ``'definition'`` - computed using the definition - ``'recursive'`` - computation based on an efficient formula that recursively computes the iterated right palindromic closure without having to recompute the longest `f`-palindromic suffix at each iteration [2].
def iterated_right_palindromic_closure(self, f=None, algorithm='recursive'): r""" Returns the iterated (`f`-)palindromic closure of self. INPUT:
l = len(sub)
L = len(sub)
def find(self, sub, start=0, end=None): r""" Returns the index of the first occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
i = len(self) - l
i = len(self) - L
def find(self, sub, start=0, end=None): r""" Returns the index of the first occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
i = start - l
i = start - L
def find(self, sub, start=0, end=None): r""" Returns the index of the first occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
if self[i:i+l] == sub: return i
if self[i:i+L] == sub: return i
def find(self, sub, start=0, end=None): r""" Returns the index of the first occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
l = len(sub)
L = len(sub)
def rfind(self, sub, start=0, end=None): r""" Returns the index of the last occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
i = len(self) - l
i = len(self) - L
def rfind(self, sub, start=0, end=None): r""" Returns the index of the last occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
i = end - l
i = end - L
def rfind(self, sub, start=0, end=None): r""" Returns the index of the last occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
if self[i:i+l] == sub: return i
if self[i:i+L] == sub: return i
def rfind(self, sub, start=0, end=None): r""" Returns the index of the last occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure. INPUT:
""" qE = self._q
sage: T = EllipticCurve('14').tate_curve(7) sage: T.E2(30) 2 + 4*7 + 7^2 + 3*7^3 + 6*7^4 + 5*7^5 + 2*7^6 + 7^7 + 5*7^8 + 6*7^9 + 5*7^10 + 2*7^11 + 6*7^12 + 4*7^13 + 3*7^15 + 5*7^16 + 4*7^17 + 4*7^18 + 2*7^20 + 7^21 + 5*7^22 + 4*7^23 + 4*7^24 + 3*7^25 + 6*7^26 + 3*7^27 + 6*7^28 + O(7^30) """
def E2(self,prec=20): r""" Returns the value of the `p`-adic Eisenstein series of weight 2 evaluated on the elliptic curve having split multiplicative reduction.
Some cases that check on the negative twists::
Some harder cases fail::
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
-2
1
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
Dlist = [5,8,12,13,17,21,24,28,29]
Dlist = [5,8,12,13,17,21,24,28,29, 33, 37, 40, 41, 44, 53, 56, 57, 60, 61, 65, 69, 73, 76, 77, 85, 88, 89, 92, 93, 97]
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
while j < 9 and at0 == 0 :
while j < 30 and at0 == 0 :
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
chtw = True for ell in prime_divisors(D): chtw = chtw and ( valuation(E.conductor(),ell)<= valuation(D,ell) ) if chtw :
if all( valuation(E.conductor(),ell)<= valuation(D,ell) for ell in prime_divisors(D) ) :
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
if j == 9 and at0 == 0: sc = 1 if not self._use_eclib : msn = ModularSymbolSage(self._E,sign = -1,normalize = "L_ratio") sc = msn._scaling if sc == 0 or self._use_eclib : self.__scale_by_periods_only__() else : self._scaling = sc
if j == 30 and at0 == 0: self.__scale_by_periods_only__()
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
verbose('scale modular symbols by %s'%(l1/at0))
verbose('scale modular symbols by %s found at D=%s '%(l1/at0,D), level=2)
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
Dlist = [-3,-4,-7,-8,-11,-15,-19,-20,-23,-24]
Dlist = [-3,-4,-7,-8,-11,-15,-19,-20,-23,-24, -31, -35, -39, -40, -43, -47, -51, -52, -55, -56, -59, -67, -68, -71, -79, -83, -84, -87, -88, -91]
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
if j == 9 and at0 == 0:
if j == 30 and at0 == 0:
def _find_scaling_L_ratio(self): r""" This function is use to set ``_scaling``, the factor used to adjust the scalar multiple of the modular symbol. If `[0]`, the modular symbol evaluated at 0, is non-zero, we can just scale it with respect to the approximation of the L-value. It is known that the quotient is a rational number with small denominator. Otherwise we try to scale using quadratic twists.
self._option["pdflatex"] = False
self._option["engine"] = "latex" self._option["engine_name"] = "LaTeX"
def __init__(self, bb=False, delimiters=["(", ")"]): """ Define an object that holds LaTeX global preferences. """ self._option = {} self._option["blackboard_bold"] = bb self._option["matrix_delimiters"] = list(delimiters) self._option["vector_delimiters"] = list(delimiters) self._option["macros"] = "" self._option["preamble"] = "" self._option["pdflatex"] = False self._option["jsmath_avoid"] = []
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False):
def _run_latex_(filename, debug=False, density=150, engine=None, png=False, do_in_background=False):
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
"filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file.
"filename.dvi" (or "filename.pdf"` if engine is either ``pdflatex`` or ``xelatex'') and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file.
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
- ``pdflatex`` - bool (optional, default False): whether to use pdflatex.
- ``engine`` - string: latex engine to use.
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
``pdflatex`` is False, then a dvi file is created, but if there
engine is latex or None, then a dvi file is created, but if there
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
returned.) If ``pdflatex`` is True and there are no errors, then
returned.) If engine is pdflatex or xelatex and there are no errors, then
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
pdflatex, you must have 'convert' installed.
pdflatex or xelatex, you must have 'convert' installed.
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex():
if engine is None: engine = _Latex_prefs._option["engine"] if not engine and not have_latex():
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from"
if engine and not have_pdflatex() and not have_xelatex(): print "Error: %s does not seem to be installed. Download it from" % _Latex_prefs._option["engine_name"]
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
if not pdflatex and not (have_dvipng() or have_convert()):
if (not engine or engine == "latex") and not (have_dvipng() or have_convert()):
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
print "appear to be installed. Displaying LaTeX or PDFLaTeX output"
print "appear to be installed. Displaying LaTeX, PDFLaTeX output"
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
elif pdflatex and not have_convert():
elif engine == "pdflatex" and not have_convert():
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
if pdflatex:
if not engine or engine == "latex": command = "latex" suffix = "ps" return_suffix = "dvi" elif engine == "pdflatex":
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
command = "latex" suffix = "ps" return_suffix = "dvi"
raise ValueError, "Unsupported LaTeX engine."
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
if pdflatex:
if engine == "pdflatex" or engine == "xelatex":
def _run_latex_(filename, debug=False, density=150, pdflatex=None, png=False, do_in_background=False): """ This runs LaTeX on the TeX file "filename.tex". It produces files "filename.dvi" (or "filename.pdf"` if ``pdflatex`` is ``True``) and if ``png`` is True, "filename.png". If ``png`` is True and dvipng can't convert the dvi file to png (because of postscript specials or other issues), then dvips is called, and the PS file is converted to a png file. INPUT: - ``filename`` - string: file to process, including full path - ``debug`` - bool (optional, default False): whether to print verbose debugging output - ``density`` - integer (optional, default 150): how big output image is. - ``pdflatex`` - bool (optional, default False): whether to use pdflatex. - ``png`` - bool (optional, default False): whether to produce a png file. - ``do_in_background`` - bool (optional, default False): whether to run in the background. OUTPUT: string, which could be a string starting with 'Error' (if there was a problem), or it could be 'pdf' or 'dvi'. If ``pdflatex`` is False, then a dvi file is created, but if there appear to be problems with it (because of PS special commands, for example), then a pdf file is created instead. The function returns 'dvi' or 'pdf' to indicate which type of file is created. (Detecting problems requires that dvipng be installed; if it is not, then the dvi file is not checked for problems and 'dvi' is returned.) If ``pdflatex`` is True and there are no errors, then 'pdf' is returned. .. warning:: If ``png`` is True, then when using latex (the default), you must have 'dvipng' (or 'dvips' and 'convert') installed on your operating system, or this command won't work. When using pdflatex, you must have 'convert' installed. EXAMPLES:: sage: from sage.misc.latex import _run_latex_, _latex_file_ sage: file = os.path.join(SAGE_TMP, "temp.tex") sage: O = open(file, 'w') sage: O.write(_latex_file_([ZZ[x], RR])); O.close() sage: _run_latex_(file) # random - depends on whether latex is installed 'dvi' """ if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] if not pdflatex and not have_latex(): print "Error: LaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" if pdflatex and not have_pdflatex(): print "Error: PDFLaTeX does not seem to be installed. Download it from" print "ctan.org and try again." return "Error" # if png output + latex, check to see if dvipng or convert is installed. if png: if not pdflatex and not (have_dvipng() or have_convert()): print "" print "Error: neither dvipng nor convert (from the ImageMagick suite)" print "appear to be installed. Displaying LaTeX or PDFLaTeX output" print "requires at least one of these programs, so please install" print "and try again." print "" print "Go to http://sourceforge.net/projects/dvipng/ and" print "http://www.imagemagick.org to download these programs." return "Error" # if png output + pdflatex, check to see if convert is installed. elif pdflatex and not have_convert(): print "" print "Error: convert (from the ImageMagick suite) does not" print "appear to be installed. Displaying PDFLaTeX output" print "requires this program, so please install and try again." print "" print "Go to http://www.imagemagick.org to download it." return "Error" # check_validity: check to see if the dvi file is okay by trying # to convert to a png file. if this fails, return_suffix will be # set to "pdf". return_suffix is the return value for this # function. # # thus if not png output, check validity of dvi output if dvipng # or convert is installed. else: check_validity = have_dvipng() # set up filenames, other strings: base, filename = os.path.split(filename) filename = os.path.splitext(filename)[0] # get rid of extension if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' if do_in_background: background = ' &' else: background = '' if pdflatex: command = "pdflatex" # 'suffix' is used in the string 'convert' ... suffix = "pdf" return_suffix = "pdf" else: command = "latex" suffix = "ps" return_suffix = "dvi" # Define the commands to be used: lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) # dvipng is run with the 'picky' option: this means that if # there are warnings, no png file is created. dvipng = 'cd "%s"&& sage-native-execute dvipng --picky -q -T tight -D %s %s.dvi -o %s.png'%(base, density, filename, filename) dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) ps2pdf = 'sage-native-execute ps2pdf %s.ps %s'%(filename, redirect) # We seem to need a larger size when using convert compared to # when using dvipng: density = int(1.4 * density / 1.3) convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\ (density,density, filename, suffix, filename, redirect) e = 1 # it is possible to get through the following commands # without running a program, so in that case we force error if pdflatex: if png: cmd = ' && '.join([lt, convert]) else: cmd = lt if debug: print cmd e = os.system(cmd + ' ' + redirect + background) else: # latex, not pdflatex if (png or check_validity): if have_dvipng(): cmd = ' && '.join([lt, dvipng]) if debug: print cmd e = os.system(cmd + ' ' + redirect) dvipng_error = not os.path.exists(base + '/' + filename + '.png') # If there is no png file, then either the latex # process failed or dvipng failed. Assume that dvipng # failed, and try running dvips and convert. (If the # latex process failed, then dvips and convert will # fail also, so we'll still catch the error.) if dvipng_error: if png: if have_convert(): cmd = ' && '.join(['cd "%s"'%(base,), dvips, convert]) if debug: print "'dvipng' failed; trying 'convert' instead..." print cmd e = os.system(cmd + ' ' + redirect + background) else: print "Error: 'dvipng' failed and 'convert' is not installed." return "Error: dvipng failed." else: # not png, i.e., check_validity return_suffix = "pdf" cmd = ' && '.join(['cd "%s"'%(base,), dvips, ps2pdf]) if debug: print "bad dvi file; running dvips and ps2pdf instead..." print cmd e = os.system(cmd) if e: # error running dvips and/or ps2pdf command = "pdflatex" lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect) if debug: print "error running dvips and ps2pdf; trying pdflatex instead..." print cmd e = os.system(cmd + background) else: # don't have dvipng, so must have convert. run latex, dvips, convert. cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect + background) if e: print "An error occurred." try: print open(base + '/' + filename + '.log').read() except IOError: pass return "Error latexing slide." return return_suffix
def __init__(self, debug=False, slide=False, density=150, pdflatex=None):
def __init__(self, debug=False, slide=False, density=150, pdflatex=None, engine=None):
def __init__(self, debug=False, slide=False, density=150, pdflatex=None): self.__debug = debug self.__slide = slide self.__pdflatex = pdflatex self.__density = density
density=None, pdflatex=None, locals={}):
density=None, pdflatex=None, engine=None, locals={}):
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
- ``pdflatex`` - whether to use pdflatex.
- ``pdflatex`` - whether to use pdflatex. This is deprecated. Use ``engine`` option instead. - ``engine`` - latex engine to use. Currently latex, pdflatex, and xelatex are supported.
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
or this command won't work. When using pdflatex, you must
or this command won't work. When using pdflatex or xelatex, you must
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
if pdflatex is None: if self.__pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"]
if engine is None: if self.__engine is None: engine = _Latex_prefs._option["engine"]
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
pdflatex = bool(self.__pdflatex)
engine = self.__engine
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
density=density, pdflatex=pdflatex, png=True)
density=density, engine=engine, png=True)
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, pdflatex=None, locals={}): """ INPUT:
def pdflatex(self, t = None): """
def pdflatex(self, t = None): """ This is deprecated. Use engine("pdflatex") instead.
def pdflatex(self, t = None): """ Controls whether Sage uses PDFLaTeX or LaTeX when typesetting with :func:`view`, in ``%latex`` cells, etc. INPUT: - ``t`` -- boolean or None
sage: latex.pdflatex()
sage: latex.pdflatex() doctest:...: DeprecationWarning: Use engine() instead.
def pdflatex(self, t = None): """ Controls whether Sage uses PDFLaTeX or LaTeX when typesetting with :func:`view`, in ``%latex`` cells, etc. INPUT: - ``t`` -- boolean or None
sage: latex.pdflatex(False)
sage: latex.pdflatex(False) doctest:...: DeprecationWarning: Use engine("latex") instead.
def pdflatex(self, t = None): """ Controls whether Sage uses PDFLaTeX or LaTeX when typesetting with :func:`view`, in ``%latex`` cells, etc. INPUT: - ``t`` -- boolean or None
return _Latex_prefs._option["pdflatex"] _Latex_prefs._option["pdflatex"] = bool(t)
from sage.misc.misc import deprecation deprecation('Use engine() instead.') return _Latex_prefs._option["engine"] == "pdflatex" elif t: from sage.misc.misc import deprecation deprecation('Use engine("pdflatex") instead.') self.engine("pdflatex") else: from sage.misc.misc import deprecation deprecation('Use engine("latex") instead.') self.engine("latex") def engine(self, e = None): r""" Set Sage to use ``e`` as latex engine when typesetting with :func:`view`, in ``%latex`` cells, etc. INPUT: - ``e`` -- 'latex', 'pdflatex', 'xelatex' or None If ``e`` is None, return the current engine. If using the XeLaTeX engine, it will almost always be necessary to set the proper preamble with :func:`extra_preamble` or :func:`add_to_preamble`. For example:: latex.extra_preamble(r'''\usepackage{fontspec,xunicode,xltxtra} \setmainfont[Mapping=tex-text]{some font here} \setmonofont[Mapping=tex-text]{another font here}''') EXAMPLES:: sage: latex.engine() 'latex' sage: latex.engine("pdflatex") sage: latex.engine() 'pdflatex' sage: latex.engine("xelatex") sage: latex.engine() 'xelatex' """ if e is None: return _Latex_prefs._option["engine"] if e == "latex": _Latex_prefs._option["engine"] = "latex" _Latex_prefs._option["engine_name"] = "LaTeX" elif e == "pdflatex": _Latex_prefs._option["engine"] = "pdflatex" _Latex_prefs._option["engine_name"] = "PDFLaTeX" elif e == "xelatex": _Latex_prefs._option["engine"] = e _Latex_prefs._option["engine_name"] = "XeLaTeX" else: raise ValueError, "%s is not a supported LaTeX engine. Use latex, pdflatex, or xelatex" % e
def pdflatex(self, t = None): """ Controls whether Sage uses PDFLaTeX or LaTeX when typesetting with :func:`view`, in ``%latex`` cells, etc. INPUT: - ``t`` -- boolean or None
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds):
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, engine=None, viewer = None, tightpage = None, mode='inline', **kwds):
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
- ``pdflatex`` - bool (default: False): use pdflatex.
- ``pdflatex`` - bool (default: False): use pdflatex. This is deprecated. Use 'engine' option instead. - ``engine`` - 'latex', 'pdflatex', or 'xelatex'
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is
If ``pdflatex`` is ``True``, then the latex engine is set to pdflatex. If the engine is either pdflatex or xelatex, it produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``.
viewer, even in notebook mode. This also sets the latex engine to be ``pdflatex`` if the current engine is latex.
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
debug=debug, do_in_background=False, pdflatex=pdflatex)
debug=debug, do_in_background=False, engine=engine)
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"]
if pdflatex: engine = "pdflatex" else: engine = _Latex_prefs._option["engine"] if viewer == "pdf" and engine == "latex": engine = "pdflatex"
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False)
suffix = _run_latex_(tex_file, debug=debug, engine=engine, png=False)
def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, mode='inline', **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. - ``pdflatex`` - bool (default: False): use pdflatex. - ``viewer`` -- string or None (default: None): specify a viewer to use; currently the only options are None and 'pdf'. - ``tightpage`` - bool (default: False): use the LaTeX package 'preview' with the 'tightpage' option. - ``mode`` -- string (default: 'inline'): 'display' for displaymath or 'inline' for inline math OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, the output is displayed in a separate viewer displaying a dvi (or pdf) file, with the following: the title string is printed, centered, at the top. Beneath that, each object in ``objects`` is typeset on its own line, with the string ``sep`` inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If ``pdflatex`` is ``True``, then this produces a pdf file. Otherwise, it produces a dvi file, and if the program dvipng is installed, it checks the dvi file by trying to convert it to a png file. If this conversion fails, the dvi file probably contains some postscript special commands or it has other issues which might make displaying it a problem; in this case, the file is converted to a pdf file, which is then displayed. Setting ``viewer`` to ``'pdf'`` forces the use of a separate viewer, even in notebook mode. This also sets ``pdflatex`` to ``True``. Setting the option ``tightpage`` to ``True`` tells LaTeX to use the package 'preview' with the 'tightpage' option. Then, each object is typeset in its own page, and that page is cropped to exactly the size of the object. This is typically useful for very large pictures (like graphs) generated with tikz. This only works when using a separate viewer. Note that the object are currently typeset in plain math mode rather than displaymath, because the latter imposes a limit on the width of the picture. Technically, ``tightpage`` adds :: \\usepackage[tightpage,active]{preview} \\PreviewEnvironment{page} to the LaTeX preamble, and replaces the ``\\[`` and ``\\]`` around each object by ``\\begin{page}$`` and ``$\\end{page}``. If in notebook mode with ``viewer`` equal to ``None``, this usually uses jsMath -- see the next paragraph for the exception -- to display the output in the notebook. Only the first argument, ``objects``, is relevant; the others are ignored. If ``objects`` is a list, each object is printed on its own line. In the notebook, this *does* *not* use jsMath if the LaTeX code for ``objects`` contains a string in :meth:`latex.jsmath_avoid_list() <Latex.jsmath_avoid_list>`. In this case, it creates and displays a png file. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE = True sage: view(3) <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html> sage: view(3, mode='display') <html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</div></html> sage: sage.misc.latex.EMBEDDED_MODE = False """ if isinstance(objects, LatexExpr): s = str(objects) else: if tightpage == True: latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} else: latex_options = {} s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) # notebook if EMBEDDED_MODE and viewer is None: jsMath_okay = True for t in latex.jsmath_avoid_list(): if s.find(t) != -1: jsMath_okay = False if not jsMath_okay: break if jsMath_okay: print JSMath().eval(objects, mode=mode) # put comma at end of line? else: base_dir = os.path.abspath("") png_file = graphics_filename(ext='png') png_link = "cell://" + png_file png(objects, os.path.join(base_dir, png_file), debug=debug, do_in_background=False, pdflatex=pdflatex) print '<html><img src="%s"></html>'%png_link # put comma at end of line? return # command line if viewer == "pdf": pdflatex = True if pdflatex is None: pdflatex = _Latex_prefs._option["pdflatex"] tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") open(tex_file,'w').write(s) suffix = _run_latex_(tex_file, debug=debug, pdflatex=pdflatex, png=False) if suffix == "pdf": from sage.misc.viewer import pdf_viewer viewer = pdf_viewer() elif suffix == "dvi": from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() else: print "Latex error" return output_file = os.path.join(tmp, "sage." + suffix) os.system('sage-native-execute %s %s'%(viewer, output_file)) return
do_in_background=False, tiny=False, pdflatex=True):
do_in_background=False, tiny=False, pdflatex=True, engine='pdflatex'):
def png(x, filename, density=150, debug=False, do_in_background=False, tiny=False, pdflatex=True): """ Create a png image representation of ``x`` and save to the given filename. INPUT: - ``x`` - object to be displayed - ``filename`` - file in which to save the image - ``density`` - integer (default: 150) - ``debug`` - bool (default: False): print verbose output - ``do_in_background`` - bool (default: False): create the file in the background. - ``tiny`` - bool (default: False): use 'tiny' font - ``pdflatex`` - bool (default: False): use pdflatex. EXAMPLES:: sage: from sage.misc.latex import png sage: png(ZZ[x], SAGE_TMP + "zz.png", do_in_background=False) # random - error if no latex """ import sage.plot.all if sage.plot.all.is_Graphics(x): x.save(filename) return # if not graphics: create a string of latex code to write in a file s = _latex_file_([x], math_left='$\\displaystyle', math_right='$', title='', debug=debug, tiny=tiny, extra_preamble='\\textheight=2\\textheight') # path name for permanent png output abs_path_to_png = os.path.abspath(filename) # temporary directory to store stuff tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") png_file = os.path.join(tmp, "sage.png") # write latex string to file open(tex_file,'w').write(s) # run latex on the file, producing png output to png_file e = _run_latex_(tex_file, density=density, debug=debug, png=True, do_in_background=do_in_background, pdflatex=pdflatex) if e.find("Error") == -1: # if no errors, copy png_file to the appropriate place shutil.copy(png_file, abs_path_to_png) else: print "Latex error" if debug: return s return
- ``pdflatex`` - bool (default: False): use pdflatex.
- ``pdflatex`` - bool (default: True): use pdflatex. This option is deprecated. Use ``engine`` option instead. See below. - ``engine`` - 'latex', 'pdflatex', or 'xelatex' (default: 'pdflatex')
def png(x, filename, density=150, debug=False, do_in_background=False, tiny=False, pdflatex=True): """ Create a png image representation of ``x`` and save to the given filename. INPUT: - ``x`` - object to be displayed - ``filename`` - file in which to save the image - ``density`` - integer (default: 150) - ``debug`` - bool (default: False): print verbose output - ``do_in_background`` - bool (default: False): create the file in the background. - ``tiny`` - bool (default: False): use 'tiny' font - ``pdflatex`` - bool (default: False): use pdflatex. EXAMPLES:: sage: from sage.misc.latex import png sage: png(ZZ[x], SAGE_TMP + "zz.png", do_in_background=False) # random - error if no latex """ import sage.plot.all if sage.plot.all.is_Graphics(x): x.save(filename) return # if not graphics: create a string of latex code to write in a file s = _latex_file_([x], math_left='$\\displaystyle', math_right='$', title='', debug=debug, tiny=tiny, extra_preamble='\\textheight=2\\textheight') # path name for permanent png output abs_path_to_png = os.path.abspath(filename) # temporary directory to store stuff tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") png_file = os.path.join(tmp, "sage.png") # write latex string to file open(tex_file,'w').write(s) # run latex on the file, producing png output to png_file e = _run_latex_(tex_file, density=density, debug=debug, png=True, do_in_background=do_in_background, pdflatex=pdflatex) if e.find("Error") == -1: # if no errors, copy png_file to the appropriate place shutil.copy(png_file, abs_path_to_png) else: print "Latex error" if debug: return s return
pdflatex=pdflatex)
engine=engine)
def png(x, filename, density=150, debug=False, do_in_background=False, tiny=False, pdflatex=True): """ Create a png image representation of ``x`` and save to the given filename. INPUT: - ``x`` - object to be displayed - ``filename`` - file in which to save the image - ``density`` - integer (default: 150) - ``debug`` - bool (default: False): print verbose output - ``do_in_background`` - bool (default: False): create the file in the background. - ``tiny`` - bool (default: False): use 'tiny' font - ``pdflatex`` - bool (default: False): use pdflatex. EXAMPLES:: sage: from sage.misc.latex import png sage: png(ZZ[x], SAGE_TMP + "zz.png", do_in_background=False) # random - error if no latex """ import sage.plot.all if sage.plot.all.is_Graphics(x): x.save(filename) return # if not graphics: create a string of latex code to write in a file s = _latex_file_([x], math_left='$\\displaystyle', math_right='$', title='', debug=debug, tiny=tiny, extra_preamble='\\textheight=2\\textheight') # path name for permanent png output abs_path_to_png = os.path.abspath(filename) # temporary directory to store stuff tmp = tmp_dir('sage_viewer') tex_file = os.path.join(tmp, "sage.tex") png_file = os.path.join(tmp, "sage.png") # write latex string to file open(tex_file,'w').write(s) # run latex on the file, producing png output to png_file e = _run_latex_(tex_file, density=density, debug=debug, png=True, do_in_background=do_in_background, pdflatex=pdflatex) if e.find("Error") == -1: # if no errors, copy png_file to the appropriate place shutil.copy(png_file, abs_path_to_png) else: print "Latex error" if debug: return s return
"""
r"""
def FuzzyBallGraph(self, partition, q): """ Construct a Fuzzy Ball graph with the integer partition ``partition`` and ``q`` extra vertices.
Let q be an integer and let m_1,m_2,...m_k be a set of positive integers. Let n=q+m_1+...+m_k. The Fuzzy Ball graph with partition m_1,m_2,...,m_k and q extra vertices is the graph constructed from the graph G=K_n by attaching, for each i=1,2,...,k, a new vertex a_i to m_i distinct vertices of G. For given positive integers k and m and nonnegative integer q, the set of graphs FuzzyBallGraph(p, q) for all partitions `p=m_1+m_2+\cdots+m_k` of m are cospectral with respect to the
Let `q` be an integer and let `m_1,m_2,...,m_k` be a set of positive integers. Let `n=q+m_1+...+m_k`. The Fuzzy Ball graph with partition `m_1,m_2,...,m_k` and `q` extra vertices is the graph constructed from the graph `G=K_n` by attaching, for each `i=1,2,...,k`, a new vertex `a_i` to `m_i` distinct vertices of `G`. For given positive integers `k` and `m` and nonnegative integer `q`, the set of graphs ``FuzzyBallGraph(p, q)`` for all partitions `p=m_1+m_2+\cdots+m_k` of `m` are cospectral with respect to the
def FuzzyBallGraph(self, partition, q): """ Construct a Fuzzy Ball graph with the integer partition ``partition`` and ``q`` extra vertices.
Pick positive integers m and k and a nonnegative integer q. All the FuzzyBallGraphs constructed from partitions of m with k parts should be cospectral with respect to the normalized
Pick positive integers `m` and `k` and a nonnegative integer `q`. All the FuzzyBallGraphs constructed from partitions of `m` with `k` parts should be cospectral with respect to the normalized
def FuzzyBallGraph(self, partition, q): """ Construct a Fuzzy Ball graph with the integer partition ``partition`` and ``q`` extra vertices.