Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

# cython: old_style_globals=True 

""" 

Dependency usage tracking for citations 

""" 

from __future__ import absolute_import 

  

from sage.misc.all import tmp_filename 

from sage.env import SAGE_ROOT 

  

systems = {} 

systems['PARI'] = ['cypari2', 'sage.interfaces.gp'] 

systems['Singular'] = ['sage.interfaces.singular', '_libsingular', 

'sage.libs.singular'] 

systems['Maxima'] = ['sage.interfaces.maxima'] 

systems['GAP'] = ['sage.interfaces.gap'] 

systems['Magma'] = ['sage.interfaces.magma', 'sage.interfaces.magma_free'] 

systems['Axiom'] = ['sage.interfaces.axiom'] 

systems['ECM'] = ['sage.interfaces.ecm'] 

systems['scipy'] = ['scipy'] 

systems['numpy'] = ['numpy'] 

systems['ginac'] = ['sage.symbolic'] 

systems['Maple'] = ['sage.interfaces.maple'] 

systems['Mathematica'] = ['sage.interfaces.mathematica'] 

systems['MuPAD'] = ['sage.interfaces.mupad'] 

systems['Octave'] = ['sage.interfaces.octave'] 

systems['povray'] = ['sage.interfaces.povray'] 

systems['qsieve'] = ['sage.interfaces.qsieve'] 

systems['Macaulay2'] = ['sage.interfaces.macaulay2'] 

systems['mwrank'] = ['sage.interfaces.mwrank', 'sage.libs.eclib'] 

systems['matlab'] = ['sage.interfaces.matlab'] 

systems['LiE'] = ['sage.interfaces.lie'] 

systems['Tachyon'] = ['sage.interfaces.tachyon'] 

systems['Frobby'] = ['sage.interfaces.frobby'] 

systems['gfan'] = ['sage.interfaces.gfan'] 

systems['R'] = ['sage.interfaces.r'] 

systems['KASH'] = ['sage.interfaces.kash'] 

systems['Linbox'] = ['sage.libs.linbox'] 

systems['Symmetrica'] = ['sage.libs.symmetrica'] 

systems['NTL'] = ['sage.libs.ntl', 

'sage.rings.finite_rings.element_ntl_gf2e'] 

systems['FLINT'] = ['_flint'] 

systems['GMP'] = ['sage.rings.integer.Integer'] 

systems['MPFR'] = ['sage.rings.real_mpfr', 

'sage.rings.complex_number'] 

systems['MPFI'] = ['sage.rings.real_mpfi', 

'sage.rings.complex_interval'] 

systems['M4RI'] = ['sage.matrix.matrix_mod2_dense'] 

systems['Givaro'] = ['sage.rings.finite_rings.element_givaro'] 

systems['PolyBoRi'] = ['sage.rings.polynomial.pbori'] 

  

def get_systems(cmd): 

""" 

Returns a list of the systems used in running the command ``cmd``. 

Note that the results can sometimes include systems that did not 

actually contribute to the computation. Due to caching, it 

could miss some dependencies as well. 

  

INPUT: 

  

- ``cmd`` - a string to run 

  

.. WARNING:: 

  

In order to properly support Cython code, this requires that 

Sage was compiled with the environment variable 

``SAGE_PROFILE=yes``. If this was not the case, a warning will 

be given when calling this function. 

  

EXAMPLES:: 

  

sage: from sage.misc.citation import get_systems 

sage: get_systems('print("hello")') # random (may print warning) 

[] 

sage: integrate(x^2, x) # Priming coercion model 

1/3*x^3 

sage: get_systems('integrate(x^2, x)') 

['ginac', 'Maxima'] 

sage: R.<x,y,z> = QQ[] 

sage: I = R.ideal(x^2+y^2, z^2+y) 

sage: get_systems('I.primary_decomposition()') 

['Singular'] 

  

Here we get a spurious ``MPFR`` because some coercions need to be 

initialized. The second time it is gone:: 

  

sage: a = var('a') 

sage: get_systems('((a+1)^2).expand()') 

['MPFR', 'ginac'] 

sage: get_systems('((a+1)^2).expand()') 

['ginac'] 

""" 

import cProfile, pstats, re 

  

if not cython_profile_enabled(): 

from warnings import warn 

warn("get_systems() requires Cython profiling to be enabled, " 

"otherwise the results will be very unreliable. " 

"Rebuild Sage with the environment variable 'SAGE_PROFILE=yes' " 

"to enable profiling.") 

  

if not isinstance(cmd, basestring): 

raise TypeError("command must be a string") 

  

from sage.repl.preparse import preparse 

cmd = preparse(cmd) 

  

#Run the command and get the stats 

filename = tmp_filename() 

cProfile.runctx(cmd, globals(), {}, filename) 

stats = pstats.Stats(filename) 

  

#Strings is a list of method names and modules which get run 

strings = [a[0].replace(SAGE_ROOT, "") + " " + a[2] for a in stats.stats.keys()] 

  

#Remove trivial functions 

bad_res = [re.compile(r'is_.*Element'), re.compile("is_[a-z_]*_type")] 

for bad_re in bad_res: 

i = 0 

while i < len(strings): 

if bad_re.findall(strings[i]): 

strings.pop(i) 

else: 

i += 1 

  

#Check to see which systems appear in the profiled run 

systems_used = [] 

for system in systems: 

if any([(r in s) or (r.replace('.','/') in s) for r in systems[system] for s in strings]): 

systems_used.append(system) 

return systems_used 

  

  

cdef extern from *: 

int CYTHON_PROFILE """ 

#ifdef CYTHON_PROFILE 

CYTHON_PROFILE 

#else 

0 

#endif 

""" 

  

cpdef inline bint cython_profile_enabled(): 

""" 

Return whether Cython profiling is enabled. 

  

EXAMPLES:: 

  

sage: from sage.misc.citation import cython_profile_enabled 

sage: cython_profile_enabled() # random 

False 

sage: type(cython_profile_enabled()) is bool 

True 

""" 

return CYTHON_PROFILE