Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Convert PARI objects to/from GMP objects
Utility function to convert PARI ``GEN``s to/from the GMP types ``mpz_t`` and ``mpq_t``.
AUTHORS:
- Luca De Feo (2016-09-06): Separate Sage-specific components from generic C-interface in ``Pari`` (:trac:`20241`) """
#***************************************************************************** # Copyright (C) 2016 Luca De Feo <luca.defeo@polytechnique.edu> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from __future__ import absolute_import, division, print_function
from cysignals.signals cimport sig_on, sig_off
from sage.libs.gmp.all cimport *
from cypari2.paridecl cimport * from cypari2.stack cimport new_gen
cdef Gen new_gen_from_mpz_t(mpz_t value): """ Create a new PARI Gen of type ``t_INT`` from a given GMP integer ``value``.
EXAMPLES::
sage: pari(42) # indirect doctest 42
TESTS:
Check that the hash of an integer does not depend on existing garbage on the stack (:trac:`11611`)::
sage: foo = pari(2^(32*1024)); # Create large integer to put PARI stack in known state sage: a5 = pari(5); sage: foo = pari(0xDEADBEEF * (2^(32*1024)-1)//(2^32 - 1)); # Dirty PARI stack sage: b5 = pari(5); sage: a5.__hash__() == b5.__hash__() True """
cdef inline GEN _new_GEN_from_mpz_t(mpz_t value): r""" Create a new PARI ``t_INT`` from a ``mpz_t``.
For internal use only; this directly uses the PARI stack. One should call ``sig_on()`` before and ``sig_off()`` after. """
# Set sign and "effective length"
cdef Gen new_gen_from_mpq_t(mpq_t value): """ Create a new PARI Gen of type ``t_INT`` or ``t_FRAC`` from a given GMP rational ``value``.
EXAMPLES::
sage: pari(-2/3) -2/3 sage: pari(QQ(42)) 42 sage: pari(QQ(42)).type() 't_INT' sage: pari(QQ(1/42)).type() 't_FRAC'
TESTS:
Check that the hash of a rational does not depend on existing garbage on the stack (:trac:`11854`)::
sage: foo = pari(2^(32*1024)); # Create large integer to put PARI stack in known state sage: a5 = pari(5/7); sage: foo = pari(0xDEADBEEF * (2^(32*1024)-1)//(2^32 - 1)); # Dirty PARI stack sage: b5 = pari(5/7); sage: a5.__hash__() == b5.__hash__() True """
cdef inline GEN _new_GEN_from_mpq_t(mpq_t value): r""" Create a new PARI ``t_INT`` or ``t_FRAC`` from a ``mpq_t``.
For internal use only; this directly uses the PARI stack. One should call ``sig_on()`` before and ``sig_off()`` after. """ # Denominator is 1, return the numerator (an integer)
cdef Gen new_gen_from_padic(long ordp, long relprec, mpz_t prime, mpz_t p_pow, mpz_t unit): """ Create a new PARI Gen of type ``t_PADIC`` from the given input data as GMP integers. """ cdef GEN z
cdef GEN _new_GEN_from_mpq_t_matrix(mpq_t** B, long nr, long nc): """ Create a new PARI ``t_MAT`` from a given 2-dimensional array of GMP rationals ``mpq_t``.
For internal use only; this directly uses the PARI stack. One should call ``sig_on()`` before and ``sig_off()`` after. """ cdef GEN x # Allocate zero matrix cdef GEN A = zeromatcopy(nr, nc) cdef long i, j for i in range(nr): for j in range(nc): x = _new_GEN_from_mpq_t(B[i][j]) set_gcoeff(A, i+1, j+1, x) # A[i+1, j+1] = x (using 1-based indexing) return A
cdef Gen rational_matrix(mpq_t** B, long nr, long nc): """ Create a new PARI matrix of type ``t_MAT`` from a given array of GMP rationals ``mpq_t``.
INPUT:
- ``B`` -- a 2-dimensional array of ``mpq_t`` values. This array is accessed as ``B[i][j]``, where ``i`` is the row index and ``j`` the column index.
- ``nr`` -- number of rows of this matrix
- ``nc`` -- number of columns of this matrix
EXAMPLES::
sage: matrix(QQ,2,[1..6]).__pari__() # indirect doctest [1, 2, 3; 4, 5, 6] """ sig_on() cdef GEN g = _new_GEN_from_mpq_t_matrix(B, nr, nc) return new_gen(g)
cdef inline void INT_to_mpz(mpz_ptr value, GEN g): """ Convert a PARI ``t_INT`` to a GMP integer, stored in ``value``. """ pari_err(e_TYPE, <char*>"conversion to mpz", g)
cdef void INTFRAC_to_mpq(mpq_ptr value, GEN g): """ Convert a PARI ``t_INT`` or ``t_FRAC`` to a GMP rational, stored in ``value``. """ else: pari_err(e_TYPE, <char*>"conversion to mpq", g) |