Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/quadratic_forms/random_quadraticform.py
8817 views
1
"""
2
Creating A Random Quadratic Form
3
"""
4
from sage.quadratic_forms.quadratic_form import QuadraticForm
5
from sage.quadratic_forms.ternary_qf import TernaryQF
6
from sage.rings.ring import is_Ring
7
from sage.rings.all import ZZ
8
9
################################################
10
## Routines to create a random quadratic form ##
11
################################################
12
13
def random_quadraticform(R, n, rand_arg_list=[]):
14
"""
15
Create a random quadratic form in `n` variables defined over the ring `R`.
16
17
The last (and optional) argument ``rand_arg_list`` is a list of at most 3
18
elements which is passed (as at most 3 separate variables) into the method
19
``R.random_element()``.
20
21
INPUT:
22
23
- `R` -- a ring.
24
- `n` -- an integer `\ge 0`
25
- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by
26
``R.random_element()``.
27
28
OUTPUT:
29
30
A quadratic form over the ring `R`.
31
32
EXAMPLES::
33
34
sage: random_quadraticform(ZZ, 3, [1,5]) ## RANDOM
35
Quadratic form in 3 variables over Integer Ring with coefficients:
36
[ 3 2 3 ]
37
[ * 1 4 ]
38
[ * * 3 ]
39
40
::
41
42
sage: random_quadraticform(ZZ, 3, [-5,5]) ## RANDOM
43
Quadratic form in 3 variables over Integer Ring with coefficients:
44
[ 3 2 -5 ]
45
[ * 2 -2 ]
46
[ * * -5 ]
47
48
::
49
50
sage: random_quadraticform(ZZ, 3, [-50,50]) ## RANDOM
51
Quadratic form in 3 variables over Integer Ring with coefficients:
52
[ 1 8 -23 ]
53
[ * 0 0 ]
54
[ * * 6 ]
55
"""
56
## Sanity Checks: We have a ring and there are at most 3 parameters for randomness!
57
if len(rand_arg_list) > 3:
58
raise TypeError, "Oops! The list of randomness arguments can have at most 3 elements."
59
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
75
return QuadraticForm(R, n, rand_list)
76
77
78
def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=[]):
79
"""
80
Create a random quadratic form in `n` variables defined over the ring `R`
81
satisfying a list of boolean (i.e. True/False) conditions.
82
83
The conditions `c` appearing in the list must be boolean functions which
84
can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random
85
quadratic form.
86
87
The last (and optional) argument ``rand_arg_list`` is a list of at most 3
88
elements which is passed (as at most 3 separate variables) into the method
89
``R.random_element()``.
90
91
EXAMPLES::
92
93
sage: Q = random_quadraticform_with_conditions(ZZ, 3, [QuadraticForm.is_positive_definite], [-5, 5])
94
sage: Q ## RANDOM
95
Quadratic form in 3 variables over Integer Ring with coefficients:
96
[ 3 -2 -5 ]
97
[ * 2 2 ]
98
[ * * 3 ]
99
100
"""
101
Q = random_quadraticform(R, n, rand_arg_list)
102
Done_Flag = True
103
104
## Check that all conditions are satisfied
105
while Done_Flag:
106
Done_Flag = False
107
for c in condition_list:
108
109
## Check if condition c is satisfied
110
try:
111
bool_ans = Q.c()
112
except Exception:
113
bool_ans = c(Q)
114
115
## Create a new quadratic form if a condition fails
116
if (bool_ans == False):
117
Q = random_quadraticform(R, n, rand_arg_list)
118
Done_Flag = True
119
break
120
121
## Return the quadratic form
122
return Q
123
124
def random_ternaryqf(rand_arg_list = []):
125
"""
126
Create a random ternary quadratic form.
127
128
The last (and optional) argument ``rand_arg_list`` is a list of at most 3
129
elements which is passed (as at most 3 separate variables) into the method
130
``R.random_element()``.
131
132
INPUT:
133
134
- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by
135
``R.random_element()``.
136
137
OUTPUT:
138
139
A ternary quadratic form.
140
141
EXAMPLES::
142
143
sage: random_ternaryqf() ##RANDOM
144
Ternary quadratic form with integer coefficients:
145
[1 1 4]
146
[-1 1 -1]
147
sage: random_ternaryqf([-1, 2]) ##RANDOM
148
Ternary quadratic form with integer coefficients:
149
[1 0 1]
150
[-1 -1 -1]
151
sage: random_ternaryqf([-10, 10, "uniform"]) ##RANDOM
152
Ternary quadratic form with integer coefficients:
153
[7 -8 2]
154
[0 3 -6]
155
"""
156
157
158
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
170
return TernaryQF(rand_list)
171
172
173
174
def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]):
175
"""
176
Create a random ternary quadratic form satisfying a list of boolean
177
(i.e. True/False) conditions.
178
179
The conditions `c` appearing in the list must be boolean functions which
180
can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random
181
ternary quadratic form.
182
183
The last (and optional) argument ``rand_arg_list`` is a list of at most 3
184
elements which is passed (as at most 3 separate variables) into the method
185
``R.random_element()``.
186
187
EXAMPLES::
188
189
sage: Q = random_ternaryqf_with_conditions([TernaryQF.is_positive_definite], [-5, 5])
190
sage: Q ## RANDOM
191
Ternary quadratic form with integer coefficients:
192
[3 4 2]
193
[2 -2 -1]
194
"""
195
196
Q = random_ternaryqf(rand_arg_list)
197
Done_Flag = True
198
199
## Check that all conditions are satisfied
200
while Done_Flag:
201
Done_Flag = False
202
for c in condition_list:
203
204
## Check if condition c is satisfied
205
try:
206
bool_ans = Q.c()
207
except Exception:
208
bool_ans = c(Q)
209
210
## Create a new quadratic form if a condition fails
211
if (bool_ans == False):
212
Q = random_ternaryqf(rand_arg_list)
213
Done_Flag = True
214
break
215
return Q
216
217