Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/quadratic_forms/qfsolve.py
8817 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
31
_gp_for_simon_interpreter = None # Global GP interpreter for Denis Simon's code
32
def _gp_for_simon():
33
r"""
34
Start a GP interpreter for the use of Denis Simon's Qfsolve and Qfparam
35
if it is not started already.
36
37
EXAMPLE ::
38
39
sage: from sage.quadratic_forms.qfsolve import _gp_for_simon
40
sage: _gp_for_simon()
41
PARI/GP interpreter
42
"""
43
global _gp_for_simon_interpreter
44
if _gp_for_simon_interpreter is None:
45
_gp_for_simon_interpreter = Gp(script_subdirectory='simon')
46
_gp_for_simon_interpreter.read("qfsolve.gp")
47
return _gp_for_simon_interpreter
48
49
# \\ - Qfsolve(G,factD): pour resoudre l'equation quadratique X^t*G*X = 0
50
# \\ G doit etre une matrice symetrique n*n, a coefficients dans Z.
51
# \\ S'il n'existe pas de solution, la reponse est un entier
52
# \\ indiquant un corps local dans lequel aucune solution n'existe
53
# \\ (-1 pour les reels, p pour Q_p).
54
# \\ Si on connait la factorisation de -abs(2*matdet(G)),
55
# \\ on peut la passer par le parametre factD pour gagner du temps.
56
# \\
57
# \\ - Qfparam(G,sol,fl): pour parametrer les solutions de la forme
58
# \\ quadratique ternaire G, en utilisant la solution particuliere sol.
59
# \\ si fl>0, la 'fl'eme forme quadratique est reduite.
60
61
def qfsolve(G, factD=None):
62
r"""
63
Find a solution `x = (x_0,...,x_n)` to `x G x^t = 0` for an
64
`n \times n`-matrix ``G`` over `\QQ`.
65
66
If a solution exists, returns a tuple of rational numbers `x`.
67
Otherwise, returns `-1` if no solutions exists over the reals or a
68
prime `p` if no solution exists over the `p`-adic field `\QQ_p`.
69
70
EXAMPLES::
71
72
sage: from sage.quadratic_forms.qfsolve import qfsolve
73
sage: M = Matrix(QQ, [[0, 0, -12], [0, -12, 0], [-12, 0, -1]]); M
74
[ 0 0 -12]
75
[ 0 -12 0]
76
[-12 0 -1]
77
sage: sol = qfsolve(M); sol
78
(1, 0, 0)
79
sage: sol[0].parent() is QQ
80
True
81
82
sage: M = Matrix(QQ, [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
83
sage: ret = qfsolve(M); ret
84
-1
85
sage: ret.parent() is ZZ
86
True
87
88
sage: M = Matrix(QQ, [[1, 0, 0], [0, 1, 0], [0, 0, -7]])
89
sage: qfsolve(M)
90
7
91
92
sage: M = Matrix(QQ, [[3, 0, 0, 0], [0, 5, 0, 0], [0, 0, -7, 0], [0, 0, 0, -11]])
93
sage: qfsolve(M)
94
(-3, 4, 3, 2)
95
"""
96
gp = _gp_for_simon()
97
if factD is not None:
98
raise NotImplementedError, "qfsolve not implemented with parameter factD"
99
ret = gp('Qfsolve(%s)' % G._pari_init_())
100
if str(ret.type()) == 't_COL': # Need explicit str(), see #15522
101
return tuple(QQ(r) for r in ret)
102
return ZZ(ret)
103
104
def qfparam(G, sol):
105
r"""
106
Parametrizes the conic defined by the matrix ``G``.
107
108
INPUT:
109
110
- ``G`` -- a `3 \times 3`-matrix over `\QQ`.
111
112
- ``sol`` -- a triple of rational numbers providing a solution
113
to sol*G*sol^t = 0.
114
115
OUTPUT:
116
117
A triple of polynomials that parametrizes all solutions of
118
x*G*x^t = 0 up to scaling.
119
120
ALGORITHM:
121
122
Uses Denis Simon's pari script Qfparam.
123
124
EXAMPLES::
125
126
sage: from sage.quadratic_forms.qfsolve import qfsolve, qfparam
127
sage: M = Matrix(QQ, [[0, 0, -12], [0, -12, 0], [-12, 0, -1]]); M
128
[ 0 0 -12]
129
[ 0 -12 0]
130
[-12 0 -1]
131
sage: sol = qfsolve(M);
132
sage: ret = qfparam(M, sol); ret
133
(-t^2 - 12, 24*t, 24*t^2)
134
sage: ret[0].parent() is QQ['t']
135
True
136
"""
137
gp = _gp_for_simon()
138
R = QQ['t']
139
t = R.gen()
140
s = 'Qfparam((%s), (%s)~)*[t^2,t,1]~' % (G._pari_init_(), list(sol))
141
ret = gp(s)
142
return tuple(R(str(r)) for r in ret)
143
144