Path: blob/master/src/sage/quadratic_forms/random_quadraticform.py
8817 views
"""1Creating A Random Quadratic Form2"""3from sage.quadratic_forms.quadratic_form import QuadraticForm4from sage.quadratic_forms.ternary_qf import TernaryQF5from sage.rings.ring import is_Ring6from sage.rings.all import ZZ78################################################9## Routines to create a random quadratic form ##10################################################1112def random_quadraticform(R, n, rand_arg_list=[]):13"""14Create a random quadratic form in `n` variables defined over the ring `R`.1516The last (and optional) argument ``rand_arg_list`` is a list of at most 317elements which is passed (as at most 3 separate variables) into the method18``R.random_element()``.1920INPUT:2122- `R` -- a ring.23- `n` -- an integer `\ge 0`24- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by25``R.random_element()``.2627OUTPUT:2829A quadratic form over the ring `R`.3031EXAMPLES::3233sage: random_quadraticform(ZZ, 3, [1,5]) ## RANDOM34Quadratic form in 3 variables over Integer Ring with coefficients:35[ 3 2 3 ]36[ * 1 4 ]37[ * * 3 ]3839::4041sage: random_quadraticform(ZZ, 3, [-5,5]) ## RANDOM42Quadratic form in 3 variables over Integer Ring with coefficients:43[ 3 2 -5 ]44[ * 2 -2 ]45[ * * -5 ]4647::4849sage: random_quadraticform(ZZ, 3, [-50,50]) ## RANDOM50Quadratic form in 3 variables over Integer Ring with coefficients:51[ 1 8 -23 ]52[ * 0 0 ]53[ * * 6 ]54"""55## Sanity Checks: We have a ring and there are at most 3 parameters for randomness!56if len(rand_arg_list) > 3:57raise TypeError, "Oops! The list of randomness arguments can have at most 3 elements."58if not is_Ring(R):59raise TypeError, "Oops! The first argument must be a ring."6061## Create a list of upper-triangular entries for the quadratic form62L = len(rand_arg_list)63nn = int(n*(n+1)/2)64if L == 0:65rand_list = [R.random_element() for _ in range(nn)]66elif L == 1:67rand_list = [R.random_element(rand_arg_list[0]) for _ in range(nn)]68elif L == 2:69rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(nn)]70elif L == 3:71rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(nn)]7273## Return the Quadratic Form74return QuadraticForm(R, n, rand_list)757677def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=[]):78"""79Create a random quadratic form in `n` variables defined over the ring `R`80satisfying a list of boolean (i.e. True/False) conditions.8182The conditions `c` appearing in the list must be boolean functions which83can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random84quadratic form.8586The last (and optional) argument ``rand_arg_list`` is a list of at most 387elements which is passed (as at most 3 separate variables) into the method88``R.random_element()``.8990EXAMPLES::9192sage: Q = random_quadraticform_with_conditions(ZZ, 3, [QuadraticForm.is_positive_definite], [-5, 5])93sage: Q ## RANDOM94Quadratic form in 3 variables over Integer Ring with coefficients:95[ 3 -2 -5 ]96[ * 2 2 ]97[ * * 3 ]9899"""100Q = random_quadraticform(R, n, rand_arg_list)101Done_Flag = True102103## Check that all conditions are satisfied104while Done_Flag:105Done_Flag = False106for c in condition_list:107108## Check if condition c is satisfied109try:110bool_ans = Q.c()111except Exception:112bool_ans = c(Q)113114## Create a new quadratic form if a condition fails115if (bool_ans == False):116Q = random_quadraticform(R, n, rand_arg_list)117Done_Flag = True118break119120## Return the quadratic form121return Q122123def random_ternaryqf(rand_arg_list = []):124"""125Create a random ternary quadratic form.126127The last (and optional) argument ``rand_arg_list`` is a list of at most 3128elements which is passed (as at most 3 separate variables) into the method129``R.random_element()``.130131INPUT:132133- ``rand_arg_list`` -- a list of at most 3 arguments which can be taken by134``R.random_element()``.135136OUTPUT:137138A ternary quadratic form.139140EXAMPLES::141142sage: random_ternaryqf() ##RANDOM143Ternary quadratic form with integer coefficients:144[1 1 4]145[-1 1 -1]146sage: random_ternaryqf([-1, 2]) ##RANDOM147Ternary quadratic form with integer coefficients:148[1 0 1]149[-1 -1 -1]150sage: random_ternaryqf([-10, 10, "uniform"]) ##RANDOM151Ternary quadratic form with integer coefficients:152[7 -8 2]153[0 3 -6]154"""155156157R = ZZ158n = 6159L = len(rand_arg_list)160if L == 0:161rand_list = [ R.random_element() for _ in range(n)]162elif L == 1:163rand_list = [ R.random_element(rand_arg_list[0]) for _ in range(6)]164elif L == 2:165rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(6)]166elif L == 3:167rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(6)]168169return TernaryQF(rand_list)170171172173def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]):174"""175Create a random ternary quadratic form satisfying a list of boolean176(i.e. True/False) conditions.177178The conditions `c` appearing in the list must be boolean functions which179can be called either as ``Q.c()`` or ``c(Q)``, where ``Q`` is the random180ternary quadratic form.181182The last (and optional) argument ``rand_arg_list`` is a list of at most 3183elements which is passed (as at most 3 separate variables) into the method184``R.random_element()``.185186EXAMPLES::187188sage: Q = random_ternaryqf_with_conditions([TernaryQF.is_positive_definite], [-5, 5])189sage: Q ## RANDOM190Ternary quadratic form with integer coefficients:191[3 4 2]192[2 -2 -1]193"""194195Q = random_ternaryqf(rand_arg_list)196Done_Flag = True197198## Check that all conditions are satisfied199while Done_Flag:200Done_Flag = False201for c in condition_list:202203## Check if condition c is satisfied204try:205bool_ans = Q.c()206except Exception:207bool_ans = c(Q)208209## Create a new quadratic form if a condition fails210if (bool_ans == False):211Q = random_ternaryqf(rand_arg_list)212Done_Flag = True213break214return Q215216217