Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/quadratic_forms/qfsolve.py
4072 views
1
"""
2
qfsolve: Programme de resolution des equations quadratiques.
3
4
Interface to the GP quadratic forms code of Denis Simon.
5
6
AUTHORS:
7
8
* Denis Simon (GP code)
9
10
* Nick Alexander (Sage interface)
11
"""
12
13
#*****************************************************************************
14
# Copyright (C) 2008 Nick Alexander
15
#
16
# Distributed under the terms of the GNU General Public License (GPL)
17
#
18
# This code is distributed in the hope that it will be useful,
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
# General Public License for more details.
22
#
23
# The full text of the GPL is available at:
24
#
25
# http://www.gnu.org/licenses/
26
#*****************************************************************************
27
28
from sage.interfaces.gp import Gp
29
from sage.rings.all import ZZ, QQ
30
from sage.libs.pari.gen import pari
31
32
_gp_for_simon_interpreter = None # Global GP interpreter for Denis Simon's code
33
def _gp_for_simon():
34
r"""
35
Start a GP interpreter for the use of Denis Simon's Qfsolve and Qfparam
36
if it is not started already.
37
38
EXAMPLE ::
39
40
sage: from sage.quadratic_forms.qfsolve import _gp_for_simon
41
sage: _gp_for_simon()
42
PARI/GP interpreter
43
"""
44
global _gp_for_simon_interpreter
45
if _gp_for_simon_interpreter is None:
46
_gp_for_simon_interpreter = Gp(script_subdirectory='simon')
47
_gp_for_simon_interpreter.read("qfsolve.gp")
48
return _gp_for_simon_interpreter
49
50
# \\ - Qfsolve(G,factD): pour resoudre l'equation quadratique X^t*G*X = 0
51
# \\ G doit etre une matrice symetrique n*n, a coefficients dans Z.
52
# \\ S'il n'existe pas de solution, la reponse est un entier
53
# \\ indiquant un corps local dans lequel aucune solution n'existe
54
# \\ (-1 pour les reels, p pour Q_p).
55
# \\ Si on connait la factorisation de -abs(2*matdet(G)),
56
# \\ on peut la passer par le parametre factD pour gagner du temps.
57
# \\
58
# \\ - Qfparam(G,sol,fl): pour parametrer les solutions de la forme
59
# \\ quadratique ternaire G, en utilisant la solution particuliere sol.
60
# \\ si fl>0, la 'fl'eme forme quadratique est reduite.
61
62
def qfsolve(G, factD=None):
63
r"""
64
Find a solution `x = (x_0,...,x_n)` to `x G x^t = 0` for an
65
`n \times n`-matrix ``G`` over `\QQ`.
66
67
If a solution exists, returns a tuple of rational numbers `x`.
68
Otherwise, returns `-1` if no solutions exists over the reals or a
69
prime `p` if no solution exists over the `p`-adic field `\QQ_p`.
70
71
EXAMPLES::
72
73
sage: from sage.quadratic_forms.qfsolve import qfsolve
74
sage: M = Matrix(QQ, [[0, 0, -12], [0, -12, 0], [-12, 0, -1]]); M
75
[ 0 0 -12]
76
[ 0 -12 0]
77
[-12 0 -1]
78
sage: sol = qfsolve(M); sol
79
(1, 0, 0)
80
sage: sol[0].parent() is QQ
81
True
82
83
sage: M = Matrix(QQ, [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
84
sage: ret = qfsolve(M); ret
85
-1
86
sage: ret.parent() is ZZ
87
True
88
89
sage: M = Matrix(QQ, [[1, 0, 0], [0, 1, 0], [0, 0, -7]])
90
sage: qfsolve(M)
91
7
92
93
sage: M = Matrix(QQ, [[3, 0, 0, 0], [0, 5, 0, 0], [0, 0, -7, 0], [0, 0, 0, -11]])
94
sage: qfsolve(M)
95
(-3, 4, 3, 2)
96
"""
97
gp = _gp_for_simon()
98
if factD is not None:
99
raise NotImplementedError, "qfsolve not implemented with parameter factD"
100
ret = pari(gp('Qfsolve(%s)' % G._pari_()))
101
if ret.type() == 't_COL':
102
return tuple([QQ(r) for r in ret])
103
return ZZ(ret)
104
105
def qfparam(G, sol):
106
r"""
107
Parametrizes the conic defined by the matrix ``G``.
108
109
INPUT:
110
111
- ``G`` -- a `3 \times 3`-matrix over `\QQ`.
112
113
- ``sol`` -- a triple of rational numbers providing a solution
114
to sol*G*sol^t = 0.
115
116
OUTPUT:
117
118
A triple of polynomials that parametrizes all solutions of
119
x*G*x^t = 0 up to scaling.
120
121
ALGORITHM:
122
123
Uses Denis Simon's pari script Qfparam.
124
125
EXAMPLES::
126
127
sage: from sage.quadratic_forms.qfsolve import qfsolve, qfparam
128
sage: M = Matrix(QQ, [[0, 0, -12], [0, -12, 0], [-12, 0, -1]]); M
129
[ 0 0 -12]
130
[ 0 -12 0]
131
[-12 0 -1]
132
sage: sol = qfsolve(M);
133
sage: ret = qfparam(M, sol); ret
134
(-t^2 - 12, 24*t, 24*t^2)
135
sage: ret[0].parent() is QQ['t']
136
True
137
"""
138
gp = _gp_for_simon()
139
R = QQ['t']
140
t = R.gen()
141
s = 'Qfparam(%s, (%s)~)*[t^2,t,1]~' % (G._pari_(), pari(gp(sol))._pari_())
142
ret = pari(gp(s))
143
return tuple([R(r) for r in ret])
144
145
146