1
1
"""
2
- Creating A Random Quadratic Form
2
+ Random Quadratic Forms
3
+
4
+ This file contains a set of routines to create a random quadratic form.
3
5
"""
4
6
from sage .quadratic_forms .quadratic_form import QuadraticForm
5
7
from sage .quadratic_forms .ternary_qf import TernaryQF
6
8
from sage .rings .ring import is_Ring
7
9
from sage .rings .integer_ring import ZZ
8
10
11
+
9
12
################################################
10
- ## Routines to create a random quadratic form ##
13
+ # Routines to create a random quadratic form ##
11
14
################################################
12
15
13
16
def random_quadraticform (R , n , rand_arg_list = []):
@@ -31,51 +34,47 @@ def random_quadraticform(R, n, rand_arg_list=[]):
31
34
32
35
EXAMPLES::
33
36
34
- sage: random_quadraticform(ZZ, 3, [1,5]) ## RANDOM
37
+ sage: random_quadraticform(ZZ, 3, [1,5]) # random
35
38
Quadratic form in 3 variables over Integer Ring with coefficients:
36
39
[ 3 2 3 ]
37
40
[ * 1 4 ]
38
41
[ * * 3 ]
39
42
40
- ::
41
-
42
- sage: random_quadraticform(ZZ, 3, [-5,5]) ## RANDOM
43
+ sage: random_quadraticform(ZZ, 3, [-5,5]) # random
43
44
Quadratic form in 3 variables over Integer Ring with coefficients:
44
45
[ 3 2 -5 ]
45
46
[ * 2 -2 ]
46
47
[ * * -5 ]
47
48
48
- ::
49
-
50
- sage: random_quadraticform(ZZ, 3, [-50,50]) ## RANDOM
49
+ sage: random_quadraticform(ZZ, 3, [-50,50]) # random
51
50
Quadratic form in 3 variables over Integer Ring with coefficients:
52
51
[ 1 8 -23 ]
53
52
[ * 0 0 ]
54
53
[ * * 6 ]
54
+
55
+ TESTS::
56
+
57
+ sage: random_quadraticform(ZZ, 3, [1,2,3,4])
58
+ Traceback (most recent call last):
59
+ ...
60
+ TypeError: the list of randomness arguments can have at most 3 elements
55
61
"""
56
- ## Sanity Checks: We have a ring and there are at most 3 parameters for randomness!
57
62
if len (rand_arg_list ) > 3 :
58
- raise TypeError ("Oops! The list of randomness arguments can have at most 3 elements." )
63
+ raise TypeError ("the list of randomness arguments can have "
64
+ "at most 3 elements" )
59
65
if not is_Ring (R ):
60
- raise TypeError ("Oops! The first argument must be a ring." )
61
-
62
- ## Create a list of upper-triangular entries for the quadratic form
63
- L = len (rand_arg_list )
64
- nn = int (n * (n + 1 )/ 2 )
65
- if L == 0 :
66
- rand_list = [R .random_element () for _ in range (nn )]
67
- elif L == 1 :
68
- rand_list = [R .random_element (rand_arg_list [0 ]) for _ in range (nn )]
69
- elif L == 2 :
70
- rand_list = [R .random_element (rand_arg_list [0 ], rand_arg_list [1 ]) for _ in range (nn )]
71
- elif L == 3 :
72
- rand_list = [R .random_element (rand_arg_list [0 ], rand_arg_list [1 ], rand_arg_list [2 ]) for _ in range (nn )]
73
-
74
- ## Return the Quadratic Form
66
+ raise TypeError ("the first argument must be a ring" )
67
+ # Create a list of upper-triangular entries for the quadratic form
68
+ n2 = (n * (n + 1 )) // 2
69
+ if not rand_arg_list :
70
+ rand_list = [R .random_element () for _ in range (n2 )]
71
+ else :
72
+ rand_list = [R .random_element (* rand_arg_list ) for _ in range (n2 )]
75
73
return QuadraticForm (R , n , rand_list )
76
74
77
75
78
- def random_quadraticform_with_conditions (R , n , condition_list = [], rand_arg_list = []):
76
+ def random_quadraticform_with_conditions (R , n , condition_list = [],
77
+ rand_arg_list = []):
79
78
"""
80
79
Create a random quadratic form in `n` variables defined over the ring `R`
81
80
satisfying a list of boolean (i.e. True/False) conditions.
@@ -90,38 +89,39 @@ def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=
90
89
91
90
EXAMPLES::
92
91
93
- sage: Q = random_quadraticform_with_conditions(ZZ, 3, [QuadraticForm.is_positive_definite], [-5, 5])
94
- sage: Q ## RANDOM
92
+ sage: check = QuadraticForm.is_positive_definite
93
+ sage: Q = random_quadraticform_with_conditions(ZZ, 3, [check], [-5, 5])
94
+ sage: Q # random
95
95
Quadratic form in 3 variables over Integer Ring with coefficients:
96
96
[ 3 -2 -5 ]
97
97
[ * 2 2 ]
98
98
[ * * 3 ]
99
-
100
99
"""
101
100
Q = random_quadraticform (R , n , rand_arg_list )
102
- Done_Flag = True
101
+ done_flag = True
103
102
104
- ## Check that all conditions are satisfied
105
- while Done_Flag :
106
- Done_Flag = False
103
+ # Check that all conditions are satisfied
104
+ while done_flag :
105
+ done_flag = False
107
106
for c in condition_list :
108
107
109
- ## Check if condition c is satisfied
108
+ # Check if condition c is satisfied
110
109
try :
111
110
bool_ans = Q .c ()
112
111
except Exception :
113
112
bool_ans = c (Q )
114
113
115
- ## Create a new quadratic form if a condition fails
114
+ # Create a new quadratic form if a condition fails
116
115
if not bool_ans :
117
116
Q = random_quadraticform (R , n , rand_arg_list )
118
- Done_Flag = True
117
+ done_flag = True
119
118
break
120
119
121
- ## Return the quadratic form
120
+ # Return the quadratic form
122
121
return Q
123
122
124
- def random_ternaryqf (rand_arg_list = []):
123
+
124
+ def random_ternaryqf (rand_arg_list = []):
125
125
"""
126
126
Create a random ternary quadratic form.
127
127
@@ -140,37 +140,27 @@ def random_ternaryqf(rand_arg_list = []):
140
140
141
141
EXAMPLES::
142
142
143
- sage: random_ternaryqf() ##RANDOM
143
+ sage: random_ternaryqf() # random
144
144
Ternary quadratic form with integer coefficients:
145
145
[1 1 4]
146
146
[-1 1 -1]
147
- sage: random_ternaryqf([-1, 2]) ##RANDOM
147
+ sage: random_ternaryqf([-1, 2]) # random
148
148
Ternary quadratic form with integer coefficients:
149
149
[1 0 1]
150
150
[-1 -1 -1]
151
- sage: random_ternaryqf([-10, 10, "uniform"]) ##RANDOM
151
+ sage: random_ternaryqf([-10, 10, "uniform"]) # random
152
152
Ternary quadratic form with integer coefficients:
153
153
[7 -8 2]
154
154
[0 3 -6]
155
155
"""
156
-
157
-
158
156
R = ZZ
159
- n = 6
160
- L = len (rand_arg_list )
161
- if L == 0 :
162
- rand_list = [ R .random_element () for _ in range (n )]
163
- elif L == 1 :
164
- rand_list = [ R .random_element (rand_arg_list [0 ]) for _ in range (6 )]
165
- elif L == 2 :
166
- rand_list = [ R .random_element (rand_arg_list [0 ], rand_arg_list [1 ]) for _ in range (6 )]
167
- elif L == 3 :
168
- rand_list = [ R .random_element (rand_arg_list [0 ], rand_arg_list [1 ], rand_arg_list [2 ]) for _ in range (6 )]
169
-
157
+ if not rand_arg_list :
158
+ rand_list = [R .random_element () for _ in range (6 )]
159
+ else :
160
+ rand_list = [R .random_element (* rand_arg_list ) for _ in range (6 )]
170
161
return TernaryQF (rand_list )
171
162
172
163
173
-
174
164
def random_ternaryqf_with_conditions (condition_list = [], rand_arg_list = []):
175
165
"""
176
166
Create a random ternary quadratic form satisfying a list of boolean
@@ -186,30 +176,29 @@ def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]):
186
176
187
177
EXAMPLES::
188
178
189
- sage: Q = random_ternaryqf_with_conditions([TernaryQF.is_positive_definite], [-5, 5])
190
- sage: Q ## RANDOM
179
+ sage: check = TernaryQF.is_positive_definite
180
+ sage: Q = random_ternaryqf_with_conditions([check], [-5, 5])
181
+ sage: Q # random
191
182
Ternary quadratic form with integer coefficients:
192
183
[3 4 2]
193
184
[2 -2 -1]
194
185
"""
195
-
196
186
Q = random_ternaryqf (rand_arg_list )
197
- Done_Flag = True
187
+ done_flag = True
198
188
199
- ## Check that all conditions are satisfied
200
- while Done_Flag :
201
- Done_Flag = False
189
+ # Check that all conditions are satisfied
190
+ while done_flag :
191
+ done_flag = False
202
192
for c in condition_list :
203
-
204
- ## Check if condition c is satisfied
193
+ # Check if condition c is satisfied
205
194
try :
206
195
bool_ans = Q .c ()
207
196
except Exception :
208
197
bool_ans = c (Q )
209
198
210
- ## Create a new quadratic form if a condition fails
199
+ # Create a new quadratic form if a condition fails
211
200
if not bool_ans :
212
201
Q = random_ternaryqf (rand_arg_list )
213
- Done_Flag = True
202
+ done_flag = True
214
203
break
215
204
return Q
0 commit comments