Path: blob/master/sage/schemes/plane_conics/constructor.py
4073 views
r"""1Plane conic constructor23AUTHORS:45- Marco Streng (2010-07-20)67- Nick Alexander (2008-01-08)89"""10#*****************************************************************************11# Copyright (C) 2008 Nick Alexander <[email protected]>12# Copyright (C) 2009/2010 Marco Streng <[email protected]>13#14# Distributed under the terms of the GNU General Public License (GPL)15#16# This code is distributed in the hope that it will be useful,17# but WITHOUT ANY WARRANTY; without even the implied warranty of18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU19# General Public License for more details.20#21# The full text of the GPL is available at:22#23# http://www.gnu.org/licenses/24#*****************************************************************************2526from sage.matrix.constructor import Matrix27from sage.modules.free_module_element import vector28from sage.quadratic_forms.all import is_QuadraticForm29from sage.rings.all import (is_MPolynomial, PolynomialRing,30is_IntegralDomain, is_FiniteField,31is_PrimeFiniteField,32is_RationalField)33from sage.rings.number_field.number_field import is_NumberField34from sage.schemes.generic.all import ProjectiveSpace35from sage.schemes.generic.morphism import (36SchemeMorphism_point_affine, SchemeMorphism_point_projective_field)37from sage.structure.all import Sequence38from sage.structure.element import is_Matrix3940from con_field import ProjectiveConic_field41from con_finite_field import ProjectiveConic_finite_field42from con_prime_finite_field import ProjectiveConic_prime_finite_field43from con_number_field import ProjectiveConic_number_field44from con_rational_field import ProjectiveConic_rational_field4546def Conic(base_field, F=None, names=None, unique=True):47r"""48Return the plane projective conic curve defined by ``F``49over ``base_field``.5051The input form ``Conic(F, names=None)`` is also accepted,52in which case the fraction field of the base ring of ``F``53is used as base field.5455INPUT:5657- ``base_field`` -- The base field of the conic.5859- ``names`` -- a list, tuple, or comma separated string60of three variable names specifying the names61of the coordinate functions of the ambient62space `\Bold{P}^3`. If not specified or read63off from ``F``, then this defaults to ``'x,y,z'``.6465- ``F`` -- a polynomial, list, matrix, ternary quadratic form,66or list or tuple of 5 points in the plane.6768If ``F`` is a polynomial or quadratic form,69then the output is the curve in the projective plane70defined by ``F = 0``.7172If ``F`` is a polynomial, then it must be a polynomial73of degree at most 2 in 2 variables, or a homogeneous74polynomial in of degree 2 in 3 variables.7576If ``F`` is a matrix, then the output is the zero locus77of `(x,y,z) F (x,y,z)^t`.7879If ``F`` is a list of coefficients, then it has80length 3 or 6 and gives the coefficients of81the monomials `x^2, y^2, z^2` or all 6 monomials82`x^2, xy, xz, y^2, yz, z^2` in lexicographic order.8384If ``F`` is a list of 5 points in the plane, then the output85is a conic through those points.8687- ``unique`` -- Used only if ``F`` is a list of points in the plane.88If the conic through the points is not unique, then89raise ``ValueError`` if and only if ``unique`` is True9091OUTPUT:9293A plane projective conic curve defined by ``F`` over a field.9495EXAMPLES:9697Conic curves given by polynomials ::9899sage: X,Y,Z = QQ['X,Y,Z'].gens()100sage: Conic(X^2 - X*Y + Y^2 - Z^2)101Projective Conic Curve over Rational Field defined by X^2 - X*Y + Y^2 - Z^2102sage: x,y = GF(7)['x,y'].gens()103sage: Conic(x^2 - x + 2*y^2 - 3, 'U,V,W')104Projective Conic Curve over Finite Field of size 7 defined by U^2 + 2*V^2 - U*W - 3*W^2105106Conic curves given by matrices ::107108sage: Conic(matrix(QQ, [[1, 2, 0], [4, 0, 0], [7, 0, 9]]), 'x,y,z')109Projective Conic Curve over Rational Field defined by x^2 + 6*x*y + 7*x*z + 9*z^2110111sage: x,y,z = GF(11)['x,y,z'].gens()112sage: C = Conic(x^2+y^2-2*z^2); C113Projective Conic Curve over Finite Field of size 11 defined by x^2 + y^2 - 2*z^2114sage: Conic(C.symmetric_matrix(), 'x,y,z')115Projective Conic Curve over Finite Field of size 11 defined by x^2 + y^2 - 2*z^2116117Conics given by coefficients ::118119sage: Conic(QQ, [1,2,3])120Projective Conic Curve over Rational Field defined by x^2 + 2*y^2 + 3*z^2121sage: Conic(GF(7), [1,2,3,4,5,6], 'X')122Projective Conic Curve over Finite Field of size 7 defined by X0^2 + 2*X0*X1 - 3*X1^2 + 3*X0*X2 - 2*X1*X2 - X2^2123124The conic through a set of points ::125126sage: C = Conic(QQ, [[10,2],[3,4],[-7,6],[7,8],[9,10]]); C127Projective Conic Curve over Rational Field defined by x^2 + 13/4*x*y - 17/4*y^2 - 35/2*x*z + 91/4*y*z - 37/2*z^2128sage: C.rational_point()129(10 : 2 : 1)130sage: C.point([3,4])131(3 : 4 : 1)132133sage: a=AffineSpace(GF(13),2)134sage: Conic([a([x,x^2]) for x in range(5)])135Projective Conic Curve over Finite Field of size 13 defined by x^2 - y*z136"""137if not (is_IntegralDomain(base_field) or base_field == None):138if names is None:139names = F140F = base_field141base_field = None142if isinstance(F, (list,tuple)):143if len(F) == 1:144return Conic(base_field, F[0], names)145if names == None:146names = 'x,y,z'147if len(F) == 5:148L=[]149for f in F:150if isinstance(f, SchemeMorphism_point_affine):151C = Sequence(f, universe = base_field)152if len(C) != 2:153raise TypeError, "points in F (=%s) must be planar"%F154C.append(1)155elif isinstance(f, SchemeMorphism_point_projective_field):156C = Sequence(f, universe = base_field)157elif isinstance(f, (list, tuple)):158C = Sequence(f, universe = base_field)159if len(C) == 2:160C.append(1)161else:162raise TypeError, "F (=%s) must be a sequence of planar " \163"points" % F164if len(C) != 3:165raise TypeError, "points in F (=%s) must be planar" % F166P = C.universe()167if not is_IntegralDomain(P):168raise TypeError, "coordinates of points in F (=%s) must " \169"be in an integral domain" % F170L.append(Sequence([C[0]**2, C[0]*C[1], C[0]*C[2], C[1]**2,171C[1]*C[2], C[2]**2], P.fraction_field()))172M=Matrix(L)173if unique and M.rank() != 5:174raise ValueError, "points in F (=%s) do not define a unique " \175"conic" % F176con = Conic(base_field, Sequence(M.right_kernel().gen()), names)177con.point(F[0])178return con179F = Sequence(F, universe = base_field)180base_field = F.universe().fraction_field()181temp_ring = PolynomialRing(base_field, 3, names)182(x,y,z) = temp_ring.gens()183if len(F) == 3:184return Conic(F[0]*x**2 + F[1]*y**2 + F[2]*z**2)185if len(F) == 6:186return Conic(F[0]*x**2 + F[1]*x*y + F[2]*x*z + F[3]*y**2 + \187F[4]*y*z + F[5]*z**2)188raise TypeError, "F (=%s) must be a sequence of 3 or 6" \189"coefficients" % F190if is_QuadraticForm(F):191F = F.matrix()192if is_Matrix(F) and F.is_square() and F.ncols() == 3:193if names == None:194names = 'x,y,z'195temp_ring = PolynomialRing(F.base_ring(), 3, names)196F = vector(temp_ring.gens()) * F * vector(temp_ring.gens())197198if not is_MPolynomial(F):199raise TypeError, "F (=%s) must be a three-variable polynomial or " \200"a sequence of points or coefficients" % F201202if F.total_degree() != 2:203raise TypeError, "F (=%s) must have degree 2" % F204205if base_field == None:206base_field = F.base_ring()207if not is_IntegralDomain(base_field):208raise ValueError, "Base field (=%s) must be a field" % base_field209base_field = base_field.fraction_field()210if names == None:211names = F.parent().variable_names()212pol_ring = PolynomialRing(base_field, 3, names)213214if F.parent().ngens() == 2:215(x,y,z) = pol_ring.gens()216F = pol_ring(F(x/z,y/z)*z**2)217218if F == 0:219raise ValueError, "F must be nonzero over base field %s" % base_field220221if F.total_degree() != 2:222raise TypeError, "F (=%s) must have degree 2 over base field %s" % \223(F, base_field)224225if F.parent().ngens() == 3:226P2 = ProjectiveSpace(2, base_field, names)227if is_PrimeFiniteField(base_field):228return ProjectiveConic_prime_finite_field(P2, F)229if is_FiniteField(base_field):230return ProjectiveConic_finite_field(P2, F)231if is_RationalField(base_field):232return ProjectiveConic_rational_field(P2, F)233if is_NumberField(base_field):234return ProjectiveConic_number_field(P2, F)235return ProjectiveConic_field(P2, F)236237raise TypeError, "Number of variables of F (=%s) must be 2 or 3" % F238239240