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

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

""" 

Creating A Random Quadratic Form 

""" 

from sage.quadratic_forms.quadratic_form import QuadraticForm 

from sage.quadratic_forms.ternary_qf import TernaryQF 

from sage.rings.ring import is_Ring 

from sage.rings.all import ZZ 

 

################################################ 

## Routines to create a random quadratic form ## 

################################################ 

 

def random_quadraticform(R, n, rand_arg_list=[]): 

""" 

Create a random quadratic form in `n` variables defined over the ring `R`. 

 

The last (and optional) argument ``rand_arg_list`` is a list of at most 3 

elements which is passed (as at most 3 separate variables) into the method 

``R.random_element()``. 

 

INPUT: 

 

- `R` -- a ring. 

- `n` -- an integer `\ge 0` 

- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by 

``R.random_element()``. 

 

OUTPUT: 

 

A quadratic form over the ring `R`. 

 

EXAMPLES:: 

 

sage: random_quadraticform(ZZ, 3, [1,5]) ## RANDOM 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 3 2 3 ] 

[ * 1 4 ] 

[ * * 3 ] 

 

:: 

 

sage: random_quadraticform(ZZ, 3, [-5,5]) ## RANDOM 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 3 2 -5 ] 

[ * 2 -2 ] 

[ * * -5 ] 

 

:: 

 

sage: random_quadraticform(ZZ, 3, [-50,50]) ## RANDOM 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 1 8 -23 ] 

[ * 0 0 ] 

[ * * 6 ] 

""" 

## Sanity Checks: We have a ring and there are at most 3 parameters for randomness! 

if len(rand_arg_list) > 3: 

raise TypeError("Oops! The list of randomness arguments can have at most 3 elements.") 

if not is_Ring(R): 

raise TypeError("Oops! The first argument must be a ring.") 

 

## Create a list of upper-triangular entries for the quadratic form 

L = len(rand_arg_list) 

nn = int(n*(n+1)/2) 

if L == 0: 

rand_list = [R.random_element() for _ in range(nn)] 

elif L == 1: 

rand_list = [R.random_element(rand_arg_list[0]) for _ in range(nn)] 

elif L == 2: 

rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(nn)] 

elif L == 3: 

rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(nn)] 

 

## Return the Quadratic Form 

return QuadraticForm(R, n, rand_list) 

 

 

def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=[]): 

""" 

Create a random quadratic form in `n` variables defined over the ring `R` 

satisfying a list of boolean (i.e. True/False) conditions. 

 

The conditions `c` appearing in the list must be boolean functions which 

can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random 

quadratic form. 

 

The last (and optional) argument ``rand_arg_list`` is a list of at most 3 

elements which is passed (as at most 3 separate variables) into the method 

``R.random_element()``. 

 

EXAMPLES:: 

 

sage: Q = random_quadraticform_with_conditions(ZZ, 3, [QuadraticForm.is_positive_definite], [-5, 5]) 

sage: Q ## RANDOM 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 3 -2 -5 ] 

[ * 2 2 ] 

[ * * 3 ] 

 

""" 

Q = random_quadraticform(R, n, rand_arg_list) 

Done_Flag = True 

 

## Check that all conditions are satisfied 

while Done_Flag: 

Done_Flag = False 

for c in condition_list: 

 

## Check if condition c is satisfied 

try: 

bool_ans = Q.c() 

except Exception: 

bool_ans = c(Q) 

 

## Create a new quadratic form if a condition fails 

if not bool_ans: 

Q = random_quadraticform(R, n, rand_arg_list) 

Done_Flag = True 

break 

 

## Return the quadratic form 

return Q 

 

def random_ternaryqf(rand_arg_list = []): 

""" 

Create a random ternary quadratic form. 

 

The last (and optional) argument ``rand_arg_list`` is a list of at most 3 

elements which is passed (as at most 3 separate variables) into the method 

``R.random_element()``. 

 

INPUT: 

 

- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by 

``R.random_element()``. 

 

OUTPUT: 

 

A ternary quadratic form. 

 

EXAMPLES:: 

 

sage: random_ternaryqf() ##RANDOM 

Ternary quadratic form with integer coefficients: 

[1 1 4] 

[-1 1 -1] 

sage: random_ternaryqf([-1, 2]) ##RANDOM 

Ternary quadratic form with integer coefficients: 

[1 0 1] 

[-1 -1 -1] 

sage: random_ternaryqf([-10, 10, "uniform"]) ##RANDOM 

Ternary quadratic form with integer coefficients: 

[7 -8 2] 

[0 3 -6] 

""" 

 

 

R = ZZ 

n = 6 

L = len(rand_arg_list) 

if L == 0: 

rand_list = [ R.random_element() for _ in range(n)] 

elif L == 1: 

rand_list = [ R.random_element(rand_arg_list[0]) for _ in range(6)] 

elif L == 2: 

rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(6)] 

elif L == 3: 

rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(6)] 

 

return TernaryQF(rand_list) 

 

 

 

def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]): 

""" 

Create a random ternary quadratic form satisfying a list of boolean 

(i.e. True/False) conditions. 

 

The conditions `c` appearing in the list must be boolean functions which 

can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random 

ternary quadratic form. 

 

The last (and optional) argument ``rand_arg_list`` is a list of at most 3 

elements which is passed (as at most 3 separate variables) into the method 

``R.random_element()``. 

 

EXAMPLES:: 

 

sage: Q = random_ternaryqf_with_conditions([TernaryQF.is_positive_definite], [-5, 5]) 

sage: Q ## RANDOM 

Ternary quadratic form with integer coefficients: 

[3 4 2] 

[2 -2 -1] 

""" 

 

Q = random_ternaryqf(rand_arg_list) 

Done_Flag = True 

 

## Check that all conditions are satisfied 

while Done_Flag: 

Done_Flag = False 

for c in condition_list: 

 

## Check if condition c is satisfied 

try: 

bool_ans = Q.c() 

except Exception: 

bool_ans = c(Q) 

 

## Create a new quadratic form if a condition fails 

if not bool_ans: 

Q = random_ternaryqf(rand_arg_list) 

Done_Flag = True 

break 

return Q