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

155

156

157

158

159

160

""" 

Enumeration of Totally Real Fields: PHC interface 

 

AUTHORS: 

 

-- John Voight (2007-10-10): 

* Zeroth attempt. 

""" 

 

#***************************************************************************** 

# Copyright (C) 2007 William Stein and John Voight 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

import os 

import sage.misc.misc 

 

 

def coefficients_to_power_sums(n, m, a): 

r""" 

Takes the list a, representing a list of initial coefficients of 

a (monic) polynomial of degree n, and returns the power sums 

of the roots of f up to (m-1)th powers. 

 

INPUT: 

 

- n -- integer, the degree 

- a -- list of integers, the coefficients 

 

OUTPUT: 

 

list of integers. 

 

NOTES: 

 

Uses Newton's relations, which are classical. 

 

AUTHORS: 

 

- John Voight (2007-09-19) 

 

EXAMPLES:: 

 

sage: from sage.rings.number_field.totallyreal_phc import coefficients_to_power_sums 

sage: coefficients_to_power_sums(3,2,[1,5,7]) 

[3, -7, 39] 

sage: coefficients_to_power_sums(5,4,[1,5,7,9,8]) 

[5, -8, 46, -317, 2158] 

""" 

S = [n] + [0]*m 

for k in range(1,m+1): 

S[k] = -sum([a[n-i]*S[k-i] for i in range(1,k)])-k*a[n-k] 

return S 

 

 

def __lagrange_bounds_phc(n, m, a, tmpfile=None): 

r""" 

This function determines the bounds on the roots in 

the enumeration of totally real fields via Lagrange multipliers. 

It is used internally by the main function 

enumerate_totallyreal_fields_prim(), which should be consulted for 

further information. 

 

INPUT: 

 

- k -- integer, the index of the next coefficient 

- a -- list of integers, the coefficients 

 

OUTPUT: 

 

the lower and upper bounds as real numbers. 

 

NOTES: 

 

See Cohen [Cohen2000]_ for the general idea and unpublished work of the 

author for more detail. 

 

REFERENCES: 

 

.. [Cohen2000] Henri Cohen, Advanced topics in computational number theory, 

Graduate Texts in Mathematics, vol. 193, Springer-Verlag, New York, 

2000. 

 

AUTHORS: 

 

- John Voight (2007-09-19) 

 

EXAMPLES:: 

 

sage: from sage.rings.number_field.totallyreal_phc import __lagrange_bounds_phc 

sage: __lagrange_bounds_phc(3,5,[8,1,2,0,1]) # optional - phc 

[] 

sage: x, y = __lagrange_bounds_phc(3,2,[8,1,2,0,1]) # optional - phc 

sage: x # optional - phc 

-1.3333333333333299 

sage: y < 0.00000001 # optional - phc 

True 

sage: __lagrange_bounds_phc(3,1,[8,1,2,0,1]) # optional - phc 

[] 

""" 

 

# Compute power sums. 

S = coefficients_to_power_sums(n,m,a) 

 

# Look for phc. 

fi, fo = os.popen2('which phc') 

find_phc = fo.readlines() 

fi.close() 

fo.close() 

if find_phc == []: 

raise RuntimeError("PHCpack not installed.") 

 

# Initialization. 

if tmpfile is None: 

tmpfile = sage.misc.misc.tmp_filename() 

f = open(tmpfile + '.phc', 'w') 

f.close() 

 

output_data = [] 

 

# By the method of Lagrange multipliers, if we maximize x_n subject to 

# S_j(x) = S[j] (j = 1, ..., m), 

# then there are at most m-1 distinct values amongst the x_i. 

# Therefore we must solve the implied equations for each partition of n-1 

# into m-1 parts. 

for P in sage.combinat.partition.Partitions(n-1,length=m-1): 

f = open(tmpfile, 'w') 

# First line: number of variables/equations 

f.write('%d'%m + '\n') 

# In the next m-1 lines, write the equation S_j(x) = S[j] 

for j in range(1,m+1): 

for i in range(m-1): 

f.write('%d'%P[i] + '*x%d'%i + '**%d'%j + ' + ') 

f.write('xn**%d'%j + ' - (%d'%S[j] + ');\n') 

f.close() 

 

os.remove(tmpfile + '.phc') 

os.popen('phc -b ' + tmpfile + ' ' + tmpfile + '.phc') 

f = open(tmpfile + '.phc', 'r') 

f_str = f.read() 

pos = f_str.find('= real ') 

crits = [] 

while pos != -1: 

posl = f_str.rfind('xn', 0, pos) 

f_str_split = f_str[posl:pos].split() 

crits += [float(f_str_split[2])] 

pos = f_str.find('= real ', pos+1) 

 

if len(crits) > 0: 

output_data += [[P, min(crits), max(crits)]] 

 

if len(output_data) > 0: 

return [min([v[1] for v in output_data]), max([v[2] for v in output_data])] 

else: 

return []